lcly-data-admin/app/Console/Commands/LinkosDeviceLogArchiveComma...

170 lines
5.1 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Enums\DeviceType;
use App\Models\Device;
use App\Models\LinkosDeviceLog;
use App\Models\MeteorologicalMonitoringLog;
use App\Models\SoilMonitoringLog;
use App\Models\WaterQualityMonitoringLog;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
class LinkosDeviceLogArchiveCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'linkos:device-log-archive';
/**
* The console command description.
*
* @var string
*/
protected $description = 'LinkOS 设备流水归档';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$devices = Device::whereIn('type', [DeviceType::Meteorological, DeviceType::Soil, DeviceType::WaterQuality])->get();
// 物联平台目前只有水质监测设备和气象监测设备
LinkosDeviceLog::orderBy('reported_at', 'asc')->lazy()->each(function ($log) use ($devices) {
if (empty($log->data)) {
return;
}
foreach ($devices as $device) {
if ($device->sn !== $log->device_id) {
continue;
}
match ($device->type) {
DeviceType::Soil => $this->handleSoildDeviceLog($device, $log),
DeviceType::WaterQuality => $this->handleWaterQualityDeviceLog($device, $log),
DeviceType::Meteorological => $this->handleMeteorologicalDeviceLog($device, $log),
};
}
});
return Command::SUCCESS;
}
protected function handleSoildDeviceLog(Device $device, LinkosDeviceLog $log)
{
$attributes = [
'conductivity' => 'conductivity',
'soil_humidity' => 'humidity',
'soil_temperature' => 'temperature',
'nitrogen_content' => 'n',
'phosphorus_content' => 'p',
'potassium_content' => 'k',
];
if (! Arr::hasAny($log->data, array_keys($attributes))) {
return;
}
$monitoringLog = SoilMonitoringLog::firstOrCreate([
'device_id' => $device->id,
'monitored_at' => $log->reported_at->startOfHour(),
], [
'agricultural_base_id' => $device->agricultural_base_id,
]);
foreach ($attributes as $key => $attribute) {
if (! array_key_exists($key, $log->data)) {
continue;
}
$monitoringLog->{$attribute} = $log->data[$key];
}
$monitoringLog->save();
}
protected function handleWaterQualityDeviceLog(Device $device, LinkosDeviceLog $log)
{
$attributes = [
'conductivity' => 'conductivity',
'oxygen' => 'oxygen',
'chlorine' => 'chlorine',
'turbidity' => 'turbidity',
'temp' => 'temperature',
'ph' => 'ph',
];
if (! Arr::hasAny($log->data, array_keys($attributes))) {
return;
}
$monitoringLog = WaterQualityMonitoringLog::firstOrCreate([
'device_id' => $device->id,
'monitored_at' => $log->reported_at->startOfHour(),
], [
'agricultural_base_id' => $device->agricultural_base_id,
]);
foreach ($attributes as $key => $attribute) {
if (! array_key_exists($key, $log->data)) {
continue;
}
$monitoringLog->{$attribute} = $log->data[$key];
}
$monitoringLog->save();
}
protected function handleMeteorologicalDeviceLog(Device $device, LinkosDeviceLog $log)
{
$attributes = [
'wind_speed' => 'wind_speed',
'wind_power' => 'wind_power',
'wind_direction' => 'wind_direction',
'wind_degree' => 'wind_degree',
'box_humidity' => 'air_humidity',
'box_temperature' => 'air_temperature',
'box_pressure' => 'air_pressure',
'box_carbon' => 'co2',
'box_noise' => 'noise',
'box_illumination' => 'illumination',
'accumulate_rainfall' => 'accumulated_rainfall',
'current_rainfall' => 'current_rainfall',
'moment_rainfall' => 'moment_rainfall',
'day_rainfall' => 'day_rainfall',
'pm25_concentration' => 'pm25',
'pm10_concentration' => 'pm10',
];
if (! Arr::hasAny($log->data, array_keys($attributes))) {
return;
}
$monitoringLog = MeteorologicalMonitoringLog::firstOrCreate([
'device_id' => $device->id,
'monitored_at' => $log->reported_at->startOfHour(),
], [
'agricultural_base_id' => $device->agricultural_base_id,
]);
foreach ($attributes as $key => $attribute) {
if (! array_key_exists($key, $log->data)) {
continue;
}
$monitoringLog->{$attribute} = $log->data[$key];
}
$monitoringLog->save();
}
}