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']; } } }