增加测量结果数据处理
authorxue <2144439972@qq.com>
Mon, 7 Jul 2025 07:15:37 +0000 (15:15 +0800)
committerxue <2144439972@qq.com>
Mon, 7 Jul 2025 07:15:37 +0000 (15:15 +0800)
code/sdk/application/wearable/fd230app/nativeui/bloodpressure/include/BPModel.h
code/sdk/application/wearable/fd230app/nativeui/bloodpressure/src/BPModel.cpp
code/sdk/application/wearable/fd230app/nativeui/bloodpressure/src/detection/BPDetectFirstView.cpp

index a25e57f..7580ad7 100644 (file)
 
 #include <stdint.h>
 #include <string>
-#include <deque> 
+#include <deque>
 #include <vector>
 #include "map"
 
 #define SBP_VALUE 120
 #define DBP_VALUE 80
 
-namespace OHOS {
-// 检测状态
-enum class BPState {
-  // 结束检测
-  BP_End = 0,
-  // 第一次校准血压中 30s
-  BP_CALIBRATING_FIRSTTIME,
-  // 第二次校准血压中 30s
-  BP_CALIBRATING_SECONDTIME,
-  // 校准分析中 5s
-  BP_CALIBRATING_ANGLYSIS,
-  // 检测血压中 30s
-  BP_DETECTING,
-  // 压力测量中
-  STRESS_MEASURE,
-  // 疲劳度测量中
-  FATIGUE_MEASURE,
-  // ECG提示中
-  ECG_REMIND,
-  // ECG测量中
-  ECG_MEASURE,
-  // 情绪测量中
-  EMO_MEASURE,
-  // 压力分析中
-  STRESS_ANALYSIS,
-  // 疲劳度分析中
-  FATIGUE_ANALYSIS,
-  // ECG分析中
-  ECG_ANALYSIS,
-  // ECG结果中
-  ECG_RESULT,
-  // 情绪分析中
-  EMO_ANALYSIS,
-  // 普通状态
-  BP_NORMAL,
-  // 暂停检测
-  BP_PAUSE,
-
-  BP_STATE_MAX,
-};
-// 血压检测结果
-enum class BPRESULT {
-  NOT_TESTED = 0, // 未检测
-  SUCCESS,        // 检测成功
-  NOT_WORN,       // 未佩戴检测
-  TIMEOUT,        // 检测超时
-  HIGH_RESULT,    // 检测结果过高
-  LOW_RESULT,     // 检测结果过低
-  INVALID_DATA    // 无效数据
-};
-
-enum class BP_VALUE_TYPE {
-  SBP = 0,
-  DBP,
-};
-
-struct LAST_CHECK_DATA {  
-  uint8_t last_check_sbp_value;      // 上次检测收缩压值
-  uint8_t last_check_dbp_value;      // 上次检测舒张压值
-  uint32_t timestamp;             // 时间戳  单位s
-};
-
-#define BP_CHECK_TIME_MAX_TEST 90   //测量所需时间
-#define BP_CHECK_TIME_MAX 30   //测量所需时间
-#define BP_ANLYSISI_TIME_MAX 5 //分析所需时间
-#define BP_REMIND_TIME_MAX 5   //提醒所需时间
-class BPModel {
-public:
-  BPModel();
-  ~BPModel();
-  static BPModel *GetInstance();
-  // 获取第几次校准
-  int16_t GetCalibrationFlag();
-  void SetCalibrationFlag(int value);
-  // 获取当前时间年月日时分字符串
-  std::string GetCurrentTimeStr();
-  std::string GetCurrentTimeStr(uint64_t timeStamp);
-  std::string GetCurrentTimePath();
-  //获取当前时间戳
-  uint32_t GetCurrentTime();
-  std::string GetLastCalibrationTime();
-  // 当前状态管理
-  void BP_SetState(BPState state);
-  BPState GetCurrentState();
-  // 控制血压检测
-  void BP_Control(BPState state);
-  void BP_SetValue(BP_VALUE_TYPE type, bool set_value);
-  int GetValue(BP_VALUE_TYPE type);
-  void BP_timer_callback_out();
-  void BP_timer_callback_proc(int data);
-
-  ///////////////////////////////////////////////
-  void BP_StartTimer(uint32_t interval = 1 * 1000); // 1s定时器
-  void BP_StopTimer(void);
-
-  void BP_START(void);
-  void BP_STOP(void);
-  void BP_GetData(int *sbp,int *dbp);
-
-  std::map<int, std::pair<int, int>> GetBPValueMap(void);
-  void SetBPValueMap(int sbp,int dbp,int index);
-  int GetBPValueCnt(void);
-  int mapValue(int value);
-  void mapDataArray(int value);
-  bool IsSameDay(const std::string &timeStr1, const std::string &timeStr2);
-
-
-  /* --------------------------------- 最近检测数据 --------------------------------- */
-  static constexpr const char *BP_JSON_PATH = "/user/userdata/bp/";
-  bool get_last_check_bp_data(LAST_CHECK_DATA &data);
-  int get_last_check_data_num(void);
-  const uint8_t max_checkout_num = 30;
-  std::deque<LAST_CHECK_DATA> lastCheckData;          // 存储最近检测数据
-  bool CreateBPFolder();
-  // 存储bin文件
-  bool save_bin_BP(const LAST_CHECK_DATA &data);
-  bool UpdateHealthData(const LAST_CHECK_DATA &data);
-   bool IsTimestampToday(uint32_t timestamp);
-   bool LoadBP();
-  /* -------------------------------------------------------------------------- */
-
-
-private:
-  BPState current_state = BPState::BP_NORMAL;
-  int custom_sbp = SBP_VALUE;
-  int custom_dbp = DBP_VALUE;
-  std::map<int, std::pair<int, int>> bp_check_value_map;  //记录获取的sbp和dbp值
-  int bp_check_cnt = 0;                                   //记录获取的次数
-  std::vector<int> bp_check_value_vector;                 //测量值
-  std::string LastCalibrationTime;
-};
+namespace OHOS
+{
+  // 检测状态
+  enum class BPState
+  {
+    // 结束检测
+    BP_End = 0,
+    // 第一次校准血压中 30s
+    BP_CALIBRATING_FIRSTTIME,
+    // 第二次校准血压中 30s
+    BP_CALIBRATING_SECONDTIME,
+    // 校准分析中 5s
+    BP_CALIBRATING_ANGLYSIS,
+    // 检测血压中 30s
+    BP_DETECTING,
+    // 压力测量中
+    STRESS_MEASURE,
+    // 疲劳度测量中
+    FATIGUE_MEASURE,
+    // ECG提示中
+    ECG_REMIND,
+    // ECG测量中
+    ECG_MEASURE,
+    // 情绪测量中
+    EMO_MEASURE,
+    // 压力分析中
+    STRESS_ANALYSIS,
+    // 疲劳度分析中
+    FATIGUE_ANALYSIS,
+    // ECG分析中
+    ECG_ANALYSIS,
+    // ECG结果中
+    ECG_RESULT,
+    // 情绪分析中
+    EMO_ANALYSIS,
+    // 普通状态
+    BP_NORMAL,
+    // 暂停检测
+    BP_PAUSE,
+
+    BP_STATE_MAX,
+  };
+  // 血压检测结果
+  enum class BPRESULT
+  {
+    NOT_TESTED = 0, // 未检测
+    SUCCESS,        // 检测成功
+    NOT_WORN,       // 未佩戴检测
+    TIMEOUT,        // 检测超时
+    HIGH_RESULT,    // 检测结果过高
+    LOW_RESULT,     // 检测结果过低
+    INVALID_DATA    // 无效数据
+  };
+
+  enum class BP_VALUE_TYPE
+  {
+    SBP = 0,
+    DBP,
+  };
+
+  struct LAST_CHECK_DATA
+  {
+    uint32_t timestamp;           // 时间戳  单位s
+    uint8_t last_check_sbp_value; // 上次检测收缩压值
+    uint8_t last_check_dbp_value; // 上次检测舒张压值
+  };
+
+#define BP_CHECK_TIME_MAX_TEST 90 // 测量所需时间
+#define BP_CHECK_TIME_MAX 30      // 测量所需时间
+#define BP_ANLYSISI_TIME_MAX 5    // 分析所需时间
+#define BP_REMIND_TIME_MAX 5      // 提醒所需时间
+  class BPModel
+  {
+  public:
+    BPModel();
+    ~BPModel();
+    static BPModel *GetInstance();
+    // 获取第几次校准
+    int16_t GetCalibrationFlag();
+    void SetCalibrationFlag(int value);
+    // 获取当前时间年月日时分字符串
+    std::string GetCurrentTimeStr();
+    std::string GetCurrentTimeStr(uint64_t timeStamp);
+    // 获取当前时间戳
+    uint32_t GetCurrentTime();
+    std::string GetLastCalibrationTime();
+    // 当前状态管理
+    void BP_SetState(BPState state);
+    BPState GetCurrentState();
+    void BP_SetResultState(BPRESULT state);
+    BPRESULT GettResultState();
+        // 控制血压检测
+    void BP_Control(BPState state);
+    void BP_SetValue(BP_VALUE_TYPE type, bool set_value);
+    int GetValue(BP_VALUE_TYPE type);
+    void BP_timer_callback_out();
+    void BP_timer_callback_proc(int data);
+
+    ///////////////////////////////////////////////
+    void BP_StartTimer(uint32_t interval = 1 * 1000); // 1s定时器
+    void BP_StopTimer(void);
+
+    void BP_START(void);
+    void BP_STOP(void);
+    void BP_GetData(int *sbp, int *dbp);
+
+    std::map<int, std::pair<int, int>> GetBPValueMap(void);
+    void SetBPValueMap(int sbp, int dbp, int index);
+    int GetBPValueCnt(void);
+    int mapValue(int value);
+    void mapDataArray(int value);
+
+    /* --------------------------------- 检测数据 --------------------------------- */
+    static constexpr const char *BP_JSON_PATH = "/user/userdata/bp/";
+    std::deque<LAST_CHECK_DATA> lastCheckData;          // 存储最近检测数据
+    std::string GetCurrentTimePath();                   // 获取存储文件名
+    bool CreateBPFolder();                              // 创建文件夹
+    bool save_bin_BP(const LAST_CHECK_DATA &data);      // 存储bin文件
+    bool UpdateHealthData(const LAST_CHECK_DATA &data); // 存储数据
+    bool IsTimestampToday(uint32_t timestamp);          // 判断是否为同一天
+    bool LoadBP();                                      // 加载数据
+    /* -------------------------------------------------------------------------- */
+
+  private:
+    BPState current_state = BPState::BP_NORMAL; // 当前状态
+    BPRESULT bp_resullt = BPRESULT::NOT_TESTED; // 测量状态
+    int custom_sbp = SBP_VALUE;
+    int custom_dbp = DBP_VALUE;
+    std::map<int, std::pair<int, int>> bp_check_value_map; // 记录获取的sbp和dbp值
+    int bp_check_cnt = 0;                                  // 记录获取的次数
+    std::vector<int> bp_check_value_vector;                // 测量值
+    std::string LastCalibrationTime;                       // 校准时间标志位
+              
+  };
 } // namespace OHOS
 
 #endif
index 1b7a142..f74b5ac 100644 (file)
@@ -180,6 +180,9 @@ namespace OHOS
   // 管理当前状态
   void BPModel::BP_SetState(BPState state) { current_state = state; }
   BPState BPModel::GetCurrentState() { return current_state; }
+  // 管理测量结果状态
+  void BPModel::BP_SetResultState(BPRESULT state) { bp_resullt = state; }
+  BPRESULT BPModel::GettResultState() { return bp_resullt; }
 
   // 进行传感器、定时器等控制
   void BPModel::BP_Control(BPState state)
@@ -213,20 +216,17 @@ namespace OHOS
         开启传感器
         开启常亮
       */
-      bp_check_value_map.clear();
-      bp_check_value_vector.clear();
       if (bp_check_cnt != 0)
         bp_check_cnt = 0; // 重置计数
       if (Time != BP_CHECK_TIME_MAX_TEST)
-        Time = BP_CHECK_TIME_MAX_TEST; // 重置时间120秒
+        Time = BP_CHECK_TIME_MAX_TEST; // 重置时间90秒
       BP_StartTimer();
       ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); // 后期 周期被动状态 需要做区别
       WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-                    "开启测量");
+                    "start detecting");
       BP_START(); // 底层接口开启血压
       custom_dbp = 0;
       custom_sbp = 0;
-
       break;
     }
 
@@ -377,12 +377,11 @@ namespace OHOS
       {
         bpDetectionView->ChangeView(CALIBRATION_BPVIEW, RESULT_BPVIEW);
       }
-      BP_STOP();
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-                    "LoadBP1111");
-      LoadBP();
-      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-                    "LoadBP2222");
+      BP_STOP(); // 停止检测
+      if(bp_resullt == BPRESULT::NOT_TESTED)//未测量到数据
+      {
+         BP_SetResultState(BPRESULT::TIMEOUT);//测量超时
+      }
       BP_SetState(BPState::BP_NORMAL);
       return;
     }
@@ -532,12 +531,33 @@ namespace OHOS
       {
 
         BP_GetData(&custom_sbp, &custom_dbp);
-        if (custom_sbp > 0 && custom_dbp > 0) // 检测到数据
+        if (bp_resullt == BPRESULT::NOT_TESTED && custom_sbp > 0 && custom_dbp > 0) // 检测到数据
         {
           LAST_CHECK_DATA newData;
-          newData.last_check_sbp_value = custom_sbp; // 测试数据
-          newData.last_check_dbp_value = custom_dbp;
           newData.timestamp = BPModel::GetCurrentTime();
+          newData.last_check_sbp_value = custom_sbp;
+          newData.last_check_dbp_value = custom_dbp;
+          // 测量数据结果分析
+          if (newData.last_check_sbp_value >= 140 || newData.last_check_dbp_value >= 90) // 高血压:收缩压≥140 mmHg 或 舒张压≥90 mmHg。
+          {
+            BP_SetResultState(BPRESULT::HIGH_RESULT);
+            UpdateHealthData(newData); // 存储到文件
+          }
+          else if (newData.last_check_sbp_value < 90 || newData.last_check_dbp_value < 60) // 低血压:收缩压<90 mmHg 且 舒张压<60 mmHg。
+          {
+            BP_SetResultState(BPRESULT::LOW_RESULT);
+            UpdateHealthData(newData); 
+          }
+          else if (newData.last_check_sbp_value < 90 || newData.last_check_dbp_value < 60) // 正常血压:收缩压(高压)<120 mmHg 且 舒张压(低压)<80 mmHg。
+          {
+            BP_SetResultState(BPRESULT::SUCCESS);
+            UpdateHealthData(newData); 
+          }
+          else // 数据异常
+          {
+            BP_SetResultState(BPRESULT::INVALID_DATA); // 无效数据,不存储
+          }
+
           WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SBP: %d, DBP: %d", custom_sbp, custom_dbp);
         }
 #if 1
@@ -813,24 +833,6 @@ namespace OHOS
   5.更新完毕,存储到文件
   */
 
-  bool BPModel::get_last_check_bp_data(LAST_CHECK_DATA &data)
-  {
-    // 获取队列中最后一次检测数据
-    if (lastCheckData.size() > 0)
-    {
-      data = lastCheckData.back();
-      return true;
-    }
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "BPModel::get_last_check_bp_data is empty");
-    return false;
-  }
-
-  // 获取最近检测数据数量
-  int BPModel::get_last_check_data_num(void)
-  {
-    return lastCheckData.size();
-  }
-
   bool BPModel::UpdateHealthData(const LAST_CHECK_DATA &data)
   {
 
@@ -840,7 +842,7 @@ namespace OHOS
       WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreateBPFolder file");
       return false;
     }
-    if (!save_bin_BP(data))
+    if (!save_bin_BP(data) || (!IsTimestampToday(data.timestamp)))
     { // 存储bin文件[协议使用]
       WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SaveBP failed");
       return false;
@@ -928,11 +930,10 @@ namespace OHOS
   /*加载文件中数据*/
   bool BPModel::LoadBP()
   {
-    // 清空存储数组
-
+    bp_check_value_vector.clear();                  // 清空数组
     std::string binFilePath = GetCurrentTimePath(); // 文件夹+时间+文件名
 
-    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "binFilePath %s", binFilePath.c_str()); //打开失败,今天没有测量记录则没有这个文件
+    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "binFilePath %s", binFilePath.c_str()); // 打开失败,今天没有测量记录则没有这个文件
 
     std::ifstream ifs(binFilePath, std::ios::binary);
     if (!ifs.is_open())
@@ -968,7 +969,7 @@ namespace OHOS
                       "Failed to read heart rate value");
         break; // 如果读取失败,退出循环
       }
-      //如果时间为当天
+      // 如果时间为当天
       if (IsTimestampToday(data.timestamp))
       {
 
index 8c0d826..c1a1149 100644 (file)
@@ -111,17 +111,17 @@ namespace OHOS
             return;
         }
         // 获取上一次检测数据 ? 待定
-        LAST_CHECK_DATA data;
-        if (BPModel::GetInstance()->get_last_check_bp_data(data))
-        {
-            // 获取时间更新时间
-            uint64_t timeStr = data.timestamp;
-            std::string dbpValue = std::to_string(data.last_check_dbp_value);
-            std::string sbpValue = std::to_string(data.last_check_sbp_value);
-            timeLabel->SetText(BPModel::GetInstance()->GetCurrentTimeStr(data.timestamp).c_str());
-            DBPText->SetText(dbpValue.c_str());
-            SBPText->SetText(sbpValue.c_str());
-        }
+        // LAST_CHECK_DATA data;
+        // if (BPModel::GetInstance()->get_last_check_bp_data(data))
+        // {
+        //     // 获取时间更新时间
+        //     uint64_t timeStr = data.timestamp;
+        //     std::string dbpValue = std::to_string(data.last_check_dbp_value);
+        //     std::string sbpValue = std::to_string(data.last_check_sbp_value);
+        //     timeLabel->SetText(BPModel::GetInstance()->GetCurrentTimeStr(data.timestamp).c_str());
+        //     DBPText->SetText(dbpValue.c_str());
+        //     SBPText->SetText(sbpValue.c_str());
+        // }
     }
 
     void BPDetectFirstView::WriteMapToPointArrays(const std::map<int, std::pair<int, int>> &bpMap)