Update
parent
32a6925725
commit
31874831f7
|
|
@ -46,9 +46,9 @@ class LinkosDeviceLogArchiveCommand extends Command
|
|||
}
|
||||
|
||||
match ($device->type) {
|
||||
DeviceType::Soil => $linkosDeviceLogService->handleSoilMonitoringLog($device, $log->data, $log->reported_at),
|
||||
DeviceType::WaterQuality => $linkosDeviceLogService->handleWaterQualityMonitoringLog($device, $log->data, $log->reported_at),
|
||||
DeviceType::Meteorological => $linkosDeviceLogService->handleMeteorologicalMonitoringLog($device, $log->data, $log->reported_at),
|
||||
DeviceType::Soil => $linkosDeviceLogService->handleSoilDeviceData($device, $log->data, $log->reported_at),
|
||||
DeviceType::Meteorological => $linkosDeviceLogService->handleMeteorologicalDeviceData($device, $log->data, $log->reported_at),
|
||||
DeviceType::WaterQuality => $linkosDeviceLogService->handleWaterQualityDeviceData($device, $log->data, $log->reported_at),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use App\Enums\WindDirection;
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class MeteorologicalMonitoringHourlyLog extends Model
|
||||
class MeteorologicalMonitoringLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
|
|
@ -5,7 +5,7 @@ namespace App\Models;
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SoilMonitoringHourlyLog extends Model
|
||||
class SoilMonitoringLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
|
|
@ -5,7 +5,7 @@ namespace App\Models;
|
|||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class WaterQualityMonitoringHourlyLog extends Model
|
||||
class WaterQualityMonitoringLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
|
|
@ -6,14 +6,65 @@ 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 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 设备数据
|
||||
*
|
||||
|
|
@ -45,9 +96,9 @@ class LinkosDeviceLogService
|
|||
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),
|
||||
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),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -56,134 +107,98 @@ class LinkosDeviceLogService
|
|||
}
|
||||
|
||||
/**
|
||||
* 处理气象监控数据
|
||||
* 处理土壤设备数据
|
||||
*
|
||||
* @param \App\Models\Device $device
|
||||
* @param array $data
|
||||
* @param \Carbon\Carbon $monitoredAt
|
||||
* @param \Carbon\Carbon $reportedAt
|
||||
* @return void
|
||||
*/
|
||||
public function handleMeteorologicalMonitoringLog(Device $device, array $data, Carbon $monitoredAt): void
|
||||
public function handleSoilDeviceData(Device $device, array $data, Carbon $reportedAt): 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)) {
|
||||
if (! Arr::hasAny($data, $this->soilMonitoringFields)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$hourlyLog = MeteorologicalMonitoringHourlyLog::firstOrCreate([
|
||||
$log = SoilMonitoringLog::firstOrCreate([
|
||||
'device_id' => $device->id,
|
||||
'monitored_at' => $monitoredAt->startOfHour(),
|
||||
'monitored_at' => $reportedAt->startOfHour(),
|
||||
], [
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
]);
|
||||
|
||||
foreach ($attributes as $key => $value) {
|
||||
if (! array_key_exists($value, $data)) {
|
||||
foreach ($this->soilMonitoringFields as $attribute => $key) {
|
||||
if (! array_key_exists($key, $data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hourlyLog->{$key} = $data[$value];
|
||||
$log->{$attribute} = $data[$key];
|
||||
}
|
||||
|
||||
$hourlyLog->save();
|
||||
$log->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理水质监控数据
|
||||
* 处理气象设备数据
|
||||
*
|
||||
* @param \App\Models\Device $device
|
||||
* @param array $data
|
||||
* @param \Carbon\Carbon $monitoredAt
|
||||
* @param \Carbon\Carbon $reportedAt
|
||||
* @return void
|
||||
*/
|
||||
public function handleWaterQualityMonitoringLog(Device $device, array $data, Carbon $monitoredAt): void
|
||||
public function handleMeteorologicalDeviceData(Device $device, array $data, Carbon $reportedAt): void
|
||||
{
|
||||
$attributes = [
|
||||
'conductivity' => 'conductivity',
|
||||
'oxygen' => 'oxygen',
|
||||
'chlorine' => 'chlorine',
|
||||
'turbidity' => 'turbidity',
|
||||
'temperature' => 'temp',
|
||||
'ph' => 'ph',
|
||||
];
|
||||
|
||||
if (! Arr::hasAny($data, $attributes)) {
|
||||
if (! Arr::hasAny($data, $this->meteorologicalMonitoringFields)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$hourlyLog = WaterQualityMonitoringHourlyLog::firstOrCreate([
|
||||
$log = MeteorologicalMonitoringLog::firstOrCreate([
|
||||
'device_id' => $device->id,
|
||||
'monitored_at' => $monitoredAt->startOfHour(),
|
||||
'monitored_at' => $reportedAt->startOfHour(),
|
||||
], [
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
]);
|
||||
|
||||
foreach ($attributes as $key => $value) {
|
||||
if (! array_key_exists($value, $data)) {
|
||||
foreach ($this->meteorologicalMonitoringFields as $attribute => $key) {
|
||||
if (! array_key_exists($key, $data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hourlyLog->{$key} = $data[$value];
|
||||
$log->{$attribute} = $data[$key];
|
||||
}
|
||||
|
||||
$hourlyLog->save();
|
||||
$log->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理土壤监控数据
|
||||
* 处理水质设备数据
|
||||
*
|
||||
* @param \App\Models\Device $device
|
||||
* @param array $data
|
||||
* @param \Carbon\Carbon $monitoredAt
|
||||
* @param \Carbon\Carbon $reportedAt
|
||||
* @return void
|
||||
*/
|
||||
public function handleSoilMonitoringLog(Device $device, array $data, Carbon $monitoredAt): void
|
||||
public function handleWaterQualityDeviceData(Device $device, array $data, Carbon $reportedAt): void
|
||||
{
|
||||
$attributes = [
|
||||
'conductivity' => 'conductivity',
|
||||
'humidity' => 'soil_humidity',
|
||||
'temperature' => 'soil_temperature',
|
||||
'n' => 'nitrogen_content',
|
||||
'p' => 'phosphorus_content',
|
||||
'k' => 'potassium_content',
|
||||
];
|
||||
|
||||
if (! Arr::hasAny($data, $attributes)) {
|
||||
if (! Arr::hasAny($data, $this->waterQualityMonitoringFields)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$hourlyLog = SoilMonitoringHourlyLog::firstOrCreate([
|
||||
$log = WaterQualityMonitoringLog::firstOrCreate([
|
||||
'device_id' => $device->id,
|
||||
'monitored_at' => $monitoredAt->startOfHour(),
|
||||
'monitored_at' => $reportedAt->startOfHour(),
|
||||
], [
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
]);
|
||||
|
||||
foreach ($attributes as $key => $value) {
|
||||
if (! array_key_exists($value, $data)) {
|
||||
foreach ($this->waterQualityMonitoringFields as $attribute => $key) {
|
||||
if (! array_key_exists($key, $data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hourlyLog->{$key} = $data[$value];
|
||||
$log->{$attribute} = $data[$key];
|
||||
}
|
||||
|
||||
$hourlyLog->save();
|
||||
$log->save();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ return new class extends Migration
|
|||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('soil_monitoring_hourly_logs', function (Blueprint $table) {
|
||||
Schema::create('soil_monitoring_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('device_id')->comment('设备ID');
|
||||
$table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID');
|
||||
|
|
@ -38,6 +38,6 @@ return new class extends Migration
|
|||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('soil_monitoring_hourly_logs');
|
||||
Schema::dropIfExists('soil_monitoring_logs');
|
||||
}
|
||||
};
|
||||
|
|
@ -13,7 +13,7 @@ return new class extends Migration
|
|||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('meteorological_monitoring_hourly_logs', function (Blueprint $table) {
|
||||
Schema::create('meteorological_monitoring_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('device_id')->comment('设备ID');
|
||||
$table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID');
|
||||
|
|
@ -47,6 +47,6 @@ return new class extends Migration
|
|||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('meteorological_monitoring_hourly_logs');
|
||||
Schema::dropIfExists('meteorological_monitoring_logs');
|
||||
}
|
||||
};
|
||||
|
|
@ -13,7 +13,7 @@ return new class extends Migration
|
|||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('water_quality_monitoring_hourly_logs', function (Blueprint $table) {
|
||||
Schema::create('water_quality_monitoring_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('device_id')->comment('设备ID');
|
||||
$table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID');
|
||||
|
|
@ -38,6 +38,6 @@ return new class extends Migration
|
|||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('water_quality_monitoring_hourly_logs');
|
||||
Schema::dropIfExists('water_quality_monitoring_logs');
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue