后台显示连接的设备

This commit is contained in:
iVampireSP.com 2022-11-28 22:54:00 +08:00
parent 0fb78d7f04
commit 915682af20
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
7 changed files with 202 additions and 0 deletions

View File

@ -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=

View File

@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Support\EmqxSupport;
use Illuminate\Http\Request;
class DeviceController extends Controller
{
//
public function index(Request $request)
{
$emqx = new EmqxSupport();
$clients = $emqx->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', '此客户端已下线。');
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace App\Support;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
class EmqxSupport
{
private function api(): PendingRequest
{
return Http::baseUrl(config('emqx.api_url'))->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']);
}
}
}
}
}
}

7
config/emqx.php Normal file
View File

@ -0,0 +1,7 @@
<?php
return [
'api_url' => env('EMQX_API_URL'),
'api_key' => env('EMQX_API_KEY'),
'api_secret' => env('EMQX_API_SECRET'),
];

View File

@ -0,0 +1,87 @@
@extends('layouts.admin')
@section('title', '设备')
@section('content')
<h3>物联设备</h3>
<p>这里列出了当前连接到 MQTT 的设备。</p>
<table class="table table-hover">
<thead>
<th>
设备 ID
</th>
<th>
用户 ID
</th>
<th>
节点
</th>
<th>
协议
</th>
<th>
IP 地址
</th>
<th>
参数
</th>
<th>
订阅数
</th>
<th>
踢出
</th>
</thead>
<tbody>
@foreach($clients['data'] as $c)
<tr>
<td>
{{ $c['clientid'] }}
</td>
<td>
<a href="?username={{ $c['username'] }}">{{ $c['username'] }}</a>
</td>
<td>
{{ $c['node'] }}
</td>
<td>
{{ $c['proto_name'] . ' v' . $c['proto_ver'] }}
</td>
<td>
{{ $c['ip_address'] }}
</td>
<td>
@if ($c['clean_start'])
<span class="badge text-success">干净启动</span>
@endif
@if ($c['recv_oct'])
<br/>
<span class="badge text-success">接收字节: {{ $c['recv_oct'] }}</span>
@endif
@if ($c['send_oct'])
<br/>
<span class="badge text-success">发送字节: {{ $c['send_oct'] }}</span>
@endif
</td>
<td>
@if ($c['subscriptions_cnt'] > 0)
<span class="text-success">{{ $c['subscriptions_cnt'] }} </span>
@else
<span class="text-danger">没有</span>
@endif
</td>
<td>
<form action="{{ route('admin.devices.destroy', $c['clientid']) }}" method="post">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">踢出</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection

View File

@ -58,6 +58,9 @@
<li class="nav-item">
<a class="nav-link" href="{{ route('admin.applications.index') }}">应用程序</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('admin.devices.index') }}">物联设备</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ route('admin.commands') }}">速查表</a>
</li>

View File

@ -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');