homeos/app/Events/DeviceStateChanged.php

44 lines
1.1 KiB
PHP

<?php
namespace App\Events;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
/**
* A device entity's current state changed. Broadcast immediately (never queued behind a
* slow job) on the private `home` channel so the UI reflects it live (§3).
*/
class DeviceStateChanged implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public function __construct(
public string $deviceUuid,
public string $entityKey,
public array $state,
) {}
public function broadcastOn(): array
{
return [new PrivateChannel('home')];
}
public function broadcastAs(): string
{
return 'DeviceStateChanged';
}
public function broadcastWith(): array
{
return [
'device' => $this->deviceUuid,
'entity' => $this->entityKey,
'state' => $this->state,
];
}
}