get(); // 物联平台目前只有水质监测设备和气象监测设备 LinkosDeviceLog::orderBy('reported_at', 'asc')->lazy()->each(function ($log) use ($devices) { if (empty($log->data)) { return; } foreach ($devices as $device) { if ($device->sn !== $log->device_id) { continue; } match ($device->type) { DeviceType::Soil => $this->handleSoildDeviceLog($device, $log), DeviceType::WaterQuality => $this->handleWaterQualityDeviceLog($device, $log), DeviceType::Meteorological => $this->handleMeteorologicalDeviceLog($device, $log), }; } }); return Command::SUCCESS; } protected function handleSoildDeviceLog(Device $device, LinkosDeviceLog $log) { $attributes = [ 'conductivity' => 'conductivity', 'soil_humidity' => 'humidity', 'soil_temperature' => 'temperature', 'nitrogen_content' => 'n', 'phosphorus_content' => 'p', 'potassium_content' => 'k', ]; if (! Arr::hasAny($log->data, array_keys($attributes))) { return; } $monitoringLog = SoilMonitoringLog::firstOrCreate([ 'device_id' => $device->id, 'monitored_at' => $log->reported_at->startOfHour(), ], [ 'agricultural_base_id' => $device->agricultural_base_id, ]); foreach ($attributes as $key => $attribute) { if (! array_key_exists($key, $log->data)) { continue; } $monitoringLog->{$attribute} = $log->data[$key]; } $monitoringLog->save(); } protected function handleWaterQualityDeviceLog(Device $device, LinkosDeviceLog $log) { $attributes = [ 'conductivity' => 'conductivity', 'oxygen' => 'oxygen', 'chlorine' => 'chlorine', 'turbidity' => 'turbidity', 'temp' => 'temperature', 'ph' => 'ph', ]; if (! Arr::hasAny($log->data, array_keys($attributes))) { return; } $monitoringLog = WaterQualityMonitoringLog::firstOrCreate([ 'device_id' => $device->id, 'monitored_at' => $log->reported_at->startOfHour(), ], [ 'agricultural_base_id' => $device->agricultural_base_id, ]); foreach ($attributes as $key => $attribute) { if (! array_key_exists($key, $log->data)) { continue; } $monitoringLog->{$attribute} = $log->data[$key]; } $monitoringLog->save(); } protected function handleMeteorologicalDeviceLog(Device $device, LinkosDeviceLog $log) { $attributes = [ 'wind_speed' => 'wind_speed', 'wind_power' => 'wind_power', 'wind_direction' => 'wind_direction', 'wind_degree' => 'wind_degree', 'box_humidity' => 'air_humidity', 'box_temperature' => 'air_temperature', 'box_pressure' => 'air_pressure', 'box_carbon' => 'co2', 'box_noise' => 'noise', 'box_illumination' => 'illumination', 'accumulate_rainfall' => 'accumulated_rainfall', 'current_rainfall' => 'current_rainfall', 'moment_rainfall' => 'moment_rainfall', 'day_rainfall' => 'day_rainfall', 'pm25_concentration' => 'pm25', 'pm10_concentration' => 'pm10', ]; if (! Arr::hasAny($log->data, array_keys($attributes))) { return; } $monitoringLog = MeteorologicalMonitoringLog::firstOrCreate([ 'device_id' => $device->id, 'monitored_at' => $log->reported_at->startOfHour(), ], [ 'agricultural_base_id' => $device->agricultural_base_id, ]); foreach ($attributes as $key => $attribute) { if (! array_key_exists($key, $log->data)) { continue; } $monitoringLog->{$attribute} = $log->data[$key]; } $monitoringLog->save(); } }