feat: adapt device models api

This commit is contained in:
Benjamin Nussbaum 2025-08-16 09:41:00 +02:00
parent a88e72b75e
commit 731d995f20
29 changed files with 2379 additions and 215 deletions

View file

@ -1,6 +1,7 @@
<?php
use App\Jobs\FirmwareDownloadJob;
use App\Models\DeviceModel;
use App\Models\Firmware;
use App\Models\Playlist;
use App\Models\PlaylistItem;
@ -19,6 +20,7 @@ new class extends Component {
public $height;
public $rotate;
public $image_format;
public $device_model_id;
// Sleep mode and special function
public $sleep_mode_enabled = false;
@ -34,6 +36,9 @@ new class extends Component {
public $active_until;
public $refresh_time = null;
// Device model properties
public $deviceModels;
// Firmware properties
public $firmwares;
public $selected_firmware_id;
@ -56,6 +61,12 @@ new class extends Component {
$this->height = $device->height;
$this->rotate = $device->rotate;
$this->image_format = $device->image_format;
$this->device_model_id = $device->device_model_id;
$this->deviceModels = DeviceModel::orderBy('label')->get()->sortBy(function ($deviceModel) {
// Put TRMNL models at the top, then sort alphabetically within each group
$isTrmnl = str_starts_with($deviceModel->label, 'TRMNL');
return $isTrmnl ? '0' . $deviceModel->label : '1' . $deviceModel->label;
});
$this->playlists = $device->playlists()->with('items.plugin')->orderBy('created_at')->get();
$this->firmwares = \App\Models\Firmware::orderBy('latest', 'desc')->orderBy('created_at', 'desc')->get();
$this->selected_firmware_id = $this->firmwares->where('latest', true)->first()?->id;
@ -77,6 +88,24 @@ new class extends Component {
redirect()->route('devices');
}
public function updatedDeviceModelId()
{
// Convert empty string to null for custom selection
if (empty($this->device_model_id)) {
$this->device_model_id = null;
return;
}
if ($this->device_model_id) {
$deviceModel = DeviceModel::find($this->device_model_id);
if ($deviceModel) {
$this->width = $deviceModel->width;
$this->height = $deviceModel->height;
$this->rotate = $deviceModel->rotation;
}
}
}
public function updateDevice()
{
abort_unless(auth()->user()->devices->contains($this->device), 403);
@ -90,12 +119,16 @@ new class extends Component {
'height' => 'required|integer|min:1',
'rotate' => 'required|integer|min:0|max:359',
'image_format' => 'required|string',
'device_model_id' => 'nullable|exists:device_models,id',
'sleep_mode_enabled' => 'boolean',
'sleep_mode_from' => 'nullable|date_format:H:i',
'sleep_mode_to' => 'nullable|date_format:H:i',
'special_function' => 'nullable|string',
]);
// Convert empty string to null for custom selection
$deviceModelId = empty($this->device_model_id) ? null : $this->device_model_id;
$this->device->update([
'name' => $this->name,
'friendly_id' => $this->friendly_id,
@ -105,6 +138,7 @@ new class extends Component {
'height' => $this->height,
'rotate' => $this->rotate,
'image_format' => $this->image_format,
'device_model_id' => $deviceModelId,
'sleep_mode_enabled' => $this->sleep_mode_enabled,
'sleep_mode_from' => $this->sleep_mode_from,
'sleep_mode_to' => $this->sleep_mode_to,
@ -357,20 +391,33 @@ new class extends Component {
<flux:input label="Friendly ID" wire:model="friendly_id"/>
<flux:input label="MAC Address" wire:model="mac_address"/>
<flux:separator class="my-4" text="Advanced Device Settings" />
<div class="flex gap-4">
<flux:input label="Width (px)" wire:model="width" type="number" />
<flux:input label="Height (px)" wire:model="height" type="number"/>
<flux:input label="Rotate °" wire:model="rotate" type="number"/>
</div>
<flux:select label="Image Format" wire:model="image_format">
@foreach(\App\Enums\ImageFormat::cases() as $format)
<flux:select.option value="{{ $format->value }}">{{$format->label()}}</flux:select.option>
@endforeach
</flux:select>
<flux:input label="Default Refresh Interval (seconds)" wire:model="default_refresh_interval"
type="number"/>
<flux:select label="Device Model" wire:model.live="device_model_id">
<flux:select.option value="">Custom (Manual Dimensions)</flux:select.option>
@foreach($deviceModels as $deviceModel)
<flux:select.option value="{{ $deviceModel->id }}">
{{ $deviceModel->label }} ({{ $deviceModel->width }}x{{ $deviceModel->height }})
</flux:select.option>
@endforeach
</flux:select>
@if(empty($device_model_id))
<flux:separator class="my-4" text="Advanced Device Settings" />
<div class="flex gap-4">
<flux:input label="Width (px)" wire:model="width" type="number" />
<flux:input label="Height (px)" wire:model="height" type="number"/>
<flux:input label="Rotate °" wire:model="rotate" type="number"/>
</div>
<flux:select label="Image Format" wire:model="image_format">
@foreach(\App\Enums\ImageFormat::cases() as $format)
<flux:select.option value="{{ $format->value }}">{{$format->label()}}</flux:select.option>
@endforeach
</flux:select>
@endif
<flux:separator class="my-4" text="Special Functions" />
<flux:select label="Special Function" wire:model="special_function">
<flux:select.option value="sleep">Sleep</flux:select.option>

View file

@ -1,6 +1,7 @@
<?php
use App\Models\Device;
use App\Models\DeviceModel;
use Livewire\Volt\Component;
new class extends Component {
@ -22,6 +23,8 @@ new class extends Component {
public $is_mirror = false;
public $mirror_device_id = null;
public $device_model_id = null;
public $deviceModels;
public ?int $pause_duration;
@ -29,15 +32,29 @@ new class extends Component {
'mac_address' => 'required',
'api_key' => 'required',
'default_refresh_interval' => 'required|integer',
'device_model_id' => 'nullable|exists:device_models,id',
'mirror_device_id' => 'required_if:is_mirror,true',
];
public function mount()
{
$this->devices = auth()->user()->devices;
$this->deviceModels = DeviceModel::orderBy('label')->get()->sortBy(function ($deviceModel) {
// Put TRMNL models at the top, then sort alphabetically within each group
$isTrmnl = str_starts_with($deviceModel->label, 'TRMNL');
return $isTrmnl ? '0' . $deviceModel->label : '1' . $deviceModel->label;
});
return view('livewire.devices.manage');
}
public function updatedDeviceModelId(): void
{
// Convert empty string to null for custom selection
if (empty($this->device_model_id)) {
$this->device_model_id = null;
}
}
public function createDevice(): void
{
$this->validate();
@ -49,6 +66,9 @@ new class extends Component {
abort_if($mirrorDevice->mirror_device_id !== null, 403, 'Cannot mirror a device that is already a mirror device');
}
// Convert empty string to null for custom selection
$deviceModelId = empty($this->device_model_id) ? null : $this->device_model_id;
Device::create([
'name' => $this->name,
'mac_address' => $this->mac_address,
@ -56,6 +76,7 @@ new class extends Component {
'default_refresh_interval' => $this->default_refresh_interval,
'friendly_id' => $this->friendly_id,
'user_id' => auth()->id(),
'device_model_id' => $deviceModelId,
'mirror_device_id' => $this->is_mirror ? $this->mirror_device_id : null,
]);
@ -154,6 +175,19 @@ new class extends Component {
autofocus/>
</div>
<div class="mb-4">
<flux:select label="Device Model" wire:model.live="device_model_id">
<flux:select.option value="">Custom (Manual Dimensions)</flux:select.option>
@if ($deviceModels && $deviceModels->count() > 0)
@foreach($deviceModels as $deviceModel)
<flux:select.option value="{{ $deviceModel->id }}">
{{ $deviceModel->label }} ({{ $deviceModel->width }}x{{ $deviceModel->height }})
</flux:select.option>
@endforeach
@endif
</flux:select>
</div>
<div class="mb-4">
<flux:checkbox wire:model.live="is_mirror" label="Mirrors Device"/>
</div>