接入电压和移动设备

dev
Jing Li 2023-12-30 19:00:09 +08:00
parent efbbcb78c1
commit 96195debda
4 changed files with 152 additions and 13 deletions

View File

@ -0,0 +1,126 @@
<?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;
}
}
}
}

View File

@ -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();
}
/**

View File

@ -771,21 +771,26 @@ class DeviceController extends Controller
// 中国电信魔镜
case 'device-supplier-dianxin':
$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/{$device->sn}",
"/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,
];

View File

@ -23,7 +23,7 @@ class Device extends Model
protected $casts = [
'type' => DeviceType::class,
'status' => DeviceStatus::class,
'extends' => 'array',
'extends' => 'json',
];
protected $fillable = [