分店 SendRoomSwitchCommand 邏輯加入 20250528
This commit is contained in:
parent
144aa499b6
commit
e87b06c669
@ -255,17 +255,24 @@ class RoomControlController extends Controller
|
|||||||
public function sendSwitch(SendRoomSwitchCommandRequest $request): JsonResponse
|
public function sendSwitch(SendRoomSwitchCommandRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
$validated = $request->validated();
|
$validated = $request->validated();
|
||||||
|
$branch = Branch::where('name',$validated['branch_name'])->first();
|
||||||
$room = Room::where([
|
$room = Room::where([
|
||||||
['branch_id', $validated['branch_id']],
|
['branch_id', $branch->id],
|
||||||
['name', $validated['room_name']],
|
['name', $validated['room_name']],
|
||||||
])->first();
|
])->first();
|
||||||
|
|
||||||
|
if (!$branch) {
|
||||||
|
return ApiResponse::error('分店不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($branch->external_ip) ) {
|
||||||
|
return ApiResponse::error('分店未設定 外部URL');
|
||||||
|
}
|
||||||
|
|
||||||
if (!$room) {
|
if (!$room) {
|
||||||
return ApiResponse::error('房間不存在');
|
return ApiResponse::error('房間不存在');
|
||||||
}
|
}
|
||||||
|
|
||||||
// 檢查必要欄位是否缺失或狀態為錯誤
|
|
||||||
if (empty($room->internal_ip) || empty($room->port)) {
|
if (empty($room->internal_ip) || empty($room->port)) {
|
||||||
return ApiResponse::error('房間未設定 IP 或 Port');
|
return ApiResponse::error('房間未設定 IP 或 Port');
|
||||||
}
|
}
|
||||||
@ -274,32 +281,23 @@ class RoomControlController extends Controller
|
|||||||
return ApiResponse::error('房間目前處於錯誤狀態,無法操作');
|
return ApiResponse::error('房間目前處於錯誤狀態,無法操作');
|
||||||
}
|
}
|
||||||
|
|
||||||
$suffix = substr($room->name, -3) ?: $room->name;
|
$command = $validated['command'];
|
||||||
$signal = match ($validated['command']) {
|
$payload = [
|
||||||
'active' => 'O',
|
'branch_name' => $validated['branch_name'],
|
||||||
'closed' => 'X',
|
'room_name' => $validated['room_name'],
|
||||||
'fire' => 'F',
|
'command' => $command,
|
||||||
default => 'X', // fallback 保險起見
|
'started_at' => $validated['started_at'] ?? null,
|
||||||
};
|
'ended_at' => $validated['ended_at'] ?? null,
|
||||||
$data = $suffix . "," . $signal;
|
];
|
||||||
|
$user = \App\Models\User::find(2);
|
||||||
//dd($data);
|
$token = $user->api_plain_token;
|
||||||
$client = new TcpSocketClient($room->internal_ip, $room->port);
|
$api = new \App\Services\ApiClient();
|
||||||
try {
|
$response = $api->setToken($token)->setBaseUrl($branch->external_ip)->post('/room/sendSwitch', $payload);
|
||||||
$response = $client->send($data);
|
|
||||||
|
if (!$response->successful()) {
|
||||||
$room->status=$validated['command'];
|
return ApiResponse::error('指令發送失敗:' . $response->body());
|
||||||
$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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ApiResponse::success("命令已發送:$command");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use Illuminate\Foundation\Http\FormRequest;
|
|||||||
/**
|
/**
|
||||||
* @OA\Schema(
|
* @OA\Schema(
|
||||||
* schema="ReceiveRoomRegisterRequest",
|
* 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="branch_name", type="string", example="測試"),
|
||||||
* @OA\Property(property="room_name", type="string", example="PC101"),
|
* @OA\Property(property="room_name", type="string", example="PC101"),
|
||||||
* @OA\Property(property="room_ip", type="string", example="192.168.1.1"),
|
* @OA\Property(property="room_ip", type="string", example="192.168.1.1"),
|
||||||
|
@ -7,8 +7,8 @@ use Illuminate\Foundation\Http\FormRequest;
|
|||||||
/**
|
/**
|
||||||
* @OA\Schema(
|
* @OA\Schema(
|
||||||
* schema="SendRoomSwitchCommandRequest",
|
* schema="SendRoomSwitchCommandRequest",
|
||||||
* required={"branch_id", "room_name", "command"},
|
* required={"branch_name", "room_name", "command"},
|
||||||
* @OA\Property(property="branch_id", type="integer", example="5"),
|
* @OA\Property(property="branch_name", type="string", example="測試"),
|
||||||
* @OA\Property(property="room_name", type="string", example="pc102"),
|
* @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="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"),
|
* @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
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'branch_id' => 'required|integer',
|
'branch_name' => ['required','string','exists:branches,name'],
|
||||||
'room_name' => 'required|string',
|
'room_name' => ['required','string'],
|
||||||
'command' => 'required|string',
|
'command' => ['required','in:active,closed,fire'],
|
||||||
'started_at' => 'nullable|date_format:Y-m-d H:i:s',
|
'started_at'=> ['nullable','date_format:Y-m-d H:i:s'],
|
||||||
'ended_at' => 'nullable|date_format:Y-m-d H:i:s',
|
'ended_at' => ['nullable','date_format:Y-m-d H:i:s'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Livewire\Admin;
|
namespace App\Livewire\Admin;
|
||||||
|
|
||||||
use App\Models\Room;
|
use App\Models\Room;
|
||||||
|
use App\Models\Branch;
|
||||||
|
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
use App\Services\ApiClient;
|
use App\Services\ApiClient;
|
||||||
@ -19,11 +20,13 @@ class RoomDetailModal extends Component
|
|||||||
];
|
];
|
||||||
|
|
||||||
public $room;
|
public $room;
|
||||||
|
public $branch;
|
||||||
public bool $showModal = false;
|
public bool $showModal = false;
|
||||||
|
|
||||||
public function openModal($roomId)
|
public function openModal($roomId)
|
||||||
{
|
{
|
||||||
$this->room = Room::find($roomId);
|
$this->room = Room::find($roomId);
|
||||||
|
$this->branch = Branch::find($this->room->branch_id);
|
||||||
$this->showModal = true;
|
$this->showModal = true;
|
||||||
}
|
}
|
||||||
public function closeModal()
|
public function closeModal()
|
||||||
@ -62,7 +65,7 @@ class RoomDetailModal extends Component
|
|||||||
protected function buildNotifyData(string $command, $startedAt = null, $endedAt = null): array
|
protected function buildNotifyData(string $command, $startedAt = null, $endedAt = null): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'branch_id' => $this->room->branch_id ?? 0,
|
'branch_name' => $this->branch->name ?? '',
|
||||||
'room_name' => $this->room->name ?? '',
|
'room_name' => $this->room->name ?? '',
|
||||||
'command' => $command,
|
'command' => $command,
|
||||||
'started_at' => $startedAt ? $startedAt->toDateTimeString() : null,
|
'started_at' => $startedAt ? $startedAt->toDateTimeString() : null,
|
||||||
|
@ -13,6 +13,10 @@ return [
|
|||||||
| a conventional file to locate the various service credentials.
|
| a conventional file to locate the various service credentials.
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
'room_api' => [
|
||||||
|
'base_url' => env('API_BASE_URL', env('APP_URL') . '/api'),
|
||||||
|
],
|
||||||
|
|
||||||
'postmark' => [
|
'postmark' => [
|
||||||
'token' => env('POSTMARK_TOKEN'),
|
'token' => env('POSTMARK_TOKEN'),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user