dev
Jing Li 2023-08-11 19:44:51 +08:00
parent f95a64535c
commit d304f30a52
2 changed files with 27 additions and 34 deletions

View File

@ -72,7 +72,7 @@ class DeviceLogSyncCommand extends Command
try { try {
switch ($device->supplier?->key) { switch ($device->supplier?->key) {
case 'device-supplier-biang': case 'device-supplier-biang':
(new BiAngDeviceLogService())->sync($device); (new BiAngDeviceLogService())->sync($device, $start, $end);
break; break;
} }

View File

@ -17,28 +17,18 @@ use Illuminate\Support\Facades\DB;
class BiAngDeviceLogService class BiAngDeviceLogService
{ {
public function sync(Device $device): void public function sync(Device $device, Carbon $startAt, Carbon $endAt): void
{ {
$config = json_decode($device->project?->value, true); $config = json_decode($device->project?->value, true);
if (! isset($config['username'], $config['password'])) { if (! isset($config['username'], $config['password'])) {
return; return;
} }
$httpClient = new BiAngHttpClient($config['username'], $config['password']); $httpClient = new BiAngHttpClient($config['username'], $config['password']);
$data = null;
switch ($device->type) { switch ($device->type) {
case DeviceType::Soil: case DeviceType::Soil:
$data = $httpClient->getLatestSoilReport($device->sn); $data = $httpClient->getLatestSoilReport($device->sn);
break;
case DeviceType::Meteorological:
$data = $httpClient->getLatestMeteorologicalReport($device->sn);
break;
}
if (is_array($data) && isset($data['time'])) {
$log = DeviceLog::firstOrCreate([ $log = DeviceLog::firstOrCreate([
'device_id' => $device->id, 'device_id' => $device->id,
'reported_at' => $data['time'], 'reported_at' => $data['time'],
@ -46,24 +36,27 @@ class BiAngDeviceLogService
'data' => Arr::except($data, ['deviceId', 'time']), 'data' => Arr::except($data, ['deviceId', 'time']),
]); ]);
switch ($device->status) { if (in_array($device->status, [DeviceStatus::Online, DeviceStatus::Offline])) {
case DeviceStatus::Online:
// 如果设备数据在线时最近60分钟内没有数据则视为离线
if (now()->subMinutes(60)->gt($log->reported_at)) {
$device->update([ $device->update([
'status' => DeviceStatus::Offline, 'status' => $startAt->lt($log->reported_at) ? DeviceStatus::Online : DeviceStatus::Offline,
]);
}
case DeviceType::Meteorological:
$data = $httpClient->getLatestMeteorologicalReport($device->sn);
$log = DeviceLog::firstOrCreate([
'device_id' => $device->id,
'reported_at' => $data['time'],
], [
'data' => Arr::except($data, ['deviceId', 'time']),
]);
if (in_array($device->status, [DeviceStatus::Online, DeviceStatus::Offline])) {
$device->update([
'status' => $startAt->lt($log->reported_at) ? DeviceStatus::Online : DeviceStatus::Offline,
]); ]);
} }
break; break;
case DeviceStatus::Offline:
// 如果设备数据离线时最近60分钟内有数据则视为在线
if (now()->subMinutes(60)->lt($log->reported_at)) {
$device->update([
'status' => DeviceStatus::Online,
]);
}
break;
}
} }
} }