فهرست منبع

提交设备详情和mqtt代码优化

wangming 1 ماه پیش
والد
کامیت
4c9466004f
2فایلهای تغییر یافته به همراه35 افزوده شده و 34 حذف شده
  1. 30 30
      src/pagesA/deviceDetail/deviceDetail.vue
  2. 5 4
      src/utils/globalMqtt.js

+ 30 - 30
src/pagesA/deviceDetail/deviceDetail.vue

@@ -465,12 +465,7 @@ export default {
                 animationDuration: 100,
             },
             index: 0,
-            loopTimer: null,
-            mqttClienTwoFlag: false,
-            mqttClientOne: false,
-            mqttData: null,
             showModle: false,
-            mqttMessageBound: false,
         };
     },
     computed: {},
@@ -991,34 +986,39 @@ export default {
         console.log(this.clientId, "clientIDetail");
         this.clientId = clientId;
         const bindMqtt = (mqttClientData) => {
-            mqttClientData.subscribe(
-                `/dev/${this.clientId}/tracker_targets`,
-                (err) => {
-                    if (err) console.error("DATA MQTT 订阅失败", err);
-                    else
-                        console.log(
-                            `DATA MQTT 订阅成功: /dev/${this.clientId}/tracker_targets`
-                        );
+            if (!mqttClientData) {
+                console.error("MQTT 客户端未初始化");
+                return;
+            }
+
+            // 进入设备详情,先取消之前的订阅,避免重复
+            const topic = `/dev/${this.clientId}/tracker_targets`;
+            mqttClientData.unsubscribe(topic, (err) => {
+                if (err) {
+                    console.warn("取消订阅失败,可能之前没订阅过", err);
                 }
-            );
-            if (!this.mqttMessageBound) {
-                mqttClientData.on("message", (topic, message) => {
-                    console.log("topicDeviceDetail", topic, message);
-                    const dataMatch = topic.match(
-                        /^\/dev\/(.+)\/tracker_targets$/
-                    );
-                    const cmdMatch = topic.match(/^\/mps\/wx_(.+)\/notice$/);
-                    if (dataMatch && dataMatch[1] === this.clientId) {
-                        this.handleMessage(topic, message, this.clientId);
-                    } else if (cmdMatch) {
-                        this.$refs.alarmModel?.hanOtherMessage?.(
-                            topic,
-                            message
-                        );
+                // 再订阅一次,确保立即生效
+                mqttClientData.subscribe(topic, (err) => {
+                    if (err) {
+                        console.error("DATA MQTT 订阅失败", err);
+                    } else {
+                        console.log(`DATA MQTT 订阅成功: ${topic}`);
                     }
                 });
-                this.mqttMessageBound = true;
-            }
+            });
+            mqttClientData.removeAllListeners("message");
+            mqttClientData.on("message", (topic, message) => {
+                console.log("打印的点位", message.toString());
+
+                const dataMatch = topic.match(/^\/dev\/(.+)\/tracker_targets$/);
+                const cmdMatch = topic.match(/^\/mps\/wx_(.+)\/notice$/);
+
+                if (dataMatch && dataMatch[1] === this.clientId) {
+                    this.handleMessage(topic, message, this.clientId);
+                } else if (cmdMatch) {
+                    this.$refs.alarmModel.hanOtherMessage(topic, message);
+                }
+            });
         };
 
         if (this.$mqttClientData) {

+ 5 - 4
src/utils/globalMqtt.js

@@ -14,8 +14,8 @@ export function createMqttClient() {
     }
     const params = {
         // keepalive: 60,
-        clean: true,
-        connectTimeout: 30 * 1000,
+        // clean: false,
+        // connectTimeout: 30 * 1000,
         clientId:
             "xcx_mqtt_cmd1" +
             uni.getStorageSync("userId") +
@@ -84,8 +84,8 @@ export function createMqttData() {
 
     const params = {
         // keepalive: 60,
-        clean: true,
-        connectTimeout: 1500,
+        // clean: false,
+        // connectTimeout: 1500,
         clientId: "xcx_mqtt_data1_" + uni.getStorageSync("userId") + "_" + Date.now(),
         username: "lnradar",
         password: "lnradar",
@@ -106,6 +106,7 @@ export function createMqttData() {
     mqttClientData.on("connect", () => {
         console.log("DATA MQTT 连接成功");
         mqttDataConnected = true;
+        // uni.$emit("mqttData-ready", mqttClientData)
     });
 
     mqttClientData.on("message", (topic, message) => {