diff --git a/app/Console/Commands/DeviceLogSyncCommand.php b/app/Console/Commands/DeviceLogSyncCommand.php index d5ea029..8a900ea 100644 --- a/app/Console/Commands/DeviceLogSyncCommand.php +++ b/app/Console/Commands/DeviceLogSyncCommand.php @@ -68,6 +68,13 @@ class DeviceLogSyncCommand extends Command $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); diff --git a/app/Models/Device.php b/app/Models/Device.php index cd09d9d..bc21c8b 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -64,4 +64,9 @@ class Device extends Model { return $this->belongsTo(Keyword::class, 'powered_by'); } + + public function isTypeSoil(): bool + { + return $this->type === static::TYPE_SOIL; + } } diff --git a/app/Services/DeviceLogService.php b/app/Services/DeviceLogService.php index eb5b9b7..6c185ca 100644 --- a/app/Services/DeviceLogService.php +++ b/app/Services/DeviceLogService.php @@ -4,6 +4,7 @@ namespace App\Services; use App\Iot\Linkos\HttpClient as LinkosHttpClient; use App\Models\Device; +use Illuminate\Support\Arr; use Illuminate\Support\Carbon; class DeviceLogService @@ -44,6 +45,18 @@ class DeviceLogService } foreach ($data['content'] as $item) { + $isSoilMonitoring = Arr::hasAny($item['data'], [ + 'soil_humidity', + 'soil_temperature', + 'nitrogen_content', + 'potassium_content', + 'phosphorus_content', + ]); + + if (($device->isTypeSoil() && ! $isSoilMonitoring) || (! $device->isTypeSoil() && $isSoilMonitoring)) { + continue; + } + $device->logs()->firstOrCreate([ 'reported_at' => $item['createTime'], ], [