457 lines
14 KiB
PHP
457 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Enums\DeviceType;
|
|
use App\Enums\WindDirection;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\Device;
|
|
use App\Models\LinkosDeviceLog;
|
|
use App\Models\MeteorologicalMonitoringDailyLog;
|
|
use App\Models\MeteorologicalMonitoringLog;
|
|
use App\Models\SoilMonitoringDailyLog;
|
|
use App\Models\SoilMonitoringLog;
|
|
use App\Models\WaterQualityMonitoringDailyLog;
|
|
use App\Models\WaterQualityMonitoringLog;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class LinkosDeviceLogService
|
|
{
|
|
/**
|
|
* 土壤监测数据字段
|
|
*
|
|
* @var array<string,string>
|
|
*/
|
|
protected $soilMonitoringFields = [
|
|
'conductivity' => 'conductivity',
|
|
'humidity' => 'soil_humidity',
|
|
'temperature' => 'soil_temperature',
|
|
'n' => 'nitrogen_content',
|
|
'p' => 'phosphorus_content',
|
|
'k' => 'potassium_content',
|
|
];
|
|
|
|
/**
|
|
* 气象监测数据字段
|
|
*
|
|
* @var array<string,string>
|
|
*/
|
|
protected $meteorologicalMonitoringFields = [
|
|
'wind_speed' => 'wind_speed',
|
|
'wind_power' => 'wind_power',
|
|
'wind_direction' => 'wind_direction',
|
|
'wind_degree' => 'wind_degree',
|
|
'air_humidity' => 'box_humidity',
|
|
'air_temperature' => 'box_temperature',
|
|
'air_pressure' => 'box_pressure',
|
|
'co2' => 'box_carbon',
|
|
'noise' => 'box_noise',
|
|
'illumination' => 'box_illumination',
|
|
'accumulated_rainfall' => 'accumulate_rainfall',
|
|
'current_rainfall' => 'current_rainfall',
|
|
'moment_rainfall' => 'moment_rainfall',
|
|
'pm25' => 'pm25_concentration',
|
|
'pm10' => 'pm10_concentration',
|
|
];
|
|
|
|
/**
|
|
* 水质监测数据字段
|
|
*
|
|
* @var array<string,string>
|
|
*/
|
|
protected $waterQualityMonitoringFields = [
|
|
'conductivity' => 'conductivity',
|
|
'oxygen' => 'oxygen',
|
|
'chlorine' => 'chlorine',
|
|
'turbidity' => 'turbidity',
|
|
'temperature' => 'temp',
|
|
'ph' => 'ph',
|
|
];
|
|
|
|
/**
|
|
* 创建 Linkos 设备数据
|
|
*
|
|
* @param string $deviceId
|
|
* @param string $deviceUnit
|
|
* @param string $deviceCategory
|
|
* @param array $data
|
|
* @param \Carbon\Carbon $reportedAt
|
|
* @return \App\Models\LinkosDeviceLog
|
|
*
|
|
* @throws \App\Exceptions\BizException
|
|
*/
|
|
public function create(string $deviceId, string $deviceUnit, string $deviceCategory, array $data, Carbon $reportedAt): LinkosDeviceLog
|
|
{
|
|
$devices = Device::where('sn', $deviceId)->get();
|
|
|
|
if ($devices->isEmpty()) {
|
|
throw new BizException("设备未找到, 设备编号: {$deviceId}");
|
|
}
|
|
|
|
$log = LinkosDeviceLog::create([
|
|
'device_id' => $deviceId,
|
|
'device_unit' => $deviceUnit,
|
|
'device_category' => $deviceCategory,
|
|
'data' => $data,
|
|
'reported_at' => $reportedAt,
|
|
]);
|
|
|
|
if ($log->data) {
|
|
foreach ($devices as $device) {
|
|
switch ($device->type) {
|
|
case DeviceType::Soil:
|
|
$this->handleSoilMonitoringLog($device, $log->data, $log->reported_at);
|
|
$this->handleSoilMonitoringDailyLog($device, $log->reported_at);
|
|
break;
|
|
|
|
case DeviceType::Meteorological:
|
|
$this->handleMeteorologicalMonitoringLog($device, $log->data, $log->reported_at);
|
|
$this->handleMeteorologicalMonitoringDailyLog($device, $log->reported_at);
|
|
break;
|
|
|
|
case DeviceType::WaterQuality:
|
|
$this->handleWaterQualityMonitoringLog($device, $log->data, $log->reported_at);
|
|
$this->handleWaterQualityMonitoringDailyLog($device, $log->reported_at);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* 按小时处理土壤监测数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param array $data
|
|
* @param \Carbon\Carbon $reportedAt
|
|
* @return void
|
|
*/
|
|
public function handleSoilMonitoringLog(Device $device, array $data, Carbon $reportedAt): void
|
|
{
|
|
if (! Arr::hasAny($data, $this->soilMonitoringFields)) {
|
|
return;
|
|
}
|
|
|
|
$log = SoilMonitoringLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $reportedAt->startOfHour(),
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($this->soilMonitoringFields as $attribute => $key) {
|
|
if (! array_key_exists($key, $data)) {
|
|
continue;
|
|
}
|
|
|
|
$log->{$attribute} = $data[$key];
|
|
}
|
|
|
|
$log->save();
|
|
}
|
|
|
|
/**
|
|
* 按天处理土壤监测数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param \Carbon\Carbon $date
|
|
* @return void
|
|
*/
|
|
public function handleSoilMonitoringDailyLog(Device $device, Carbon $date)
|
|
{
|
|
$logs = SoilMonitoringLog::query()
|
|
->where('device_id', $device->id)
|
|
->whereBetween('monitored_at', [$date->copy()->startOfDay(), $date->copy()->endOfDay()])
|
|
->oldest('monitored_at')
|
|
->get();
|
|
|
|
if ($logs->isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
$data = [];
|
|
|
|
foreach ($logs as $log) {
|
|
foreach (['conductivity', 'humidity', 'temperature', 'n', 'p', 'k'] as $key) {
|
|
if (is_null($v = $log->{$key})) {
|
|
continue;
|
|
}
|
|
|
|
$item = $data[$key] ?? ['sum' => 0, 'count' => 0];
|
|
$item['sum'] = bcadd($item['sum'], $v, 2);
|
|
$item['count']++;
|
|
|
|
$data[$key] = $item;
|
|
}
|
|
}
|
|
|
|
$dailyLog = SoilMonitoringDailyLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $date,
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($data as $key => $item) {
|
|
$dailyLog->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
|
|
}
|
|
|
|
$dailyLog->save();
|
|
}
|
|
|
|
/**
|
|
* 按小时处理气象监测数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param array $data
|
|
* @param \Carbon\Carbon $reportedAt
|
|
* @return void
|
|
*/
|
|
public function handleMeteorologicalMonitoringLog(Device $device, array $data, Carbon $reportedAt): void
|
|
{
|
|
if (! Arr::hasAny($data, $this->meteorologicalMonitoringFields)) {
|
|
return;
|
|
}
|
|
|
|
$log = MeteorologicalMonitoringLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $reportedAt->startOfHour(),
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($this->meteorologicalMonitoringFields as $attribute => $key) {
|
|
if (! array_key_exists($key, $data)) {
|
|
continue;
|
|
}
|
|
|
|
$log->{$attribute} = $data[$key];
|
|
}
|
|
|
|
$log->save();
|
|
}
|
|
|
|
/**
|
|
* 按天处理气象监测数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param \Carbon\Carbon $date
|
|
* @return void
|
|
*/
|
|
public function handleMeteorologicalMonitoringDailyLog(Device $device, Carbon $date)
|
|
{
|
|
$logs = MeteorologicalMonitoringLog::query()
|
|
->where('device_id', $device->id)
|
|
->whereBetween('monitored_at', [$date->copy()->startOfDay(), $date->copy()->endOfDay()])
|
|
->oldest('monitored_at')
|
|
->get();
|
|
|
|
if ($logs->isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
$data = [];
|
|
|
|
foreach ($logs as $log) {
|
|
foreach ([
|
|
'wind_speed',
|
|
'air_humidity',
|
|
'air_temperature',
|
|
'air_pressure',
|
|
'co2',
|
|
'noise',
|
|
'illumination',
|
|
'pm25',
|
|
'pm10',
|
|
] as $key) {
|
|
if (is_null($v = $log->{$key})) {
|
|
continue;
|
|
}
|
|
|
|
$item = $data[$key] ?? ['sum' => 0, 'count' => 0];
|
|
$item['sum'] = bcadd($item['sum'], $v, 2);
|
|
$item['count']++;
|
|
|
|
$data[$key] = $item;
|
|
}
|
|
|
|
// 日积雨量
|
|
if (! is_null($log->current_rainfall)) {
|
|
$data['current_rainfall'] = $log->current_rainfall;
|
|
}
|
|
|
|
if (! is_null($log->wind_degree) && ! is_null($log->wind_speed)) {
|
|
$data['wind_samples'][] = [
|
|
'wind_degree' => $log->wind_degree, // 风向度数
|
|
'wind_speed' => $log->wind_speed, // 风速
|
|
];
|
|
}
|
|
}
|
|
|
|
$dailyLog = MeteorologicalMonitoringDailyLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $date,
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($data as $key => $item) {
|
|
switch ($key) {
|
|
case 'wind_samples':
|
|
$windDegree = $this->calculateWindDegree($item);
|
|
|
|
if ($windDegree >= 22.5 && $windDegree < 67.5) {
|
|
$dailyLog->wind_direction = WindDirection::Northeast;
|
|
} elseif ($windDegree >= 67.5 && $windDegree < 112.5) {
|
|
$dailyLog->wind_direction = WindDirection::East;
|
|
} elseif ($windDegree >= 112.5 && $windDegree < 157.5) {
|
|
$dailyLog->wind_direction = WindDirection::Southeast;
|
|
} elseif ($windDegree >= 157.5 && $windDegree < 202.5) {
|
|
$dailyLog->wind_direction = WindDirection::South;
|
|
} elseif ($windDegree >= 202.5 && $windDegree < 247.5) {
|
|
$dailyLog->wind_direction = WindDirection::Southwest;
|
|
} elseif ($windDegree >= 247.5 && $windDegree < 292.5) {
|
|
$dailyLog->wind_direction = WindDirection::West;
|
|
} elseif ($windDegree >= 292.5 && $windDegree < 337.5) {
|
|
$dailyLog->wind_direction = WindDirection::Northwest;
|
|
} else {
|
|
$dailyLog->wind_direction = WindDirection::North;
|
|
}
|
|
|
|
$dailyLog->wind_degree = $windDegree;
|
|
break;
|
|
|
|
case 'current_rainfall':
|
|
$dailyLog->daily_rainfall = $item;
|
|
break;
|
|
|
|
default:
|
|
$dailyLog->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
|
|
break;
|
|
}
|
|
}
|
|
|
|
$dailyLog->save();
|
|
}
|
|
|
|
/**
|
|
* 按小时处理水质监测数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param array $data
|
|
* @param \Carbon\Carbon $monitoredAt
|
|
* @return void
|
|
*/
|
|
public function handleWaterQualityMonitoringLog(Device $device, array $data, Carbon $monitoredAt): void
|
|
{
|
|
if (! Arr::hasAny($data, $this->waterQualityMonitoringFields)) {
|
|
return;
|
|
}
|
|
|
|
$log = WaterQualityMonitoringLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $monitoredAt->startOfHour(),
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($this->waterQualityMonitoringFields as $attribute => $key) {
|
|
if (! array_key_exists($key, $data)) {
|
|
continue;
|
|
}
|
|
|
|
$log->{$attribute} = $data[$key];
|
|
}
|
|
|
|
$log->save();
|
|
}
|
|
|
|
/**
|
|
* 按天处理水质监测数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param \Carbon\Carbon $date
|
|
* @return void
|
|
*/
|
|
public function handleWaterQualityMonitoringDailyLog(Device $device, Carbon $date)
|
|
{
|
|
$logs = WaterQualityMonitoringLog::query()
|
|
->where('device_id', $device->id)
|
|
->whereBetween('monitored_at', [$date->copy()->startOfDay(), $date->copy()->endOfDay()])
|
|
->oldest('monitored_at')
|
|
->get();
|
|
|
|
if ($logs->isEmpty()) {
|
|
return;
|
|
}
|
|
|
|
$data = [];
|
|
|
|
foreach ($logs as $log) {
|
|
foreach (['chlorine', 'conductivity', 'oxygen', 'ph', 'temperature', 'turbidity'] as $key) {
|
|
if (is_null($v = $log->{$key})) {
|
|
continue;
|
|
}
|
|
|
|
$item = $data[$key] ?? ['sum' => 0, 'count' => 0];
|
|
$item['sum'] = bcadd($item['sum'], $v, 2);
|
|
$item['count']++;
|
|
|
|
$data[$key] = $item;
|
|
}
|
|
}
|
|
|
|
$dailyLog = WaterQualityMonitoringDailyLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $date,
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($data as $key => $item) {
|
|
$dailyLog->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
|
|
}
|
|
|
|
$dailyLog->save();
|
|
}
|
|
|
|
/**
|
|
* 矢量法计算风向度数
|
|
*
|
|
* @param array $windSamples
|
|
* @return int
|
|
*/
|
|
protected function calculateWindDegree(array $windSamples): int
|
|
{
|
|
$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(atan($x / $y)));
|
|
|
|
if (($x > 0 || $x < 0) && $y < 0) {
|
|
$degree += 180;
|
|
} elseif ($x < 0 && $y > 0) {
|
|
$degree += 360;
|
|
}
|
|
|
|
return $degree;
|
|
}
|
|
}
|