按天处理土壤监测数据
parent
3d66d38a1a
commit
b6a2a3cdf0
|
|
@ -46,9 +46,9 @@ class LinkosDeviceLogArchiveCommand extends Command
|
|||
}
|
||||
|
||||
match ($device->type) {
|
||||
DeviceType::Soil => $linkosDeviceLogService->handleSoilDeviceData($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::WaterQuality => $linkosDeviceLogService->handleWaterQualityDeviceData($device, $log->data, $log->reported_at),
|
||||
DeviceType::WaterQuality => $linkosDeviceLogService->handleWaterQualityMonitoringLog($device, $log->data, $log->reported_at),
|
||||
};
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class SoilMonitoringDailyLog extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $casts = [
|
||||
'monitored_at' => 'date',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'device_id',
|
||||
'agricultural_base_id',
|
||||
'conductivity',
|
||||
'humidity',
|
||||
'temperature',
|
||||
'n',
|
||||
'p',
|
||||
'k',
|
||||
'monitored_at',
|
||||
];
|
||||
}
|
||||
|
|
@ -10,7 +10,7 @@ class WaterQualityMonitoringDailyLog extends Model
|
|||
use HasFactory;
|
||||
|
||||
protected $casts = [
|
||||
'monitored_at' => 'datetime',
|
||||
'monitored_at' => 'date',
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ use App\Exceptions\BizException;
|
|||
use App\Models\Device;
|
||||
use App\Models\LinkosDeviceLog;
|
||||
use App\Models\MeteorologicalMonitoringLog;
|
||||
use App\Models\SoilMonitoringDailyLog;
|
||||
use App\Models\SoilMonitoringLog;
|
||||
use App\Models\WaterQualityMonitoringDailyLog;
|
||||
use App\Models\WaterQualityMonitoringLog;
|
||||
|
|
@ -98,6 +99,8 @@ class LinkosDeviceLogService
|
|||
foreach ($devices as $device) {
|
||||
switch ($device->type) {
|
||||
case DeviceType::Soil:
|
||||
$this->handleSoilMonitoringLog($device, $log->data, $log->reported_at);
|
||||
$this->handleSoilMonitoringDailyLog($device, $log->reported_at);
|
||||
break;
|
||||
|
||||
case DeviceType::Meteorological:
|
||||
|
|
@ -115,14 +118,14 @@ class LinkosDeviceLogService
|
|||
}
|
||||
|
||||
/**
|
||||
* 处理土壤设备数据
|
||||
* 处理土壤监测数据
|
||||
*
|
||||
* @param \App\Models\Device $device
|
||||
* @param array $data
|
||||
* @param \Carbon\Carbon $reportedAt
|
||||
* @return void
|
||||
*/
|
||||
public function handleSoilDeviceData(Device $device, array $data, Carbon $reportedAt): void
|
||||
public function handleSoilMonitoringLog(Device $device, array $data, Carbon $reportedAt): void
|
||||
{
|
||||
if (! Arr::hasAny($data, $this->soilMonitoringFields)) {
|
||||
return;
|
||||
|
|
@ -146,6 +149,55 @@ class LinkosDeviceLogService
|
|||
$log->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 按天处理土壤监测数据
|
||||
*
|
||||
* @param \App\Models\Device $device
|
||||
* @param \Carbon\Carbon $date
|
||||
* @return void
|
||||
*/
|
||||
public function handleSoilMonitoringDailyLog(Device $device, Carbon $date)
|
||||
{
|
||||
$logs = SoilMonitoringLog::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 (['conductivity', 'humidity', 'temperature', 'n', 'p', 'k'] 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 = SoilMonitoringDailyLog::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('soil_monitoring_daily_logs', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('device_id')->comment('设备ID');
|
||||
$table->unsignedBigInteger('agricultural_base_id')->comment('农业基地ID');
|
||||
$table->decimal('conductivity', 8, 2)->nullable()->comment('电导率 (单位: us/cm)');
|
||||
$table->decimal('humidity', 8, 2)->nullable()->comment('湿度 (单位: %RH)');
|
||||
$table->decimal('temperature', 8, 2)->nullable()->comment('温度 (单位: ℃)');
|
||||
$table->decimal('n', 8, 2)->nullable()->comment('氮 (单位: mg/kg)');
|
||||
$table->decimal('p', 8, 2)->nullable()->comment('磷 (单位: mg/kg)');
|
||||
$table->decimal('k', 8, 2)->nullable()->comment('钾 (单位: mg/kg)');
|
||||
$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('soil_monitoring_daily_logs');
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue