diff --git a/app/Http/Controllers/RoomControlController.php b/app/Http/Controllers/RoomControlController.php index efdd6fa..505f195 100644 --- a/app/Http/Controllers/RoomControlController.php +++ b/app/Http/Controllers/RoomControlController.php @@ -255,17 +255,24 @@ class RoomControlController extends Controller public function sendSwitch(SendRoomSwitchCommandRequest $request): JsonResponse { $validated = $request->validated(); - + $branch = Branch::where('name',$validated['branch_name'])->first(); $room = Room::where([ - ['branch_id', $validated['branch_id']], + ['branch_id', $branch->id], ['name', $validated['room_name']], ])->first(); + if (!$branch) { + return ApiResponse::error('分店不存在'); + } + + if (empty($branch->external_ip) ) { + return ApiResponse::error('分店未設定 外部URL'); + } + if (!$room) { return ApiResponse::error('房間不存在'); } - // 檢查必要欄位是否缺失或狀態為錯誤 if (empty($room->internal_ip) || empty($room->port)) { return ApiResponse::error('房間未設定 IP 或 Port'); } @@ -274,32 +281,23 @@ class RoomControlController extends Controller return ApiResponse::error('房間目前處於錯誤狀態,無法操作'); } - $suffix = substr($room->name, -3) ?: $room->name; - $signal = match ($validated['command']) { - 'active' => 'O', - 'closed' => 'X', - 'fire' => 'F', - default => 'X', // fallback 保險起見 - }; - $data = $suffix . "," . $signal; - - //dd($data); - $client = new TcpSocketClient($room->internal_ip, $room->port); - try { - $response = $client->send($data); - - $room->status=$validated['command']; - $room->started_at=$validated['started_at']; - $room->ended_at=$validated['ended_at']; - $room->save(); - - return ApiResponse::success($room); - } catch (\Throwable $e) { - $room->status=RoomStatus::Error; - $room->started_at=null; - $room->ended_at=null; - $room->save(); - return ApiResponse::error($e->getMessage()); + $command = $validated['command']; + $payload = [ + 'branch_name' => $validated['branch_name'], + 'room_name' => $validated['room_name'], + 'command' => $command, + 'started_at' => $validated['started_at'] ?? null, + 'ended_at' => $validated['ended_at'] ?? null, + ]; + $user = \App\Models\User::find(2); + $token = $user->api_plain_token; + $api = new \App\Services\ApiClient(); + $response = $api->setToken($token)->setBaseUrl($branch->external_ip)->post('/room/sendSwitch', $payload); + + if (!$response->successful()) { + return ApiResponse::error('指令發送失敗:' . $response->body()); } + + return ApiResponse::success("命令已發送:$command"); } } diff --git a/app/Http/Requests/ReceiveRoomRegisterRequest.php b/app/Http/Requests/ReceiveRoomRegisterRequest.php index 21f4710..15fdc52 100644 --- a/app/Http/Requests/ReceiveRoomRegisterRequest.php +++ b/app/Http/Requests/ReceiveRoomRegisterRequest.php @@ -7,7 +7,7 @@ use Illuminate\Foundation\Http\FormRequest; /** * @OA\Schema( * schema="ReceiveRoomRegisterRequest", - * required={"branch", "room_name", "email" ,"password"}, + * required={"branch_name", "room_name", "email" ,"password"}, * @OA\Property(property="branch_name", type="string", example="測試"), * @OA\Property(property="room_name", type="string", example="PC101"), * @OA\Property(property="room_ip", type="string", example="192.168.1.1"), diff --git a/app/Http/Requests/SendRoomSwitchCommandRequest.php b/app/Http/Requests/SendRoomSwitchCommandRequest.php index ba5a533..049d84a 100644 --- a/app/Http/Requests/SendRoomSwitchCommandRequest.php +++ b/app/Http/Requests/SendRoomSwitchCommandRequest.php @@ -7,8 +7,8 @@ use Illuminate\Foundation\Http\FormRequest; /** * @OA\Schema( * schema="SendRoomSwitchCommandRequest", - * required={"branch_id", "room_name", "command"}, - * @OA\Property(property="branch_id", type="integer", example="5"), + * required={"branch_name", "room_name", "command"}, + * @OA\Property(property="branch_name", type="string", example="測試"), * @OA\Property(property="room_name", type="string", example="pc102"), * @OA\Property(property="command", type="string", enum={"active", "closed", "fire", "maintenance"}, example="active"), * @OA\Property(property="started_at", type="string", nullable=true, example="2025-05-19 09:31:00"), @@ -25,11 +25,11 @@ class SendRoomSwitchCommandRequest extends ApiRequest public function rules(): array { return [ - 'branch_id' => 'required|integer', - 'room_name' => 'required|string', - 'command' => 'required|string', - 'started_at' => 'nullable|date_format:Y-m-d H:i:s', - 'ended_at' => 'nullable|date_format:Y-m-d H:i:s', + 'branch_name' => ['required','string','exists:branches,name'], + 'room_name' => ['required','string'], + 'command' => ['required','in:active,closed,fire'], + 'started_at'=> ['nullable','date_format:Y-m-d H:i:s'], + 'ended_at' => ['nullable','date_format:Y-m-d H:i:s'], ]; } } diff --git a/app/Livewire/Admin/RoomDetailModal.php b/app/Livewire/Admin/RoomDetailModal.php index 517ad59..bb494f4 100644 --- a/app/Livewire/Admin/RoomDetailModal.php +++ b/app/Livewire/Admin/RoomDetailModal.php @@ -3,6 +3,7 @@ namespace App\Livewire\Admin; use App\Models\Room; +use App\Models\Branch; use Livewire\Component; use App\Services\ApiClient; @@ -19,11 +20,13 @@ class RoomDetailModal extends Component ]; public $room; + public $branch; public bool $showModal = false; public function openModal($roomId) { $this->room = Room::find($roomId); + $this->branch = Branch::find($this->room->branch_id); $this->showModal = true; } public function closeModal() @@ -62,7 +65,7 @@ class RoomDetailModal extends Component protected function buildNotifyData(string $command, $startedAt = null, $endedAt = null): array { return [ - 'branch_id' => $this->room->branch_id ?? 0, + 'branch_name' => $this->branch->name ?? '', 'room_name' => $this->room->name ?? '', 'command' => $command, 'started_at' => $startedAt ? $startedAt->toDateTimeString() : null, diff --git a/config/services.php b/config/services.php index 27a3617..46f3d85 100644 --- a/config/services.php +++ b/config/services.php @@ -13,6 +13,10 @@ return [ | a conventional file to locate the various service credentials. | */ + + 'room_api' => [ + 'base_url' => env('API_BASE_URL', env('APP_URL') . '/api'), + ], 'postmark' => [ 'token' => env('POSTMARK_TOKEN'),