From 7f3dd149a9c00cb1f7fc64bed8ac6f8497d2647d Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Fri, 20 Jan 2023 00:06:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=9A=84=20MQTT=20=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Modules/DeviceController.php | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 app/Http/Controllers/Modules/DeviceController.php diff --git a/app/Http/Controllers/Modules/DeviceController.php b/app/Http/Controllers/Modules/DeviceController.php new file mode 100644 index 0000000..1efd0f6 --- /dev/null +++ b/app/Http/Controllers/Modules/DeviceController.php @@ -0,0 +1,71 @@ +pagination([ + 'like_username' => $request->user('module')->id . '.', + ]); + } catch (EmqxSupportException $e) { + Log::error('emqx connect failed.', [$e]); + return $this->error('unable connectto emqx'); + } + + return $this->success($clients); + } + + public function destroy(Request $request, $client_id = null): JsonResponse + { + $request->validate([ + 'client_id' => 'nullable|string|max:255', + 'name' => 'nullable|string|max:255', + ]); + + $emqx = new EmqxSupport(); + + $client_id = $client_id ?? $request->input('client_id'); + + if ($client_id) { + try { + $client = $emqx->client($client_id); + } catch (EmqxSupportException $e) { + return $this->failed('client not found'); + } + + $module_name = explode('.', $client['username'])[0]; + + if ($request->user('module')->id === $module_name) { + $emqx->kickClient($client_id); + + return $this->deleted(); + } else { + return $this->failed('client not found'); + } + } + + if ($request->filled('name')) { + $username = $request->input('name'); + $username = $request->user('module')->id . '.' . $username; + + $this->dispatch(new EMQXKickClientJob(null, $username, false)); + + return $this->deleted(); + } + + return $this->failed('missing client_id or username'); + } +}