KTVCentral/app/Console/Commands/CheckRoomOnlineStatus.php

51 lines
1.6 KiB
PHP
Raw Normal View History

<?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) {
$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->status = RoomStatus::Error;
$room->save();
$response = (new MachineStatusForwarder(
$branch->external_ip ?? '',
'/api/room/receiveSwitch',
[
'branch_name' => $branch->name,
'room_name' => $room->type->value.$room->name,
'command' => 'error',
]
))->forward();
$this->info("Room [{$room->name}] marked as offline (no recent MachineStatus)");
}
}
return 0;
}
}