2025-05-28 09:24:03 +08:00
|
|
|
<?php
|
|
|
|
|
2025-07-29 13:46:41 +08:00
|
|
|
namespace App\Livewire\Grids;
|
2025-05-28 09:24:03 +08:00
|
|
|
|
|
|
|
use App\Models\Room;
|
|
|
|
use App\Models\Branch;
|
|
|
|
use App\Enums\RoomType;
|
2025-09-09 09:37:39 +08:00
|
|
|
use App\Services\TcpSocketClient;
|
2025-05-28 09:24:03 +08:00
|
|
|
use Livewire\Component;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2025-09-09 09:37:39 +08:00
|
|
|
use WireUi\Traits\WireUiActions;
|
2025-05-28 09:24:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-29 13:46:41 +08:00
|
|
|
class RoomGrid extends Component
|
2025-05-28 09:24:03 +08:00
|
|
|
{
|
2025-09-09 09:37:39 +08:00
|
|
|
use WireUiActions;
|
|
|
|
|
|
|
|
public $branch ;
|
2025-05-28 09:24:03 +08:00
|
|
|
public $branchName="";
|
2025-09-09 09:37:39 +08:00
|
|
|
protected $listeners = [
|
|
|
|
'fireAlarm', 'shutdownAll',
|
|
|
|
'muteAll', 'pauseAll',
|
|
|
|
];
|
2025-05-28 09:24:03 +08:00
|
|
|
public array $roomTypes;
|
|
|
|
|
|
|
|
|
|
|
|
public function mount()
|
|
|
|
{
|
2025-09-09 09:37:39 +08:00
|
|
|
$this->branch = Branch::first();
|
|
|
|
$this->branchName = $this->branch->name ?? '';
|
2025-05-28 09:24:03 +08:00
|
|
|
$this->roomTypes = ['all' => '全部'] + collect(RoomType::cases())->mapWithKeys(fn($e) => [$e->value => $e->labels()])->toArray();
|
|
|
|
}
|
2025-09-09 09:37:39 +08:00
|
|
|
public function fireAlarm()
|
|
|
|
{
|
|
|
|
$this->send('F');
|
|
|
|
$this->notification()->send([
|
|
|
|
'icon' => 'success',
|
|
|
|
'title' => '成功',
|
|
|
|
'description' => '命令已成功發送:🔥 火災警報!',
|
|
|
|
]);
|
|
|
|
}
|
2025-05-28 09:24:03 +08:00
|
|
|
|
2025-09-09 09:37:39 +08:00
|
|
|
public function shutdownAll()
|
2025-05-28 09:24:03 +08:00
|
|
|
{
|
2025-09-09 09:37:39 +08:00
|
|
|
$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()
|
|
|
|
{
|
2025-05-28 09:24:03 +08:00
|
|
|
$rooms = collect(); // 預設為空集合
|
|
|
|
$floors = [];
|
|
|
|
|
2025-09-09 09:37:39 +08:00
|
|
|
if ($this->branch) {
|
|
|
|
$rooms = Room::where('branch_id', $this->branch->id)->orderBy('name', 'asc')->get();
|
2025-05-28 09:24:03 +08:00
|
|
|
$floors = $rooms->pluck('floor')->unique()->sort()->values()->toArray();
|
2025-08-14 13:47:31 +08:00
|
|
|
array_unshift($floors, 'all');
|
2025-05-28 09:24:03 +08:00
|
|
|
}
|
2025-06-11 17:37:31 +08:00
|
|
|
|
2025-07-29 13:46:41 +08:00
|
|
|
return view('livewire.grids.room-grid', [
|
2025-05-28 09:24:03 +08:00
|
|
|
'rooms' => $rooms,
|
|
|
|
'floors' => $floors,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|