4 Commits
1.0.3 ... 1.0.5

Author SHA1 Message Date
René Preuß
d3e1eccfa5 Fix sleep 2024-08-22 10:59:38 +02:00
René Preuß
7c8086fea3 Remove license key 2024-08-18 22:40:12 +02:00
René Preuß
bbc50dc6dd Merge branch 'main' of https://github.com/bitinflow/print-cli 2024-08-18 21:21:00 +02:00
René Preuß
5d0caeda1c Improve error handling 2024-08-18 21:20:11 +02:00
2 changed files with 28 additions and 7 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Commands; namespace App\Commands;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException; use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
@@ -35,16 +36,33 @@ class ServeCommand extends Command
/** /**
* Execute the console command. * Execute the console command.
* @throws RequestException
*/ */
public function handle(): void public function handle(): void
{
try {
$this->serve();
} catch (Throwable $e) {
if ($e instanceof RequestException && $e->response->status() === 422) {
$this->error('Please check your configuration and try again.');
$this->error($e->response->json()['message']);
} else {
$this->error($e->getMessage());
}
}
}
/**
* @throws RequestException
* @throws ConnectionException
*/
private function serve(): void
{ {
$this->info('Starting service...'); $this->info('Starting service...');
$yaml = $this->getConfiguration(); $yaml = $this->getConfiguration();
$printerIds = array_map(fn($printer) => $printer['id'], $yaml['printers']); $printerIds = array_map(fn($printer) => $printer['id'], $yaml['printers']);
$response = Http::patch(sprintf('%s/api/printers/register', $yaml['base_url']), [ $response = Http::acceptJson()->patch(sprintf('%s/api/printers/register', $yaml['base_url']), [
'printers' => $yaml['printers'], 'printers' => $yaml['printers'],
]); ]);
@@ -54,7 +72,7 @@ class ServeCommand extends Command
do { do {
try { try {
$response = Http::get(sprintf('%s/api/printers/jobs', $yaml['base_url']), [ $response = Http::acceptJson()->get(sprintf('%s/api/printers/jobs', $yaml['base_url']), [
'printer_ids' => $printerIds, 'printer_ids' => $printerIds,
]); ]);
@@ -67,6 +85,7 @@ class ServeCommand extends Command
$jobs = $response->json(); $jobs = $response->json();
if (empty($jobs)) { if (empty($jobs)) {
sleep(2);
continue; continue;
} }
@@ -98,6 +117,7 @@ class ServeCommand extends Command
/** /**
* @throws RequestException * @throws RequestException
* @throws ConnectionException
*/ */
private function handleJob(array $job, mixed $yaml): void private function handleJob(array $job, mixed $yaml): void
{ {
@@ -150,10 +170,11 @@ class ServeCommand extends Command
/** /**
* @throws RequestException * @throws RequestException
* @throws ConnectionException
*/ */
private function markCompleted(array $job, mixed $yaml, int $jobId): void private function markCompleted(array $job, mixed $yaml, int $jobId): void
{ {
$response = Http::patch(sprintf('%s/api/printers/jobs/%s/complete', $yaml['base_url'], $job['id']), [ $response = Http::acceptJson()->patch(sprintf('%s/api/printers/jobs/%s/complete', $yaml['base_url'], $job['id']), [
'job_id' => $jobId, 'job_id' => $jobId,
]); ]);
$response->throw(); $response->throw();
@@ -161,10 +182,11 @@ class ServeCommand extends Command
/** /**
* @throws RequestException * @throws RequestException
* @throws ConnectionException
*/ */
private function markFailed(array $job, mixed $yaml, string $reason): void private function markFailed(array $job, mixed $yaml, string $reason): void
{ {
$response = Http::patch(sprintf('%s/api/printers/jobs/%s/fail', $yaml['base_url'], $job['id']), [ $response = Http::acceptJson()->patch(sprintf('%s/api/printers/jobs/%s/fail', $yaml['base_url'], $job['id']), [
'reason' => $reason, 'reason' => $reason,
]); ]);
$response->throw(); $response->throw();

View File

@@ -93,12 +93,11 @@ using a different user, make sure to replace `print-cli` with the correct userna
Next create a configuration file `~/print-cli.yml`, and add the following content: Next create a configuration file `~/print-cli.yml`, and add the following content:
> Make sure to replace the `license_key`, `your-printer-uuid`, `address`, `username`, and `password` with your own > Make sure to replace the `your-printer-uuid`, `address`, `username`, and `password` with your own
> values. Also make sure to replace the `base_url` with the correct URL to our events platform. > values. Also make sure to replace the `base_url` with the correct URL to our events platform.
```yaml ```yaml
base_url: 'https://events.anikeen.com' base_url: 'https://events.anikeen.com'
license_key: 'your-license-key'
printers: printers:
- id: 'your-printer-uuid' - id: 'your-printer-uuid'
name: EPSON ET 2750 name: EPSON ET 2750