From da7b84a1c1e637fe9b23b2e4a607a1c4a8c6409e Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Thu, 5 Jun 2025 12:28:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=20MachineStatuese=2020250605?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/ClearMachineStatuses.php | 46 ----- .../Controllers/RoomControlController.php | 182 ------------------ app/Models/MachineStatus.php | 31 --- ...1_170205_create_machine_statuses_table.php | 34 ---- routes/console.php | 6 - 5 files changed, 299 deletions(-) delete mode 100644 app/Console/Commands/ClearMachineStatuses.php delete mode 100644 app/Models/MachineStatus.php delete mode 100644 database/migrations/2025_05_21_170205_create_machine_statuses_table.php diff --git a/app/Console/Commands/ClearMachineStatuses.php b/app/Console/Commands/ClearMachineStatuses.php deleted file mode 100644 index 6645547..0000000 --- a/app/Console/Commands/ClearMachineStatuses.php +++ /dev/null @@ -1,46 +0,0 @@ -format('l'); // e.g. "Monday" - $targetTable = "machine_statuses_" . $day; - - DB::statement("CREATE TABLE IF NOT EXISTS _machine_statuses LIKE machine_statuses"); - - // 先刪除舊表(如存在) - DB::statement("DROP TABLE IF EXISTS {$targetTable}"); - - // 改名備份 - DB::statement("RENAME TABLE machine_statuses TO {$targetTable}"); - - // 空表回命名 - DB::statement("RENAME TABLE _machine_statuses TO machine_statuses"); - - $this->info("Machine statuses backed up to {$targetTable} and table cleared."); - } -} diff --git a/app/Http/Controllers/RoomControlController.php b/app/Http/Controllers/RoomControlController.php index cd35a74..d3add42 100644 --- a/app/Http/Controllers/RoomControlController.php +++ b/app/Http/Controllers/RoomControlController.php @@ -10,7 +10,6 @@ use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Auth; use App\Models\Branch; use App\Models\Room; -use App\Models\MachineStatus; use App\Enums\RoomStatus; use App\Http\Responses\ApiResponse; @@ -23,187 +22,6 @@ use Illuminate\Support\Facades\Log; */ class RoomControlController extends Controller { - /** - * @OA\Post( - * path="/api/room/receiveRegister", - * summary="包廂註冊控制指令", - * description="依據傳入的 branch_id 與 room_name,知道過來的設備來之於那個IP設備。", - * operationId="registerRoomCommand", - * tags={"Room Control"}, - * @OA\RequestBody( - * required=true, - * @OA\JsonContent(ref="#/components/schemas/ReceiveRoomRegisterRequest") - * ), - * @OA\Response( - * response=200, - * description="成功傳送指令並回傳 TCP 回應", - * @OA\JsonContent( - * allOf={ - * @OA\Schema(ref="#/components/schemas/ApiResponse"), - * @OA\Schema( - * @OA\Property(property="data", ref="#/components/schemas/Room") - * ) - * } - * ) - * ), - * @OA\Response( - * response=401, - * description="Unauthorized", - * @OA\JsonContent( - * allOf={ - * @OA\Schema(ref="#/components/schemas/ApiResponse"), - * @OA\Schema( - * @OA\Property(property="code", type="string", example="UNAUTHORIZED"), - * @OA\Property(property="message", type="string", example="Unauthorized"), - * @OA\Property(property="data", type="null") - * ) - * } - * ) - * ), - * @OA\Parameter( - * name="Accept", - * in="header", - * required=true, - * @OA\Schema(type="string", default="application/json") - * ) - * ) - */ - public function receiveRegister(ReceiveRoomRegisterRequest $request): JsonResponse - { - $data = $request->only(['branch', 'room_name', 'room_ip', 'email']); // 不記錄密碼 - Log::info('Token Request Payload:', $data); - - // 1. 驗證帳密(登入用) - $credentials = $request->only('email', 'password'); - - if (!Auth::attempt($credentials)) { - return ApiResponse::unauthorized(); - } - - // 2. 取得登入使用者 - $user = Auth::user(); - - // 3. 產生或取得 Token - if (empty($user->api_plain_token)) { - $token = $user->createToken('pc-heartbeat')->plainTextToken; - $user->api_plain_token = $token; - $user->save(); - } else { - $token = $user->api_plain_token; - } - - // 4. 驗證其他註冊欄位 - $validated = $request->validated(); // branch_id, room_name, room_ip - - // 5. 找出對應包廂 - $roomType = null; - $roomName = null; - // 從 room_name(例如 PC101, SVR01)中擷取 type 與 name - if (preg_match('/^([A-Za-z]+)(\d+)$/', $validated['room_name'], $matches)) { - $roomType = strtolower($matches[1]); // 'PC' → 'pc' - $roomName = $matches[2]; // '101' - } - $branch=Branch::where('name',$validated['branch_name'])->first(); - $room = Room::where('branch_id', $branch->id) - ->where('name', $roomName) - ->where('type', $roomType) - ->first(); - - if (!$room) { - return ApiResponse::error('找不到對應包廂'); - } - - // 6. 更新包廂資訊 - $room->internal_ip = $validated['room_ip']; - $room->port = 1000; // 預設值 - $room->is_online =1; - $room->status = RoomStatus::Closed; - $room->touch(); // 更新 updated_at - $room->save(); - - // 7. 回傳 token 與包廂資料 - return ApiResponse::success([ - 'token' => $token, - 'room' => $room, - ]); - } - - /** - * @OA\Post( - * path="/api/room/heartbeat", - * summary="包廂心跳封包指令", - * description="記錄設備連線狀況", - * operationId="heartbeatRoomCommand", - * tags={"Room Control"}, - * security={{"Authorization":{}}}, - * @OA\RequestBody( - * required=true, - * @OA\JsonContent(ref="#/components/schemas/ReceiveRoomStatusDataRequest") - * ), - * @OA\Response( - * response=200, - * description="成功傳送指令並回傳 TCP 回應", - * @OA\JsonContent( - * allOf={ - * @OA\Schema(ref="#/components/schemas/ApiResponse"), - * @OA\Schema( - * @OA\Property(property="data", ref="#/components/schemas/MachineStatus") - * ) - * } - * ) - * ), - * @OA\Response( - * response=401, - * description="Unauthorized", - * @OA\JsonContent( - * allOf={ - * @OA\Schema(ref="#/components/schemas/ApiResponse"), - * @OA\Schema( - * @OA\Property(property="code", type="string", example="UNAUTHORIZED"), - * @OA\Property(property="message", type="string", example="Unauthorized"), - * @OA\Property(property="data", type="null") - * ) - * } - * ) - * ), - * @OA\Parameter( - * name="Accept", - * in="header", - * required=true, - * @OA\Schema(type="string", default="application/json") - * ) - * ) - */ - public function StatusReport(ReceiveRoomStatusDataRequest $request) - { - $validated = $request->validated(); - $roomType = null; - $roomName = null; - // 從 room_name(例如 PC101, SVR01)中擷取 type 與 name - if (preg_match('/^([A-Za-z]+)(\d+)$/', $validated['hostname'], $matches)) { - $roomType = strtolower($matches[1]); // 'PC' → 'pc' - $roomName = $matches[2]; // '101' - } - $branch=Branch::where('name',$validated['branch_name'])->first(); - $room = Room::where('branch_id', $branch->id) - ->where('name', $roomName) - ->where('type', $roomType) - ->first(); - // 決定 status 欄位值 - $validated['status']= 'error'; - if($room){ - $validated['status']= 'online'; - $room->internal_ip = $validated['ip']; - $room->port = 1000; - $room->is_online=1; - $room->touch(); // 更新 updated_at - $room->save(); - } - - return ApiResponse::success([ - 'data' => MachineStatus::create($validated), - ]); - } /** * @OA\Post( * path="/api/room/sendSwitch", diff --git a/app/Models/MachineStatus.php b/app/Models/MachineStatus.php deleted file mode 100644 index fd9f19b..0000000 --- a/app/Models/MachineStatus.php +++ /dev/null @@ -1,31 +0,0 @@ -id(); - $table->string('branch_name'); - $table->string('hostname'); - $table->string('ip')->nullable(); - $table->decimal('cpu', 5, 2)->nullable(); - $table->unsignedInteger('memory')->nullable(); - $table->decimal('disk', 10, 2)->nullable(); - $table->string('status'); - $table->timestamps(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::dropIfExists('machine_statuses'); - } -}; diff --git a/routes/console.php b/routes/console.php index d597c15..6ff5ea9 100644 --- a/routes/console.php +++ b/routes/console.php @@ -8,9 +8,3 @@ Artisan::command('inspire', function () { $this->comment(Inspiring::quote()); })->purpose('Display an inspiring quote'); - -Schedule::command('app:clear-machine-statuses')->dailyAt('12:00'); // 每天凌晨 12:10 執行 -//首次部署或有新增命令時)建立或更新任務排程 Crontab -// 檢查是否已有下列 crontab 設定(crontab -e): -//分鐘 小時 日 月 星期 指令 -// * * * * * cd /Users/allen.yan/work/KTV && php artisan schedule:run >> /dev/null 2>&1 \ No newline at end of file