From 235fbeafe6c0a5a98d8d0e5263ed16d9053491df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Fri, 28 Apr 2023 14:02:31 +0800 Subject: [PATCH 1/4] Update linkos http client --- app/Iot/Linkos/HttpClient.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Iot/Linkos/HttpClient.php b/app/Iot/Linkos/HttpClient.php index 9b7265a..fa792d3 100644 --- a/app/Iot/Linkos/HttpClient.php +++ b/app/Iot/Linkos/HttpClient.php @@ -25,16 +25,16 @@ class HttpClient * @param array $pageable * @return array */ - public function getDeviceFlowList(string $deviceId, Carbon $start, Carbon $end, array $pageable = []): array + public function getDeviceFlowList(string $deviceId, Carbon $start, Carbon $end, int $page = 0, int $perPage = 50): array { $result = $this->post('/api/deviceFlow/v1/list', [ 'device_id' => $deviceId, 'start_time' => $start->unix() * 1000, 'end_time' => $end->unix() * 1000, - 'pageable' => array_merge([ - 'page' => 0, - 'size' => 20, - ], $pageable), + 'pageable' => [ + 'page' => $page, + 'size' => $perPage, + ], ]); return $result['data']; From f2a1be3adc7169e8b9725849af497ca4b1371448 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Fri, 28 Apr 2023 14:06:35 +0800 Subject: [PATCH 2/4] Update linkos http client --- app/Iot/Linkos/HttpClient.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Iot/Linkos/HttpClient.php b/app/Iot/Linkos/HttpClient.php index fa792d3..1b01273 100644 --- a/app/Iot/Linkos/HttpClient.php +++ b/app/Iot/Linkos/HttpClient.php @@ -22,17 +22,18 @@ class HttpClient * @param string $deviceId * @param \Illuminate\Support\Carbon $start * @param \Illuminate\Support\Carbon $end - * @param array $pageable + * @param int $page + * @param int $perPage * @return array */ - public function getDeviceFlowList(string $deviceId, Carbon $start, Carbon $end, int $page = 0, int $perPage = 50): array + public function getDeviceFlowList(string $deviceId, Carbon $start, Carbon $end, int $page = 1, int $perPage = 50): array { $result = $this->post('/api/deviceFlow/v1/list', [ 'device_id' => $deviceId, 'start_time' => $start->unix() * 1000, 'end_time' => $end->unix() * 1000, 'pageable' => [ - 'page' => $page, + 'page' => $page - 1, 'size' => $perPage, ], ]); From 0eef056851e548798ad495f579691ab9e4b9222a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Fri, 28 Apr 2023 16:56:38 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E8=AE=BE=E5=A4=87=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/Device.php | 10 +++++ app/Models/DeviceLog.php | 16 ++++++++ ..._04_28_113344_create_device_logs_table.php | 37 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 app/Models/DeviceLog.php create mode 100644 database/migrations/2023_04_28_113344_create_device_logs_table.php diff --git a/app/Models/Device.php b/app/Models/Device.php index aa5f9fe..81706f8 100644 --- a/app/Models/Device.php +++ b/app/Models/Device.php @@ -5,6 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use EloquentFilter\Filterable; +use Illuminate\Database\Eloquent\Relations\HasMany; class Device extends Model { @@ -16,6 +17,10 @@ class Device extends Model public const TYPE_WATER_QUALITY = 3; //水质设备 public const TYPE_METEOROLOGICAL = 4; //气象设备 + public const STATE_DISABLED = 0; + public const STATE_ONLINE = 1; + public const STATE_OFFLINE = 2; + public const STATE_FAULT = 3; protected $fillable = [ 'name', 'sn', 'powered_by', 'type', 'model_sn', 'state', 'extends', @@ -26,6 +31,11 @@ class Device extends Model return $date->format('Y-m-d H:i:s'); } + public function logs(): HasMany + { + return $this->hasMany(DeviceLog::class); + } + public static function typeMap() { return [ diff --git a/app/Models/DeviceLog.php b/app/Models/DeviceLog.php new file mode 100644 index 0000000..fe2bfd6 --- /dev/null +++ b/app/Models/DeviceLog.php @@ -0,0 +1,16 @@ + 'json', + 'reported_at' => 'datetime', + ]; +} diff --git a/database/migrations/2023_04_28_113344_create_device_logs_table.php b/database/migrations/2023_04_28_113344_create_device_logs_table.php new file mode 100644 index 0000000..fd9332c --- /dev/null +++ b/database/migrations/2023_04_28_113344_create_device_logs_table.php @@ -0,0 +1,37 @@ +id(); + $table->unsignedBigInteger('device_id'); + $table->json('data')->nullable(); + $table->timestamp('reported_at'); + $table->timestamps(); + + $table->index(['device_id', 'reported_at']); + $table->index('reported_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('device_logs'); + } +}; From 7b641539cd52f6ab7e6ae85eeb0c8ecbca8ede48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Fri, 28 Apr 2023 17:40:20 +0800 Subject: [PATCH 4/4] =?UTF-8?q?linkos=20=E8=AE=BE=E5=A4=87=E7=A6=BB?= =?UTF-8?q?=E7=BA=BF=E3=80=81=E4=B8=8A=E7=BA=BF=E9=80=9A=E7=9F=A5=E9=80=9A?= =?UTF-8?q?=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Callback/LinkosCallbackController.php | 47 +++++++++++++++++++ app/Http/Middleware/VerifyCsrfToken.php | 2 +- app/Models/DeviceLog.php | 6 +++ routes/web.php | 4 ++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Callback/LinkosCallbackController.php diff --git a/app/Http/Controllers/Callback/LinkosCallbackController.php b/app/Http/Controllers/Callback/LinkosCallbackController.php new file mode 100644 index 0000000..e7f6f4a --- /dev/null +++ b/app/Http/Controllers/Callback/LinkosCallbackController.php @@ -0,0 +1,47 @@ +filled('notify_type')) { + $this->handleDeviceStateNotify($request); + } + + return response()->json(['code' => 0, 'msg' => 'ok']); + } + + /** + * 设备状态通知 + */ + protected function handleDeviceStateNotify(Request $request): void + { + if ($request->notify_type !== 'online_state_change') { + return; + } + + foreach ($request->input('data', []) as $item) { + if (is_null($device = Device::where('sn', $item['device_id'])->first())) { + continue; + } + + if ($request->filled('online_state')) { + $state = match ((int) $request->online_state) { + 0 => Device::STATE_OFFLINE, + 1 => Device::STATE_ONLINE, + default => $device->state, + }; + + $device->forceFill([ + 'state' => $state, + ])->save(); + } + } + } +} diff --git a/app/Http/Middleware/VerifyCsrfToken.php b/app/Http/Middleware/VerifyCsrfToken.php index 9e86521..331eef3 100644 --- a/app/Http/Middleware/VerifyCsrfToken.php +++ b/app/Http/Middleware/VerifyCsrfToken.php @@ -12,6 +12,6 @@ class VerifyCsrfToken extends Middleware * @var array */ protected $except = [ - // + 'callback/*' ]; } diff --git a/app/Models/DeviceLog.php b/app/Models/DeviceLog.php index fe2bfd6..fff5f0d 100644 --- a/app/Models/DeviceLog.php +++ b/app/Models/DeviceLog.php @@ -13,4 +13,10 @@ class DeviceLog extends Model 'data' => 'json', 'reported_at' => 'datetime', ]; + + protected $fillable = [ + 'device_id', + 'data', + 'reported_at', + ]; } diff --git a/routes/web.php b/routes/web.php index b130397..16744b1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -13,6 +13,10 @@ use Illuminate\Support\Facades\Route; | */ +Route::prefix('callback')->group(function () { + Route::post('linkos', \App\Http\Controllers\Callback\LinkosCallbackController::class); +}); + Route::get('/', function () { return view('welcome'); });