From 3f43e9953f102a77576b7f8c3590aad085f83a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Fri, 5 May 2023 16:27:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=95=B0=E6=8D=AE=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/DeviceLogSyncCommand.php | 83 +++++++++++++++++++ app/Models/Device.php | 16 +++- app/Services/DeviceLogService.php | 59 +++++++++++++ 3 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 app/Console/Commands/DeviceLogSyncCommand.php create mode 100644 app/Services/DeviceLogService.php diff --git a/app/Console/Commands/DeviceLogSyncCommand.php b/app/Console/Commands/DeviceLogSyncCommand.php new file mode 100644 index 0000000..4dda41d --- /dev/null +++ b/app/Console/Commands/DeviceLogSyncCommand.php @@ -0,0 +1,83 @@ +deviceLogService = $deviceLogService; + + $sleep = value(fn ($sleep) => is_numeric($sleep) ? (int) $sleep : 60, $this->option('sleep')); + + while (true) { + $end = now(); + $start = $end->copy()->subHours(1); + + $this->info('------------------------------------------'); + $this->info('开始时间: '. $start); + $this->info('结束时间: '. $end); + + $this->performSync($start, $end); + + $this->info('------------------------------------------'); + $this->newLine(); + + sleep($sleep); + }; + } + + protected function performSync(Carbon $start, Carbon $end): void + { + /** @var \Illuminate\Database\Eloquent\Collection */ + $devices = Device::poweredBy($this->argument('factory'))->get(); + + foreach ($devices as $device) { + $this->info('=========================================='); + $this->info('设备编号: ' . $device->sn); + $this->info('设备名称: ' . $device->name); + + try { + $this->deviceLogService->sync($device, $start, $end); + + $this->info('同步成功!'); + } catch (Throwable $e) { + report($e); + + $this->error('同步失败: '. $e->getMessage()); + } + + $this->info('=========================================='); + } + } +} diff --git a/app/Models/Device.php b/app/Models/Device.php index 9d33899..cd09d9d 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; use EloquentFilter\Filterable; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Relations\HasMany; class Device extends Model @@ -22,15 +23,21 @@ class Device extends Model public const STATE_OFFLINE = 2; public const STATE_FAULT = 3; + protected $casts = [ + 'extends' => 'json' + ]; + protected $fillable = [ 'name', 'sn', 'powered_by', 'type', 'model_sn', 'state', 'extends', 'is_enable', 'sort', 'is_recommend', 'group_tags' ]; - protected $casts = [ - 'extends' => 'json' - ]; + public function scopePoweredBy(Builder $query, string $factory): void + { + $query->whereHas('factory', fn ($query) => $query->where('key', $factory)); + } + protected function serializeDate(\DateTimeInterface $date){ return $date->format('Y-m-d H:i:s'); @@ -53,7 +60,8 @@ class Device extends Model return $this->hasMany(DeviceLog::class); } - public function factory(){ + public function factory() + { return $this->belongsTo(Keyword::class, 'powered_by'); } } diff --git a/app/Services/DeviceLogService.php b/app/Services/DeviceLogService.php new file mode 100644 index 0000000..eb5b9b7 --- /dev/null +++ b/app/Services/DeviceLogService.php @@ -0,0 +1,59 @@ +factory?->key) { + case 'link-os': + $this->syncLinkosDeviceLogs($device, $start, $end); + break; + } + } + + /** + * 同步 Linkos 设备历史流水 + */ + protected function syncLinkosDeviceLogs(Device $device, Carbon $start, Carbon $end): void + { + /** @var \App\Iot\Linkos\HttpClient */ + $httpClient = app(LinkosHttpClient::class); + + $page = 1; + + $perPage = 50; + + do { + $data = $httpClient->getDeviceFlowList( + $device->sn, $start, $end, $page, $perPage + ); + + $countResults = count($data['content']); + + if ($countResults === 0) { + break; + } + + foreach ($data['content'] as $item) { + $device->logs()->firstOrCreate([ + 'reported_at' => $item['createTime'], + ], [ + 'data' => empty($item['data']) ? (new \stdClass) : $item['data'], + ]); + } + + unset($data); + + $page++; + } while ($countResults === $perPage); + } +}