فهرست منبع

提交两个mqtt

wangming 1 ماه پیش
والد
کامیت
8d39a3a3e0
5فایلهای تغییر یافته به همراه251 افزوده شده و 51 حذف شده
  1. 0 7
      src/App.vue
  2. 16 30
      src/components/component/alarModel.vue
  3. 2 2
      src/components/js_sdk/index.js
  4. 1 0
      src/pages/home/home.vue
  5. 232 12
      src/pagesA/deviceDetail/deviceDetail.vue

+ 0 - 7
src/App.vue

@@ -1,10 +1,3 @@
-<template>
-    <view>
-        <slot></slot>
-        <alarModel />
-    </view>
-</template>
-
 <script>
 import Vue from "vue";
 

+ 16 - 30
src/components/component/alarModel.vue

@@ -417,20 +417,6 @@ export default {
         },
         // mqtt相关方法
         connectMQTTwo() {
-            // if (this.mqttClientTwo && this.mqttClientTwo.connected) {
-            //     console.log("MQTT连接已存在,无需重新创建", this.mqttClientTwo);
-            //     return;
-            // }
-            // if (this.mqttClientTwo) {
-            //     try {
-            //         this.mqttClientTwo.end();
-            //         this.mqttClientTwo = null;
-            //         console.log("清理旧MQTT连接");
-            //     } catch (e) {
-            //         console.error("清理连接时出错:", e);
-            //     }
-            // }
-
             if (this.mqttClientFlag) {
                 console.log("主题已订阅");
                 return;
@@ -464,7 +450,7 @@ export default {
             if (!selectedService || selectedService == "aloneServe") {
                 if (__wxConfig.envVersion == "develop") {
                     client = mqtt.connect(
-                        "wxs://api.radar-power.asia:8084/mqtt",
+                        "wxs://cmd.radar-power.com:8084/mqtt",
                         params
                     );
                     // client = mqtt.connect(
@@ -474,7 +460,7 @@ export default {
                 }
                 if (__wxConfig.envVersion == "trial") {
                     client = mqtt.connect(
-                        "wxs://radar-power.cn:8084/mqtt",
+                        "wxs://cmd.radar-power.com:8084/mqtt",
                         params
                     );
                 }
@@ -495,20 +481,20 @@ export default {
                     }
                 });
                 console.log(this.clientIdProp);
-                if (this.clientIdProp !== null) {
-                    client.subscribe(
-                        `/dev/${this.clientIdProp}/dsp_data`,
-                        (err) => {
-                            if (err) {
-                                console.error("订阅clientId失败", err);
-                            } else {
-                                console.log(
-                                    `成功订阅设备主题: /dev/${this.clientIdProp}/dsp_data`
-                                );
-                            }
-                        }
-                    );
-                }
+                // if (this.clientIdProp !== null) {
+                //     client.subscribe(
+                //         `/dev/${this.clientIdProp}/dsp_data`,
+                //         (err) => {
+                //             if (err) {
+                //                 console.error("订阅clientId失败", err);
+                //             } else {
+                //                 console.log(
+                //                     `成功订阅设备主题: /dev/${this.clientIdProp}/dsp_data`
+                //                 );
+                //             }
+                //         }
+                //     );
+                // }
             });
             client.on("disconnect", () => {
                 console.log("MQTT不在连接");

+ 2 - 2
src/components/js_sdk/index.js

@@ -5,9 +5,9 @@ http.setConfig(config => {
     if (__wxConfig.envVersion == 'develop') {
         let selectedService = uni.getStorageSync("sercviceChoice")
         if (!selectedService || selectedService == 'aloneServe') {
-            // config.baseUrl = "https://api.radar-power.asia:4443/portal-service-server/";
+            config.baseUrl = "https://api.radar-power.asia:4443/portal-service-server/";
         }
-        config.baseUrl = "https://radar-power.cn/portal-service-server/";
+        // config.baseUrl = "https://radar-power.cn/portal-service-server/";
     }
     if (__wxConfig.envVersion == 'trial') {
         let selectedService = uni.getStorageSync("sercviceChoice")

+ 1 - 0
src/pages/home/home.vue

@@ -132,6 +132,7 @@
             <image src="../../static/arrFour.png" class="warmImage"></image>
         </view>
         <tenMinutes v-if="isIniTenMinutes" />
+        <alarModel v-if="isInitAlarm" />
     </view>
 </template>
 <script>

+ 232 - 12
src/pagesA/deviceDetail/deviceDetail.vue

@@ -361,17 +361,25 @@
             ></l-echart>
         </view>
 
-        <alarModel
+        <!-- <alarModel
             v-if="isInitAlarm"
             :clientIdProp="clientIdProp"
             @sendChange="receptionChange"
             @sendHealth="receptHealth"
+        /> -->
+
+        <alarModel
+            v-if="isInitAlarm"
+            @sendChange="receptionChange"
+            @sendHealth="receptHealth"
         />
     </view>
 </template>
 <script>
 import * as echarts from "../../uni_modules/lime-echart/static/echarts.min";
 
+import mqtt from "../../utils/mqtt";
+
 export default {
     data() {
         return {
@@ -473,6 +481,8 @@ export default {
             },
             index: 0,
             loopTimer: null,
+            mqttClienTwoFlag: false,
+            mqttClientOne: false,
         };
     },
     computed: {},
@@ -735,13 +745,7 @@ export default {
             this.breathRate = val;
             this.setEcharts(val);
         },
-        getToday() {
-            const today = new Date();
-            const year = today.getFullYear();
-            const month = today.getMonth() + 1;
-            const day = today.getDate();
-            this.todayDate = `${year}年${month}月${day}日`;
-        },
+
         // echarts图表模块
 
         getOption(list) {
@@ -836,9 +840,6 @@ export default {
                 this.breathRpmList = [];
             }
             this.breathRpmList.push(val);
-            // if (this.breathRpmList.length > 60) {
-            //     this.breathRpmList = this.breathRpmList.shift();
-            // }
             if (!this.chartInstance) return;
             const option = this.getOption(this.breathRpmList);
             this.chartInstance.setOption(option, { notMerge: true });
@@ -877,12 +878,229 @@ export default {
                 },
             });
         },
+        // 订阅mqtt实时点位
+        connectMqtt() {
+            if (this.mqttClienTwoFlag) {
+                console.log("主题已订阅");
+                return;
+            }
+            const THRESHOLD = 2;
+            const params = {
+                keepalive: 6000,
+                clean: true,
+                connectTimeout: 30 * 1000,
+                clientId:
+                    "wx_mqtt_" + Math.random().toString(16).substring(2, 8),
+                username: "admin",
+                password: "public",
+                // 微信小程序特定配置
+                wsOptions: {
+                    WebSocket: function (url) {
+                        return wx.connectSocket({
+                            url: url,
+                            header: {
+                                "content-type": "application/json",
+                            },
+                            protocols: ["mqtt"],
+                        });
+                    },
+                },
+                reconnectPeriod: 0,
+                rejectUnauthorized: false,
+            };
+            let clientTwo = "";
+            let selectedService = uni.getStorageSync("sercviceChoice");
+            if (!selectedService || selectedService == "aloneServe") {
+                if (__wxConfig.envVersion == "develop") {
+                    clientTwo = mqtt.connect(
+                        "wxs://data.radar-power.com:8084/mqtt",
+                        params
+                    );
+                }
+                if (__wxConfig.envVersion == "trial") {
+                    clientTwo = mqtt.connect(
+                        "wxs://data.radar-power.com:8084/mqtt",
+                        params
+                    );
+                }
+            }
+            // 存储client引用以便后续操作
+            this.mqttClientOne = clientTwo;
+            clientTwo.on("connect", () => {
+                console.log("MQTT连接成功");
+                this.mqttClienTwoFlag = true;
+                let userId = uni.getStorageSync("userId");
+                if (this.clientIdProp !== null) {
+                    clientTwo.subscribe(
+                        `/dev/${this.clientIdProp}/dsp_data`,
+                        (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) => {
+                console.log(JSON.parse(message.toString()), "8870");
+                // 处理点位消息
+                if (this.clientIdProp !== null) {
+                    this.handleMessage(topic, message, this.clientIdProp);
+                }
+            });
+        },
+
+        handleMessage(topic, message, clientId) {
+            // 清除不活动定时器
+            clearTimeout(this.inactivityTimer);
+            this.inactivityTimer = setTimeout(() => {
+                this.targetPoints = {};
+            }, 1500);
+
+            // 验证topic格式
+            const match = topic.match(/^\/dev\/(.+)\/dsp_data$/);
+            if (!match || match[1] !== clientId) return;
+
+            try {
+                const data = JSON.parse(message.toString());
+                if (data.health) {
+                    if (
+                        data.health.breath_rpm ||
+                        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);
+            } catch (e) {
+                console.error("MQTT消息解析失败", e);
+            }
+        },
+
+        processTrackerData(arr) {
+            if (Array.isArray(arr) && arr.length > 0 && Array.isArray(arr[0])) {
+                this.targetPoints = {};
+                const currentIds = new Set();
+                const newTargetPoints = {};
+                // 处理每个追踪目标
+                arr.forEach((item) => {
+                    if (!Array.isArray(item) || item.length < 4) return;
+
+                    const [x, y, z, id] = item;
+                    currentIds.add(id.toString());
+
+                    // 处理新点或更新现有点
+                    if (!this.targetPoints[id]) {
+                        newTargetPoints[id] = this.createNewTargetPoint(
+                            x,
+                            y,
+                            z,
+                            id
+                        );
+                    } else {
+                        newTargetPoints[id] = this.updateExistingTargetPoint(
+                            this.targetPoints[id],
+                            x,
+                            y,
+                            z,
+                            2
+                        );
+                    }
+                });
+
+                // 移除不存在的点
+                Object.keys(this.targetPoints).forEach((id) => {
+                    if (!currentIds.has(id)) {
+                        delete this.targetPoints[id];
+                    }
+                });
+
+                // 更新目标点
+                this.targetPoints = {
+                    ...this.targetPoints,
+                    ...newTargetPoints,
+                };
+                if (Array.isArray(this.targetPoints)) {
+                    this.targetPoints = this.targetPoints.filter(
+                        (item) => item !== null && item !== undefined
+                    );
+                }
+                console.log("当前目标点11111", this.targetPoints);
+            }
+        },
+        createNewTargetPoint(x, y, z, id) {
+            return {
+                x,
+                y,
+                z,
+                id,
+                displayX: x,
+                displayY: y,
+                lastX: x,
+                lastY: y,
+            };
+        },
+        updateExistingTargetPoint(existingPoint, x, y, z, THRESHOLD) {
+            const dx = x - existingPoint.lastX;
+            const dy = y - existingPoint.lastY;
+            const distance = Math.sqrt(dx * dx + dy * dy);
+
+            if (distance > THRESHOLD) {
+                return {
+                    ...existingPoint,
+                    x,
+                    y,
+                    z,
+                    lastX: x,
+                    lastY: y,
+                    displayX: x,
+                    displayY: y,
+                };
+            }
+
+            return existingPoint;
+        },
+
+        closemqtTwo() {
+            this.mqttClienTwoFlag = false;
+            if (this.mqttClientOne) {
+                this.mqttClientOne.end();
+                this.mqttClientOne = null;
+                console.log("MQTT连接已断开");
+            }
+        },
     },
 
     onShow() {
         this.clientIdProp = uni.getStorageSync("clientIDetail");
         this.isInitAlarm = true;
-        this.getToday();
+        this.todayDate = this.$time(new Date(), 2);
+        this.connectMqtt();
     },
     onLoad(options) {
         const devItem = this.parseDeviceItem(options.devItem);
@@ -899,11 +1117,13 @@ export default {
         this.isInitAlarm = false;
         this.inactivityTimer = null;
         this.autoPlayinterval = null;
+        this.closemqtTwo();
     },
     onHide() {
         this.inactivityTimer = null;
         this.autoPlayinterval = null;
         this.isInitAlarm = false;
+        this.closemqtTwo();
     },
 };
 </script>