按天处理气象监测数据

dev
Jing Li 2022-10-24 17:27:29 +08:00
parent 3ae51b4f4e
commit 3b3acf74aa
6 changed files with 35705 additions and 4 deletions

View File

@ -47,7 +47,7 @@ class LinkosDeviceLogArchiveCommand extends Command
match ($device->type) { match ($device->type) {
DeviceType::Soil => $linkosDeviceLogService->handleSoilMonitoringLog($device, $log->data, $log->reported_at), DeviceType::Soil => $linkosDeviceLogService->handleSoilMonitoringLog($device, $log->data, $log->reported_at),
DeviceType::Meteorological => $linkosDeviceLogService->handleMeteorologicalDeviceData($device, $log->data, $log->reported_at), DeviceType::Meteorological => $linkosDeviceLogService->handleMeteorologicalMonitoringLog($device, $log->data, $log->reported_at),
DeviceType::WaterQuality => $linkosDeviceLogService->handleWaterQualityMonitoringLog($device, $log->data, $log->reported_at), DeviceType::WaterQuality => $linkosDeviceLogService->handleWaterQualityMonitoringLog($device, $log->data, $log->reported_at),
}; };
} }

View File

@ -0,0 +1,34 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MeteorologicalMonitoringDailyLog extends Model
{
use HasFactory;
protected $casts = [
'wind_direction' => WindDirection::class,
'monitored_at' => 'date',
];
protected $fillable = [
'device_id',
'agricultural_base_id',
'wind_speed',
'wind_direction',
'wind_degree',
'air_humidity',
'air_temperature',
'air_pressure',
'co2',
'noise',
'illumination',
'daily_rainfall',
'pm25',
'pm10',
'monitored_at',
];
}

View File

@ -3,9 +3,11 @@
namespace App\Services; namespace App\Services;
use App\Enums\DeviceType; use App\Enums\DeviceType;
use App\Enums\WindDirection;
use App\Exceptions\BizException; use App\Exceptions\BizException;
use App\Models\Device; use App\Models\Device;
use App\Models\LinkosDeviceLog; use App\Models\LinkosDeviceLog;
use App\Models\MeteorologicalMonitoringDailyLog;
use App\Models\MeteorologicalMonitoringLog; use App\Models\MeteorologicalMonitoringLog;
use App\Models\SoilMonitoringDailyLog; use App\Models\SoilMonitoringDailyLog;
use App\Models\SoilMonitoringLog; use App\Models\SoilMonitoringLog;
@ -104,6 +106,8 @@ class LinkosDeviceLogService
break; break;
case DeviceType::Meteorological: case DeviceType::Meteorological:
$this->handleMeteorologicalMonitoringLog($device, $log->data, $log->reported_at);
$this->handleMeteorologicalMonitoringDailyLog($device, $log->reported_at);
break; break;
case DeviceType::WaterQuality: case DeviceType::WaterQuality:
@ -199,14 +203,14 @@ class LinkosDeviceLogService
} }
/** /**
* 处理气象设备数据 * 按小时处理气象监测数据
* *
* @param \App\Models\Device $device * @param \App\Models\Device $device
* @param array $data * @param array $data
* @param \Carbon\Carbon $reportedAt * @param \Carbon\Carbon $reportedAt
* @return void * @return void
*/ */
public function handleMeteorologicalDeviceData(Device $device, array $data, Carbon $reportedAt): void public function handleMeteorologicalMonitoringLog(Device $device, array $data, Carbon $reportedAt): void
{ {
if (! Arr::hasAny($data, $this->meteorologicalMonitoringFields)) { if (! Arr::hasAny($data, $this->meteorologicalMonitoringFields)) {
return; return;
@ -230,6 +234,109 @@ class LinkosDeviceLogService
$log->save(); $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(bcmul($item['sum'], $item['count'], 2), 2);
break;
}
}
$dailyLog->save();
}
/** /**
* 按小时处理水质监测数据 * 按小时处理水质监测数据
* *
@ -310,4 +417,38 @@ class LinkosDeviceLogService
$dailyLog->save(); $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);
}
$degree = round(rad2deg(atan($x/$y)));
if (($x > 0 || $x < 0 ) && $y < 0) {
$degree += 180;
} elseif ($x < 0 && $y > 0) {
$degree += 360;
}
return $degree;
}
} }

View File

@ -23,7 +23,7 @@ return new class extends Migration
$table->decimal('ph', 8, 2)->nullable()->comment('PH'); $table->decimal('ph', 8, 2)->nullable()->comment('PH');
$table->decimal('temperature', 8, 2)->nullable()->comment('温度 (单位: ℃)'); $table->decimal('temperature', 8, 2)->nullable()->comment('温度 (单位: ℃)');
$table->decimal('turbidity', 8, 2)->nullable()->comment('浊度 (单位: NTU)'); $table->decimal('turbidity', 8, 2)->nullable()->comment('浊度 (单位: NTU)');
$table->date('monitored_at')->comment('监控时间(小时)'); $table->date('monitored_at')->comment('监控日期');
$table->timestamps(); $table->timestamps();
$table->index('agricultural_base_id'); $table->index('agricultural_base_id');

View File

@ -0,0 +1,49 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('meteorological_monitoring_daily_logs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('device_id')->comment('设备ID');
$table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID');
$table->decimal('wind_speed', 8, 2)->nullable()->comment('风速 (单位: m/s)');
$table->tinyInteger('wind_direction')->nullable()->comment('风向: 0 北风, 1 东北风, 2 东风, 3 东南风, 4 南风, 5 西南风, 6 西风, 7 西北风');
$table->integer('wind_degree')->nullable()->comment('风向度数');
$table->decimal('air_humidity', 8, 2)->nullable()->comment('空气湿度 (单位: %RH)');
$table->decimal('air_temperature', 8, 2)->nullable()->comment('气温 (单位: ℃)');
$table->decimal('air_pressure', 8, 2)->nullable()->comment('气压 (单位: Kpa)');
$table->decimal('co2', 8, 2)->nullable()->comment('二氧化碳浓度 (单位: ppm)');
$table->decimal('noise', 8, 2)->nullable()->comment('噪声 (单位: db)');
$table->decimal('illumination', 10, 2)->nullable()->comment('光照度 (单位: Lux)');
$table->decimal('daily_rainfall', 8, 2)->nullable()->comment('日积雨量 (单位: mm)');
$table->decimal('pm25', 8, 2)->nullable()->comment('PM2.5浓度 (单位: ug/m3)');
$table->decimal('pm10', 8, 2)->nullable()->comment('PM10浓度 (单位: ug/m3)');
$table->date('monitored_at')->comment('监控日期');
$table->timestamps();
$table->index('agricultural_base_id');
$table->unique(['device_id', 'monitored_at']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('meteorological_monitoring_daily_logs');
}
};

35477
map.geojson 100644

File diff suppressed because it is too large Load Diff