subHour()->startOfHour(); if ($hour = $this->argument('hour')) { $time = Carbon::createFromFormat('Y-m-d H:i:s', $hour)->startOfHour(); } $devices = Device::where('type', DeviceType::WaterQuality) ->where('status', DeviceStatus::Online) ->get(); foreach ($devices as $device) { if (is_null(WaterQualityMonitoringLog::where('device_id', $device->id)->where('monitored_at', $time)->first())) { continue; } $last = WaterQualityMonitoringLog::where('device_id', $device->id) ->where('monitored_at', $time->copy()->subHour()) ->first(); if ($last === null) { continue; } try { WaterQualityMonitoringLog::create([ 'device_id' => $device->id, 'agricultural_base_id' => $device->agricultural_base_id, 'chlorine' => $last->chlorine, 'conductivity' => $last->conductivity, 'oxygen' => $last->oxygen, 'ph' => $last->ph, 'temperature' => $last->temperature, 'turbidity' => $last->turbidity, 'monitored_at' => $time, ]); $linkosDeviceLogService->handleWaterQualityMonitoringDailyLog($device, $time); } catch (Throwable $e) { report($e); } } return Command::SUCCESS; } }