127 lines
3.4 KiB
PHP
127 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 = 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;
|
|
}
|
|
}
|
|
}
|
|
}
|