dev
Jing Li 2023-08-02 22:18:37 +08:00
parent a2da7a73cd
commit 025cea099e
2 changed files with 60 additions and 57 deletions

View File

@ -4,18 +4,12 @@ namespace App\Console\Commands;
use App\Enums\DeviceType;
use App\Models\Device;
use App\Models\MeteorologicalDailyReport;
use App\Models\MeteorologicalMonitoringDailyLog;
use App\Models\MeteorologicalMonitoringLog;
use App\Models\MeteorologicalReport;
use App\Models\SoilDailyReport;
use App\Models\SoilMonitoringDailyLog;
use App\Models\SoilMonitoringLog;
use App\Models\SoilReport;
use App\Models\WaterQualityDailyReport;
use App\Models\WaterQualityMonitoringDailyLog;
use App\Models\WaterQualityMonitoringLog;
use App\Models\WaterQualityReport;
use App\Services\DeviceLogService;
use Illuminate\Console\Command;

View File

@ -412,8 +412,7 @@ class DeviceLogService
$attributes = value(function ($meteorologicalReports) {
$data = [
'today_rainfall' => ['sum' => 0, 'count' => 0],
// 'yesterday_rainfall' => ['sum' => 0, 'count' => 0],
'current_rainfall' => 0,
'accumulated_rainfall' => ['sum' => 0, 'count' => 0],
'moment_rainfall' => ['sum' => 0, 'count' => 0],
'pm10' => ['sum' => 0, 'count' => 0],
@ -424,6 +423,7 @@ class DeviceLogService
'air_temperature' => ['sum' => 0, 'count' => 0],
'air_humidity' => ['sum' => 0, 'count' => 0],
'noise' => ['sum' => 0, 'count' => 0],
'wind_speed' => ['sum' => 0, 'count' => 0],
'wind_samples' => [],
];
@ -439,8 +439,12 @@ class DeviceLogService
'wind_speed' => $meteorologicalReport->wind_speed, // 风速
];
} elseif (! is_null($v = $meteorologicalReport->{$k})) {
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
if ($k === 'current_rainfall') {
$item = $v;
} else {
$item['sum'] = bcadd($item['sum'], $v, 2);
$item['count']++;
}
}
$data[$k] = $item;
@ -451,63 +455,68 @@ class DeviceLogService
foreach ($data as $key => $item) {
switch ($key) {
case 'current_rainfall':
$attributes['daily_rainfall'] = $item;
break;
case 'wind_samples':
$attributes['wind_degree'] = value(function (array $windSamples) {
if (empty($windSamples)) {
return null;
}
$x = 0;
$y = 0;
foreach ($windSamples as $sample) {
if ($sample['wind_degree'] == 0 && $sample['wind_speed'] == 0) {
continue;
if (! empty($item)) {
$attributes['wind_degree'] = value(function (array $windSamples) {
if (empty($windSamples)) {
return null;
}
// 角度转弧度
$radian = deg2rad($sample['wind_degree']);
$x = 0;
$y = 0;
// $x += $sample['wind_speed'] * sin($radian);
// $y += $sample['wind_speed'] * cos($radian);
$x += sin($radian);
$y += cos($radian);
}
foreach ($windSamples as $sample) {
if ($sample['wind_degree'] == 0 && $sample['wind_speed'] == 0) {
continue;
}
$degree = round(rad2deg(atan2($y, $x)));
// 角度转弧度
$radian = deg2rad($sample['wind_degree']);
if (($x > 0 || $x < 0) && $y < 0) {
$degree += 180;
} elseif ($x < 0 && $y > 0) {
$degree += 360;
}
// $x += $sample['wind_speed'] * sin($radian);
// $y += $sample['wind_speed'] * cos($radian);
$x += sin($radian);
$y += cos($radian);
}
return $degree;
}, $item);
$degree = round(rad2deg(atan2($y, $x)));
$attributes['wind_direction'] = value(function ($windDegree) {
if (is_null($windDegree)) {
return null;
}
if (($x > 0 || $x < 0) && $y < 0) {
$degree += 180;
} elseif ($x < 0 && $y > 0) {
$degree += 360;
}
if ($windDegree >= 22.5 && $windDegree < 67.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_NORTHEAST;
} elseif ($windDegree >= 67.5 && $windDegree < 112.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_EAST;
} elseif ($windDegree >= 112.5 && $windDegree < 157.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_SOUTHEAST;
} elseif ($windDegree >= 157.5 && $windDegree < 202.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_SOUTH;
} elseif ($windDegree >= 202.5 && $windDegree < 247.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_SOUTHWEST;
} elseif ($windDegree >= 247.5 && $windDegree < 292.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_WEST;
} elseif ($windDegree >= 292.5 && $windDegree < 337.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_NORTHWEST;
}
return $degree;
}, $item);
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_NORTH;
}, $attributes['wind_degree']);
$attributes['wind_direction'] = value(function ($windDegree) {
if (is_null($windDegree)) {
return null;
}
if ($windDegree >= 22.5 && $windDegree < 67.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_NORTHEAST;
} elseif ($windDegree >= 67.5 && $windDegree < 112.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_EAST;
} elseif ($windDegree >= 112.5 && $windDegree < 157.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_SOUTHEAST;
} elseif ($windDegree >= 157.5 && $windDegree < 202.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_SOUTH;
} elseif ($windDegree >= 202.5 && $windDegree < 247.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_SOUTHWEST;
} elseif ($windDegree >= 247.5 && $windDegree < 292.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_WEST;
} elseif ($windDegree >= 292.5 && $windDegree < 337.5) {
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_NORTHWEST;
}
return MeteorologicalMonitoringDailyLog::WIND_DIRECTION_NORTH;
}, $attributes['wind_degree']);
}
break;
default: