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, ]); } }