KTVCentral/app/Console/Commands/CheckRoomOnlineStatus.php
allen.yan 13b5e3e3e1 BUG 包廂控制畫面開不啟來問題
加入驗証包廂是否在線
20250605
2025-06-05 11:15:24 +08:00

49 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Room;
use App\Models\MachineStatus;
use App\Enums\RoomStatus;
use Carbon\Carbon;
use App\Services\MachineStatusForwarder;
class CheckRoomOnlineStatus extends Command
{
protected $signature = 'rooms:check-online-status';
protected $description = 'Check if each room has recent MachineStatus data, mark offline if outdated';
public function handle()
{
$now = Carbon::now();
$threshold = $now->subMinutes(10);
// 所有房間
$rooms = Room::with('branch')->where('is_online',1)->get();
foreach ($rooms as $room) {
// 找出最近的一筆 MachineStatus 資料(比對 hostname 和 branch_name
$latestStatus = MachineStatus::where('hostname', $room->type->value.$room->name)
//->where('branch_name', optional($room->branch)->name)
->latest('created_at')
->first();
if (!$latestStatus || $latestStatus->created_at < $threshold) {
$room->is_online = false;
$room->status = RoomStatus::Error;
$room->save();
$response = (new MachineStatusForwarder($rooms->branch->external_ip, "/api/room/receiveSwitch", [
"branch_name"=>$rooms->branch->name,
"room_name"=>$rooms->type->value.$rooms->name,
"command" =>"error",
]))->forward();
$this->info("Room [{$room->name}] marked as offline (no recent MachineStatus)");
}
}
return 0;
}
}