205 lines
5.9 KiB
PHP
205 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Enums\DeviceType;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\Device;
|
|
use App\Models\LinkosDeviceLog;
|
|
use App\Models\MeteorologicalMonitoringLog;
|
|
use App\Models\SoilMonitoringLog;
|
|
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) {
|
|
match ($device->type) {
|
|
DeviceType::Soil => $this->handleSoilDeviceData($device, $log->data, $log->reported_at),
|
|
DeviceType::Meteorological => $this->handleMeteorologicalDeviceData($device, $log->data, $log->reported_at),
|
|
DeviceType::WaterQuality => $this->handleWaterQualityDeviceData($device, $log->data, $log->reported_at),
|
|
};
|
|
}
|
|
}
|
|
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* 处理土壤设备数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param array $data
|
|
* @param \Carbon\Carbon $reportedAt
|
|
* @return void
|
|
*/
|
|
public function handleSoilDeviceData(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 array $data
|
|
* @param \Carbon\Carbon $reportedAt
|
|
* @return void
|
|
*/
|
|
public function handleMeteorologicalDeviceData(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 array $data
|
|
* @param \Carbon\Carbon $reportedAt
|
|
* @return void
|
|
*/
|
|
public function handleWaterQualityDeviceData(Device $device, array $data, Carbon $reportedAt): void
|
|
{
|
|
if (! Arr::hasAny($data, $this->waterQualityMonitoringFields)) {
|
|
return;
|
|
}
|
|
|
|
$log = WaterQualityMonitoringLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $reportedAt->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();
|
|
}
|
|
}
|