linkosDeviceLogService = $linkosDeviceLogService; $this->fill(now()->subHour()); } protected function fill(Carbon $monitoredAt) { $data = [ 'conductivity' => rand(560, 565), 'oxygen' => rand(1000, 1100) / 100, 'chlorine' => 0.09, '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; } $this->linkosDeviceLogService->handleWaterQualityMonitoringLog($device, $data, $monitoredAt); $this->linkosDeviceLogService->handleWaterQualityMonitoringDailyLog($device, $monitoredAt); } } }