diff --git a/app/Console/Commands/DeviceLogDailyReportCommand.php b/app/Console/Commands/DeviceLogDailyReportCommand.php index 5c6c2f4..373d88d 100644 --- a/app/Console/Commands/DeviceLogDailyReportCommand.php +++ b/app/Console/Commands/DeviceLogDailyReportCommand.php @@ -76,7 +76,7 @@ class DeviceLogDailyReportCommand extends Command protected function createReportToLinkosMeteorologicalDevice(Device $device): void { $lastReportedAt = MeteorologicalDailyReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at') - ?: MeteorologicalReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'); + ?: MeteorologicalReport::where('device_id', $device->id)->oldest('reported_at')->value('reported_at'); if ($lastReportedAt === null) { return; @@ -104,7 +104,7 @@ class DeviceLogDailyReportCommand extends Command protected function createReportToLinkosWaterQualityDevice(Device $device): void { $lastReportedAt = WaterQualityDailyReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at') - ?: WaterQualityReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'); + ?: WaterQualityReport::where('device_id', $device->id)->oldest('reported_at')->value('reported_at'); if ($lastReportedAt === null) { return; diff --git a/app/Console/Commands/DeviceLogReportCommand.php b/app/Console/Commands/DeviceLogReportCommand.php index 35e961c..567a514 100644 --- a/app/Console/Commands/DeviceLogReportCommand.php +++ b/app/Console/Commands/DeviceLogReportCommand.php @@ -6,6 +6,7 @@ use App\Models\Device; use App\Models\MeteorologicalReport; use App\Models\SoilReport; use App\Models\WaterQualityReport; +use App\Services\DeviceLogService; use Illuminate\Console\Command; use Illuminate\Support\Arr; @@ -27,11 +28,18 @@ class DeviceLogReportCommand extends Command */ protected $description = '按设备厂商生成监控报告'; + /** + * @var \App\Services\DeviceLogService + */ + protected $deviceLogService; + /** * Execute the console command. */ - public function handle() + public function handle(DeviceLogService $deviceLogService) { + $this->deviceLogService = $deviceLogService; + $factory = $this->argument('factory'); $sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 300, $this->option('sleep')); @@ -43,7 +51,15 @@ class DeviceLogReportCommand extends Command foreach ($devices as $device) { switch ($device->factory?->key) { case 'link-os': - $this->createReportToLinkosDevice($device); + switch ($device->type) { + case Device::TYPE_METEOROLOGICAL: + $this->createReportToLinkosMeteorologicalDevice($device); + break; + + case Device::TYPE_WATER_QUALITY: + $this->createReportToLinkosWaterQualityDevice($device); + break; + } break; } } @@ -52,95 +68,9 @@ class DeviceLogReportCommand extends Command }; } - protected function createReportToLinkosDevice(Device $device): void - { - switch ($device->type) { - case Device::TYPE_SOIL: - $this->createReportToLinkosSoilDevice($device); - break; - - case Device::TYPE_METEOROLOGICAL: - $this->createReportToLinkosMeteorologicalDevice($device); - break; - - case Device::TYPE_WATER_QUALITY: - $this->createReportToLinkosWaterQualityDevice($device); - break; - } - } - - protected function createReportToLinkosSoilDevice(Device $device): void - { - $lastSoilReport = SoilReport::where('device_id', $device->id) - ->latest('reported_at') - ->first(); - - $lastReportedAt = $lastSoilReport?->reported_at - ?: $device->logs()->oldest('reported_at')->value('reported_at'); - - if ($lastReportedAt === null) { - return; - } - - $latestReportedAt = $device->logs()->latest('reported_at')->value('reported_at'); - - if ($latestReportedAt === null) { - return; - } - - /** @var \Carbon\Carbon */ - $startAt = $lastReportedAt->copy()->startOfHour(); - /** @var \Carbon\Carbon */ - $endAt = $latestReportedAt->copy()->startOfHour(); - - do { - /** @var \Illuminate\Database\Eloquent\Collection */ - $logs = $device->logs() - ->whereBetween('reported_at', [$startAt, $startAt->copy()->endOfHour()]) - ->oldest('reported_at') - ->get(); - - if ($logs->isNotEmpty()) { - $soilReport = SoilReport::firstOrCreate( - [ - 'device_id' => $device->id, - 'reported_at' => $startAt, - ], - Arr::except($lastSoilReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at']) - ); - - /** @var \App\Models\DeviceLog */ - foreach ($logs as $log) { - foreach ([ - 'conductivity' => 'conductivity', - 'soil_humidity' => 'humidity', - 'soil_temperature' => 'temperature', - 'nitrogen_content' => 'n', - 'potassium_content' => 'k', - 'phosphorus_content' => 'p', - ] as $key => $attribute) { - if (! is_array($log->data) || ! array_key_exists($key, $log->data)) { - continue; - } - - $soilReport->{$attribute} = $log->data[$key]; - } - - $lastSoilReport = tap($soilReport)->save(); - } - } - - $startAt->addHour(); - } while ($endAt->gte($startAt)); - } - protected function createReportToLinkosMeteorologicalDevice(Device $device): void { - $lastMeteorologicalReport = MeteorologicalReport::where('device_id', $device->id) - ->latest('reported_at') - ->first(); - - $lastReportedAt = $lastMeteorologicalReport?->reported_at + $lastReportedAt = MeteorologicalReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at') ?: $device->logs()->oldest('reported_at')->value('reported_at'); if ($lastReportedAt === null) { @@ -159,51 +89,7 @@ class DeviceLogReportCommand extends Command $endAt = $latestReportedAt->copy()->startOfHour(); do { - /** @var \Illuminate\Database\Eloquent\Collection */ - $logs = $device->logs() - ->whereBetween('reported_at', [$startAt, $startAt->copy()->endOfHour()]) - ->oldest('reported_at') - ->get(); - - if ($logs->isNotEmpty()) { - $meteorologicalReport = MeteorologicalReport::firstOrCreate( - [ - 'device_id' => $device->id, - 'reported_at' => $startAt, - ], - Arr::except($lastMeteorologicalReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at']) - ); - - /** @var \App\Models\DeviceLog */ - foreach ($logs as $log) { - foreach ([ - 'current_rainfall' => 'today_rainfall', - 'day_rainfall' => 'yesterday_rainfall', - 'accumulate_rainfall' => 'accumulate_rainfall', - 'moment_rainfall' => 'moment_rainfall', - 'pm10_concentration' => 'pm10', - 'pm25_concentration' => 'pm25', - 'box_illumination' => 'box_illumination', - 'box_pressure' => 'box_pressure', - 'box_carbon' => 'box_co2', - 'box_temperature' => 'box_temperature', - 'box_humidity' => 'box_humidity', - 'box_noise' => 'box_noise', - 'wind_degree' => 'wind_degree', - 'wind_direction' => 'wind_direction', - 'wind_power' => 'wind_power', - 'wind_speed' => 'wind_speed', - ] as $key => $attribute) { - if (! is_array($log->data) || ! array_key_exists($key, $log->data)) { - continue; - } - - $meteorologicalReport->{$attribute} = $log->data[$key]; - } - - $lastMeteorologicalReport = tap($meteorologicalReport)->save(); - } - } + $this->deviceLogService->createReportToLinkosMeteorologicalDevice($device, $startAt->copy()); $startAt->addHour(); } while ($endAt->gte($startAt)); @@ -211,11 +97,7 @@ class DeviceLogReportCommand extends Command protected function createReportToLinkosWaterQualityDevice(Device $device): void { - $lastWaterQualityReport = WaterQualityReport::where('device_id', $device->id) - ->latest('reported_at') - ->first(); - - $lastReportedAt = $lastWaterQualityReport?->reported_at + $lastReportedAt = WaterQualityReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at') ?: $device->logs()->oldest('reported_at')->value('reported_at'); if ($lastReportedAt === null) { @@ -234,41 +116,7 @@ class DeviceLogReportCommand extends Command $endAt = $latestReportedAt->copy()->startOfHour(); do { - /** @var \Illuminate\Database\Eloquent\Collection */ - $logs = $device->logs() - ->whereBetween('reported_at', [$startAt, $startAt->copy()->endOfHour()]) - ->oldest('reported_at') - ->get(); - - if ($logs->isNotEmpty()) { - $waterQualityReport = WaterQualityReport::firstOrCreate( - [ - 'device_id' => $device->id, - 'reported_at' => $startAt, - ], - Arr::except($lastWaterQualityReport?->setHidden([])?->attributesToArray() ?: [], ['reported_at']) - ); - - /** @var \App\Models\DeviceLog */ - foreach ($logs as $log) { - foreach ([ - 'chlorine' => 'chlorine', - 'conductivity' => 'conductivity', - 'oxygen' => 'oxygen', - 'ph' => 'ph', - 'temp' => 'temperature', - 'turbidity' => 'turbidity', - ] as $key => $attribute) { - if (! is_array($log->data) || ! array_key_exists($key, $log->data)) { - continue; - } - - $waterQualityReport->{$attribute} = $log->data[$key]; - } - - $lastWaterQualityReport = tap($waterQualityReport)->save(); - } - } + $this->deviceLogService->createReportToLinkosWaterQualityDevice($device, $startAt->copy()); $startAt->addHour(); } while ($endAt->gte($startAt)); diff --git a/app/Services/DeviceLogService.php b/app/Services/DeviceLogService.php index 72cd7e7..885c203 100644 --- a/app/Services/DeviceLogService.php +++ b/app/Services/DeviceLogService.php @@ -78,6 +78,150 @@ class DeviceLogService } while ($countResults === $perPage); } + /** + * 创建 linkos 气象设备报告 + */ + public function createReportToLinkosMeteorologicalDevice(Device $device, Carbon $time) + { + $start = $time->copy()->startOfHour(); + + /** @var \Illuminate\Database\Eloquent\Collection */ + $logs = $device->logs() + ->whereBetween('reported_at', [$start, $start->copy()->endOfHour()]) + ->oldest('reported_at') + ->get(); + + if ($logs->isEmpty()) { + return; + } + + $attributes = []; + + /** @var \App\Models\DeviceLog */ + foreach ($logs as $log) { + if (! is_array($data = $log->data)) { + continue; + } + + foreach ([ + 'current_rainfall' => 'today_rainfall', + 'day_rainfall' => 'yesterday_rainfall', + 'accumulate_rainfall' => 'accumulate_rainfall', + 'moment_rainfall' => 'moment_rainfall', + 'pm10_concentration' => 'pm10', + 'pm25_concentration' => 'pm25', + 'box_illumination' => 'box_illumination', + 'box_pressure' => 'box_pressure', + 'box_carbon' => 'box_co2', + 'box_temperature' => 'box_temperature', + 'box_humidity' => 'box_humidity', + 'box_noise' => 'box_noise', + 'wind_degree' => 'wind_degree', + 'wind_direction' => 'wind_direction', + 'wind_power' => 'wind_power', + 'wind_speed' => 'wind_speed', + ] as $k => $v) { + if (! array_key_exists($k, $data)) { + continue; + } + + $attributes[$v] = $data[$k]; + } + } + + if ( + $meteorologicalReport = MeteorologicalReport::where('device_id', $device->id) + ->where('reported_at', $start) + ->first() + ) { + $meteorologicalReport->update($attributes); + } else { + if ( + $lastMeteorologicalReport = MeteorologicalReport::where('device_id', $device->id) + ->where('reported_at', $start->copy()->subHour()) + ->first() + ) { + foreach ($attributes as $k => $v) { + if ($v === null) { + $attributes[$k] = $lastMeteorologicalReport->{$k}; + } + } + } + + MeteorologicalReport::create(array_merge($attributes, [ + 'device_id' => $device->id, + 'reported_at' => $start, + ])); + } + } + + /** + * 创建 linkos 水质设备报告 + */ + public function createReportToLinkosWaterQualityDevice(Device $device, Carbon $time) + { + $start = $time->copy()->startOfHour(); + + /** @var \Illuminate\Database\Eloquent\Collection */ + $logs = $device->logs() + ->whereBetween('reported_at', [$start, $start->copy()->endOfHour()]) + ->oldest('reported_at') + ->get(); + + if ($logs->isEmpty()) { + return; + } + + $attributes = []; + + /** @var \App\Models\DeviceLog */ + foreach ($logs as $log) { + if (! is_array($data = $log->data)) { + continue; + } + + foreach ([ + 'chlorine' => 'chlorine', + 'conductivity' => 'conductivity', + 'oxygen' => 'oxygen', + 'ph' => 'ph', + 'temp' => 'temperature', + 'turbidity' => 'turbidity', + ] as $k => $v) { + if (! array_key_exists($k, $data)) { + continue; + } + + $attributes[$v] = $data[$k]; + } + } + + if ( + $waterQualityReport = WaterQualityReport::where('device_id', $device->id) + ->where('reported_at', $start) + ->first() + ) { + $waterQualityReport->update($attributes); + } else { + if ( + $lastWaterQualityReport = WaterQualityReport::where('device_id', $device->id) + ->where('reported_at', $start->copy()->subHour()) + ->first() + ) { + foreach ($attributes as $k => $v) { + if ($v === null) { + $attributes[$k] = $lastWaterQualityReport->{$k}; + } + } + } + + WaterQualityReport::create(array_merge($attributes, [ + 'device_id' => $device->id, + 'reported_at' => $start, + ])); + } + } + /** * 创建 linkos 气象设备每日报告 */ @@ -93,116 +237,116 @@ class DeviceLogService return; } - $result = $meteorologicalReports->reduce(function (array $carry, MeteorologicalReport $meteorologicalReport) { - foreach ($carry as $key => $item) { - if (is_null($v = $meteorologicalReport->{$key})) { - continue; - } + $attributes = value(function ($meteorologicalReports) { + $data = [ + 'today_rainfall' => ['sum' => 0, 'count' => 0], + 'yesterday_rainfall' => ['sum' => 0, 'count' => 0], + 'accumulate_rainfall' => ['sum' => 0, 'count' => 0], + 'moment_rainfall' => ['sum' => 0, 'count' => 0], + 'pm10' => ['sum' => 0, 'count' => 0], + 'pm25' => ['sum' => 0, 'count' => 0], + 'box_illumination' => ['sum' => 0, 'count' => 0], + 'box_pressure' => ['sum' => 0, 'count' => 0], + 'box_co2' => ['sum' => 0, 'count' => 0], + 'box_temperature' => ['sum' => 0, 'count' => 0], + 'box_humidity' => ['sum' => 0, 'count' => 0], + 'box_noise' => ['sum' => 0, 'count' => 0], + 'wind_samples' => [], + ]; + foreach ($meteorologicalReports as $meteorologicalReport) { + foreach ($data as $k => $item) { + if ($k === 'wind_samples') { + if (is_null($meteorologicalReport->wind_degree) || is_null($meteorologicalReport->wind_speed)) { + continue; + } + + $item[] = [ + 'wind_degree' => $meteorologicalReport->wind_degree, // 风向度数 + 'wind_speed' => $meteorologicalReport->wind_speed, // 风速 + ]; + } elseif (! is_null($v = $meteorologicalReport->{$k})) { + $item['sum'] = bcadd($item['sum'], $v, 2); + $item['count']++; + } + + $data[$k] = $item; + } + } + + $attributes = []; + + foreach ($data as $key => $item) { switch ($key) { case 'wind_samples': - if (! is_null($meteorologicalReport->wind_degree) && ! is_null($meteorologicalReport->wind_speed)) { - $item['wind_samples'][] = [ - 'wind_degree' => $meteorologicalReport->wind_degree, // 风向度数 - 'wind_speed' => $meteorologicalReport->wind_speed, // 风速 - ]; - } + $attributes['wind_degree'] = value(function (array $windSamples) { + $x = 0; + $y = 0; + + foreach ($windSamples as $sample) { + if ($sample['wind_degree'] == 0 && $sample['wind_speed'] == 0) { + continue; + } + + // 角度转弧度 + $radian = deg2rad($sample['wind_degree']); + + // $x += $sample['wind_speed'] * sin($radian); + // $y += $sample['wind_speed'] * cos($radian); + $x += sin($radian); + $y += cos($radian); + } + + $degree = round(rad2deg(atan2($y, $x))); + + if (($x > 0 || $x < 0) && $y < 0) { + $degree += 180; + } elseif ($x < 0 && $y > 0) { + $degree += 360; + } + + return $degree; + }, $item); + + $attributes['wind_direction'] = value(function ($windDegree) { + if ($windDegree >= 22.5 && $windDegree < 67.5) { + return MeteorologicalDailyReport::WIND_DIRECTION_NORTHEAST; + } elseif ($windDegree >= 67.5 && $windDegree < 112.5) { + return MeteorologicalDailyReport::WIND_DIRECTION_EAST; + } elseif ($windDegree >= 112.5 && $windDegree < 157.5) { + return MeteorologicalDailyReport::WIND_DIRECTION_SOUTHEAST; + } elseif ($windDegree >= 157.5 && $windDegree < 202.5) { + return MeteorologicalDailyReport::WIND_DIRECTION_SOUTH; + } elseif ($windDegree >= 202.5 && $windDegree < 247.5) { + return MeteorologicalDailyReport::WIND_DIRECTION_SOUTHWEST; + } elseif ($windDegree >= 247.5 && $windDegree < 292.5) { + return MeteorologicalDailyReport::WIND_DIRECTION_WEST; + } elseif ($windDegree >= 292.5 && $windDegree < 337.5) { + return MeteorologicalDailyReport::WIND_DIRECTION_NORTHWEST; + } + + return MeteorologicalDailyReport::WIND_DIRECTION_NORTH; + }, $attributes['wind_degree']); break; default: - $item['sum'] = bcadd($item['sum'], $v, 2); - $item['count']++; + $attributes[$key] = $item['count'] > 0 ? round(bcdiv($item['sum'], $item['count'], 2), 2) : null; break; } - - $carry[$key] = $item; } - return $carry; - }, [ - 'today_rainfall' => ['sum' => 0, 'count' => 0], - 'yesterday_rainfall' => ['sum' => 0, 'count' => 0], - 'accumulate_rainfall' => ['sum' => 0, 'count' => 0], - 'moment_rainfall' => ['sum' => 0, 'count' => 0], - 'pm10' => ['sum' => 0, 'count' => 0], - 'pm25' => ['sum' => 0, 'count' => 0], - 'box_illumination' => ['sum' => 0, 'count' => 0], - 'box_pressure' => ['sum' => 0, 'count' => 0], - 'box_co2' => ['sum' => 0, 'count' => 0], - 'box_temperature' => ['sum' => 0, 'count' => 0], - 'box_humidity' => ['sum' => 0, 'count' => 0], - 'box_noise' => ['sum' => 0, 'count' => 0], - 'wind_samples' => [], - ]); + return $attributes; + }, $meteorologicalReports); + /** @var \App\Models\MeteorologicalDailyReport */ $meteorologicalDailyReport = MeteorologicalDailyReport::firstOrCreate([ 'device_id' => $device->id, 'reported_at' => $date, - ]); + ], $attributes); - foreach ($result as $key => $item) { - switch ($key) { - case 'wind_samples': - $meteorologicalDailyReport->wind_degree = value(function (array $windSamples) { - $x = 0; - $y = 0; - - foreach ($windSamples as $sample) { - if ($sample['wind_degree'] == 0 && $sample['wind_speed'] == 0) { - continue; - } - - // 角度转弧度 - $radian = deg2rad($sample['wind_degree']); - - // $x += $sample['wind_speed'] * sin($radian); - // $y += $sample['wind_speed'] * cos($radian); - $x += sin($radian); - $y += cos($radian); - } - - $degree = round(rad2deg(atan2($y, $x))); - - if (($x > 0 || $x < 0) && $y < 0) { - $degree += 180; - } elseif ($x < 0 && $y > 0) { - $degree += 360; - } - - return $degree; - }, $item); - - $meteorologicalDailyReport->wind_direction = value(function ($windDegree) { - if ($windDegree >= 22.5 && $windDegree < 67.5) { - return MeteorologicalDailyReport::WIND_DIRECTION_NORTHEAST; - } elseif ($windDegree >= 67.5 && $windDegree < 112.5) { - return MeteorologicalDailyReport::WIND_DIRECTION_EAST; - } elseif ($windDegree >= 112.5 && $windDegree < 157.5) { - return MeteorologicalDailyReport::WIND_DIRECTION_SOUTHEAST; - } elseif ($windDegree >= 157.5 && $windDegree < 202.5) { - return MeteorologicalDailyReport::WIND_DIRECTION_SOUTH; - } elseif ($windDegree >= 202.5 && $windDegree < 247.5) { - return MeteorologicalDailyReport::WIND_DIRECTION_SOUTHWEST; - } elseif ($windDegree >= 247.5 && $windDegree < 292.5) { - return MeteorologicalDailyReport::WIND_DIRECTION_WEST; - } elseif ($windDegree >= 292.5 && $windDegree < 337.5) { - return MeteorologicalDailyReport::WIND_DIRECTION_NORTHWEST; - } - - return MeteorologicalDailyReport::WIND_DIRECTION_NORTH; - }, $meteorologicalDailyReport->wind_degree); - - break; - - default: - if ($item['count'] > 0) { - $meteorologicalDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2); - } - break; - } + if (! $meteorologicalDailyReport->wasRecentlyCreated) { + $meteorologicalDailyReport->update($attributes); } - - $meteorologicalDailyReport->save(); } /** @@ -220,39 +364,46 @@ class DeviceLogService return; } - $result = $waterQualityReports->reduce(function (array $carry, WaterQualityReport $waterQualityReport) { - foreach ($carry as $key => $item) { - if (is_null($v = $waterQualityReport->{$key})) { - continue; + $attributes = value(function ($waterQualityReports) { + $data = [ + 'chlorine' => ['sum' => 0, 'count' => 0], + 'conductivity' => ['sum' => 0, 'count' => 0], + 'oxygen' => ['sum' => 0, 'count' => 0], + 'ph' => ['sum' => 0, 'count' => 0], + 'temperature' => ['sum' => 0, 'count' => 0], + 'turbidity' => ['sum' => 0, 'count' => 0], + ]; + + foreach ($waterQualityReports as $waterQualityReport) { + foreach ($data as $k => $item) { + if (is_null($v = $waterQualityReport->{$k})) { + continue; + } + + $item['sum'] = bcadd($item['sum'], $v, 2); + $item['count']++; + + $data[$k] = $item; } - - $item['sum'] = bcadd($item['sum'], $v, 2); - $item['count']++; - - $carry[$key] = $item; } - return $carry; - }, [ - 'chlorine' => ['sum' => 0, 'count' => 0], - 'conductivity' => ['sum' => 0, 'count' => 0], - 'oxygen' => ['sum' => 0, 'count' => 0], - 'ph' => ['sum' => 0, 'count' => 0], - 'temperature' => ['sum' => 0, 'count' => 0], - 'turbidity' => ['sum' => 0, 'count' => 0], - ]); + $attributes = []; + foreach ($data as $key => $item) { + $attributes[$key] = $item['count'] > 0 ? round(bcdiv($item['sum'], $item['count'], 2), 2) : null; + } + + return $attributes; + }, $waterQualityReports); + + /** @var \App\Models\WaterQualityDailyReport */ $waterQualityDailyReport = WaterQualityDailyReport::firstOrCreate([ 'device_id' => $device->id, 'reported_at' => $date->format('Y-m-d'), - ]); + ], $attributes); - foreach ($result as $key => $item) { - if ($item['count'] > 0) { - $waterQualityDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2); - } + if (! $waterQualityDailyReport->wasRecentlyCreated) { + $waterQualityDailyReport->update($attributes); } - - $waterQualityDailyReport->save(); } }