同步消息接收过滤机制0.5秒为限制条件 1151/1 1151/head 1153/target/1
authorSnickerSun <snickersun@163.com>
Thu, 10 Oct 2024 23:05:00 +0000 (07:05 +0800)
committerSnickerSun <snickersun@163.com>
Thu, 10 Oct 2024 23:05:00 +0000 (07:05 +0800)
code/sdk/application/wearable/fd230app/nativeui/message/src/MessageModel.cpp

index b70e889..89281d1 100644 (file)
@@ -110,7 +110,7 @@ void MessageUnsubscribeTopic(Topic topic) {
 }
 
 // 存储消息更新的定时器
-static std::chrono::milliseconds updateDelay(1000); // 设置更新延迟,1s
+static std::chrono::milliseconds updateDelay(500); // 设置更新延迟,0.5s
 static std::chrono::steady_clock::time_point lastUpdateTime;
 static bool shouldDisplayMessage = false;
 static ancs_data_info_t *lastMessageInfo = NULL;
@@ -120,75 +120,90 @@ static void Notify(void *args, const Topic topic, Request *request) {
   UNUSED(args);
   UNUSED(topic);
 
-  if (request == NULL) {
-    return;
-  }
-
-  WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "request %d\r\n", request->msgId);
-
-  if (request->msgId == TOPIC_EVENT_MESSAGE_UPDATA) {
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TOPIC_EVENT_MESSAGE_UPDATA");
-
-    // 创建新的消息信息
-    ancs_data_info_t *messageInfo = new ancs_data_info_t();
-    if (messageInfo == NULL) {
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "new ancs_data_info_t fail");
-      return;
-    }
-
-    // 复制请求数据到新的消息信息
-    int rets = memcpy_s(messageInfo, sizeof(ancs_data_info_t),
-                        (ancs_data_info_t *)request->data, request->len);
-    if (rets != EOK) {
-      delete messageInfo;
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-                    "memcpy_s ancs_data_info_t fail ret = %d", rets);
-      return;
-    }
-
-    // 更新到模型里面的数组
-    MessageModel::GetInstance().AddMessageData(messageInfo);
-
-    // 加锁以保护共享变量
-    std::lock_guard<std::mutex> lock(messageMutex);
-
-    // 更新最后一条消息信息
-    if (lastMessageInfo != NULL) {
-      delete lastMessageInfo; // 释放之前保存的消息信息
-    }
-    lastMessageInfo = messageInfo; // 保存新的消息信息
-    shouldDisplayMessage = true;   // 标记需要显示新消息
-
-    // 设置最后更新时间
-    lastUpdateTime = std::chrono::steady_clock::now();
-  }
-
-  // 检查是否需要显示新消息
-  {
-    std::lock_guard<std::mutex> lock(messageMutex); // 加锁以保护共享变量
-
-    if (shouldDisplayMessage) {
-      // 检查是否达到延迟时间
-      auto now = std::chrono::steady_clock::now();
-      if (now - lastUpdateTime >= updateDelay) {
-        // 发送信息进行弹窗
-        if (uapi_dev_runtime_get_train_status() ||
-            uapi_dev_runtime_get_saveele_status() ||
-            uapi_dev_runtime_get_respiratory_training_status()) {
-          // 增加训练模式下,不弹窗
-          // 省电模式,不弹窗
-          // 呼吸训练,不弹窗
-          return;
-        }
-
-        // 发送弹窗通知 会自动查询到数组的最新一个
-        CustomEventPublish(TOPIC_EVENT_MESSAGENOTE, NULL, 0);
-
-        // 发送完毕,重置标志
-        shouldDisplayMessage = false; // 重置状态
-      }
-    }
-  }
+  if (request == NULL) {  
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Received NULL request");  
+    return;  
+  }  
+
+  WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Received request %d", request->msgId);  
+
+  if (request->msgId == TOPIC_EVENT_MESSAGE_UPDATA) {  
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,  
+                  "Processing TOPIC_EVENT_MESSAGE_UPDATA");  
+
+    // 创建新的消息信息  
+    ancs_data_info_t *messageInfo = new ancs_data_info_t();  
+    if (messageInfo == NULL) {  
+      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,  
+                    "Failed to allocate memory for ancs_data_info_t");  
+      return;  
+    }  
+
+    // 复制请求数据到新的消息信息  
+    int rets = memcpy_s(messageInfo, sizeof(ancs_data_info_t),  
+                        (ancs_data_info_t *)request->data, request->len);  
+    if (rets != EOK) {  
+      delete messageInfo;  
+      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,  
+                    "memcpy_s ancs_data_info_t failed, ret = %d", rets);  
+      return;  
+    }  
+
+    // 更新到模型里面的数组  
+    MessageModel::GetInstance().AddMessageData(messageInfo);  
+    
+    // 加锁以保护共享变量  
+    {  
+      std::lock_guard<std::mutex> lock(messageMutex);  
+      shouldDisplayMessage = true; // 标记需要显示新消息  
+
+      // 设置最后更新时间  
+      lastUpdateTime = std::chrono::steady_clock::now();  
+    }  
+    
+    // 直接尝试显示消息(用于单条消息的情况)  
+    {  
+      std::lock_guard<std::mutex> lock(messageMutex); // 加锁以保护共享变量  
+
+      // 检查是否达到延迟时间  
+      auto now = std::chrono::steady_clock::now();  
+      auto timeDiff = now - lastUpdateTime;  
+      WEARABLE_LOGD(  
+          WEARABLE_LOG_MODULE_APP, "Time since last update: %lld milliseconds",  
+          std::chrono::duration_cast<std::chrono::milliseconds>(timeDiff)  
+              .count());  
+
+      if (timeDiff >= updateDelay) {  
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,  
+                      "Displaying new message after time delay");  
+
+        // 发送信息进行弹窗  
+        if (uapi_dev_runtime_get_train_status() ||  
+            uapi_dev_runtime_get_saveele_status() ||  
+            uapi_dev_runtime_get_respiratory_training_status()) {  
+          // 增加训练模式下,不弹窗  
+          // 省电模式,不弹窗  
+          // 呼吸训练,不弹窗  
+          return;  
+        }  
+
+        // 发送弹窗通知 会自动查询到数组的最新一个  
+        CustomEventPublish(TOPIC_EVENT_MESSAGENOTE, NULL, 0);  
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Message popup sent");  
+
+        // 发送完毕,重置标志  
+        shouldDisplayMessage = false; // 重置状态  
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,  
+                      "Reset shouldDisplayMessage to false");  
+      } else {  
+        WEARABLE_LOGE( WEARABLE_LOG_MODULE_APP, "Message display delayed, waiting for next opportunity %lld ms %lld ms",   
+                       std::chrono::duration_cast<std::chrono::milliseconds>(timeDiff).count(),   
+                       std::chrono::duration_cast<std::chrono::milliseconds>(updateDelay).count());  
+      }  
+    }  
+  } else {  
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "No new message to display");  
+  }  
 }
 
 void saveancsdata() {