40 lines
1.1 KiB
PHP
40 lines
1.1 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;
|
|
|
|
|
|
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) {
|
|
$branch = optional($room->branch);
|
|
$latestStatus = MachineStatus::where('hostname', $room->type->value.$room->name)
|
|
->latest('created_at')
|
|
->first();
|
|
|
|
if (!$latestStatus || $latestStatus->created_at < $threshold) {
|
|
$room->is_online = false;
|
|
$room->save();
|
|
$this->info("Room [{$room->name}] marked as offline (no recent MachineStatus)");
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
} |