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

259 lines
9.2 KiB
PHP

<?php
namespace App\Services;
use App\Iot\Linkos\HttpClient as LinkosHttpClient;
use App\Models\Device;
use App\Models\MeteorologicalDailyReport;
use App\Models\MeteorologicalReport;
use App\Models\WaterQualityDailyReport;
use App\Models\WaterQualityReport;
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) {
if (! isset($item['data'])) {
continue;
}
$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);
}
/**
* 创建 linkos 气象设备每日报告
*/
public function createDailyReportToLinkosMeteorologicalDevice(Device $device, Carbon $date): void
{
/** @var \Illuminate\Database\Eloquent\Collection */
$meteorologicalReports = MeteorologicalReport::where('device_id', $device->id)
->whereDate('reported_at', $date)
->oldest('reported_at')
->get();
if ($meteorologicalReports->isEmpty()) {
return;
}
$result = $meteorologicalReports->reduce(function (array $carry, MeteorologicalReport $meteorologicalReport) {
foreach ($carry as $key => $item) {
if (is_null($v = $meteorologicalReport->{$key})) {
continue;
}
switch ($key) {
case 'wind_samples':
if (! is_null($meteorologicalReport->wind_degree) && ! is_null($meteorologicalReport->wind_speed)) {
$item['wind_samples'][] = [
'wind_degree' => $meteorologicalReport->wind_degree, // 风向度数
'wind_speed' => $meteorologicalReport->wind_speed, // 风速
];
}
break;
default:
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
break;
}
$carry[$key] = $item;
}
return $carry;
}, [
'today_rainfall' => ['sum' => 0, 'count' => 0],
'yesterday_rainfall' => ['sum' => 0, 'count' => 0],
'accumulate_rainfall' => ['sum' => 0, 'count' => 0],
'moment_rainfall' => ['sum' => 0, 'count' => 0],
'pm10' => ['sum' => 0, 'count' => 0],
'pm25' => ['sum' => 0, 'count' => 0],
'box_illumination' => ['sum' => 0, 'count' => 0],
'box_pressure' => ['sum' => 0, 'count' => 0],
'box_co2' => ['sum' => 0, 'count' => 0],
'box_temperature' => ['sum' => 0, 'count' => 0],
'box_humidity' => ['sum' => 0, 'count' => 0],
'box_noise' => ['sum' => 0, 'count' => 0],
'wind_samples' => [],
]);
$meteorologicalDailyReport = MeteorologicalDailyReport::firstOrCreate([
'device_id' => $device->id,
'reported_at' => $date,
]);
foreach ($result as $key => $item) {
switch ($key) {
case 'wind_samples':
$meteorologicalDailyReport->wind_degree = value(function (array $windSamples) {
$x = 0;
$y = 0;
foreach ($windSamples as $sample) {
if ($sample['wind_degree'] == 0 && $sample['wind_speed'] == 0) {
continue;
}
// 角度转弧度
$radian = deg2rad($sample['wind_degree']);
// $x += $sample['wind_speed'] * sin($radian);
// $y += $sample['wind_speed'] * cos($radian);
$x += sin($radian);
$y += cos($radian);
}
$degree = round(rad2deg(atan2($y, $x)));
if (($x > 0 || $x < 0) && $y < 0) {
$degree += 180;
} elseif ($x < 0 && $y > 0) {
$degree += 360;
}
return $degree;
}, $item);
$meteorologicalDailyReport->wind_direction = value(function ($windDegree) {
if ($windDegree >= 22.5 && $windDegree < 67.5) {
return MeteorologicalDailyReport::WIND_DIRECTION_NORTHEAST;
} elseif ($windDegree >= 67.5 && $windDegree < 112.5) {
return MeteorologicalDailyReport::WIND_DIRECTION_EAST;
} elseif ($windDegree >= 112.5 && $windDegree < 157.5) {
return MeteorologicalDailyReport::WIND_DIRECTION_SOUTHEAST;
} elseif ($windDegree >= 157.5 && $windDegree < 202.5) {
return MeteorologicalDailyReport::WIND_DIRECTION_SOUTH;
} elseif ($windDegree >= 202.5 && $windDegree < 247.5) {
return MeteorologicalDailyReport::WIND_DIRECTION_SOUTHWEST;
} elseif ($windDegree >= 247.5 && $windDegree < 292.5) {
return MeteorologicalDailyReport::WIND_DIRECTION_WEST;
} elseif ($windDegree >= 292.5 && $windDegree < 337.5) {
return MeteorologicalDailyReport::WIND_DIRECTION_NORTHWEST;
}
return MeteorologicalDailyReport::WIND_DIRECTION_NORTH;
}, $meteorologicalDailyReport->wind_degree);
break;
default:
if ($item['count'] > 0) {
$meteorologicalDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
}
break;
}
}
$meteorologicalDailyReport->save();
}
/**
* 创建 linkos 水质设备每日报告
*/
public function createDailyReportToLinkosWaterQualityDevice(Device $device, Carbon $date): void
{
/** @var \Illuminate\Database\Eloquent\Collection */
$waterQualityReports = WaterQualityReport::where('device_id', $device->id)
->whereDate('reported_at', $date)
->oldest('reported_at')
->get();
if ($waterQualityReports->isEmpty()) {
return;
}
$result = $waterQualityReports->reduce(function (array $carry, WaterQualityReport $waterQualityReport) {
foreach ($carry as $key => $item) {
if (is_null($v = $waterQualityReport->{$key})) {
continue;
}
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
$carry[$key] = $item;
}
return $carry;
}, [
'chlorine' => ['sum' => 0, 'count' => 0],
'conductivity' => ['sum' => 0, 'count' => 0],
'oxygen' => ['sum' => 0, 'count' => 0],
'ph' => ['sum' => 0, 'count' => 0],
'temperature' => ['sum' => 0, 'count' => 0],
'turbidity' => ['sum' => 0, 'count' => 0],
]);
$waterQualityDailyReport = WaterQualityDailyReport::firstOrCreate([
'device_id' => $device->id,
'reported_at' => $date->format('Y-m-d'),
]);
foreach ($result as $key => $item) {
if ($item['count'] > 0) {
$waterQualityDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
}
}
$waterQualityDailyReport->save();
}
}