調整心跳封包邏輯 20250701

This commit is contained in:
allen.yan 2025-07-01 13:53:30 +08:00
parent ce58b1d9aa
commit f2205046ae

View File

@ -91,38 +91,10 @@ class RoomControlController extends Controller
$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->touch(); // 更新 updated_at
$room->save();
// 7. 回傳 token 與包廂資料
$branch = Branch::first();
return ApiResponse::success([
'token' => $token,
'room' => $room,
'branch_name' => $branch->name,
]);
}
@ -177,28 +149,32 @@ class RoomControlController extends Controller
$validated = $request->validated();
$roomType = null;
$roomName = null;
$floor = 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'
$floor = (int) substr($roomName, 0, 1);
}
$branch=Branch::where('name',$validated['branch_name'])->first();
$room = Room::where('branch_id', $branch->id)
->where('name', $roomName)
->where('type', $roomType)
->first();
// 決定 status 欄位值
$room = Room::firstOrNew([
'branch_id' => $branch->id,
'floor' => $floor,
'name' => $roomName,
'type' => $roomType,
]);
if ($room->exists && $room->internal_ip !== $validated['ip']) {
$validated['status'] = 'error';
if($room){
$validated['status']= 'online';
if($room->internal_ip != $validated['ip']){
$room->internal_ip = $validated['ip'];
$validated['status']='error';
} else {
$validated['status'] = 'online';
$room->internal_ip ??= $validated['ip']; // 新的話補上
}
$room->is_online=1;
$room->touch(); // 更新 updated_at
$room->save();
}
return ApiResponse::success([
'data' => MachineStatus::create($validated),