調整 room 資料同步 20250605

This commit is contained in:
allen.yan 2025-06-05 21:49:55 +08:00
parent 0734a0294a
commit fda661c616
2 changed files with 68 additions and 33 deletions

View File

@ -3,8 +3,7 @@
namespace App\Http\Controllers;
use App\Http\Requests\SendRoomSwitchCommandRequest;
use App\Http\Requests\ReceiveRoomRegisterRequest;
use App\Http\Requests\ReceiveRoomStatusDataRequest;
use App\Http\Requests\ReceiveSwitchRequest;
use App\Services\TcpSocketClient;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Auth;
@ -100,41 +99,30 @@ class RoomControlController extends Controller
return ApiResponse::success("命令已發送:$command");
}
public function receiveSwitch(SendRoomSwitchCommandRequest $request): JsonResponse
public function receiveSwitch(ReceiveSwitchRequest $request): JsonResponse
{
$validated = $request->validated();
$branch = Branch::where('name',$validated['branch_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'
}
$room = Room::where('branch_id', $branch->id)
->where('name', $roomName)
->where('type', $roomType)
->first();
if (!$branch) {
return ApiResponse::error('分店不存在');
if ($validated['status'] === 'error') {
$validated['started_at'] = null;
$validated['ended_at'] = null;
}
if (!$room) {
return ApiResponse::error('房間不存在');
}
if($validated['command']==='error'){
$room->is_online=0;
$room->status=$validated['command'];
$room->started_at=null;
$room->ended_at=null;
}else{
$room->status=$validated['command'];
$room->started_at=$validated['started_at'];
$room->ended_at=$validated['ended_at'];
}
$room->save();
return ApiResponse::success($room);
$room = Room::updateOrCreate(
['id' => $validated['id']],
[
'branch_id' => $validated['branch_id'],
'type' => $validated['type'],
'name' => $validated['name'],
'floor' => $validated['floor'],
'internal_ip' => $validated['internal_ip'],
'port' => $validated['port'],
'is_online' => $validated['is_online'],
'status' => $validated['status'],
'started_at' => $validated['started_at'],
'ended_at' => $validated['ended_at'],
]
);
return ApiResponse::success($room->refresh());
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
/**
* @OA\Schema(
* schema="ReceiveSwitchRequest",
* required={"id","branch_id", "floor", "type", "name", "is_online", "status"},
* @OA\Property(property="id", type="integer", example="1"),
* @OA\Property(property="branch_id", type="integer", example="1"),
* @OA\Property(property="floor", type="integer", example=2),
* @OA\Property(property="type", type="string", enum={"unset", "pc", "svr"}, example="pc"),
* @OA\Property(property="name", type="string", example="102"),
* @OA\Property(property="internal_ip", type="string", nullable=true, example="192.168.1.100"),
* @OA\Property(property="port", type="integer", nullable=true, example=3389),
* @OA\Property(property="is_online", type="integer", enum={0, 1}, example=1),
* @OA\Property(property="status", type="string", enum={"active", "closed", "fire", "error"}, example="active"),
* @OA\Property(property="started_at", type="string", format="date-time", nullable=true, example="2025-05-19 09:31:00"),
* @OA\Property(property="ended_at", type="string", format="date-time", nullable=true, example="2025-05-19 12:00:00")
* )
*/
class ReceiveSwitchRequest extends ApiRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'id' => ['required','integer','exists:rooms,id'],
'branch_id' => ['required','integer','exists:branches,id'],
'floor' => ['required','integer'],
'type' => ['required','in:unset,pc,svr'],
'name' => ['required','string'],
'internal_ip' => ['nullable','string'],
'port' => ['nullable','integer'],
'is_online' => ['required','integer'],
'status' => ['required','in:active,closed,fire,error'],
'started_at' => ['nullable','date_format:Y-m-d H:i:s'],
'ended_at' => ['nullable','date_format:Y-m-d H:i:s'],
];
}
}