优化创建 linkos 设备监控报告流程
parent
96bb016325
commit
d701d25b06
|
|
@ -5,8 +5,6 @@ namespace App\Console\Commands;
|
|||
use App\Models\Device;
|
||||
use App\Models\MeteorologicalDailyReport;
|
||||
use App\Models\MeteorologicalReport;
|
||||
use App\Models\SoilDailyReport;
|
||||
use App\Models\SoilReport;
|
||||
use App\Models\WaterQualityDailyReport;
|
||||
use App\Models\WaterQualityReport;
|
||||
use App\Services\DeviceLogService;
|
||||
|
|
@ -53,15 +51,7 @@ class DeviceLogDailyReportCommand extends Command
|
|||
foreach ($devices as $device) {
|
||||
switch ($device->factory?->key) {
|
||||
case 'link-os':
|
||||
switch ($device->type) {
|
||||
case Device::TYPE_METEOROLOGICAL:
|
||||
$this->createReportToLinkosMeteorologicalDevice($device);
|
||||
break;
|
||||
|
||||
case Device::TYPE_WATER_QUALITY:
|
||||
$this->createReportToLinkosWaterQualityDevice($device);
|
||||
break;
|
||||
}
|
||||
$this->createReportToLinkosDevice($device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -71,58 +61,65 @@ class DeviceLogDailyReportCommand extends Command
|
|||
}
|
||||
|
||||
/**
|
||||
* 创建 linkos 气象设备每日报告
|
||||
* 创建 linkos 设备报告
|
||||
*/
|
||||
protected function createReportToLinkosMeteorologicalDevice(Device $device): void
|
||||
protected function createReportToLinkosDevice(Device $device): void
|
||||
{
|
||||
$lastReportedAt = MeteorologicalDailyReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at')
|
||||
?: MeteorologicalReport::where('device_id', $device->id)->oldest('reported_at')->value('reported_at');
|
||||
$lastReportedAt = null;
|
||||
|
||||
switch ($device->type) {
|
||||
case Device::TYPE_WATER_QUALITY:
|
||||
$lastReportedAt = WaterQualityDailyReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->value('reported_at');
|
||||
|
||||
$lastReportedAt ??= WaterQualityReport::where('device_id', $device->id)
|
||||
->oldest('reported_at')
|
||||
->value('reported_at');
|
||||
break;
|
||||
|
||||
case Device::TYPE_METEOROLOGICAL:
|
||||
$lastReportedAt = MeteorologicalDailyReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->value('reported_at');
|
||||
|
||||
$lastReportedAt ??= MeteorologicalReport::where('device_id', $device->id)
|
||||
->oldest('reported_at')
|
||||
->value('reported_at');
|
||||
break;
|
||||
}
|
||||
|
||||
if ($lastReportedAt === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_null($latestReportedAt = MeteorologicalReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'))) {
|
||||
$latestReportedAt = null;
|
||||
|
||||
switch ($device->type) {
|
||||
case Device::TYPE_WATER_QUALITY:
|
||||
$latestReportedAt = WaterQualityReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->value('reported_at');
|
||||
break;
|
||||
|
||||
case Device::TYPE_METEOROLOGICAL:
|
||||
$latestReportedAt = MeteorologicalReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->value('reported_at');
|
||||
break;
|
||||
}
|
||||
|
||||
if ($latestReportedAt === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var \Carbon\Carbon */
|
||||
$startAt = $lastReportedAt->copy()->startOfDay();
|
||||
/** @var \Carbon\Carbon */
|
||||
$endAt = $latestReportedAt->copy()->startOfDay();
|
||||
|
||||
do {
|
||||
$this->deviceLogService->createDailyReportToLinkosMeteorologicalDevice($device, $startAt->copy());
|
||||
$this->deviceLogService->createDailyReportToLinkosDevice($device, $startAt->copy());
|
||||
|
||||
$startAt->addDay();
|
||||
} while ($endAt->gte($startAt));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 linkos 水质设备每日报告
|
||||
*/
|
||||
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)->oldest('reported_at')->value('reported_at');
|
||||
|
||||
if ($lastReportedAt === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_null($latestReportedAt = WaterQualityReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var \Carbon\Carbon */
|
||||
$startAt = $lastReportedAt->copy()->startOfDay();
|
||||
/** @var \Carbon\Carbon */
|
||||
$endAt = $latestReportedAt->copy()->startOfDay();
|
||||
|
||||
do {
|
||||
$this->deviceLogService->createDailyReportToLinkosWaterQualityDevice($device, $startAt->copy());
|
||||
|
||||
$startAt->addDay();
|
||||
} while ($endAt->gte($startAt));
|
||||
} while ($latestReportedAt->gte($startAt));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,9 @@ namespace App\Console\Commands;
|
|||
|
||||
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;
|
||||
|
||||
class DeviceLogReportCommand extends Command
|
||||
{
|
||||
|
|
@ -51,15 +49,7 @@ class DeviceLogReportCommand extends Command
|
|||
foreach ($devices as $device) {
|
||||
switch ($device->factory?->key) {
|
||||
case 'link-os':
|
||||
switch ($device->type) {
|
||||
case Device::TYPE_METEOROLOGICAL:
|
||||
$this->createReportToLinkosMeteorologicalDevice($device);
|
||||
break;
|
||||
|
||||
case Device::TYPE_WATER_QUALITY:
|
||||
$this->createReportToLinkosWaterQualityDevice($device);
|
||||
break;
|
||||
}
|
||||
$this->createReportToLinkosDevice($device);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
@ -68,57 +58,32 @@ class DeviceLogReportCommand extends Command
|
|||
};
|
||||
}
|
||||
|
||||
protected function createReportToLinkosMeteorologicalDevice(Device $device): void
|
||||
/**
|
||||
* 创建 linkos 设备报告
|
||||
*/
|
||||
protected function createReportToLinkosDevice(Device $device): void
|
||||
{
|
||||
$lastReportedAt = MeteorologicalReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at')
|
||||
?: $device->logs()->oldest('reported_at')->value('reported_at');
|
||||
$lastReportedAt = match ($device->type) {
|
||||
Device::TYPE_WATER_QUALITY => WaterQualityReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'),
|
||||
Device::TYPE_METEOROLOGICAL => MeteorologicalReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'),
|
||||
default => null,
|
||||
};
|
||||
|
||||
if ($lastReportedAt === null) {
|
||||
if (is_null($lastReportedAt ??= $device->logs()->oldest('reported_at')->value('reported_at'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$latestReportedAt = $device->logs()->latest('reported_at')->value('reported_at');
|
||||
|
||||
if ($latestReportedAt === null) {
|
||||
if (is_null($latestReportedAt = $device->logs()->latest('reported_at')->value('reported_at'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
/** @var \Carbon\Carbon */
|
||||
$startAt = $lastReportedAt->copy()->startOfHour();
|
||||
/** @var \Carbon\Carbon */
|
||||
$endAt = $latestReportedAt->copy()->startOfHour();
|
||||
|
||||
do {
|
||||
$this->deviceLogService->createReportToLinkosMeteorologicalDevice($device, $startAt->copy());
|
||||
$this->deviceLogService->createReportToLinkosDevice($device, $startAt->copy());
|
||||
|
||||
$startAt->addHour();
|
||||
} while ($endAt->gte($startAt));
|
||||
}
|
||||
|
||||
protected function createReportToLinkosWaterQualityDevice(Device $device): void
|
||||
{
|
||||
$lastReportedAt = WaterQualityReport::where('device_id', $device->id)->latest('reported_at')->value('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 {
|
||||
$this->deviceLogService->createReportToLinkosWaterQualityDevice($device, $startAt->copy());
|
||||
|
||||
$startAt->addHour();
|
||||
} while ($endAt->gte($startAt));
|
||||
} while ($latestReportedAt->gte($startAt));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace App\Services;
|
|||
|
||||
use App\Iot\Linkos\HttpClient as LinkosHttpClient;
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceLog;
|
||||
use App\Models\MeteorologicalDailyReport;
|
||||
use App\Models\MeteorologicalReport;
|
||||
use App\Models\WaterQualityDailyReport;
|
||||
|
|
@ -78,16 +79,32 @@ class DeviceLogService
|
|||
} while ($countResults === $perPage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 linkos 设备报告
|
||||
*/
|
||||
public function createReportToLinkosDevice(Device $device, Carbon $time): void
|
||||
{
|
||||
switch ($device->type) {
|
||||
case Device::TYPE_METEOROLOGICAL:
|
||||
$this->createReportToLinkosMeteorologicalDevice($device, $time);
|
||||
break;
|
||||
|
||||
case Device::TYPE_WATER_QUALITY:
|
||||
$this->createReportToLinkosWaterQualityDevice($device, $time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 linkos 气象设备报告
|
||||
*/
|
||||
public function createReportToLinkosMeteorologicalDevice(Device $device, Carbon $time)
|
||||
protected function createReportToLinkosMeteorologicalDevice(Device $device, Carbon $time): void
|
||||
{
|
||||
$start = $time->copy()->startOfHour();
|
||||
$reportedAt = $time->copy()->startOfHour();
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$logs = $device->logs()
|
||||
->whereBetween('reported_at', [$start, $start->copy()->endOfHour()])
|
||||
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
||||
->oldest('reported_at')
|
||||
->get();
|
||||
|
||||
|
|
@ -95,76 +112,70 @@ class DeviceLogService
|
|||
return;
|
||||
}
|
||||
|
||||
$attributes = [];
|
||||
$attributes = $logs->reduce(function (array $attributes, DeviceLog $log) {
|
||||
if (is_array($data = $log->data)) {
|
||||
foreach ($data as $k => $v) {
|
||||
$attribute = match ($k) {
|
||||
'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',
|
||||
default => null,
|
||||
};
|
||||
|
||||
/** @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};
|
||||
if ($attribute) {
|
||||
$attributes[$attribute] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
MeteorologicalReport::create(array_merge($attributes, [
|
||||
return $attributes;
|
||||
}, []);
|
||||
|
||||
$meteorologicalReport = MeteorologicalReport::where([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $reportedAt,
|
||||
])->first();
|
||||
|
||||
if ($meteorologicalReport === null) {
|
||||
$lastMeteorologicalReport = MeteorologicalReport::where([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $start,
|
||||
]));
|
||||
'reported_at' => $reportedAt->copy()->subHour(),
|
||||
])->first();
|
||||
|
||||
$meteorologicalReport = $lastMeteorologicalReport?->replicate() ?: new MeteorologicalReport();
|
||||
|
||||
$meteorologicalReport->fill([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $reportedAt,
|
||||
]);
|
||||
}
|
||||
|
||||
$meteorologicalReport->fill($attributes)->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 linkos 水质设备报告
|
||||
*/
|
||||
public function createReportToLinkosWaterQualityDevice(Device $device, Carbon $time)
|
||||
protected function createReportToLinkosWaterQualityDevice(Device $device, Carbon $time): void
|
||||
{
|
||||
$start = $time->copy()->startOfHour();
|
||||
$reportedAt = $time->copy()->startOfHour();
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$logs = $device->logs()
|
||||
->whereBetween('reported_at', [$start, $start->copy()->endOfHour()])
|
||||
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
||||
->oldest('reported_at')
|
||||
->get();
|
||||
|
||||
|
|
@ -172,60 +183,70 @@ class DeviceLogService
|
|||
return;
|
||||
}
|
||||
|
||||
$attributes = [];
|
||||
$attributes = $logs->reduce(function (array $attributes, DeviceLog $log) {
|
||||
if (is_array($data = $log->data)) {
|
||||
foreach ($data as $k => $v) {
|
||||
$attribute = match ($k) {
|
||||
'chlorine' => 'chlorine',
|
||||
'conductivity' => 'conductivity',
|
||||
'oxygen' => 'oxygen',
|
||||
'ph' => 'ph',
|
||||
'temp' => 'temperature',
|
||||
'turbidity' => 'turbidity',
|
||||
default => null,
|
||||
};
|
||||
|
||||
/** @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};
|
||||
if ($attribute) {
|
||||
$attributes[$attribute] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WaterQualityReport::create(array_merge($attributes, [
|
||||
return $attributes;
|
||||
}, []);
|
||||
|
||||
$waterQualityReport = WaterQualityReport::where([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $reportedAt,
|
||||
])->first();
|
||||
|
||||
if ($waterQualityReport === null) {
|
||||
$lastWaterQualityReport = WaterQualityReport::where([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $start,
|
||||
]));
|
||||
'reported_at' => $reportedAt->copy()->subHour(),
|
||||
])->first();
|
||||
|
||||
$waterQualityReport = $lastWaterQualityReport?->replicate() ?: new WaterQualityReport();
|
||||
|
||||
$waterQualityReport->fill([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $reportedAt,
|
||||
]);
|
||||
}
|
||||
|
||||
$waterQualityReport->fill($attributes)->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 linkos 设备每日报告
|
||||
*/
|
||||
public function createDailyReportToLinkosDevice(Device $device, Carbon $time): void
|
||||
{
|
||||
switch ($device->type) {
|
||||
case Device::TYPE_METEOROLOGICAL:
|
||||
$this->createDailyReportToLinkosMeteorologicalDevice($device, $time);
|
||||
break;
|
||||
|
||||
case Device::TYPE_WATER_QUALITY:
|
||||
$this->createDailyReportToLinkosWaterQualityDevice($device, $time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 linkos 气象设备每日报告
|
||||
*/
|
||||
public function createDailyReportToLinkosMeteorologicalDevice(Device $device, Carbon $date): void
|
||||
protected function createDailyReportToLinkosMeteorologicalDevice(Device $device, Carbon $date): void
|
||||
{
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$meteorologicalReports = MeteorologicalReport::where('device_id', $device->id)
|
||||
|
|
@ -352,7 +373,7 @@ class DeviceLogService
|
|||
/**
|
||||
* 创建 linkos 水质设备每日报告
|
||||
*/
|
||||
public function createDailyReportToLinkosWaterQualityDevice(Device $device, Carbon $date): void
|
||||
protected function createDailyReportToLinkosWaterQualityDevice(Device $device, Carbon $date): void
|
||||
{
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$waterQualityReports = WaterQualityReport::where('device_id', $device->id)
|
||||
|
|
|
|||
Loading…
Reference in New Issue