調整 room 資料同步 20250605
This commit is contained in:
parent
0734a0294a
commit
fda661c616
@ -3,8 +3,7 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Requests\SendRoomSwitchCommandRequest;
|
use App\Http\Requests\SendRoomSwitchCommandRequest;
|
||||||
use App\Http\Requests\ReceiveRoomRegisterRequest;
|
use App\Http\Requests\ReceiveSwitchRequest;
|
||||||
use App\Http\Requests\ReceiveRoomStatusDataRequest;
|
|
||||||
use App\Services\TcpSocketClient;
|
use App\Services\TcpSocketClient;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
@ -100,41 +99,30 @@ class RoomControlController extends Controller
|
|||||||
|
|
||||||
return ApiResponse::success("命令已發送:$command");
|
return ApiResponse::success("命令已發送:$command");
|
||||||
}
|
}
|
||||||
public function receiveSwitch(SendRoomSwitchCommandRequest $request): JsonResponse
|
public function receiveSwitch(ReceiveSwitchRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
$validated = $request->validated();
|
$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) {
|
if ($validated['status'] === 'error') {
|
||||||
return ApiResponse::error('分店不存在');
|
$validated['started_at'] = null;
|
||||||
|
$validated['ended_at'] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$room) {
|
$room = Room::updateOrCreate(
|
||||||
return ApiResponse::error('房間不存在');
|
['id' => $validated['id']],
|
||||||
}
|
[
|
||||||
if($validated['command']==='error'){
|
'branch_id' => $validated['branch_id'],
|
||||||
$room->is_online=0;
|
'type' => $validated['type'],
|
||||||
$room->status=$validated['command'];
|
'name' => $validated['name'],
|
||||||
$room->started_at=null;
|
'floor' => $validated['floor'],
|
||||||
$room->ended_at=null;
|
'internal_ip' => $validated['internal_ip'],
|
||||||
}else{
|
'port' => $validated['port'],
|
||||||
$room->status=$validated['command'];
|
'is_online' => $validated['is_online'],
|
||||||
$room->started_at=$validated['started_at'];
|
'status' => $validated['status'],
|
||||||
$room->ended_at=$validated['ended_at'];
|
'started_at' => $validated['started_at'],
|
||||||
}
|
'ended_at' => $validated['ended_at'],
|
||||||
$room->save();
|
]
|
||||||
|
);
|
||||||
return ApiResponse::success($room);
|
return ApiResponse::success($room->refresh());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
47
app/Http/Requests/ReceiveSwitchRequest.php
Normal file
47
app/Http/Requests/ReceiveSwitchRequest.php
Normal 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'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user