deviceLogService = $deviceLogService; $factory = $this->argument('factory'); $sleep = (int) value(fn ($sleep) => is_numeric($sleep) ? $sleep : 60, $this->option('sleep')); while (true) { $this->runSync($factory); sleep($sleep); }; } /** * 执行同步 */ protected function runSync(string $factory): void { $end = now(); $start = $end->copy()->subHours(1); $this->info('------------------------------------------'); $this->info('开始时间: '. $start); $this->info('结束时间: '. $end); /** @var \Illuminate\Database\Eloquent\Collection */ $devices = Device::with(['supplier'])->supplierBy($factory)->get(); /** @var \App\Models\Device */ foreach ($devices as $device) { $this->info('=========================================='); $this->info('设备编号: ' . $device->sn); $this->info('设备名称: ' . $device->name); $this->info('设备类型: ' . match ($device->type) { DeviceType::Soil => '土壤设备', DeviceType::WaterQuality => '水质设备', DeviceType::Meteorological => '气象设备', default => '其它', }); try { $this->deviceLogService->sync($device, $start, $end); $this->info('同步成功!'); } catch (Throwable $e) { report($e); $this->error('同步失败: '. $e->getMessage()); } $this->info('=========================================='); } $this->info('------------------------------------------'); $this->newLine(); } }