190 lines
5.6 KiB
PHP
190 lines
5.6 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\MeteorologicalMonitoringHourlyLog;
|
|
use App\Models\SoilMonitoringHourlyLog;
|
|
use App\Models\WaterQualityMonitoringHourlyLog;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class LinkosDeviceLogService
|
|
{
|
|
/**
|
|
* 创建 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->handleSoilMonitoringLog($device, $log->data, $log->reported_at),
|
|
DeviceType::WaterQuality => $this->handleWaterQualityMonitoringLog($device, $log->data, $log->reported_at),
|
|
DeviceType::Meteorological => $this->handleMeteorologicalMonitoringLog($device, $log->data, $log->reported_at),
|
|
};
|
|
}
|
|
}
|
|
|
|
return $log;
|
|
}
|
|
|
|
/**
|
|
* 处理气象监控数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param array $data
|
|
* @param \Carbon\Carbon $monitoredAt
|
|
* @return void
|
|
*/
|
|
public function handleMeteorologicalMonitoringLog(Device $device, array $data, Carbon $monitoredAt): void
|
|
{
|
|
$attributes = [
|
|
'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',
|
|
];
|
|
|
|
if (! Arr::hasAny($data, $attributes)) {
|
|
return;
|
|
}
|
|
|
|
$hourlyLog = MeteorologicalMonitoringHourlyLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $monitoredAt->startOfHour(),
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
if (! array_key_exists($value, $data)) {
|
|
continue;
|
|
}
|
|
|
|
$hourlyLog->{$key} = $data[$value];
|
|
}
|
|
|
|
$hourlyLog->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
|
|
{
|
|
$attributes = [
|
|
'conductivity' => 'conductivity',
|
|
'oxygen' => 'oxygen',
|
|
'chlorine' => 'chlorine',
|
|
'turbidity' => 'turbidity',
|
|
'temperature' => 'temp',
|
|
'ph' => 'ph',
|
|
];
|
|
|
|
if (! Arr::hasAny($data, $attributes)) {
|
|
return;
|
|
}
|
|
|
|
$hourlyLog = WaterQualityMonitoringHourlyLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $monitoredAt->startOfHour(),
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
if (! array_key_exists($value, $data)) {
|
|
continue;
|
|
}
|
|
|
|
$hourlyLog->{$key} = $data[$value];
|
|
}
|
|
|
|
$hourlyLog->save();
|
|
}
|
|
|
|
/**
|
|
* 处理土壤监控数据
|
|
*
|
|
* @param \App\Models\Device $device
|
|
* @param array $data
|
|
* @param \Carbon\Carbon $monitoredAt
|
|
* @return void
|
|
*/
|
|
public function handleSoilMonitoringLog(Device $device, array $data, Carbon $monitoredAt): void
|
|
{
|
|
$attributes = [
|
|
'conductivity' => 'conductivity',
|
|
'humidity' => 'soil_humidity',
|
|
'temperature' => 'soil_temperature',
|
|
'n' => 'nitrogen_content',
|
|
'p' => 'phosphorus_content',
|
|
'k' => 'potassium_content',
|
|
];
|
|
|
|
if (! Arr::hasAny($data, $attributes)) {
|
|
return;
|
|
}
|
|
|
|
$hourlyLog = SoilMonitoringHourlyLog::firstOrCreate([
|
|
'device_id' => $device->id,
|
|
'monitored_at' => $monitoredAt->startOfHour(),
|
|
], [
|
|
'agricultural_base_id' => $device->agricultural_base_id,
|
|
]);
|
|
|
|
foreach ($attributes as $key => $value) {
|
|
if (! array_key_exists($value, $data)) {
|
|
continue;
|
|
}
|
|
|
|
$hourlyLog->{$key} = $data[$value];
|
|
}
|
|
|
|
$hourlyLog->save();
|
|
}
|
|
}
|