調整心跳封包邏輯 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

@ -92,36 +92,8 @@ class RoomControlController extends Controller
} else {
$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();
$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([
'token' => $token,
'branch_name' => $branch->name,
@ -179,36 +151,39 @@ 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 欄位值
$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->touch(); // 更新 updated_at
$room->save();
$response = (
new MachineStatusForwarder(
$branch->external_ip ?? '',
'/api/room/receiveSwitch',
(new RoomResource($room))->toArray(request())
)
)->forward();
$room = Room::firstOrNew([
'branch_id' => $branch->id,
'floor' => $floor,
'name' => $roomName,
'type' => $roomType,
]);
if ($room->exists && $room->internal_ip !== $validated['ip']) {
$validated['status'] = 'error';
$room->internal_ip = $validated['ip'];
} else {
$validated['status'] = 'online';
$room->internal_ip ??= $validated['ip']; // 新的話補上
}
$room->is_online=1;
$room->touch(); // 更新 updated_at
$room->save();
$response = (
new MachineStatusForwarder(
$branch->external_ip ?? '',
'/api/room/receiveSwitch',
(new RoomResource($room))->toArray(request())
)
)->forward();
return ApiResponse::success([
'data' => MachineStatus::create($validated),
]);

View File

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