feat(#17): add commands and jobs to poll, download and update firmware

feat(#17): add commands and jobs to poll, download and update firmware

feat(#17): update firmware modal

feat(#17): add tests
This commit is contained in:
Benjamin Nussbaum 2025-05-29 00:49:56 +02:00
parent 93aac51182
commit 87b9b57c3d
13 changed files with 567 additions and 3 deletions

View file

@ -0,0 +1,37 @@
<?php
namespace App\Console\Commands;
use App\Jobs\FirmwarePollJob;
use App\Models\Firmware;
use Illuminate\Console\Command;
use function Laravel\Prompts\spin;
use function Laravel\Prompts\table;
class FirmwareCheckCommand extends Command
{
protected $signature = 'trmnl:firmware:check {--download : Download the latest firmware if available}';
protected $description = 'Checks for the latest firmware and downloads it if flag --download is passed.';
public function handle(): void
{
spin(
callback: fn () => FirmwarePollJob::dispatchSync(download: $this->option('download')),
message: 'Checking for latest firmware...'
);
$latestFirmware = Firmware::getLatest();
if ($latestFirmware) {
table(
rows: [
['Latest Version', $latestFirmware->version_tag],
['Download URL', $latestFirmware->url],
['Storage Location', $latestFirmware->storage_location],
]
);
} else {
$this->error('No firmware found.');
}
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace App\Console\Commands;
use App\Models\Device;
use App\Models\Firmware;
use Illuminate\Console\Command;
use function Laravel\Prompts\multiselect;
use function Laravel\Prompts\select;
class FirmwareUpdateCommand extends Command
{
protected $signature = 'trmnl:firmware:update';
protected $description = 'Command description';
public function handle(): void
{
$checkFirmware = select(
label: 'Check for new firmware?',
options: [
'check' => 'Check. Devices will download binary from the original source.',
'download' => 'Check & Download. Devices will download binary from BYOS.',
'no' => 'Do not check.',
],
);
if ($checkFirmware !== 'no') {
$this->call('trmnl:firmware:check', [
'--download' => $checkFirmware === 'download',
]);
}
$firmwareVersion = select(
label: 'Update to which version?',
options: Firmware::pluck('version_tag', 'id')
);
$devices = multiselect(
label: 'Which devices should be updated?',
options: [
'all' => 'ALL Devices',
...Device::all()->mapWithKeys(function ($device) {
// without _ returns index
return ["_$device->id" => "$device->name (Current version: $device->last_firmware_version)"];
})->toArray()
],
scroll: 10
);
if (empty($devices)) {
$this->error('No devices selected. Aborting.');
return;
}
if (in_array('all', $devices)) {
$devices = Device::pluck('id')->toArray();
} else {
$devices = array_map(function($selected) {
return (int) str_replace('_', '', $selected);
}, $devices);
}
foreach ($devices as $deviceId) {
Device::find($deviceId)->update(['update_firmware_id' => $firmwareVersion]);
$this->info("Device with id [$deviceId] will update firmware on next request.");
}
}
}