diff --git a/app/Console/Commands/MonitorDeviceHealthCommand.php b/app/Console/Commands/MonitorDeviceHealthCommand.php new file mode 100644 index 0000000..6be915c --- /dev/null +++ b/app/Console/Commands/MonitorDeviceHealthCommand.php @@ -0,0 +1,126 @@ +argument('supplier')) { + // 移动千里眼 + case 'yidong': + $this->checkHealthBySupplierYidong(); + break; + + // 电信魔镜 + case 'dianxin': + $this->checkHealthBySupplierDianxin(); + break; + } + } + + public function checkHealthBySupplierYidong() + { + $client = new QlyHttpClient( + config('services.ydqly.appid'), + config('services.ydqly.secret'), + config('services.ydqly.rsa'), + ); + + $page = 1; + $pageSize = 100; + + do { + $result = $client->post( + '/v3/open/api/device/list', + [ + 'page' => $page, + 'pageSize' => $pageSize, + ], + ); + + foreach ($result['data'] as $item) { + $device = Device::supplierBy('device-supplier-yidong') + ->where('type', DeviceType::Monitor) + ->where('sn', $item['deviceId']) + ->first(); + + if (! in_array($device?->status, [DeviceStatus::Online, DeviceStatus::Offline])) { + continue; + } + + $device->update([ + 'status' => $item['deviceStatus'] === 1 ? DeviceStatus::Online : DeviceStatus::Offline, + ]); + } + + $page++; + } while (count($result['data']) === $pageSize); + } + + protected function checkHealthBySupplierDianxin(): void + { + $client = new MoJingHttpClient( + config('services.dxmj.app_id'), + config('services.dxmj.app_secret'), + ); + + $result = $client->get( + "/api/v3/channel/listByOrgId", + [ + 'id' => '1736649747126530049', + ], + ); + + $devices = Device::supplierBy('device-supplier-dianxin') + ->where('type', DeviceType::Monitor) + ->whereIn('status', [DeviceStatus::Online, DeviceStatus::Offline]) + ->get(); + + foreach ($devices as $device) { + foreach ($result['data'] as $item) { + if ($device->sn !== $item['channelcode']) { + continue; + } + + $device->update([ + 'extends' => [ + 'ip' => '', + 'port' => '', + 'username' => '', + 'password' => '', + 'passage' => $item['citId'], + 'rtsp_url' => '', + ], + 'status' => $item['channelstatus'] === 1 ? DeviceStatus::Online : DeviceStatus::Offline, + ]); + + break; + } + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ebb58f0..33101b2 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -22,6 +22,14 @@ class Kernel extends ConsoleKernel $schedule->command(Commands\Linkos\WormReportCommand::class) ->hourlyAt(15) ->runInBackground(); + + $schedule->command(Commands\MonitorDeviceHealthCommand::class, ['yidong']) + ->everyFiveMinutes() + ->runInBackground(); + + $schedule->command(Commands\MonitorDeviceHealthCommand::class, ['dianxin']) + ->everyFiveMinutes() + ->runInBackground(); } /** diff --git a/app/Http/Controllers/DeviceController.php b/app/Http/Controllers/DeviceController.php index 5c4f4bb..7dc1c7b 100644 --- a/app/Http/Controllers/DeviceController.php +++ b/app/Http/Controllers/DeviceController.php @@ -771,21 +771,26 @@ class DeviceController extends Controller // 中国电信魔镜 case 'device-supplier-dianxin': - $client = new MoJingHttpClient( - config('services.dxmj.app_id'), - config('services.dxmj.app_secret'), - ); - $result = $client->get( - "/api/v3/channel/RealTimeVideo/{$device->sn}", - [ - 'transType' => 4, - 'natType' => 1, - ], - ); + $address = ''; + + if ($channelId = data_get($device->extends, 'passage')) { + $client = new MoJingHttpClient( + config('services.dxmj.app_id'), + config('services.dxmj.app_secret'), + ); + $result = $client->get( + "/api/v3/channel/RealTimeVideo/{$channelId}", + [ + 'transType' => 4, + 'natType' => 1, + ], + ); + $address = data_get($result, 'data.playUrl'); + } return [ 'type' => 'flv', - 'address' => (string) data_get($result, 'data.playUrl'), + 'address' => (string) $address, // 有效期 29 分钟 'expires' => 1740, ]; diff --git a/app/Models/Device.php b/app/Models/Device.php index f9e699a..f652420 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -23,7 +23,7 @@ class Device extends Model protected $casts = [ 'type' => DeviceType::class, 'status' => DeviceStatus::class, - 'extends' => 'array', + 'extends' => 'json', ]; protected $fillable = [