From 40df91fcc68d8a2f00d5d4cea982333a3fd9d42d Mon Sep 17 00:00:00 2001 From: Jing Li Date: Mon, 29 Jan 2024 12:44:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=AF=94=E6=98=82=E6=B0=B4?= =?UTF-8?q?=E8=B4=A8=E8=AE=BE=E5=A4=87=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/BiAng/DeviceLogSyncCommand.php | 73 +++++++------------ app/Iot/BiAng/HttpClient.php | 15 ++++ 2 files changed, 41 insertions(+), 47 deletions(-) diff --git a/app/Console/Commands/BiAng/DeviceLogSyncCommand.php b/app/Console/Commands/BiAng/DeviceLogSyncCommand.php index 1f93b16..ec8c93c 100644 --- a/app/Console/Commands/BiAng/DeviceLogSyncCommand.php +++ b/app/Console/Commands/BiAng/DeviceLogSyncCommand.php @@ -70,6 +70,7 @@ class DeviceLogSyncCommand extends Command if (! in_array($device->type, [ DeviceType::Monitor, DeviceType::Soil, + DeviceType::WaterQuality, DeviceType::Meteorological, DeviceType::Worm, DeviceType::InsectSexLure, @@ -84,6 +85,7 @@ class DeviceLogSyncCommand extends Command $this->info('设备类型: ' . match ($device->type) { DeviceType::Monitor => '苗情设备', DeviceType::Soil => '土壤设备', + DeviceType::WaterQuality => '水质设备', DeviceType::Meteorological => '气象设备', DeviceType::Worm => '虫情设备', DeviceType::InsectSexLure => '昆虫性诱设备', @@ -102,61 +104,26 @@ class DeviceLogSyncCommand extends Command ]); break; + case DeviceType::Soil: - $data = $httpClient->getLatestSoilReport($device->sn); - - if (empty($data)) { - $device->update([ - 'status' => DeviceStatus::Offline, - ]); - $this->warn('设备数据为空'); - break; - } - - $log = DeviceLog::firstOrCreate([ - 'device_id' => $device->id, - 'reported_at' => $data['time'], - ], [ - 'data' => Arr::except($data, ['deviceId', 'time']), - ]); - - $device->update([ - 'status' => $now->copy()->subMinutes(60)->lt($log->reported_at) ? DeviceStatus::Online : DeviceStatus::Offline, - ]); - break; - + case DeviceType::WaterQuality: case DeviceType::Meteorological: - $data = $httpClient->getLatestMeteorologicalReport($device->sn); - - if (empty($data)) { - $device->update([ - 'status' => DeviceStatus::Offline, - ]); - $this->warn('设备数据为空'); - break; - } - - $log = DeviceLog::firstOrCreate([ - 'device_id' => $device->id, - 'reported_at' => $data['time'], - ], [ - 'data' => Arr::except($data, ['deviceId', 'time']), - ]); - - $device->update([ - 'status' => $now->copy()->subMinutes(60)->lt($log->reported_at) ? DeviceStatus::Online : DeviceStatus::Offline, - ]); - break; - case DeviceType::InsecticidalLamp: - $data = $httpClient->getLatestLampReport($device->sn); + $data = match ($device->type) { + DeviceType::Soil => $httpClient->getLatestSoilReport($device->sn), + DeviceType::WaterQuality => $httpClient->getLatestWaterDeviceReport($device->sn), + DeviceType::Meteorological => $httpClient->getLatestMeteorologicalReport($device->sn), + DeviceType::InsecticidalLamp => $httpClient->getLatestLampReport($device->sn), + }; if (empty($data)) { $device->update([ 'status' => DeviceStatus::Offline, ]); - $this->warn('设备数据为空'); + $this->warn('设备数据: 无'); break; + } else { + $this->info('设备数据: '.json_encode($data)); } $log = DeviceLog::firstOrCreate([ @@ -183,8 +150,10 @@ class DeviceLogSyncCommand extends Command $device->update([ 'status' => DeviceStatus::Offline, ]); - $this->warn('设备数据为空'); + $this->warn('设备数据: 无'); break; + } else { + $this->info('设备数据: '.json_encode($data)); } foreach ($data['imgUrl'] as $item) { @@ -215,6 +184,16 @@ class DeviceLogSyncCommand extends Command break; } + switch ($device->status) { + case DeviceStatus::Online: + $this->info('设备状态: 在线'); + break; + + case DeviceStatus::Offline: + $this->warn('设备状态: 离线'); + break; + } + $this->info('同步成功!'); } catch (Throwable $e) { report($e); diff --git a/app/Iot/BiAng/HttpClient.php b/app/Iot/BiAng/HttpClient.php index 1c9b158..9975873 100644 --- a/app/Iot/BiAng/HttpClient.php +++ b/app/Iot/BiAng/HttpClient.php @@ -31,6 +31,21 @@ class HttpClient return $result['data'] ?? []; } + /** + * 获取最新的水质设备数据 + */ + public function getLatestWaterDeviceReport(string $deviceId) + { + $result = $this->get( + $this->apiUrl('/api/open-api/open/getWaterDeviceData'), + [ + 'deviceId' => $deviceId, + ] + ); + + return $result['data'] ?? []; + } + /** * 获取最新的气象数据 */