From b7705ae680f3056460d42f33909f3cbdfec2e575 Mon Sep 17 00:00:00 2001 From: lld <15027638633@163.com> Date: Sat, 7 Mar 2026 23:56:37 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=E5=A4=A7=E6=A3=9A=E6=B8=A9?= =?UTF-8?q?=E5=BA=A6=E6=94=B9=E4=B8=BA=E5=AE=9E=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index.vue | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/pages/index.vue b/pages/index.vue index 2a6a431..9cd690f 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -174,6 +174,7 @@ export default { return false*/ }, onShow() { + console.info("首页在线状态注册回调") mqttUtil.setOnMessageCallback(this.ackMessage); }, onLoad() { @@ -181,6 +182,7 @@ export default { this.refresh(); }, onHide() { + console.info("首页在线状态注销回调") mqttUtil.removeOnMessageCallback(); }, onReady() { @@ -369,8 +371,10 @@ export default { ) }, ackMessage(topic, payload) { - const regex2 = /^device\/\d+\/status$/; - if (!regex2.test(topic)) return; + const regex = /^device\/\d+\/status$/; + const regex1 = /^frontend\/.+\/dtu\/\d+\/listener$/; + + if (!regex.test(topic) && !regex1.test(topic)) return; let msgData = {}; // 优化:捕获JSON解析异常 @@ -381,9 +385,15 @@ export default { return; } // 设备在线状态 - if (msgData.online && "time" in msgData && "imei" in msgData) { + if (regex.test(topic) && msgData.online && "time" in msgData && "imei" in msgData) { this.updateDeviceStatusFast(msgData.imei, msgData.online) } + // 温度 + const allKeysNumeric2 = Object.keys(msgData).every(key => /^\d+$/.test(key)); + if (regex1.test(topic) && Object.keys(msgData).length > 0 && allKeysNumeric2) { + this.updateTempFast(msgData) + } + }, updateDeviceStatusFast(targetImei, isOnline) { // 直接从Map里取设备项,无需遍历数组 @@ -394,6 +404,22 @@ export default { // 直接修改对象属性,响应式正常生效 targetDevice.deviceStatus = statusText; targetDevice.online = statusText; + }, + + updateTempFast: function (msgData) { + const regexWithGroup = /^frontend\/(.+)\/dtu\/(\d+)\/listener$/; + const matchResult = regexWithGroup.match(msgData); + const imei = matchResult[2]; // 提取结果:'1234567890' + // 直接从Map里取设备项,无需遍历数组 + const targetDevice = this.imeiToDeviceMap.get(imei); + if (!targetDevice) return; + const div10 = (v) => (v == null ? null : Math.round((Number(v) / 10) * 10) / 10); + // 直接修改对象属性,响应式正常生效 + + targetDevice.temp1 = msgData['201']; + targetDevice.temp2 = msgData['202']; + targetDevice.temp3 = msgData['203']; + targetDevice.temp4 = msgData['204']; } } }