Parcourir la source

fix(device): 优化MQTT消息处理逻辑并添加时间格式化

- 使用可选链操作符处理可能为空的tracker_targets和breath_rpm数据
- 在控制台日志中添加时间戳便于调试
- 修改超时日志描述更准确
liujia il y a 2 mois
Parent
commit
32d58f0
1 fichiers modifiés avec 12 ajouts et 7 suppressions
  1. 12 7
      src/views/device/detail/index.vue

+ 12 - 7
src/views/device/detail/index.vue

@@ -175,6 +175,7 @@ import { deviceOnlineStateMap, deviceInstallPositionNameMap } from '@/const/devi
 import deviceConfigDrawer from './components/deviceConfig/index.vue'
 import deviceStatsDrawer from './components/deviceStatsDrawer/index.vue'
 import BreathLineChart from './components/breathLineChart/index.vue'
+import { formatDateTime } from '@/utils'
 
 defineOptions({
   name: 'DeviceDetail',
@@ -345,7 +346,7 @@ const resetMqttTimeout = () => {
   mqttTimeout = window.setTimeout(() => {
     Object.keys(targets).forEach((key) => delete targets[Number(key)])
     breathRpmList.value = []
-    console.log('MQTT超时未收到新消息,隐藏所有点')
+    console.log('MQTT超时未收到新消息,隐藏所有目标点')
   }, MQTT_TIMEOUT_MS)
 }
 
@@ -390,13 +391,17 @@ onMounted(() => {
     if (msgDevId !== clientId.value) return // 只处理当前设备
     try {
       const data = JSON.parse(message.toString())
-      const arr = data.tracker_targets
-      console.log('🚀 收到MQTT消息', data, {
-        '🔴 目标人数': data.tracker_targets.length,
-        '🟢 呼吸率': data.health.breath_rpm,
-        '🟡 点位图': data.tracker_targets,
+      const arr = data?.tracker_targets // 点位图数据
+      const breathRpm = data.health?.breath_rpm || 0 // 呼吸率数据
+      if (breathRpm) {
+        breathRpmList.value.push(Math.floor(breathRpm || 0))
+      }
+      console.log(`🚀 收到MQTT消息 ${formatDateTime(new Date())}`, {
+        '🔴 目标人数': arr.length,
+        '🟢 呼吸率': breathRpm,
+        '🟡 点位图': arr,
+        '🔵 接口数据': data,
       })
-      breathRpmList.value.push(Math.floor(data.health.breath_rpm || 0))
       if (Array.isArray(arr) && arr.length > 0 && Array.isArray(arr[0])) {
         // 记录本次出现的所有id
         const currentIds = new Set<number>()