Fix
parent
a33d981dcc
commit
418e227866
|
|
@ -237,116 +237,116 @@ class DeviceLogService
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $meteorologicalReports->reduce(function (array $carry, MeteorologicalReport $meteorologicalReport) {
|
$attributes = value(function ($meteorologicalReports) {
|
||||||
foreach ($carry as $key => $item) {
|
$data = [
|
||||||
if (is_null($v = $meteorologicalReport->{$key})) {
|
'today_rainfall' => ['sum' => 0, 'count' => 0],
|
||||||
continue;
|
'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) {
|
switch ($key) {
|
||||||
case 'wind_samples':
|
case 'wind_samples':
|
||||||
if (! is_null($meteorologicalReport->wind_degree) && ! is_null($meteorologicalReport->wind_speed)) {
|
$attributes['wind_degree'] = value(function (array $windSamples) {
|
||||||
$item['wind_samples'][] = [
|
$x = 0;
|
||||||
'wind_degree' => $meteorologicalReport->wind_degree, // 风向度数
|
$y = 0;
|
||||||
'wind_speed' => $meteorologicalReport->wind_speed, // 风速
|
|
||||||
];
|
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;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$item['sum'] = bcadd($item['sum'], $v, 2);
|
$attributes[$key] = $item['count'] > 0 ? round(bcdiv($item['sum'], $item['count'], 2), 2) : null;
|
||||||
$item['count']++;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$carry[$key] = $item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $carry;
|
return $attributes;
|
||||||
}, [
|
}, $meteorologicalReports);
|
||||||
'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' => [],
|
|
||||||
]);
|
|
||||||
|
|
||||||
|
/** @var \App\Models\MeteorologicalDailyReport */
|
||||||
$meteorologicalDailyReport = MeteorologicalDailyReport::firstOrCreate([
|
$meteorologicalDailyReport = MeteorologicalDailyReport::firstOrCreate([
|
||||||
'device_id' => $device->id,
|
'device_id' => $device->id,
|
||||||
'reported_at' => $date,
|
'reported_at' => $date,
|
||||||
]);
|
], $attributes);
|
||||||
|
|
||||||
foreach ($result as $key => $item) {
|
if (! $meteorologicalDailyReport->wasRecentlyCreated) {
|
||||||
switch ($key) {
|
$meteorologicalDailyReport->update($attributes);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$meteorologicalDailyReport->save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -364,39 +364,46 @@ class DeviceLogService
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $waterQualityReports->reduce(function (array $carry, WaterQualityReport $waterQualityReport) {
|
$attributes = value(function ($waterQualityReports) {
|
||||||
foreach ($carry as $key => $item) {
|
$data = [
|
||||||
if (is_null($v = $waterQualityReport->{$key})) {
|
'chlorine' => ['sum' => 0, 'count' => 0],
|
||||||
continue;
|
'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;
|
$attributes = [];
|
||||||
}, [
|
|
||||||
'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 ($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([
|
$waterQualityDailyReport = WaterQualityDailyReport::firstOrCreate([
|
||||||
'device_id' => $device->id,
|
'device_id' => $device->id,
|
||||||
'reported_at' => $date->format('Y-m-d'),
|
'reported_at' => $date->format('Y-m-d'),
|
||||||
]);
|
], $attributes);
|
||||||
|
|
||||||
foreach ($result as $key => $item) {
|
if (! $waterQualityDailyReport->wasRecentlyCreated) {
|
||||||
if ($item['count'] > 0) {
|
$waterQualityDailyReport->update($attributes);
|
||||||
$waterQualityDailyReport->{$key} = round(bcdiv($item['sum'], $item['count'], 2), 2);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$waterQualityDailyReport->save();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue