From 915682af20cd9a705da419849533799304c94bb4 Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Mon, 28 Nov 2022 22:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=98=BE=E7=A4=BA=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E7=9A=84=E8=AE=BE=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 3 + .../Controllers/Admin/DeviceController.php | 41 +++++++++ app/Support/EmqxSupport.php | 58 +++++++++++++ config/emqx.php | 7 ++ resources/views/admin/device/index.blade.php | 87 +++++++++++++++++++ resources/views/layouts/admin.blade.php | 3 + routes/admin.php | 3 + 7 files changed, 202 insertions(+) create mode 100644 app/Http/Controllers/Admin/DeviceController.php create mode 100644 app/Support/EmqxSupport.php create mode 100644 config/emqx.php create mode 100644 resources/views/admin/device/index.blade.php diff --git a/.env.example b/.env.example index 184ab91..b8bab49 100644 --- a/.env.example +++ b/.env.example @@ -87,3 +87,6 @@ MQTT_KEEP_ALIVE_INTERVAL=60 MQTT_CLEAN_SESSION=true MQTT_AUTO_RECONNECT_ENABLED=false +EMQX_API_URL=http://localhost:18083/api/v5 +EMQX_API_KEY= +EMQX_API_SECERT= diff --git a/app/Http/Controllers/Admin/DeviceController.php b/app/Http/Controllers/Admin/DeviceController.php new file mode 100644 index 0000000..5bfd7fc --- /dev/null +++ b/app/Http/Controllers/Admin/DeviceController.php @@ -0,0 +1,41 @@ +clients($request->all()); + + return view('admin.device.index', compact('clients')); + } + + // + // public function show(Request $request, $client_id) + // { + // $emqx = new EmqxSupport(); + // + // $client = $emqx->clients(['clientid' => $client_id]); + // + // return view('admin.device.show', compact('client')); + // } + + + public function destroy($client_id) + { + $emqx = new EmqxSupport(); + + $emqx->kickClient($client_id); + + return back()->with('success', '此客户端已下线。'); + } +} diff --git a/app/Support/EmqxSupport.php b/app/Support/EmqxSupport.php new file mode 100644 index 0000000..4f40524 --- /dev/null +++ b/app/Support/EmqxSupport.php @@ -0,0 +1,58 @@ +withBasicAuth(config('emqx.api_key'), config('emqx.api_secret')); + } + + public function clients($params = []) + { + // merge params + $params = array_merge([ + 'limit' => 100, + 'isTrusted' => true, + ], $params); + + $response = $this->api()->get('clients', $params); + + if ($response->successful()) { + return $response->json(); + } else { + return false; + } + } + + public function kickClient($client_id = null, $username = null): void + { + // 如果都为空,直接返回 + if (empty($client_id) && empty($username)) { + return; + } + + if ($client_id) { + $this->api()->delete('/clients/' . $client_id); + } + + if ($username) { + $clients = $this->clients(['username' => $username]); + + if ($clients) { + // 循环翻页 + for ($i = 1; $i <= $clients['meta']['count']; $i++) { + $clients = $this->clients(['username' => $username, 'page' => $i]); + + foreach ($clients['data'] as $client) { + $this->api()->delete('/clients/' . $client['clientid']); + } + } + } + } + } +} diff --git a/config/emqx.php b/config/emqx.php new file mode 100644 index 0000000..61656aa --- /dev/null +++ b/config/emqx.php @@ -0,0 +1,7 @@ + env('EMQX_API_URL'), + 'api_key' => env('EMQX_API_KEY'), + 'api_secret' => env('EMQX_API_SECRET'), +]; diff --git a/resources/views/admin/device/index.blade.php b/resources/views/admin/device/index.blade.php new file mode 100644 index 0000000..b895d14 --- /dev/null +++ b/resources/views/admin/device/index.blade.php @@ -0,0 +1,87 @@ +@extends('layouts.admin') + +@section('title', '设备') + +@section('content') +

物联设备

+

这里列出了当前连接到 MQTT 的设备。

+ + + + + + + + + + + + + + + @foreach($clients['data'] as $c) + + + + + + + + + + + @endforeach + +
+ 设备 ID + + 用户 ID + + 节点 + + 协议 + + IP 地址 + + 参数 + + 订阅数 + + 踢出 +
+ {{ $c['clientid'] }} + + {{ $c['username'] }} + + {{ $c['node'] }} + + {{ $c['proto_name'] . ' v' . $c['proto_ver'] }} + + {{ $c['ip_address'] }} + + @if ($c['clean_start']) + 干净启动 + @endif + @if ($c['recv_oct']) +
+ 接收字节: {{ $c['recv_oct'] }} + @endif + @if ($c['send_oct']) +
+ 发送字节: {{ $c['send_oct'] }} + @endif +
+ @if ($c['subscriptions_cnt'] > 0) + {{ $c['subscriptions_cnt'] }} 个 + @else + 没有 + @endif + +
+ @csrf + @method('DELETE') + +
+
+ +@endsection diff --git a/resources/views/layouts/admin.blade.php b/resources/views/layouts/admin.blade.php index ead3bd5..800b9fe 100644 --- a/resources/views/layouts/admin.blade.php +++ b/resources/views/layouts/admin.blade.php @@ -58,6 +58,9 @@ + diff --git a/routes/admin.php b/routes/admin.php index b66c474..faf1497 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -3,6 +3,7 @@ use App\Http\Controllers\Admin\AdminController; use App\Http\Controllers\Admin\ApplicationController; use App\Http\Controllers\Admin\AuthController; +use App\Http\Controllers\Admin\DeviceController; use App\Http\Controllers\Admin\HomeController; use App\Http\Controllers\Admin\HostController; use App\Http\Controllers\Admin\ModuleController; @@ -31,6 +32,8 @@ Route::resource('user-groups', UserGroupController::class); + Route::resource('devices', DeviceController::class)->only(['index', 'destroy']); + Route::view('commands', 'admin.commands')->name('commands');