129 lines
4.1 KiB
PHP
129 lines
4.1 KiB
PHP
<?php
|
|
|
|
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;
|
|
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(['factory'])->poweredBy($factory)->get();
|
|
|
|
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;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
sleep($sleep);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 创建 linkos 气象设备每日报告
|
|
*/
|
|
protected function createReportToLinkosMeteorologicalDevice(Device $device): void
|
|
{
|
|
$lastReportedAt = MeteorologicalDailyReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at')
|
|
?: MeteorologicalReport::where('device_id', $device->id)->latest('reported_at')->value('reported_at');
|
|
|
|
if ($lastReportedAt === null) {
|
|
return;
|
|
}
|
|
|
|
if (is_null($latestReportedAt = MeteorologicalReport::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->createDailyReportToLinkosMeteorologicalDevice($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)->latest('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));
|
|
}
|
|
}
|