149 lines
4.9 KiB
PHP
149 lines
4.9 KiB
PHP
<?php
|
|
|
|
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;
|
|
|
|
class DeviceLogDailyReportCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'device-log:daily-report
|
|
{factory}
|
|
{--sleep=300 : 监控报告生产后的休眠时间(秒)}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '按设备厂商生成监控报告';
|
|
|
|
/**
|
|
* @var \App\Services\DeviceLogService
|
|
*/
|
|
protected $deviceLogService;
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(DeviceLogService $deviceLogService)
|
|
{
|
|
$this->deviceLogService = $deviceLogService;
|
|
|
|
$factory = $this->argument('factory');
|
|
|
|
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 300, $this->option('sleep'));
|
|
|
|
while (true) {
|
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
|
$devices = Device::with(['supplier'])->supplierBy($factory)->get();
|
|
|
|
foreach ($devices as $device) {
|
|
switch ($device->supplier?->key) {
|
|
case 'linkos':
|
|
$this->createReportToLinkosDevice($device);
|
|
break;
|
|
}
|
|
}
|
|
|
|
sleep($sleep);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 创建 linkos 设备报告
|
|
*/
|
|
protected function createReportToLinkosDevice(Device $device): void
|
|
{
|
|
[$lastReportedAt, $latestReportedAt] = value(function (Device $device) {
|
|
$lastReportedAt = null;
|
|
|
|
$latestReportedAt = null;
|
|
|
|
switch ($device->type) {
|
|
case DeviceType::WaterQuality:
|
|
$lastReportedAt = WaterQualityMonitoringDailyLog::where('device_id', $device->id)
|
|
->latest('monitored_at')
|
|
->value('monitored_at');
|
|
|
|
$lastReportedAt ??= WaterQualityMonitoringLog::where('device_id', $device->id)
|
|
->oldest('monitored_at')
|
|
->value('monitored_at');
|
|
|
|
if ($lastReportedAt) {
|
|
$latestReportedAt = WaterQualityMonitoringLog::where('device_id', $device->id)
|
|
->latest('monitored_at')
|
|
->value('monitored_at');
|
|
}
|
|
break;
|
|
|
|
case DeviceType::Meteorological:
|
|
$lastReportedAt = MeteorologicalMonitoringDailyLog::where('device_id', $device->id)
|
|
->latest('monitored_at')
|
|
->value('monitored_at');
|
|
|
|
$lastReportedAt ??= MeteorologicalMonitoringLog::where('device_id', $device->id)
|
|
->oldest('monitored_at')
|
|
->value('monitored_at');
|
|
|
|
if ($lastReportedAt) {
|
|
$latestReportedAt = MeteorologicalMonitoringLog::where('device_id', $device->id)
|
|
->latest('monitored_at')
|
|
->value('monitored_at');
|
|
}
|
|
break;
|
|
|
|
case DeviceType::Soil:
|
|
$lastReportedAt = SoilMonitoringDailyLog::where('device_id', $device->id)
|
|
->latest('monitored_at')
|
|
->value('monitored_at');
|
|
|
|
$lastReportedAt ??= SoilMonitoringLog::where('device_id', $device->id)
|
|
->oldest('monitored_at')
|
|
->value('monitored_at');
|
|
|
|
if ($lastReportedAt) {
|
|
$latestReportedAt = SoilMonitoringLog::where('device_id', $device->id)
|
|
->latest('monitored_at')
|
|
->value('monitored_at');
|
|
}
|
|
break;
|
|
}
|
|
|
|
return [$lastReportedAt, $latestReportedAt];
|
|
}, $device);
|
|
|
|
if ($lastReportedAt === null || $latestReportedAt === null) {
|
|
return;
|
|
}
|
|
|
|
/** @var \Carbon\Carbon */
|
|
$startAt = $lastReportedAt->copy()->startOfDay();
|
|
|
|
do {
|
|
$this->deviceLogService->createDailyReportToLinkosDevice($device, $startAt->copy());
|
|
|
|
$startAt->addDay();
|
|
} while ($latestReportedAt->gte($startAt));
|
|
}
|
|
}
|