diff --git a/app/Console/Commands/BiAng/DeviceLogReportCommand.php b/app/Console/Commands/BiAng/DeviceLogReportCommand.php index aa3a110..462cc55 100644 --- a/app/Console/Commands/BiAng/DeviceLogReportCommand.php +++ b/app/Console/Commands/BiAng/DeviceLogReportCommand.php @@ -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, diff --git a/app/Services/BiAngDeviceService.php b/app/Services/BiAngDeviceService.php index d536d54..25bca21 100644 --- a/app/Services/BiAngDeviceService.php +++ b/app/Services/BiAngDeviceService.php @@ -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(); + } + /** * 创建气象设备报告 */