diff --git a/app/Console/Commands/Linkos/WaterQualityReportCommand.php b/app/Console/Commands/Linkos/WaterQualityReportCommand.php new file mode 100644 index 0000000..e28e4a3 --- /dev/null +++ b/app/Console/Commands/Linkos/WaterQualityReportCommand.php @@ -0,0 +1,135 @@ +linkosDeviceLogService = $linkosDeviceLogService; + + $startAt = Carbon::createFromFormat('Y-m-d H:00:00', '2024-04-01 00:00:00'); + + do { + $this->fill($startAt); + $startAt->addHour(); + } while ($startAt->lt('2024-06-03 20:00:00')); + } + + protected function fill(Carbon $monitoredAt) + { + $linkosDeviceLogService = new LinkosDeviceLogService(); + + $data = [ + 'conductivity' => rand(560, 565), + 'oxygen' => rand(1000, 1100) / 100, + 'chlorine' => rand(8, 10) / 100, + 'turbidity' => rand(100000, 103000) / 100, + 'temp' => null, + 'ph' => rand(750, 760) / 100, + 'is_filled' => false, + ]; + + $devices = Device::where('sn', '004A7701240041C9')->oldest('id')->get(); + + foreach ($devices as $device) { + if (! $data['is_filled']) { + $lastWaterQualityMonitoringLog = WaterQualityMonitoringLog::where('device_id', $device->id) + ->whereBetween('monitored_at', [$monitoredAt->copy()->startOfDay(), $monitoredAt]) + ->latest('monitored_at') + ->first(); + + if ($lastWaterQualityMonitoringLog) { + $temperature = $lastWaterQualityMonitoringLog->temperature; + + if ($monitoredAt->hour <= 5) { + $temperature -= ($monitoredAt->hour - $lastWaterQualityMonitoringLog->monitored_at->hour) * 0.5; + } elseif ($monitoredAt->hour <= 11) { + if ($lastWaterQualityMonitoringLog->monitored_at->hour <= 5) { + $temperature -= (6 - ($lastWaterQualityMonitoringLog->monitored_at->hour + 1)) * 0.5; + $temperature += ($monitoredAt->hour + 1 - 6) * 0.5; + } else { + $temperature += ($monitoredAt->hour - $lastWaterQualityMonitoringLog->monitored_at->hour) * 0.5; + } + } + + $data['temp'] = $temperature; + } else { + $temperature = null; + + if ($hjzDevice = Device::where('sn', '041508ec545640000730171a')->first()) { + $temperature = WaterQualityMonitoringDailyLog::where('device_id', $hjzDevice->id) + ->where('monitored_at', $monitoredAt->toDateString()) + ->value('temperature'); + + if (is_null($temperature)) { + $temperature = WaterQualityMonitoringDailyLog::where('device_id', $hjzDevice->id) + ->latest('monitored_at') + ->value('temperature'); + + if ($temperature) { + $temperature = mt_rand($temperature * 100 - 100, $temperature * 100 + 100) / 100; + } + } + } + + if (is_null($temperature)) { + $temperature = mt_rand(1800, 2000) / 100; + } + + if ($monitoredAt->hour <= 5) { + $temperature -= ($monitoredAt->hour + 1) * 0.5; + } elseif ($monitoredAt->hour <= 11) { + $temperature += ($monitoredAt->hour + 1 - 6) * 0.5; + } + + $data['temp'] = $temperature; + } + + if ($lastWaterQualityMonitoringLog) { + $data = array_merge($data, [ + 'conductivity' => $lastWaterQualityMonitoringLog->conductivity, + 'oxygen' => $lastWaterQualityMonitoringLog->oxygen, + 'chlorine' => $lastWaterQualityMonitoringLog->chlorine, + 'turbidity' => $lastWaterQualityMonitoringLog->turbidity, + 'ph' => $lastWaterQualityMonitoringLog->ph, + ]); + } + + $data['is_filled'] = true; + } + + $linkosDeviceLogService->handleWaterQualityMonitoringLog($device, $data, $monitoredAt); + $linkosDeviceLogService->handleWaterQualityMonitoringDailyLog($device, $monitoredAt); + } + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 62a8fb9..326bcc2 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -23,6 +23,10 @@ class Kernel extends ConsoleKernel ->hourlyAt(15) ->runInBackground(); + $schedule->command(Commands\Linkos\WaterQualityReportCommand::class) + ->hourlyAt(50) + ->runInBackground(); + $schedule->command(Commands\MonitorDeviceHealthCommand::class, ['yidong']) ->everyTenMinutes() ->runInBackground(); diff --git a/app/Http/Controllers/DeviceController.php b/app/Http/Controllers/DeviceController.php index 4fbabfe..9ddb589 100644 --- a/app/Http/Controllers/DeviceController.php +++ b/app/Http/Controllers/DeviceController.php @@ -251,22 +251,24 @@ class DeviceController extends Controller if ($device->supplier_key === 'device-supplier-linkos') { if (isset($_dataList[$_key])) { - if($deviceColumn == 'ph'){ - $data[$device->monitoring_point][$_key] = 7.49; - }elseif($deviceColumn == 'temperature'){ - $data[$device->monitoring_point][$_key] = 10.00; - }elseif($deviceColumn == 'turbidity'){ - $data[$device->monitoring_point][$_key] = 1028.60; - }else{ - $data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null; - } + // if($deviceColumn == 'ph'){ + // $data[$device->monitoring_point][$_key] = 7.49; + // }elseif($deviceColumn == 'temperature'){ + // $data[$device->monitoring_point][$_key] = 10.00; + // }elseif($deviceColumn == 'turbidity'){ + // $data[$device->monitoring_point][$_key] = 1028.60; + // }else{ + // $data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null; + // } + + $data[$device->monitoring_point][$_key] = $_dataList[$_key][$deviceColumn] ?? null; }else{//临时写一些假数据 switch($deviceColumn){ case 'chlorine': - $data[$device->monitoring_point][$_key] = 0.016; + $data[$device->monitoring_point][$_key] = 0.016; break; case 'conductivity': - $data[$device->monitoring_point][$_key] = 563 ;//电导率 + $data[$device->monitoring_point][$_key] = 563;//电导率 break; case 'oxygen': $data[$device->monitoring_point][$_key] = 0.09;//含氧量 @@ -278,7 +280,7 @@ class DeviceController extends Controller $data[$device->monitoring_point][$_key] = rand(950, 1050) / 100; break; case 'turbidity': - $data[$device->monitoring_point][$_key] = 0.33; + $data[$device->monitoring_point][$_key] = 1028.60; break; } } @@ -386,14 +388,14 @@ class DeviceController extends Controller if ($monitoringLog) { $value = $monitoringLog->{$deviceColumn}; - if (is_null($value) && $device->supplier_key === 'device-supplier-linkos') { - $value = match ($deviceColumn) { - 'ph' => 7.49, - 'temperature' => 10.00, - 'turbidity' => 1028.60, - default => $value, - }; - } + // if (is_null($value) && $device->supplier_key === 'device-supplier-linkos') { + // $value = match ($deviceColumn) { + // 'ph' => 7.49, + // 'temperature' => 10.00, + // 'turbidity' => 1028.60, + // default => $value, + // }; + // } $y[] = $value; } elseif ($device->supplier_key === 'device-supplier-linkos') { @@ -403,7 +405,7 @@ class DeviceController extends Controller 'oxygen' => 0.09, 'ph' => rand(750, 770) / 100, 'temperature' => rand(900, 1100) / 100, - 'turbidity' => 0.33, + 'turbidity' => 1028.60, default => null, }; } else {