From 7ec61b5d2e1664e14a1a9dd80573201573233ecf Mon Sep 17 00:00:00 2001 From: Jing Li Date: Wed, 23 Nov 2022 20:37:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=9F=E8=B4=A8=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E6=95=B0=E6=8D=AE=E5=92=8C=E6=B0=B4=E8=B4=A8=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Commands/SoilMonitoringLogFixCommand.php | 81 +++++++++++++++++++ .../WaterQualityMonitoringLogFixCommand.php | 81 +++++++++++++++++++ app/Console/Kernel.php | 8 +- 3 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/SoilMonitoringLogFixCommand.php create mode 100644 app/Console/Commands/WaterQualityMonitoringLogFixCommand.php diff --git a/app/Console/Commands/SoilMonitoringLogFixCommand.php b/app/Console/Commands/SoilMonitoringLogFixCommand.php new file mode 100644 index 0000000..b04bdd4 --- /dev/null +++ b/app/Console/Commands/SoilMonitoringLogFixCommand.php @@ -0,0 +1,81 @@ +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; + } +} diff --git a/app/Console/Commands/WaterQualityMonitoringLogFixCommand.php b/app/Console/Commands/WaterQualityMonitoringLogFixCommand.php new file mode 100644 index 0000000..e041fd8 --- /dev/null +++ b/app/Console/Commands/WaterQualityMonitoringLogFixCommand.php @@ -0,0 +1,81 @@ +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; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index d8bc1d2..8e3a6c0 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,7 +15,13 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // $schedule->command('inspire')->hourly(); + $schedule->command(Commands\SoilMonitoringLogFixCommand::class) + ->hourly() + ->runInBackground(); + + $schedule->command(Commands\WaterQualityMonitoringLogFixCommand::class) + ->hourly() + ->runInBackground(); } /**