Update
parent
01a057bfc8
commit
74fc14fd90
|
|
@ -55,7 +55,7 @@ class WormStatisticsSyncCommand extends Command
|
|||
|
||||
$latestReportedAt = WormReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at');
|
||||
|
||||
$start = $latestReportedAt ? $latestReportedAt->copy() : $today->copy()->subDays(179);
|
||||
$start = $latestReportedAt ? $latestReportedAt->copy() : $today->copy();
|
||||
|
||||
$days = $start->diffInDays($today, false);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands\YunFei;
|
||||
|
||||
use App\Enums\DeviceType;
|
||||
use App\Models\Device;
|
||||
use App\Models\InsecticidalLampDailyReport;
|
||||
use App\Models\InsecticidalLampReport;
|
||||
use App\Services\YunFeiDeviceService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class DeviceLogDailyReportCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'yunfei:device-log-daily-report
|
||||
{--sleep=300 : 监控报告生产后的休眠时间(秒)}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '按设备厂商生成监控报告';
|
||||
|
||||
/**
|
||||
* @var \App\Services\DeviceLogService
|
||||
*/
|
||||
protected $deviceLogService;
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$seconds = (int) value(fn ($seconds) => is_numeric($seconds) ? $seconds : 300, $this->option('sleep'));
|
||||
|
||||
while (true) {
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$devices = Device::supplierBy('device-supplier-yunfei')->get();
|
||||
|
||||
foreach ($devices as $device) {
|
||||
$this->createReport($device);
|
||||
}
|
||||
|
||||
sleep($seconds);
|
||||
};
|
||||
}
|
||||
|
||||
protected function createReport(Device $device): void
|
||||
{
|
||||
[$lastReportedAt, $latestReportedAt] = value(function (Device $device) {
|
||||
$lastReportedAt = null;
|
||||
|
||||
$latestReportedAt = null;
|
||||
|
||||
switch ($device->type) {
|
||||
case DeviceType::InsecticidalLamp:
|
||||
$lastReportedAt = InsecticidalLampDailyReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->value('reported_at');
|
||||
|
||||
$lastReportedAt ??= InsecticidalLampReport::where('device_id', $device->id)
|
||||
->oldest('reported_at')
|
||||
->value('reported_at');
|
||||
|
||||
if ($lastReportedAt) {
|
||||
$latestReportedAt = InsecticidalLampReport::where('device_id', $device->id)
|
||||
->latest('reported_at')
|
||||
->value('reported_at');
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return [$lastReportedAt, $latestReportedAt];
|
||||
}, $device);
|
||||
|
||||
if ($lastReportedAt === null || $latestReportedAt === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$service = new YunFeiDeviceService();
|
||||
|
||||
/** @var \Carbon\Carbon */
|
||||
$startAt = $lastReportedAt->copy()->startOfDay();
|
||||
|
||||
do {
|
||||
$service->createDailyReport($device, $startAt->copy());
|
||||
|
||||
$startAt->addDay();
|
||||
} while ($latestReportedAt->gte($startAt));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
namespace App\Console\Commands\YunFei;
|
||||
|
||||
use App\Enums\DeviceType;
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceLog;
|
||||
use App\Models\InsecticidalLampReport;
|
||||
use App\Services\YunFeiDeviceService;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class DeviceLogReportCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'yunfei:device-log-report
|
||||
{--sleep=300 : 监控报告生产后的休眠时间(秒)}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = '按设备厂商生成监控报告';
|
||||
|
||||
/**
|
||||
* @var \App\Services\DeviceLogService
|
||||
*/
|
||||
protected $deviceLogService;
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$seconds = (int) value(fn ($seconds) => is_numeric($seconds) ? $seconds : 300, $this->option('sleep'));
|
||||
|
||||
while (true) {
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$devices = Device::supplierBy('device-supplier-yunfei')->get();
|
||||
|
||||
foreach ($devices as $device) {
|
||||
$this->createReport($device);
|
||||
}
|
||||
|
||||
sleep($seconds);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建比昂设备报告
|
||||
*/
|
||||
protected function createReport(Device $device): void
|
||||
{
|
||||
$lastReportedAt = match ($device->type) {
|
||||
DeviceType::InsecticidalLamp => InsecticidalLampReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at'),
|
||||
default => null,
|
||||
};
|
||||
|
||||
if (is_null($lastReportedAt ??= DeviceLog::where('device_id', $device->id)->oldest('reported_at')->value('reported_at'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_null($latestReportedAt = DeviceLog::where('device_id', $device->id)->latest('reported_at')->value('reported_at'))) {
|
||||
return;
|
||||
}
|
||||
|
||||
$service = new YunFeiDeviceService();
|
||||
|
||||
/** @var \Carbon\Carbon */
|
||||
$startAt = $lastReportedAt->copy()->startOfHour();
|
||||
|
||||
do {
|
||||
$service->createReport($device, $startAt->copy());
|
||||
|
||||
$startAt->addHour();
|
||||
} while ($latestReportedAt->gte($startAt));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,211 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Enums\DeviceType;
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceLog;
|
||||
use App\Models\InsecticidalLampDailyReport;
|
||||
use App\Models\InsecticidalLampReport;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class YunFeiDeviceService
|
||||
{
|
||||
/**
|
||||
* 创建设备报告
|
||||
*/
|
||||
public function createReport(Device $device, Carbon $time): void
|
||||
{
|
||||
switch ($device->type) {
|
||||
case DeviceType::InsecticidalLamp:
|
||||
$this->createInsecticidalLampReport($device, $time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建杀虫灯设备报告
|
||||
*/
|
||||
protected function createInsecticidalLampReport(Device $device, Carbon $time): void
|
||||
{
|
||||
$reportedAt = $time->copy()->startOfHour();
|
||||
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$logs = DeviceLog::where('device_id', $device->id)
|
||||
->whereBetween('reported_at', [$reportedAt, $reportedAt->copy()->endOfHour()])
|
||||
->oldest('reported_at')
|
||||
->get();
|
||||
|
||||
if ($logs->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$attributes = value(function ($logs) {
|
||||
$data = [
|
||||
'battery_vol' => ['sum' => 0, 'count' => 0],
|
||||
'charging_vol' => ['sum' => 0, 'count' => 0],
|
||||
'killed_num' => ['sum' => 0, 'count' => 0],
|
||||
'air_temperature' => ['sum' => 0, 'count' => 0],
|
||||
'air_humidity' => ['sum' => 0, 'count' => 0],
|
||||
];
|
||||
|
||||
/** @var \App\Models\DeviceLog */
|
||||
foreach ($logs as $log) {
|
||||
if (! is_array($log->data)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($data as $key => $item) {
|
||||
switch ($key) {
|
||||
// 电池电压
|
||||
case 'battery_vol':
|
||||
if (! is_null($bv = Arr::get($log->data, 'bv'))) {
|
||||
$item['sum'] = bcadd($item['sum'], $bv, 2);
|
||||
$item['count'] += 1;
|
||||
}
|
||||
break;
|
||||
|
||||
// 充电电压
|
||||
case 'charging_vol':
|
||||
if (! is_null($cv = Arr::get($log->data, 'cv'))) {
|
||||
$item['sum'] = bcadd($item['sum'], $cv, 2);
|
||||
$item['count'] += 1;
|
||||
}
|
||||
break;
|
||||
|
||||
// 杀虫数
|
||||
case 'killed_num':
|
||||
if (! is_null($ct = Arr::get($log->data, 'ct'))) {
|
||||
$item['sum'] += $ct;
|
||||
$item['count'] += 1;
|
||||
}
|
||||
break;
|
||||
|
||||
// 温度
|
||||
case 'air_temperature':
|
||||
if (! is_null($at = Arr::get($log->data, 'at'))) {
|
||||
$item['sum'] = bcadd($item['sum'], $at, 2);
|
||||
$item['count'] += 1;
|
||||
}
|
||||
break;
|
||||
|
||||
// 湿度
|
||||
case 'air_humidity':
|
||||
if (! is_null($ah = Arr::get($log->data, 'ah'))) {
|
||||
$item['sum'] = bcadd($item['sum'], $ah, 2);
|
||||
$item['count'] += 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$data[$key] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
$attributes = [];
|
||||
|
||||
foreach ($data as $key => $item) {
|
||||
if ($item['count'] > 0) {
|
||||
if ($key === 'killed_num') {
|
||||
$attributes[$key] = (int) $item['sum'];
|
||||
} else {
|
||||
$attributes[$key] = round(bcdiv($item['sum'], $item['count'], 2), 2);
|
||||
}
|
||||
} else {
|
||||
$attributes[$key] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}, $logs);
|
||||
|
||||
/** @var \App\Models\InsecticidalLampReport */
|
||||
$insecticidalLampReport = InsecticidalLampReport::firstOrNew([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $reportedAt,
|
||||
], [
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
]);
|
||||
|
||||
$insecticidalLampReport->fill($attributes)->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建设备每日报告
|
||||
*/
|
||||
public function createDailyReport(Device $device, Carbon $time): void
|
||||
{
|
||||
switch ($device->type) {
|
||||
case DeviceType::InsecticidalLamp:
|
||||
$this->createInsecticidalLampDailyReport($device, $time);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 杀虫灯每日报告
|
||||
*/
|
||||
protected function createInsecticidalLampDailyReport(Device $device, Carbon $date): void
|
||||
{
|
||||
/** @var \Illuminate\Database\Eloquent\Collection */
|
||||
$insecticidalLampReports = InsecticidalLampReport::where('device_id', $device->id)
|
||||
->whereDate('reported_at', $date)
|
||||
->oldest('reported_at')
|
||||
->get();
|
||||
|
||||
if ($insecticidalLampReports->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$attributes = value(function ($insecticidalLampReports) {
|
||||
$data = [
|
||||
'battery_vol' => ['sum' => 0, 'count' => 0],
|
||||
'charging_vol' => ['sum' => 0, 'count' => 0],
|
||||
'killed_num' => ['sum' => 0, 'count' => 0],
|
||||
'air_temperature' => ['sum' => 0, 'count' => 0],
|
||||
'air_humidity' => ['sum' => 0, 'count' => 0],
|
||||
'high_vol' => ['sum' => 0, 'count' => 0],
|
||||
];
|
||||
|
||||
foreach ($insecticidalLampReports as $insecticidalLampReport) {
|
||||
foreach ($data as $k => $item) {
|
||||
if (is_null($v = $insecticidalLampReport->{$k})) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item['sum'] = bcadd($item['sum'], $v, 2);
|
||||
$item['count']++;
|
||||
|
||||
$data[$k] = $item;
|
||||
}
|
||||
}
|
||||
|
||||
$attributes = [];
|
||||
|
||||
foreach ($data as $key => $item) {
|
||||
if ($item['count'] > 0) {
|
||||
if ($key === 'killed_num') {
|
||||
$attributes[$key] = (int) $item['sum'];
|
||||
} else {
|
||||
$attributes[$key] = round(bcdiv($item['sum'], $item['count'], 2), 2);
|
||||
}
|
||||
} else {
|
||||
$attributes[$key] = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}, $insecticidalLampReports);
|
||||
|
||||
/** @var \App\Models\InsecticidalLampDailyReport */
|
||||
$insecticidalLampDailyReport = InsecticidalLampDailyReport::firstOrNew([
|
||||
'device_id' => $device->id,
|
||||
'reported_at' => $date->format('Y-m-d'),
|
||||
], [
|
||||
'agricultural_base_id' => $device->agricultural_base_id,
|
||||
]);
|
||||
|
||||
$insecticidalLampDailyReport->fill($attributes)->save();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue