心率:优化心率数据的debug界面
authorCaisking <3449750381@qq.com>
Fri, 29 Nov 2024 13:05:52 +0000 (21:05 +0800)
committerCaisking <3449750381@qq.com>
Fri, 29 Nov 2024 13:05:52 +0000 (21:05 +0800)
code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp
code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.h
code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp

index 629ae1d..5a7f324 100644 (file)
@@ -692,4 +692,47 @@ void HealthService::send_blood_save() {
   do_custom_fileoperate_service_check_msg(
       CUSTOM_FILEOPERATE_MSG_TYPE_BLOODXYGEN_SAVE, nullptr, 0);
 }
+
+std::string HealthService::formatTime(uint32_t time) {
+  char labelTime[20];
+  if (uapi_get_TIME_STR(time, labelTime, '.', ':', DATE_Y_M_D_H_M) !=
+      ERRCODE_SUCC) {
+    return "--"; // 默认值
+  } else {
+    return labelTime; // 成功获取时间
+  }
+}
+// 读取心率数据
+std::string HealthService::get_heart_data() {
+  std::string binFilePath = GetBinFilePath(HEALTH_TYPE_HEART);
+  WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "binFilePath %s", binFilePath.c_str());
+  FILE *fp = fopen(binFilePath.c_str(), "rb"); // 以二进制读取模式打开文件
+  if (fp == NULL) {
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+                  "Failed to open binary file for reading");
+    return "Error: Unable to open file \n"; // 返回错误信息
+  }
+
+  std::string result;
+  HeartRateData data;
+
+  result += "file:" + binFilePath + "\n";
+
+  // 读取数据直到文件结束
+  while (fread(&data, sizeof(HeartRateData), 1, fp) == 1) {
+    // 将数据格式化为字符串,拼接到结果中
+    result +=
+        formatTime(data.timestamp) + "," + std::to_string(data.Heart) + "\n";
+  }
+
+  if (ferror(fp)) {
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+                  "Error reading data from binary file");
+    fclose(fp);
+    return "Error: Reading error \n"; // 返回读取错误信息
+  }
+
+  fclose(fp);                                       // 关闭文件
+  return result.empty() ? "No data found \n" : result; // 检查是否读取到数据
+}
 } // namespace OHOS
index 4ab119d..f7df7d8 100644 (file)
@@ -13,6 +13,7 @@
 #include <fstream>
 #include <ctime>
 #include <cstdint>
+#include <string>
 
 #define HEART_MAX_POINTS 360
 #define MSG_SAVE_TEST 0
@@ -100,6 +101,10 @@ namespace OHOS
         void send_heart_save();
         void send_blood_save();
 
+        std::string get_heart_data();
+        std::string get_blood_data();
+        std::string formatTime(uint32_t time);
+
     private:
         static const int MAX_HOURS = 24;
         HeartRateData dailyHeartRates[MAX_HOURS]; // 存储心率值,检测时间戳?使用结构体?
index c6589d2..7fd0c5f 100644 (file)
@@ -578,8 +578,13 @@ namespace OHOS
             << "Time: " << timestr << "\n";
 
         result << "=====================\n";
+
+        // 3.读取文件里面的心率数据
+        auto healthService = HealthService::GetInstance();
+        result << healthService->get_heart_data();
+        result << "=====================\n";
         
-        // 3. 获取一天的心率列表  
+        // 4. 获取一天的心率列表  
         result << "Today's HeartRate list\n";  
         for (int serialIndex = 0; serialIndex < 24; ++serialIndex) {  
             uint32_t heartRate = HealthService::GetInstance()->GetHourlyHeartRate(serialIndex);