lcly-data-admin/app/Console/Commands/MonitorDeviceHealthCommand.php

115 lines
3.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\DeviceStatus;
use App\Enums\DeviceType;
use App\Models\Device;
use App\Iot\MoJing\HttpClient as MoJingHttpClient;
use App\Iot\Qly\HttpClient as QlyHttpClient;
use Illuminate\Console\Command;
class MonitorDeviceHealthCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monitor-device-health {supplier}';
/**
* The console command description.
*
* @var string
*/
protected $description = '监控设备心跳检查';
/**
* Execute the console command.
*/
public function handle()
{
switch ($this->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::where('sn', $item['deviceId'])
->where('supplier_key', 'device-supplier-yidong')
->where('type', DeviceType::Monitor)
->whereIn('status', [DeviceStatus::Online, DeviceStatus::Offline])
->update([
'status' => $item['deviceStatus'] === 1 ? DeviceStatus::Online : DeviceStatus::Offline,
'updated_at' => now(),
]);
}
$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',
],
);
foreach ($result['data'] as $item) {
Device::where('sn', $item['channelcode'])
->where('supplier_key', 'device-supplier-dianxin')
->where('type', DeviceType::Monitor)
->whereIn('status', [DeviceStatus::Online, DeviceStatus::Offline])
->update([
'extends' => json_encode([
'ip' => '',
'port' => '',
'username' => '',
'password' => '',
'passage' => $item['citId'],
'rtsp_url' => '',
]),
'status' => $item['channelstatus'] === 1 ? DeviceStatus::Online : DeviceStatus::Offline,
'updated_at' => now(),
]);
}
}
}