From feb008dfeba3762150b9069401d506618ab68ed9 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Tue, 9 Sep 2025 09:37:39 +0800 Subject: [PATCH] =?UTF-8?q?202509090937=20=E5=8A=A0=E5=85=A5=E5=85=A8?= =?UTF-8?q?=E5=A0=B4=E9=96=8B=E9=97=9C=E5=8B=95=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Livewire/Grids/RoomGrid.php | 76 +++++++++++++++++-- app/Livewire/Pages/SoundControl.php | 1 - .../views/livewire/grids/room-grid.blade.php | 23 ++++++ 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/app/Livewire/Grids/RoomGrid.php b/app/Livewire/Grids/RoomGrid.php index 3358169..8551702 100644 --- a/app/Livewire/Grids/RoomGrid.php +++ b/app/Livewire/Grids/RoomGrid.php @@ -5,33 +5,95 @@ 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() - { - $branch = Branch::first(); - $this->branchName = $branch->name ?? ''; - + { $rooms = collect(); // 預設為空集合 $floors = []; - if ($branch) { - $rooms = Room::where('branch_id', $branch->id)->orderBy('name', 'asc')->get(); + 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'); } diff --git a/app/Livewire/Pages/SoundControl.php b/app/Livewire/Pages/SoundControl.php index aa57db8..c929f43 100644 --- a/app/Livewire/Pages/SoundControl.php +++ b/app/Livewire/Pages/SoundControl.php @@ -4,7 +4,6 @@ namespace App\Livewire\Pages; use App\Models\RoomSession; use Livewire\Component; -use App\Services\ApiClient; use WireUi\Traits\WireUiActions; use App\Services\TcpSocketClient; diff --git a/resources/views/livewire/grids/room-grid.blade.php b/resources/views/livewire/grids/room-grid.blade.php index 177b40f..cc02e52 100644 --- a/resources/views/livewire/grids/room-grid.blade.php +++ b/resources/views/livewire/grids/room-grid.blade.php @@ -1,4 +1,27 @@ + {{-- 全域控制按鈕 --}} +
+ + + + +
{{-- 樓層 Tab --}}