forked from drone/command-center-dashboard

罗广辉
2025-04-15 2abd8e08f2148331e14bf1332e05f5d6302f82de
src/hooks/controlDrone/useMqtt.js
@@ -6,23 +6,18 @@
export function useMqtt (deviceTopicInfo) {
export function useMqtt (mqttState,deviceTopicInfo) {
  let cacheSubscribeArr= []
  const store = useStore()
  const mqttState = computed(() => {
    return store.state.common.mqttState
  })
  function publishMqtt (topic, body, ots) {
    mqttState.value?.publishMqtt(topic, JSON.stringify(body), ots)
    mqttState?.publishMqtt(topic, JSON.stringify(body), ots)
  }
  function subscribeMqtt (topic, handleMessageMqtt) {
    mqttState.value?.subscribeMqtt(topic)
    mqttState?.subscribeMqtt(topic)
    const handler = handleMessageMqtt || onMessageMqtt
    mqttState.value?.on('onMessageMqtt', handler)
    mqttState?.on('onMessageMqtt', handler)
    cacheSubscribeArr.push({
      topic,
      callback: handler,
@@ -52,8 +47,8 @@
  function unsubscribeDrc () {
    // 销毁已订阅事件
    cacheSubscribeArr.forEach(item => {
      mqttState.value?.off('onMessageMqtt', item.callback)
      mqttState.value?.unsubscribeMqtt(item.topic)
      mqttState?.off('onMessageMqtt', item.callback)
      mqttState?.unsubscribeMqtt(item.topic)
    })
    cacheSubscribeArr = []
  }
@@ -87,7 +82,7 @@
      },
    }
    const pingInterval = setInterval(() => {
      if (!mqttState.value) return
      if (!mqttState) return
      heartBeatSeq.value += 1
      body.data.ts = new Date().getTime()
      body.data.seq = heartBeatSeq.value