homeos/app/Livewire/Panel.php

51 lines
1.4 KiB
PHP

<?php
namespace App\Livewire;
use App\Models\Entity;
use App\Services\DeviceCommandService;
use Livewire\Attributes\Layout;
use Livewire\Attributes\On;
use Livewire\Component;
/**
* Tablet control panel — big touch tiles for lights/switches with quick on/off,
* a colour/brightness modal for lights, and drag-to-reorder (persisted per entity).
*/
#[Layout('layouts.app')]
class Panel extends Component
{
#[On('echo-private:home,.DeviceStateChanged')]
public function onDeviceStateChanged(): void {}
public function toggle(int $entityId): void
{
$entity = Entity::with('device', 'state')->find($entityId);
if ($entity !== null && in_array($entity->type, ['switch', 'light'], true)) {
app(DeviceCommandService::class)->toggle($entity);
}
}
/** Persist a new tile order from the drag interaction. */
public function reorder(array $order): void
{
foreach (array_values($order) as $index => $uuid) {
Entity::where('uuid', $uuid)->update(['panel_sort' => $index]);
}
}
public function render()
{
$tiles = Entity::query()
->whereIn('type', ['switch', 'light'])
->where('panel_hidden', false)
->with('device.room', 'state')
->orderByRaw('panel_sort IS NULL, panel_sort')
->orderBy('id')
->get();
return view('livewire.panel', ['tiles' => $tiles]);
}
}