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