KTVCentral/app/Livewire/Grids/RoomGrid.php
allen.yan feb008dfeb 202509090937
加入全場開關動作
2025-09-09 09:37:39 +08:00

107 lines
2.9 KiB
PHP

<?php
namespace App\Livewire\Grids;
use App\Models\Room;
use App\Models\Branch;
use App\Enums\RoomType;
use App\Services\TcpSocketClient;
use Livewire\Component;
use Illuminate\Database\Eloquent\Collection;
use WireUi\Traits\WireUiActions;
class RoomGrid extends Component
{
use WireUiActions;
public $branch ;
public $branchName="";
protected $listeners = [
'fireAlarm', 'shutdownAll',
'muteAll', 'pauseAll',
];
public array $roomTypes;
public function mount()
{
$this->branch = Branch::first();
$this->branchName = $this->branch->name ?? '';
$this->roomTypes = ['all' => '全部'] + collect(RoomType::cases())->mapWithKeys(fn($e) => [$e->value => $e->labels()])->toArray();
}
public function fireAlarm()
{
$this->send('F');
$this->notification()->send([
'icon' => 'success',
'title' => '成功',
'description' => '命令已成功發送:🔥 火災警報!',
]);
}
public function shutdownAll()
{
$this->send('X');
$this->notification()->send([
'icon' => 'success',
'title' => '成功',
'description' => '命令已成功發送:⏻ 已送出全館關台!' ,
]);
}
public function muteAll()
{
$this->send('mute');
$this->notification()->send([
'icon' => 'success',
'title' => '成功',
'description' => '命令已成功發送:🔇 全館靜音中!' ,
]);
}
public function pauseAll()
{
$this->send('pause');
$this->notification()->send([
'icon' => 'success',
'title' => '成功',
'description' => '命令已成功發送:⏸ 已暫停全館播放!' ,
]);
}
public function send(string $action)
{
$rooms=Room::where([['is_online',true],['branch_id',$this->branch->id]])->get();
foreach($rooms as $room){
$client = new TcpSocketClient($room->internal_ip, $room->port);
try {
$response = $client->send($room->name . "," . $action);
} catch (\Throwable $e) {
logger()->error('❌ TCP 傳送失敗: ' . $e->getMessage(), [
'room_id' => $room->id,
'ip' => $room->internal_ip,
'port' => $room->port,
]);
}
}
}
public function render()
{
$rooms = collect(); // 預設為空集合
$floors = [];
if ($this->branch) {
$rooms = Room::where('branch_id', $this->branch->id)->orderBy('name', 'asc')->get();
$floors = $rooms->pluck('floor')->unique()->sort()->values()->toArray();
array_unshift($floors, 'all');
}
return view('livewire.grids.room-grid', [
'rooms' => $rooms,
'floors' => $floors,
]);
}
}