|
@@ -1,31 +1,88 @@
|
|
|
-import { connect, disconnect, listen, publish } from '@kuyoonjo/tauri-plugin-mqtt'
|
|
|
|
|
|
|
+import {
|
|
|
|
|
+ connect, disconnect, listen, publish
|
|
|
|
|
+} from '@kuyoonjo/tauri-plugin-mqtt'
|
|
|
|
|
+import { Buffer } from 'buffer'
|
|
|
import { injectable, Service } from './service'
|
|
import { injectable, Service } from './service'
|
|
|
-import useStore from '@/store/index'
|
|
|
|
|
|
|
+import useStore from '@/store'
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* MQTT服务
|
|
* MQTT服务
|
|
|
*/
|
|
*/
|
|
|
@injectable
|
|
@injectable
|
|
|
export default class MqttService extends Service {
|
|
export default class MqttService extends Service {
|
|
|
|
|
+ private client_id = 'contrller'
|
|
|
|
|
+
|
|
|
|
|
+ private client_channel = ''
|
|
|
|
|
+
|
|
|
private store = useStore()
|
|
private store = useStore()
|
|
|
|
|
|
|
|
- async connect(url: string, name: string, room: string) {
|
|
|
|
|
|
|
+ constructor() {
|
|
|
|
|
+ super()
|
|
|
|
|
+ this.onListen()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 链接mqtt
|
|
|
|
|
+ * @param url string
|
|
|
|
|
+ * @param name string
|
|
|
|
|
+ * @param room string
|
|
|
|
|
+ * @returns
|
|
|
|
|
+ */
|
|
|
|
|
+ async connect(url: string, room: string, name: string) {
|
|
|
|
|
+ if (!url || !name || !room) return null
|
|
|
const mqttUrl = url.replace(/(\/\/)/, `$1${room}:${name}@`)
|
|
const mqttUrl = url.replace(/(\/\/)/, `$1${room}:${name}@`)
|
|
|
- await connect(this.store.mqtt_client_id, mqttUrl)
|
|
|
|
|
|
|
+ return await connect(this.client_id, mqttUrl, { skipVerification: true })
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async send(params: string) {
|
|
|
|
|
- await publish(this.store.mqtt_client_id, this.store.mqtt_channel, 1, false, params)
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送消息
|
|
|
|
|
+ * @param params string
|
|
|
|
|
+ * @param qos number 0 | 1 | 2
|
|
|
|
|
+ * @returns
|
|
|
|
|
+ */
|
|
|
|
|
+ async send(params: string, qos = 0) {
|
|
|
|
|
+ if (!this.client_channel || !this.client_id) return null
|
|
|
|
|
+ return await publish(this.client_id, this.client_channel, qos, false, params)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 断开连接
|
|
|
|
|
+ */
|
|
|
async disconnect() {
|
|
async disconnect() {
|
|
|
- await disconnect(this.store.mqtt_client_id)
|
|
|
|
|
|
|
+ this.client_channel = ''
|
|
|
|
|
+ await disconnect(this.client_id)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- async listen(callback: (data: any) => void) {
|
|
|
|
|
- await listen(client => {
|
|
|
|
|
- // if(client.payload)
|
|
|
|
|
- // 如果断开连接,如果有订阅需要取消订阅,然后重新连接
|
|
|
|
|
- callback(client.payload)
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 监听mqtt消息
|
|
|
|
|
+ */
|
|
|
|
|
+ private onListen() {
|
|
|
|
|
+ listen((client) => {
|
|
|
|
|
+ const { event } = client.payload
|
|
|
|
|
+ // 连接成功
|
|
|
|
|
+ if (event.connect) {
|
|
|
|
|
+ console.log('已连接服务器')
|
|
|
|
|
+ this.store.setMqttMessage({ type: 'connect' })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 断开连接
|
|
|
|
|
+ if (event.disconnect) {
|
|
|
|
|
+ console.log('已断开服务器,插件未自动断开已提is')
|
|
|
|
|
+ this.store.setMqttMessage({ type: 'disconnect' })
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 收到消息
|
|
|
|
|
+ if (event.message) {
|
|
|
|
|
+ console.log('收到消息', event.message)
|
|
|
|
|
+ try {
|
|
|
|
|
+ const { payload, topic } = event.message
|
|
|
|
|
+ this.client_channel = topic
|
|
|
|
|
+ const data = JSON.parse(Buffer.from(payload).toString())
|
|
|
|
|
+ this.store.setMqttMessage({ ...data })
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ this.throw('mqtt消息解析失败')
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|