1
0
Fork 0
internet-everythings-agricu.../app/Services/DeviceLogService.php

73 lines
1.9 KiB
PHP

<?php
namespace App\Services;
use App\Iot\Linkos\HttpClient as LinkosHttpClient;
use App\Models\Device;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
class DeviceLogService
{
/**
* 同步设备历史流水
*/
public function sync(Device $device, Carbon $start, Carbon $end): void
{
switch ($device->factory?->key) {
case 'link-os':
$this->syncLinkosDeviceLogs($device, $start, $end);
break;
}
}
/**
* 同步 Linkos 设备历史流水
*/
protected function syncLinkosDeviceLogs(Device $device, Carbon $start, Carbon $end): void
{
/** @var \App\Iot\Linkos\HttpClient */
$httpClient = app(LinkosHttpClient::class);
$page = 1;
$perPage = 50;
do {
$data = $httpClient->getDeviceFlowList(
$device->sn, $start, $end, $page, $perPage
);
$countResults = count($data['content']);
if ($countResults === 0) {
break;
}
foreach ($data['content'] as $item) {
$isSoilMonitoring = Arr::hasAny($item['data'], [
'soil_humidity',
'soil_temperature',
'nitrogen_content',
'potassium_content',
'phosphorus_content',
]);
if (($device->isTypeSoil() && ! $isSoilMonitoring) || (! $device->isTypeSoil() && $isSoilMonitoring)) {
continue;
}
$device->logs()->firstOrCreate([
'reported_at' => $item['createTime'],
], [
'data' => empty($item['data']) ? (new \stdClass) : $item['data'],
]);
}
unset($data);
$page++;
} while ($countResults === $perPage);
}
}