87 lines
2.6 KiB
PHP
87 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands\BiAng;
|
|
|
|
use App\Enums\DeviceType;
|
|
use App\Models\Device;
|
|
use App\Models\DeviceLog;
|
|
use App\Models\InsecticidalLampReport;
|
|
use App\Models\MeteorologicalMonitoringLog;
|
|
use App\Models\SoilMonitoringLog;
|
|
use App\Services\BiAngDeviceService;
|
|
use Illuminate\Console\Command;
|
|
|
|
class DeviceLogReportCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'biang: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()
|
|
{
|
|
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 300, $this->option('sleep'));
|
|
|
|
while (true) {
|
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
|
$devices = Device::supplierBy("device-supplier-biang")->get();
|
|
|
|
foreach ($devices as $device) {
|
|
$this->createReport($device);
|
|
}
|
|
|
|
sleep($sleep);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 创建比昂设备报告
|
|
*/
|
|
protected function createReport(Device $device): void
|
|
{
|
|
$lastReportedAt = match ($device->type) {
|
|
DeviceType::Soil => SoilMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
|
|
DeviceType::Meteorological => MeteorologicalMonitoringLog::where('device_id', $device->id)->latest('monitored_at')->value('monitored_at'),
|
|
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 BiAngDeviceService();
|
|
|
|
/** @var \Carbon\Carbon */
|
|
$startAt = $lastReportedAt->copy()->startOfHour();
|
|
|
|
do {
|
|
$service->createReport($device, $startAt->copy());
|
|
|
|
$startAt->addHour();
|
|
} while ($latestReportedAt->gte($startAt));
|
|
}
|
|
}
|