diff --git a/app/Http/Controllers/Admin/DeviceController.php b/app/Http/Controllers/Admin/DeviceController.php index 09c68d7..710130c 100644 --- a/app/Http/Controllers/Admin/DeviceController.php +++ b/app/Http/Controllers/Admin/DeviceController.php @@ -4,6 +4,7 @@ use App\Exceptions\EmqxSupportException; use App\Http\Controllers\Controller; +use App\Jobs\Support\EMQXKickClientJob; use App\Support\EmqxSupport; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; @@ -38,16 +39,22 @@ public function index(Request $request): RedirectResponse|View // return view('admin.device.show', compact('client')); // } - - /** - * @throws EmqxSupportException - */ - public function destroy($client_id): RedirectResponse + public function destroy(Request $request): RedirectResponse { $emqx = new EmqxSupport(); - $emqx->kickClient($client_id); + if ($request->filled('client_id')) { + $emqx->kickClient($request->input('client_id')); + } - return back()->with('success', '此客户端已下线。'); + if ($request->filled('username')) { + $username = $request->input('username'); + $module_name = explode('.', $username)[0]; + + $this->dispatch(new EMQXKickClientJob(null, $module_name, false)); + $this->dispatch(new EMQXKickClientJob(null, $module_name . '.', true)); + } + + return back()->with('success', '正在让它们下线。'); } } diff --git a/app/Jobs/Support/EMQXKickClientJob.php b/app/Jobs/Support/EMQXKickClientJob.php new file mode 100644 index 0000000..ac5240a --- /dev/null +++ b/app/Jobs/Support/EMQXKickClientJob.php @@ -0,0 +1,76 @@ +client_id = $client_id; + $this->username = $username; + $this->like_username = $like_username; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle(): void + { + $emqx = new EmqxSupport(); + + if ($this->client_id) { + $emqx->api()->delete('/clients/' . $this->client_id); + } + + if ($this->username) { + $query = 'username'; + if ($this->like_username) { + $query = 'like_username'; + } + + try { + $clients = $emqx->clients([$query => $this->username]); + } catch (EmqxSupportException $e) { + Log::error('emqx connect failed.', [$e]); + return; + } + + if ($clients) { + // 循环翻页 + for ($i = 1; $i <= $clients['meta']['count']; $i++) { + try { + $clients = $emqx->clients([$query => $this->username, 'page' => $i]); + } catch (EmqxSupportException $e) { + Log::error('emqx connect failed.', [$e]); + continue; + } + + foreach ($clients['data'] as $client) { + dispatch(new self($client['clientid'], null)); + } + } + } + } + } +} diff --git a/app/Support/EmqxSupport.php b/app/Support/EmqxSupport.php index f376233..d4e7e8a 100644 --- a/app/Support/EmqxSupport.php +++ b/app/Support/EmqxSupport.php @@ -3,6 +3,7 @@ namespace App\Support; use App\Exceptions\EmqxSupportException; +use App\Jobs\Support\EMQXKickClientJob; use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\PendingRequest; use Illuminate\Support\Facades\Http; @@ -11,9 +12,9 @@ class EmqxSupport { /** - * @throws EmqxSupportException + * 移除客户端 */ - public function kickClient($client_id = null, $username = null): void + public function kickClient($client_id = null, $username = null, bool $like_username = false): void { // 如果都为空,直接返回 if (empty($client_id) && empty($username)) { @@ -25,26 +26,11 @@ public function kickClient($client_id = null, $username = null): void } if ($username) { - try { - $clients = $this->clients(['username' => $username]); - } catch (EmqxSupportException $e) { - throw new EmqxSupportException($e->getMessage()); - } - - 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']); - } - } - } + dispatch(new EMQXKickClientJob(null, $username, $like_username)); } } - private function api(): PendingRequest + public function api(): PendingRequest { return Http::baseUrl(config('emqx.api_url'))->withBasicAuth(config('emqx.api_key'), config('emqx.api_secret')); } diff --git a/resources/views/admin/device/index.blade.php b/resources/views/admin/device/index.blade.php index d37183f..41c34a9 100644 --- a/resources/views/admin/device/index.blade.php +++ b/resources/views/admin/device/index.blade.php @@ -70,15 +70,26 @@ @if ($c['subscriptions_cnt'] > 0) {{ $c['subscriptions_cnt'] }} 个 @else - 没有 + 没有订阅 @endif