修复心率血氧数据json的时间戳
authorCaisking <3449750381@qq.com>
Thu, 13 Jun 2024 10:14:06 +0000 (18:14 +0800)
committerCaisking <3449750381@qq.com>
Thu, 13 Jun 2024 10:14:06 +0000 (18:14 +0800)
code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/include/BloodOxygenPresenter.h
code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp
code/sdk/application/wearable/fd230app/nativeui/heart_blood/src/BloodCardView.cpp
code/sdk/application/wearable/fd230app/nativeui/heartrate/include/HeartPresenter.h
code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartModel.cpp
code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp

index 3324fe3..aed3211 100644 (file)
@@ -1,3 +1,9 @@
+/*** 
+ * @Date: 2024-04-15 10:55:58
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-06-13 17:05:26
+ * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/include/BloodOxygenPresenter.h
+ */
 
 /*
  * Copyright (c) CompanyNameMagicTag 2021-2021. All rights reserved.
@@ -52,8 +58,8 @@ namespace OHOS
         bool OnClick(UIView &view, const ClickEvent &event) override;
         bool OnKeyAct(UIView &view, const KeyEvent &event) override; // 增加按键事件
 
-        uint64_t GetCurrentTime();
-        int GetTDOA(uint64_t new_time, uint64_t old_time);
+        uint32_t GetCurrentTime();
+        int GetTDOA(uint32_t new_time, uint32_t old_time);
 
     private:
         bool CheckPointersValue(void);
index 7956aef..8847d76 100644 (file)
@@ -43,33 +43,29 @@ namespace OHOS
     static constexpr uint16_t PROGRESS_VALUE_MAX = 10;
     static BloodOxygenPresenter *g_pMainView;
     /* -------------------------------------------------------------------------- */
-    // 获取当前的"时间分"
-    uint64_t BloodOxygenPresenter::GetCurrentTime()
+    // 获取当前的时间戳
+    uint32_t BloodOxygenPresenter::GetCurrentTime()
     {
-
-        // time_t now = time(0);
-        // struct tm *tblock = localtime(&now);
-
-        /*优化为更安全的localtime_r()*/
-        struct timeval tv;
-        gettimeofday(&tv, NULL);
-        struct tm tblock;
-        localtime_r(&tv.tv_sec, &tblock);
-
-        uint64_t current_time = tblock.tm_hour * 60 + tblock.tm_min;
-        return current_time;
+        uint32_t time;
+        if (uapi_get_timestampe_ss(&time) != ERRCODE_SUCC)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get timestamp");
+            return 0;
+        }
+        return time;
     }
 
     // 获取时间差,现在传进来的是两个时间戳
-    int BloodOxygenPresenter::GetTDOA(uint64_t new_time, uint64_t old_time)
+    int BloodOxygenPresenter::GetTDOA(uint32_t new_time, uint32_t old_time)
     {
         if (new_time == old_time || old_time > new_time)
         {
             return 0;
         }
-
-        int time_diff = new_time - old_time;
-        return time_diff;
+        int time_diff_seconds = new_time - old_time;
+        // 转换为分钟的差值
+        int time_diff_minutes = time_diff_seconds / 60;
+        return time_diff_minutes;
     }
 
     /* -------------------------------------------------------------------------- */
index 09d869f..6dc6d0e 100644 (file)
@@ -7,11 +7,9 @@
 #include <ctime>
 namespace OHOS
 {
-    static HealthService *g_pHealthService = nullptr;
-    static constexpr const char *HEALTH_JSON_PATH_HEART = "/user/userdata/health/heart/";
-
-    static constexpr const char *HEALTH_JSON_PATH_OXYGEN = "/user/userdata/health/oxygen/";
 
+    static HealthService *g_pHealthService = nullptr;
+    /*获取日期*/
     std::string GetCurrentDate()
     {
         time_t now = time(0);
@@ -20,17 +18,7 @@ namespace OHOS
         strftime(date, sizeof(date), "%Y%m%d", ltm);
         return std::string(date);
     }
-    //static char *JSON_F_buf=NULL;
-    const char *GET_HEALTH_JSON_PATH_C_STR_HEART()
-    {
-        return HEALTH_JSON_PATH_HEART;
-    }
-
-    const char *GET_HEALTH_JSON_PATH_C_STR_OXYGEN()
-    {
-        return HEALTH_JSON_PATH_OXYGEN;
-    }
-
+    // static char *JSON_F_buf=NULL;
     cJSON *heart_root = NULL;
     cJSON *oxygen_root = NULL;
     HealthService::HealthService()
@@ -38,46 +26,61 @@ namespace OHOS
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "HealthService::HealthService");
         g_pHealthService = this;
     }
-
     HealthService::~HealthService()
     {
         g_pHealthService = nullptr;
     }
-
     HealthService *HealthService::GetInstance()
     {
         static HealthService instance;
         return &instance;
     }
+
+    static constexpr const char *HEALTH_JSON_PATH = "/user/userdata/health/";
+    static constexpr const char *HEALTH_JSON_PATH_HEART = "/user/userdata/health/heart/";
+    static constexpr const char *HEALTH_JSON_PATH_OXYGEN = "/user/userdata/health/oxygen/";
+
     /* --------------------------------- 文件IO操作 --------------------------------- */
     /*创建文件夹*/
     bool HealthService::CreateHealthFolder()
     {
         struct stat info;
+        // 判断HEALTH_JSON_PATH是否存在 不存在就创建
+        if (stat(HEALTH_JSON_PATH, &info) != 0)
+        {
+            if (mkdir(HEALTH_JSON_PATH, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
+            {
+                WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "mkdir HEALTH_JSON_PATH is error");
+                return false; // Failed to create folder
+            }
+        }
+        // 判断HEALTH_JSON_PATH_HEART是否存在 不存在就创建
         if (stat(HEALTH_JSON_PATH_HEART, &info) != 0)
         {
             if (mkdir(HEALTH_JSON_PATH_HEART, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
             {
+                WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "mkdir HEALTH_JSON_PATH_HEART is error");
                 return false; // Failed to create folder
             }
         }
+        // 判断HEALTH_JSON_PATH_OXYGEN是否存在 不存在就创建
         if (stat(HEALTH_JSON_PATH_OXYGEN, &info) != 0)
         {
             if (mkdir(HEALTH_JSON_PATH_OXYGEN, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0)
             {
+                WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "mkdir HEALTH_JSON_PATH_OXYGEN is error");
                 return false; // Failed to create folder
             }
         }
 
         return true;
     }
-    /*获取日期*/
-   
     /*生成文件*/
     std::string GetJsonFilePath(uint8_t type)
     {
         std::string currentDate = GetCurrentDate();
-        if(type==HEALTH_TYPE_HEART)
+
+        if (type == HEALTH_TYPE_HEART)
         {
             return std::string(HEALTH_JSON_PATH_HEART) + currentDate + ".json";
         }
@@ -85,28 +88,31 @@ namespace OHOS
         {
             return std::string(HEALTH_JSON_PATH_OXYGEN) + currentDate + ".json";
         }
-      
     }
-
-
+    const char *GET_HEALTH_JSON_PATH_C_STR_HEART()
+    {
+        return HEALTH_JSON_PATH_HEART;
+    }
+    const char *GET_HEALTH_JSON_PATH_C_STR_OXYGEN()
+    {
+        return HEALTH_JSON_PATH_OXYGEN;
+    }
     bool HealthService::LoadJsonData(uint8_t type)
     {
         // 创建健康数据文件夹
-       
-        if(!CreateHealthFolder())
+        if (!CreateHealthFolder())
         {
             return false;
         }
-        
-      
         // 获取当天日期对应的JSON文件路径
         std::string jsonFilePath = GetJsonFilePath(type);
         std::ifstream inFile(jsonFilePath);
-        cJSON *defaultJson=NULL;
+        cJSON *defaultJson = NULL;
         if (!inFile.is_open())
         {
+#if 0
             // 如果文件不存在,则创建一个包含默认值的JSON文件
-            if(type==HEALTH_TYPE_HEART)
+            if (type == HEALTH_TYPE_HEART)
             {
                 defaultJson = cJSON_Parse("{\
                 \"heart_latest_data\": {\
@@ -121,7 +127,7 @@ namespace OHOS
                 ]\
             }");
             }
-            else if(type==HEALTH_TYPE_OXYGEN)
+            else if (type == HEALTH_TYPE_OXYGEN)
             {
                 defaultJson = cJSON_Parse("{\
                 \"spo2_latest_data\": {\
@@ -136,7 +142,6 @@ namespace OHOS
                 ]\
             }");
             }
-            
             std::ofstream outFile(jsonFilePath);
             if (!outFile.is_open())
             {
@@ -148,21 +153,20 @@ namespace OHOS
             cJSON_free(defaultJsonString);
             cJSON_Delete(defaultJson);
             inFile.open(jsonFilePath); // 重新打开文件
+#endif
         }
         // 读取JSON文件内容
         std::string jsonDataString((std::istreambuf_iterator<char>(inFile)), std::istreambuf_iterator<char>());
         const char *jsonData = jsonDataString.c_str();
-        if(type==HEALTH_TYPE_HEART)
+        if (type == HEALTH_TYPE_HEART)
         {
-             heart_root = cJSON_Parse(jsonData);
+            heart_root = cJSON_Parse(jsonData);
         }
-        if(type==HEALTH_TYPE_OXYGEN)
+        if (type == HEALTH_TYPE_OXYGEN)
         {
-             oxygen_root = cJSON_Parse(jsonData);
+            oxygen_root = cJSON_Parse(jsonData);
         }
 
-       
-
         return true;
     }
 #if 0
@@ -224,7 +228,8 @@ namespace OHOS
         HeartRateData *data = new HeartRateData();
         cJSON *latestData = cJSON_GetObjectItem(heart_root, "heart_latest_data");
         data->Heart = cJSON_GetObjectItem(latestData, "value")->valueint;
-        data->timestamp = cJSON_GetObjectItem(latestData, "timestamp")->valuedouble;
+        data->timestamp = cJSON_GetObjectItem(latestData, "timestamp")->valueint;
+        WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ReadHeartRateData: value %d timestamp%d", data->Heart, data->timestamp);
         cJSON_Delete(heart_root);
         return data;
     }
@@ -269,7 +274,7 @@ namespace OHOS
         return true;
     }
 
-    bool HealthService::UpdateJsonData( uint8_t type)
+    bool HealthService::UpdateJsonData(uint8_t type)
     {
         std::string jsonFilePath = GetJsonFilePath(type);
         std::ofstream outFile(jsonFilePath);
@@ -277,12 +282,12 @@ namespace OHOS
         {
             return false;
         }
-        char *jsonData=NULL;
+        char *jsonData = NULL;
 
-        if(type==HEALTH_TYPE_HEART)
-        jsonData = cJSON_Print(heart_root);
+        if (type == HEALTH_TYPE_HEART)
+            jsonData = cJSON_Print(heart_root);
         else
-        jsonData = cJSON_Print(oxygen_root);
+            jsonData = cJSON_Print(oxygen_root);
 
         if (jsonData == NULL)
         {
index 467a930..0bf0687 100644 (file)
@@ -47,8 +47,8 @@ namespace OHOS
             std::string bloodoxygenStr = std::to_string(spo2_);\r
             bloodLabel->SetText(bloodoxygenStr.c_str());\r
             //  更新时间\r
-            uint64_t time = BloodOxygenPresenter::GetInstance()->GetCurrentTime();\r
-            uint64_t check_time = BloodOxygenModel::GetInstance()->GetCheckTime();\r
+            uint32_t time = BloodOxygenPresenter::GetInstance()->GetCurrentTime();\r
+            uint32_t check_time = BloodOxygenModel::GetInstance()->GetCheckTime();\r
             std::string TimeStr = BloodOxygenPresenter::GetInstance()->GetTDOA(time, check_time) + "检测";\r
             timeLabel->SetText(TimeStr.c_str());\r
         }\r
@@ -118,8 +118,8 @@ namespace OHOS
         if (bloodoxygen != 0)\r
         {\r
             // 更新时间\r
-            uint64_t time = BloodOxygenPresenter::GetInstance()->GetCurrentTime();\r
-            uint64_t check_time = BloodOxygenModel::GetInstance()->GetCheckTime();\r
+            uint32_t time = BloodOxygenPresenter::GetInstance()->GetCurrentTime();\r
+            uint32_t check_time = BloodOxygenModel::GetInstance()->GetCheckTime();\r
             std::string TimeStr = BloodOxygenPresenter::GetInstance()->GetTDOA(time, check_time) + "检测";\r
             timeLabel->SetText(TimeStr.c_str());\r
         }\r
index 970ae3e..d0916d2 100644 (file)
@@ -49,8 +49,8 @@ namespace OHOS
         bool OnClick(UIView &view, const ClickEvent &event) override;
         bool OnKeyAct(UIView &view, const KeyEvent &event) override; // 增加按键事件
 
-        uint64_t GetCurrentTime();
-        int GetTDOA(uint64_t new_time, uint64_t old_time);
+        uint32_t GetCurrentTime();
+        int GetTDOA(uint32_t new_time, uint32_t old_time);
 
     private:
         bool CheckPointersValue(void);
index dd5f1e9..62bdf93 100644 (file)
@@ -630,7 +630,7 @@ namespace OHOS
             HeartRateData *data = new HeartRateData;\r
             data->Heart = heart;\r
             data->timestamp = HeartPresenter::GetInstance()->GetCurrentTime();\r
-            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d %lld", heart, data->timestamp);\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d %d", heart, data->timestamp);\r
             SetCheckTime(data->timestamp);\r
             HealthService::GetInstance()->UpdateHeartRateData(*data);\r
             // 更新卡片心率值\r
index 3235812..42f7da6 100644 (file)
@@ -1,3 +1,9 @@
+/***
+ * @Date: 2024-04-17 16:49:23
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-06-13 17:09:11
+ * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp
+ */
 
 #include "common/task_manager.h"
 #include "components/ui_label.h"
@@ -27,6 +33,7 @@
 #include "heartrate/include/HeartBandoffView.h"
 #include "health_service.h"
 #include "cywee_service.h"
+#include "custom_utils.h"
 
 #define HEARTKEYABLE 0
 
@@ -38,30 +45,37 @@ namespace OHOS
     static constexpr uint16_t PROGRESS_VALUE_MIN = 0;
     static constexpr uint16_t PROGRESS_VALUE_MAX = 10;
 
-    uint64_t HeartPresenter::GetCurrentTime()
+    uint32_t HeartPresenter::GetCurrentTime()
     {
-        // time_t now = time(0);
-        // struct tm *tblock = localtime(&now);
-
+#if 0
         /*优化为更安全的localtime_r()*/
         struct timeval tv;
         gettimeofday(&tv, NULL);
         struct tm tblock;
         localtime_r(&tv.tv_sec, &tblock);
 
-        uint64_t current_time = tblock.tm_hour * 60 + tblock.tm_min;
+        uint32_t current_time = tblock.tm_hour * 60 + tblock.tm_min;
         return current_time;
+#endif
+        uint32_t time;
+        if (uapi_get_timestampe_ss(&time) != ERRCODE_SUCC)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to get timestamp");
+            return 0;
+        }
+        return time;
     }
 
-    int HeartPresenter::GetTDOA(uint64_t new_time, uint64_t old_time)
+    int HeartPresenter::GetTDOA(uint32_t new_time, uint32_t old_time)
     {
         if (new_time == old_time || old_time > new_time)
         {
             return 0;
         }
-
-        int time_diff = new_time - old_time;
-        return time_diff;
+        int time_diff_seconds = new_time - old_time;
+        // 转换为分钟的差值
+        int time_diff_minutes = time_diff_seconds / 60;
+        return time_diff_minutes;
     }
 
     HeartPresenter::HeartPresenter()