按天处理水质监测数据
parent
3653cad347
commit
3d66d38a1a
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class WaterQualityMonitoringDailyLog extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $casts = [
|
||||||
|
'monitored_at' => 'datetime',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'device_id',
|
||||||
|
'agricultural_base_id',
|
||||||
|
'chlorine',
|
||||||
|
'conductivity',
|
||||||
|
'oxygen',
|
||||||
|
'ph',
|
||||||
|
'temperature',
|
||||||
|
'turbidity',
|
||||||
|
'monitored_at',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -8,6 +8,7 @@ use App\Models\Device;
|
||||||
use App\Models\LinkosDeviceLog;
|
use App\Models\LinkosDeviceLog;
|
||||||
use App\Models\MeteorologicalMonitoringLog;
|
use App\Models\MeteorologicalMonitoringLog;
|
||||||
use App\Models\SoilMonitoringLog;
|
use App\Models\SoilMonitoringLog;
|
||||||
|
use App\Models\WaterQualityMonitoringDailyLog;
|
||||||
use App\Models\WaterQualityMonitoringLog;
|
use App\Models\WaterQualityMonitoringLog;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
|
@ -95,11 +96,18 @@ class LinkosDeviceLogService
|
||||||
|
|
||||||
if ($log->data) {
|
if ($log->data) {
|
||||||
foreach ($devices as $device) {
|
foreach ($devices as $device) {
|
||||||
match ($device->type) {
|
switch ($device->type) {
|
||||||
DeviceType::Soil => $this->handleSoilDeviceData($device, $log->data, $log->reported_at),
|
case DeviceType::Soil:
|
||||||
DeviceType::Meteorological => $this->handleMeteorologicalDeviceData($device, $log->data, $log->reported_at),
|
break;
|
||||||
DeviceType::WaterQuality => $this->handleWaterQualityDeviceData($device, $log->data, $log->reported_at),
|
|
||||||
};
|
case DeviceType::Meteorological:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DeviceType::WaterQuality:
|
||||||
|
$this->handleWaterQualityMonitoringLog($device, $log->data, $log->reported_at);
|
||||||
|
$this->handleWaterQualityMonitoringDailyLog($device, $log->reported_at);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -171,14 +179,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 $monitoredAt
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function handleWaterQualityDeviceData(Device $device, array $data, Carbon $reportedAt): void
|
public function handleWaterQualityMonitoringLog(Device $device, array $data, Carbon $monitoredAt): void
|
||||||
{
|
{
|
||||||
if (! Arr::hasAny($data, $this->waterQualityMonitoringFields)) {
|
if (! Arr::hasAny($data, $this->waterQualityMonitoringFields)) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -186,7 +194,7 @@ class LinkosDeviceLogService
|
||||||
|
|
||||||
$log = WaterQualityMonitoringLog::firstOrCreate([
|
$log = WaterQualityMonitoringLog::firstOrCreate([
|
||||||
'device_id' => $device->id,
|
'device_id' => $device->id,
|
||||||
'monitored_at' => $reportedAt->startOfHour(),
|
'monitored_at' => $monitoredAt->startOfHour(),
|
||||||
], [
|
], [
|
||||||
'agricultural_base_id' => $device->agricultural_base_id,
|
'agricultural_base_id' => $device->agricultural_base_id,
|
||||||
]);
|
]);
|
||||||
|
|
@ -201,4 +209,53 @@ class LinkosDeviceLogService
|
||||||
|
|
||||||
$log->save();
|
$log->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按天处理水质监测数据
|
||||||
|
*
|
||||||
|
* @param \App\Models\Device $device
|
||||||
|
* @param \Carbon\Carbon $date
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handleWaterQualityMonitoringDailyLog(Device $device, Carbon $date)
|
||||||
|
{
|
||||||
|
$logs = WaterQualityMonitoringLog::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 (['chlorine', 'conductivity', 'oxygen', 'ph', 'temperature', 'turbidity'] 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$dailyLog = WaterQualityMonitoringDailyLog::firstOrCreate([
|
||||||
|
'device_id' => $device->id,
|
||||||
|
'monitored_at' => $date,
|
||||||
|
], [
|
||||||
|
'agricultural_base_id' => $device->agricultural_base_id,
|
||||||
|
]);
|
||||||
|
|
||||||
|
foreach ($data as $key => $item) {
|
||||||
|
$dailyLog->{$key} = round(bcmul($item['sum'], $item['count'], 2), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
$dailyLog->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?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('water_quality_monitoring_daily_logs', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->unsignedBigInteger('device_id')->comment('设备ID');
|
||||||
|
$table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID');
|
||||||
|
$table->decimal('chlorine', 8, 2)->nullable()->comment('余氯 (单位: mg/L)');
|
||||||
|
$table->decimal('conductivity', 8, 2)->nullable()->comment('电导率 (单位: us/cm)');
|
||||||
|
$table->decimal('oxygen', 8, 2)->nullable()->comment('溶解氧 (单位: mg/L)');
|
||||||
|
$table->decimal('ph', 8, 2)->nullable()->comment('PH');
|
||||||
|
$table->decimal('temperature', 8, 2)->nullable()->comment('温度 (单位: ℃)');
|
||||||
|
$table->decimal('turbidity', 8, 2)->nullable()->comment('浊度 (单位: NTU)');
|
||||||
|
$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('water_quality_monitoring_daily_logs');
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue