From 9b8499f65584b9dda06dd945b5cca32301a8fb48 Mon Sep 17 00:00:00 2001 From: "allen.yan" Date: Thu, 22 May 2025 15:25:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A8=AD=E5=82=99=20=E5=BF=83=E8=B7=B3?= =?UTF-8?q?=E5=B0=81=E5=8C=85=20=E8=B3=87=E6=96=99=E9=A9=97=E8=A8=BC?= =?UTF-8?q?=E5=A4=9A=E5=8A=A0=E5=85=A5=20branch=5Fname=E8=88=87room.type,r?= =?UTF-8?q?oom.name=20=E7=94=A8=E6=96=BC=E8=B3=87=E6=96=99=E6=AD=A3?= =?UTF-8?q?=E8=A6=8F=2020250522?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/RoomControlController.php | 43 +++++++++++++++++-- .../Requests/ReceiveRoomRegisterRequest.php | 2 +- .../Requests/ReceiveRoomStatusDataRequest.php | 12 +++--- app/Models/MachineStatus.php | 14 +++--- ...1_170205_create_machine_statuses_table.php | 5 ++- 5 files changed, 56 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/RoomControlController.php b/app/Http/Controllers/RoomControlController.php index 59580af..32a604f 100644 --- a/app/Http/Controllers/RoomControlController.php +++ b/app/Http/Controllers/RoomControlController.php @@ -8,6 +8,7 @@ use App\Http\Requests\ReceiveRoomStatusDataRequest; use App\Services\TcpSocketClient; 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; @@ -91,9 +92,18 @@ class RoomControlController extends Controller $validated = $request->validated(); // branch_id, room_name, room_ip // 5. 找出對應包廂 - $room = Room::where('branch_id', $validated['branch_id']) - ->where('name', $validated['room_name']) - ->first(); + $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('找不到對應包廂'); @@ -162,8 +172,33 @@ class RoomControlController extends Controller */ 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'; + if($room->internal_ip != $validated['ip']){ + $room->internal_ip = $validated['ip']; + $validated['status']='error'; + } + $room->is_online=1; + $room->save(); + } + return ApiResponse::success([ - 'data' => MachineStatus::create($request->validated()), + 'data' => MachineStatus::create($validated), ]); } /** diff --git a/app/Http/Requests/ReceiveRoomRegisterRequest.php b/app/Http/Requests/ReceiveRoomRegisterRequest.php index bdd176f..74a0d5d 100644 --- a/app/Http/Requests/ReceiveRoomRegisterRequest.php +++ b/app/Http/Requests/ReceiveRoomRegisterRequest.php @@ -25,7 +25,7 @@ class ReceiveRoomRegisterRequest extends ApiRequest public function rules(): array { return [ - 'branch_id' => 'required|integer|exists:branches,id', + 'branch_name' => 'required|string|exists:branches,name', 'room_name' => 'required|string', 'room_ip' => 'required|string', 'email' => 'required|email', diff --git a/app/Http/Requests/ReceiveRoomStatusDataRequest.php b/app/Http/Requests/ReceiveRoomStatusDataRequest.php index cb8c571..fd71bfa 100644 --- a/app/Http/Requests/ReceiveRoomStatusDataRequest.php +++ b/app/Http/Requests/ReceiveRoomStatusDataRequest.php @@ -8,12 +8,11 @@ use Illuminate\Foundation\Http\FormRequest; * @OA\Schema( * schema="ReceiveRoomStatusDataRequest", * required={"hostname", "ip", "status"}, - * @OA\Property(property="hostname", type="string", example=""), - * @OA\Property(property="ip", type="string", example=""), - * @OA\Property(property="cpu", type="numeric", example=""), - * @OA\Property(property="memory", type="numeric", example=""), - * @OA\Property(property="disk", type="numeric", example=""), - * @OA\Property(property="status", type="string", example=""), + * @OA\Property(property="hostname", type="string", example="PC101"), + * @OA\Property(property="ip", type="string", example="192.168.XX.XX"), + * @OA\Property(property="cpu", type="numeric", example="0.00"), + * @OA\Property(property="memory", type="numeric", example="25603"), + * @OA\Property(property="disk", type="numeric", example="158266.49"), * ) */ class ReceiveRoomStatusDataRequest extends ApiRequest @@ -31,7 +30,6 @@ class ReceiveRoomStatusDataRequest extends ApiRequest 'cpu' => 'nullable|numeric', 'memory' => 'nullable|numeric', 'disk' => 'nullable|numeric', - 'status' => 'required|string', ]; } } diff --git a/app/Models/MachineStatus.php b/app/Models/MachineStatus.php index 226584d..fd9f19b 100644 --- a/app/Models/MachineStatus.php +++ b/app/Models/MachineStatus.php @@ -8,17 +8,19 @@ use Illuminate\Database\Eloquent\Model; * @OA\Schema( * schema="MachineStatus", * type="object", - * @OA\Property(property="hostname", type="string", example=""), - * @OA\Property(property="ip", type="string", example=""), - * @OA\Property(property="cpu", type="string"), - * @OA\Property(property="memory", type="string", example=""), - * @OA\Property(property="disk", type="string", example=""), - * @OA\Property(property="status", type="string", example=""), + * @OA\Property(property="branch_name", type="string", example="測試"), + * @OA\Property(property="hostname", type="string", example="PC101"), + * @OA\Property(property="ip", type="string", example="192.168.XX.XX"), + * @OA\Property(property="cpu", type="numeric", example="0.00"), + * @OA\Property(property="memory", type="numeric", example="25603"), + * @OA\Property(property="disk", type="numeric", example="158266.49"), + * @OA\Property(property="status", type="string", example="online,error"), * ) */ class MachineStatus extends Model { protected $fillable = [ + 'branch_name', 'hostname', 'ip', 'cpu', diff --git a/database/migrations/2025_05_21_170205_create_machine_statuses_table.php b/database/migrations/2025_05_21_170205_create_machine_statuses_table.php index 4e21b81..8833ab7 100644 --- a/database/migrations/2025_05_21_170205_create_machine_statuses_table.php +++ b/database/migrations/2025_05_21_170205_create_machine_statuses_table.php @@ -13,11 +13,12 @@ return new class extends Migration { Schema::create('machine_statuses', function (Blueprint $table) { $table->id(); + $table->string('branch_name'); $table->string('hostname'); $table->string('ip')->nullable(); $table->decimal('cpu', 5, 2)->nullable(); - $table->decimal('memory', 5, 2)->nullable(); - $table->decimal('disk', 5, 2)->nullable(); + $table->unsignedInteger('memory')->nullable(); + $table->decimal('disk', 10, 2)->nullable(); $table->string('status'); $table->timestamps(); });