KTVCentral/app/Console/Commands/CheckRoomOnlineStatus.php
allen.yan 3307c062ab 加入 包廂控制紀錄
加入 設備紀錄
加入 歌曲庫 cache 列表
調整 包廂控制邏輯
調整 心跳封包邏輯
調整 驗証包廂狀態邏輯
20250606
2025-06-06 18:11:22 +08:00

45 lines
1.3 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) {
$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)");
}
$response = (new MachineStatusForwarder(
$branch->external_ip ?? '',
'/api/room/receiveSwitch',
$room->toArray()
))->forward();
}
return 0;
}
}