比昂水质设备生成每小时的报告

dev
Jing Li 2024-01-29 13:44:35 +08:00
parent 40df91fcc6
commit cf7c12e01b
2 changed files with 63 additions and 0 deletions

View File

@ -8,6 +8,7 @@ use App\Models\DeviceLog;
use App\Models\InsecticidalLampReport;
use App\Models\MeteorologicalMonitoringLog;
use App\Models\SoilMonitoringLog;
use App\Models\WaterQualityMonitoringLog;
use App\Services\BiAngDeviceService;
use Illuminate\Console\Command;
use Throwable;
@ -64,6 +65,7 @@ class DeviceLogReportCommand extends Command
{
$lastReportedAt = match ($device->type) {
DeviceType::Soil => SoilMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
DeviceType::WaterQuality => WaterQualityMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
DeviceType::Meteorological => MeteorologicalMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
DeviceType::InsecticidalLamp => InsecticidalLampReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'),
default => null,

View File

@ -13,6 +13,7 @@ use App\Models\MeteorologicalMonitoringDailyLog;
use App\Models\MeteorologicalMonitoringLog;
use App\Models\SoilMonitoringDailyLog;
use App\Models\SoilMonitoringLog;
use App\Models\WaterQualityMonitoringLog;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
@ -56,6 +57,10 @@ class BiAngDeviceService
$this->createSoilReport($device, $time);
break;
case DeviceType::WaterQuality:
$this->createWaterQualityReport($device, $time);
break;
case DeviceType::Meteorological:
$this->createMeteorologicalReport($device, $time);
break;
@ -128,6 +133,62 @@ class BiAngDeviceService
$soilReport->fill($attributes)->save();
}
protected function createWaterQualityReport(Device $device, Carbon $time): void
{
$reportedAt = $time->copy()->startOfHour();
/** @var \Illuminate\Database\Eloquent\Collection */
$logs = DeviceLog::where('device_id', $device->id)
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
->oldest('reported_at')
->get();
if ($logs->isEmpty()) {
return;
}
$attributes = $logs->reduce(function (array $attributes, DeviceLog $log) {
if (is_array($data = $log->data)) {
foreach ($data as $k => $v) {
$attribute = match ($k) {
'ec1' => 'conductivity',
'waterdo' => 'oxygen',
'zd' => 'turbidity',
default => null,
};
if ($attribute) {
$attributes[$attribute] = $v;
}
}
}
return $attributes;
}, []);
$waterQualityReport = WaterQualityMonitoringLog::where([
'device_id' => $device->id,
'monitored_at' => $reportedAt,
])->first();
if ($waterQualityReport === null) {
$lastWaterQualityReport = WaterQualityMonitoringLog::where([
'device_id' => $device->id,
'monitored_at' => $reportedAt->copy()->subHour(),
])->first();
$waterQualityReport = $lastWaterQualityReport?->replicate() ?: new WaterQualityMonitoringLog();
$waterQualityReport->fill([
'device_id' => $device->id,
'monitored_at' => $reportedAt,
'agricultural_base_id' => $device->agricultural_base_id,
]);
}
$waterQualityReport->fill($attributes)->save();
}
/**
* 创建气象设备报告
*/