|
|
@@ -0,0 +1,33 @@
|
|
|
+import { EventCallback } from '@tauri-apps/api/event'
|
|
|
+import {
|
|
|
+ connect, disconnect, publish, listen, Payload
|
|
|
+} from '@kuyoonjo/tauri-plugin-mqtt'
|
|
|
+import { injectable, Service } from './service'
|
|
|
+
|
|
|
+/**
|
|
|
+ * MQTT服务
|
|
|
+ */
|
|
|
+@injectable
|
|
|
+export default class MqttService extends Service {
|
|
|
+ private mqtt_client_id: string = 'mqtt_contrl_id'
|
|
|
+
|
|
|
+ public mqtt_channel: string = ''
|
|
|
+
|
|
|
+ async connect(url: string, name: string, room: string) {
|
|
|
+ const newUrl = `${url}?username=${room}&password=${name}`
|
|
|
+ return await connect(this.mqtt_client_id, newUrl)
|
|
|
+ }
|
|
|
+
|
|
|
+ async disconnect() {
|
|
|
+ await disconnect(this.mqtt_client_id)
|
|
|
+ }
|
|
|
+
|
|
|
+ async publish(payload: string) {
|
|
|
+ if (!this.mqtt_channel) return
|
|
|
+ await publish(this.mqtt_client_id, this.mqtt_channel, 0, false, payload)
|
|
|
+ }
|
|
|
+
|
|
|
+// (callback: EventCallback<Payload>) {
|
|
|
+// listen(callback)
|
|
|
+// }
|
|
|
+}
|