49 lines
1.6 KiB
PHP
49 lines
1.6 KiB
PHP
<?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;
|
||
}
|
||
} |