Quellcode durchsuchen

提交优化之后的mqtt

wangming vor 1 Monat
Ursprung
Commit
ddd113118b

+ 41 - 2
src/components/component/alarModel.vue

@@ -135,7 +135,7 @@ export default {
                 sn,
                 modelId,
             });
-            console.log(snTicket, sn, 99999999);
+            console.log(snTicket, sn, 7777555);
             var that = this;
             wx.requestDeviceVoIP({
                 sn,
@@ -223,7 +223,7 @@ export default {
                 sn,
                 modelId,
             });
-            console.log(snTicket, sn, 99999999);
+            console.log(snTicket, sn, 997777);
             var that = this;
             wx.requestDeviceVoIP({
                 sn,
@@ -597,6 +597,45 @@ export default {
         sendChange() {
             this.$emit("sendChange", this.targetPoints);
         },
+        hanOtherMessage(topic, message) {
+            console.log("接收到组件的消息:", JSON.parse(message.toString()));
+            console.log(JSON.parse(message.toString()), "8870");
+            // 处理点位消息
+            let userId = uni.getStorageSync("userId");
+            const noticeMatch = /^\/mps\/wx_(.+)\/notice$/;
+            const match = topic.match(noticeMatch);
+            if (!match) return;
+            this.alarmModel = true;
+            const data = JSON.parse(message.toString());
+            this.clientId = data.clientId;
+            this.devName = data.devName;
+            this.sn = data.clientId;
+            this.eventListId = data.eventListId;
+            this.getVoipAuthor(this.clientId, userId);
+            console.log(data, topic, "接收到消息222:");
+            console.log(
+                "接收到消息:",
+                this.clientId,
+                this.devName,
+                this.sn,
+                this.eventListId,
+                data
+            );
+            const now = new Date();
+            const year = now.getFullYear();
+            const month = (now.getMonth() + 1).toString().padStart(2, "0");
+            const day = now.getDate().toString().padStart(2, "0");
+            const hours = now.getHours().toString().padStart(2, "0");
+            const minutes = now.getMinutes().toString().padStart(2, "0");
+            const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}`;
+            this.nowTime = formattedTime;
+            console.log("isWmpf:", isWmpf);
+            if (!isWmpf) {
+                console.log("isWmpf:", isWmpf);
+                this.getVoipDevices();
+            }
+            console.log("接收到消息:", topic, data);
+        },
     },
     // mounted() {
     //     this.connectMQTTwo();

+ 7 - 111
src/pagesA/deviceDetail/deviceDetail.vue

@@ -360,7 +360,7 @@
                 @click="breathShow = false"
             ></l-echart>
         </view>
-        <alarModel v-if="showModle" />
+        <alarModel v-if="showModle" ref="alarmModel" />
     </view>
 </template>
 <script>
@@ -857,7 +857,6 @@ export default {
                             });
                         },
                         fail: (err) => {
-                            console.error("保存失败:", err);
                             uni.showToast({
                                 title: "保存失败",
                                 icon: "none",
@@ -866,7 +865,6 @@ export default {
                     });
                 },
                 fail: (err) => {
-                    console.error("导出失败:", err);
                     uni.showToast({
                         title: "导出失败",
                         icon: "none",
@@ -874,99 +872,6 @@ export default {
                 },
             });
         },
-        // 订阅mqtt实时点位
-        connectMqtt() {
-            this.mqttClienTwoFlag = false;
-            if (this.mqttClienTwoFlag) {
-                console.log("主题已订阅");
-                return;
-            }
-            const THRESHOLD = 2;
-            const params = {
-                keepalive: 6000,
-                clean: true,
-                connectTimeout: 30 * 1000,
-                clientId:
-                    "xcx_mqtt_data1" +
-                    this.clientIdProp +
-                    Date.now() +
-                    "_" +
-                    Math.random().toString(16).substring(2, 8),
-                username: "lnradar",
-                password: "lnradar",
-                // 微信小程序特定配置
-                wsOptions: {
-                    WebSocket: function (url) {
-                        return wx.connectSocket({
-                            url: url,
-                            header: {
-                                "content-type": "application/json",
-                            },
-                            protocols: ["mqtt"],
-                        });
-                    },
-                },
-            };
-            let clientTwo = "";
-            let selectedService = uni.getStorageSync("sercviceChoice");
-            if (!selectedService || selectedService == "aloneServe") {
-                if (__wxConfig.envVersion == "develop") {
-                    clientTwo = mqtt.connect(
-                        "wxs://data.radar-power.cn/mqtt/",
-                        params
-                    );
-                }
-                if (__wxConfig.envVersion == "trial") {
-                    clientTwo = mqtt.connect(
-                        "wxs://data.radar-power.cn/mqtt/",
-                        params
-                    );
-                }
-            }
-            clientTwo = mqtt.connect("wxs://data.radar-power.cn/mqtt/", params);
-            console.log("主题开始订阅5555");
-            // 存储client引用以便后续操作
-            this.mqttClientOne = clientTwo;
-            clientTwo.on("connect", () => {
-                console.log("MQTT_DATA连接成功");
-                this.mqttClienTwoFlag = true;
-                if (this.clientIdProp !== null) {
-                    clientTwo.subscribe(
-                        `/dev/${this.clientIdProp}/tracker_targets`,
-                        (err) => {
-                            if (err) {
-                                console.error("订阅clientId失败", err);
-                            } else {
-                                console.log(
-                                    `成功订阅设备主题: /dev/${this.clientIdProp}/dsp_data`
-                                );
-                            }
-                        }
-                    );
-                }
-                console.log(this.clientIdProp);
-            });
-            clientTwo.on("disconnect", () => {
-                console.log("MQTT不在连接");
-            });
-            clientTwo.on("error", (err) => {
-                this.mqttClienTwoFlag = false;
-                setTimeout(() => {
-                    this.connectMqtt();
-                }, 1000);
-            });
-
-            clientTwo.on("reconnect", () => {});
-
-            clientTwo.on("close", () => {});
-
-            clientTwo.on("message", (topic, message) => {
-                // 处理点位消息
-                if (this.clientIdProp !== null) {
-                    this.handleMessage(topic, message, this.clientIdProp);
-                }
-            });
-        },
 
         handleMessage(topic, message, clientId) {
             // 清除不活动定时器
@@ -974,10 +879,6 @@ export default {
             this.inactivityTimer = setTimeout(() => {
                 this.targetPoints = {};
             }, 1500);
-
-            console.log(topic, 99999);
-            console.log(JSON.parse(message.toString()), "99999999");
-
             // 验证topic格式
             const match = topic.match(/^\/dev\/(.+)\/tracker_targets$/);
             if (!match || match[1] !== clientId) return;
@@ -990,16 +891,11 @@ export default {
                         data.health.breath_rpm === 0
                     ) {
                         this.receptHealth(Math.floor(data.health.breath_rpm));
-                        // this.$emit(
-                        //     "sendHealth",
-                        //     Math.floor(data.health.breath_rpm)
-                        // );
                     } else {
-                        // this.$emit("sendHealth", 0);
                     }
                 }
                 this.processTrackerData(data.tracker_targets);
-                console.log(data.tracker_targets, "MQTT消息解析成功22222");
+                // console.log(data.tracker_targets, "MQTT消息解析成功22222");
             } catch (e) {
                 console.error("MQTT消息解析失败", e);
             }
@@ -1053,7 +949,6 @@ export default {
                         (item) => item !== null && item !== undefined
                     );
                 }
-                console.log("当前目标点11111", this.targetPoints);
             }
         },
         createNewTargetPoint(x, y, z, id) {
@@ -1094,7 +989,6 @@ export default {
             if (this.mqttClientOne) {
                 this.mqttClientOne.end();
                 this.mqttClientOne = null;
-                console.log("MQTT连接已断开");
             }
         },
     },
@@ -1102,7 +996,6 @@ export default {
         this.clientIdProp = uni.getStorageSync("clientIDetail");
         this.todayDate = this.$time(new Date(), 2);
         this.showModle = true;
-        console.log(this.showModle, "showModle");
     },
     onLoad(options) {
         const devItem = this.parseDeviceItem(options.devItem);
@@ -1114,13 +1007,16 @@ export default {
             this.autoSwipe();
         }, 3000);
         this.getCurrentDate();
-        // this.connectMqtt();
 
-        console.log(clientId, "clientId88888");
         this.mqttData = createMqttData(clientId);
 
         if (this.mqttData) {
             this.mqttData.on("message", (topic, message) => {
+                console.log("topic", topic, message);
+                const match = topic.match(/^\/dev\/(.+)\/tracker_targets$/);
+                if (!match || match[1] !== clientId) {
+                    this.$refs.alarmModel.hanOtherMessage(topic, message);
+                }
                 this.handleMessage(topic, message, clientId);
             });
         }

+ 1 - 1
src/utils/globalMqtt.js

@@ -121,7 +121,7 @@ export function createMqttData(clientIdProp) {
     });
 
     mqttClientData.on("message", (topic, message) => {
-        console.log("DATA MQTT 消息:", topic, message.toString());
+        // console.log("DATA MQTT 消息:", topic, message.toString());
         // handleDataMessage(topic, message, clientIdProp);
     });