調整心跳封包邏輯 20250701

This commit is contained in:
allen.yan 2025-07-01 13:59:19 +08:00
parent 38ce25dc1c
commit da4ef0f0f3
2 changed files with 27 additions and 51 deletions

View File

@ -93,35 +93,7 @@ class RoomControlController extends Controller
$token = $user->api_plain_token; $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::first(); $branch = Branch::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 與包廂資料
return ApiResponse::success([ return ApiResponse::success([
'token' => $token, 'token' => $token,
'branch_name' => $branch->name, 'branch_name' => $branch->name,
@ -179,24 +151,29 @@ class RoomControlController extends Controller
$validated = $request->validated(); $validated = $request->validated();
$roomType = null; $roomType = null;
$roomName = null; $roomName = null;
$floor = null;
// 從 room_name例如 PC101, SVR01中擷取 type 與 name // 從 room_name例如 PC101, SVR01中擷取 type 與 name
if (preg_match('/^([A-Za-z]+)(\d+)$/', $validated['hostname'], $matches)) { if (preg_match('/^([A-Za-z]+)(\d+)$/', $validated['hostname'], $matches)) {
$roomType = strtolower($matches[1]); // 'PC' → 'pc' $roomType = strtolower($matches[1]); // 'PC' → 'pc'
$roomName = $matches[2]; // '101' $roomName = $matches[2]; // '101'
$floor = (int) substr($roomName, 0, 1);
} }
$branch=Branch::where('name',$validated['branch_name'])->first(); $branch=Branch::where('name',$validated['branch_name'])->first();
$room = Room::where('branch_id', $branch->id) $room = Room::firstOrNew([
->where('name', $roomName) 'branch_id' => $branch->id,
->where('type', $roomType) 'floor' => $floor,
->first(); 'name' => $roomName,
// 決定 status 欄位值 'type' => $roomType,
$validated['status']= 'error'; ]);
if($room){
$validated['status']= 'online'; if ($room->exists && $room->internal_ip !== $validated['ip']) {
if($room->internal_ip != $validated['ip']){ $validated['status'] = 'error';
$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->is_online=1;
$room->touch(); // 更新 updated_at $room->touch(); // 更新 updated_at
$room->save(); $room->save();
@ -207,8 +184,6 @@ class RoomControlController extends Controller
(new RoomResource($room))->toArray(request()) (new RoomResource($room))->toArray(request())
) )
)->forward(); )->forward();
}
return ApiResponse::success([ return ApiResponse::success([
'data' => MachineStatus::create($validated), 'data' => MachineStatus::create($validated),
]); ]);

View File

@ -28,6 +28,7 @@ class Room extends Model
use HasFactory, LogsModelActivity; use HasFactory, LogsModelActivity;
protected $fillable = [ protected $fillable = [
'branch_id',
'floor', 'floor',
'type', 'type',
'name', 'name',