1
0
Fork 0
main
Jing Li 2023-08-09 19:31:54 +08:00
parent 07e0158115
commit e422c3f896
4 changed files with 61 additions and 88 deletions

View File

@ -9,7 +9,7 @@ use App\Models\SoilDailyReport;
use App\Models\SoilReport;
use App\Models\WaterQualityDailyReport;
use App\Models\WaterQualityReport;
use App\Services\DeviceLogService;
use App\Services\LinkosDeviceService;
use Illuminate\Console\Command;
class DeviceLogDailyReportCommand extends Command
@ -30,18 +30,11 @@ class DeviceLogDailyReportCommand extends Command
*/
protected $description = '按设备厂商生成监控报告';
/**
* @var \App\Services\DeviceLogService
*/
protected $deviceLogService;
/**
* Execute the console command.
*/
public function handle(DeviceLogService $deviceLogService)
public function handle()
{
$this->deviceLogService = $deviceLogService;
$factory = $this->argument('factory');
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 300, $this->option('sleep'));
@ -133,7 +126,7 @@ class DeviceLogDailyReportCommand extends Command
$startAt = $lastReportedAt->copy()->startOfDay();
do {
$this->deviceLogService->createDailyReportToLinkosDevice($device, $startAt->copy());
(new LinkosDeviceService())->createDailyReport($device, $startAt->copy());
$startAt->addDay();
} while ($latestReportedAt->gte($startAt));

View File

@ -6,7 +6,7 @@ use App\Models\Device;
use App\Models\MeteorologicalReport;
use App\Models\SoilReport;
use App\Models\WaterQualityReport;
use App\Services\DeviceLogService;
use App\Services\LinkosDeviceService;
use Illuminate\Console\Command;
class DeviceLogReportCommand extends Command
@ -27,18 +27,11 @@ class DeviceLogReportCommand extends Command
*/
protected $description = '按设备厂商生成监控报告';
/**
* @var \App\Services\DeviceLogService
*/
protected $deviceLogService;
/**
* Execute the console command.
*/
public function handle(DeviceLogService $deviceLogService)
public function handle()
{
$this->deviceLogService = $deviceLogService;
$factory = $this->argument('factory');
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 300, $this->option('sleep'));
@ -83,7 +76,7 @@ class DeviceLogReportCommand extends Command
$startAt = $lastReportedAt->copy()->startOfHour();
do {
$this->deviceLogService->createReportToLinkosDevice($device, $startAt->copy());
(new LinkosDeviceService())->createReport($device, $startAt->copy());
$startAt->addHour();
} while ($latestReportedAt->gte($startAt));

View File

@ -3,7 +3,8 @@
namespace App\Console\Commands;
use App\Models\Device;
use App\Services\DeviceLogService;
use App\Services\LinkosDeviceService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Throwable;
@ -25,18 +26,11 @@ class DeviceLogSyncCommand extends Command
*/
protected $description = '按设备厂商同步数据';
/**
* @var \App\Services\DeviceLogService
*/
protected $deviceLogService;
/**
* Execute the console command.
*/
public function handle(DeviceLogService $deviceLogService)
public function handle()
{
$this->deviceLogService = $deviceLogService;
$factory = $this->argument('factory');
$sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 60, $this->option('sleep'));
@ -65,36 +59,44 @@ class DeviceLogSyncCommand extends Command
/** @var \App\Models\Device */
foreach ($devices as $device) {
// 忽略通风设备
if ($device->isTypeAir()) {
continue;
}
$this->info('==========================================');
$this->info('设备编号: ' . $device->sn);
$this->info('设备名称: ' . $device->name);
$this->info('设备类型: ' . match ($device->type) {
Device::TYPE_SOIL => '土壤设备',
Device::TYPE_WATER_QUALITY => '水质设备',
Device::TYPE_METEOROLOGICAL => '气象设备',
Device::TYPE_AIR => '通风设备',
default => '其它',
});
try {
$this->deviceLogService->sync($device, $start, $end);
$this->info('同步成功!');
} catch (Throwable $e) {
report($e);
$this->error('同步失败: '. $e->getMessage());
}
$this->info('==========================================');
$this->sync($device, $start, $end);
}
$this->info('------------------------------------------');
$this->newLine();
}
protected function sync(Device $device, Carbon $start, Carbon $end)
{
$this->info('==========================================');
$this->info('设备编号: ' . $device->sn);
$this->info('设备名称: ' . $device->name);
$this->info('设备厂家: ' . $device->factory?->name);
$this->info('设备类型: ' . match ($device->type) {
Device::TYPE_SOIL => '土壤设备',
Device::TYPE_WATER_QUALITY => '水质设备',
Device::TYPE_METEOROLOGICAL => '气象设备',
Device::TYPE_AIR => '通风设备',
default => '其它',
});
switch ($device->factory?->key) {
case 'link-os':
// 忽略通风设备
if ($device->isTypeAir()) {
return;
}
try {
(new LinkosDeviceService())->sync($device, $start, $end);
$this->info('同步成功!');
} catch (Throwable $e) {
report($e);
$this->error('同步失败: '. $e->getMessage());
}
break;
}
$this->info('==========================================');
}
}

View File

@ -14,24 +14,12 @@ use App\Models\WaterQualityReport;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
class DeviceLogService
class LinkosDeviceService
{
/**
* 同步设备历史流水
*/
public function sync(Device $device, Carbon $start, Carbon $end): void
{
switch ($device->factory?->key) {
case 'link-os':
$this->syncLinkosDeviceLogs($device, $start, $end);
break;
}
}
/**
* 同步 Linkos 设备历史流水
*/
protected function syncLinkosDeviceLogs(Device $device, Carbon $start, Carbon $end): void
public function sync(Device $device, Carbon $start, Carbon $end): void
{
/** @var \App\Iot\Linkos\HttpClient */
$httpClient = app(LinkosHttpClient::class);
@ -94,27 +82,24 @@ class DeviceLogService
/**
* 创建 linkos 设备报告
*/
public function createReportToLinkosDevice(Device $device, Carbon $time): void
public function createReport(Device $device, Carbon $time): void
{
switch ($device->type) {
case Device::TYPE_SOIL:
$this->createReportToLinkosSoilDevice($device, $time);
$this->createSoilReport($device, $time);
break;
case Device::TYPE_METEOROLOGICAL:
$this->createReportToLinkosMeteorologicalDevice($device, $time);
$this->createMeteorologicalReport($device, $time);
break;
case Device::TYPE_WATER_QUALITY:
$this->createReportToLinkosWaterQualityDevice($device, $time);
$this->createWaterQualityReport($device, $time);
break;
}
}
/**
* 创建 linkos 土壤设备报告
*/
protected function createReportToLinkosSoilDevice(Device $device, Carbon $time): void
protected function createSoilReport(Device $device, Carbon $time): void
{
$reportedAt = $time->copy()->startOfHour();
@ -180,7 +165,7 @@ class DeviceLogService
/**
* 创建 linkos 气象设备报告
*/
protected function createReportToLinkosMeteorologicalDevice(Device $device, Carbon $time): void
protected function createMeteorologicalReport(Device $device, Carbon $time): void
{
$reportedAt = $time->copy()->startOfHour();
@ -246,7 +231,7 @@ class DeviceLogService
}
$meteorologicalReport->fill($attributes)->save();
(new \App\Services\Admin\DeviceService())->meteorologicalControAir($device, $meteorologicalReport, $time);
(new DeviceWarningService())->judgeLog($device, $meteorologicalReport, $time);
}
@ -254,7 +239,7 @@ class DeviceLogService
/**
* 创建 linkos 水质设备报告
*/
protected function createReportToLinkosWaterQualityDevice(Device $device, Carbon $time): void
protected function createWaterQualityReport(Device $device, Carbon $time): void
{
$reportedAt = $time->copy()->startOfHour();
@ -317,19 +302,19 @@ class DeviceLogService
/**
* 创建 linkos 设备每日报告
*/
public function createDailyReportToLinkosDevice(Device $device, Carbon $time): void
public function createDailyReport(Device $device, Carbon $time): void
{
switch ($device->type) {
case Device::TYPE_METEOROLOGICAL:
$this->createDailyReportToLinkosMeteorologicalDevice($device, $time);
$this->createMeteorologicalDailyReport($device, $time);
break;
case Device::TYPE_WATER_QUALITY:
$this->createDailyReportToLinkosWaterQualityDevice($device, $time);
$this->createWaterQualityDailyReport($device, $time);
break;
case Device::TYPE_SOIL:
$this->createDailyReportToLinkosSoilDevice($device, $time);
$this->createSoilDailyReport($device, $time);
break;
}
}
@ -337,7 +322,7 @@ class DeviceLogService
/**
* 创建 linkos 土壤设备每日报告
*/
protected function createDailyReportToLinkosSoilDevice(Device $device, Carbon $date): void
protected function createSoilDailyReport(Device $device, Carbon $date): void
{
/** @var \Illuminate\Database\Eloquent\Collection */
$soilReports = SoilReport::where('device_id', $device->id)
@ -394,7 +379,7 @@ class DeviceLogService
/**
* 创建 linkos 气象设备每日报告
*/
protected function createDailyReportToLinkosMeteorologicalDevice(Device $device, Carbon $date): void
protected function createMeteorologicalDailyReport(Device $device, Carbon $date): void
{
/** @var \Illuminate\Database\Eloquent\Collection */
$meteorologicalReports = MeteorologicalReport::where('device_id', $device->id)
@ -527,7 +512,7 @@ class DeviceLogService
/**
* 创建 linkos 水质设备每日报告
*/
protected function createDailyReportToLinkosWaterQualityDevice(Device $device, Carbon $date): void
protected function createWaterQualityDailyReport(Device $device, Carbon $date): void
{
/** @var \Illuminate\Database\Eloquent\Collection */
$waterQualityReports = WaterQualityReport::where('device_id', $device->id)