From b295bb7fa82523c575f7e1fd11b112c4d2f06f19 Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Thu, 13 Jun 2024 18:14:06 +0800 Subject: [PATCH] =?utf8?q?=E4=BF=AE=E5=A4=8D=E5=BF=83=E7=8E=87=E8=A1=80?= =?utf8?q?=E6=B0=A7=E6=95=B0=E6=8D=AEjson=E7=9A=84=E6=97=B6=E9=97=B4?= =?utf8?q?=E6=88=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../include/BloodOxygenPresenter.h | 10 +- .../bloodoxygen/src/BloodOxygenPresenter.cpp | 32 +++---- .../health_service/health_service.cpp | 91 ++++++++++--------- .../heart_blood/src/BloodCardView.cpp | 8 +- .../heartrate/include/HeartPresenter.h | 4 +- .../nativeui/heartrate/src/HeartModel.cpp | 2 +- .../nativeui/heartrate/src/HeartPresenter.cpp | 32 +++++-- 7 files changed, 100 insertions(+), 79 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/include/BloodOxygenPresenter.h b/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/include/BloodOxygenPresenter.h index 3324fe354..aed3211fa 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/include/BloodOxygenPresenter.h +++ b/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/include/BloodOxygenPresenter.h @@ -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); diff --git a/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp index 7956aefc7..8847d7653 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/bloodoxygen/src/BloodOxygenPresenter.cpp @@ -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; } /* -------------------------------------------------------------------------- */ diff --git a/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp b/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp index 09d869f24..6dc6d0e8b 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.cpp @@ -7,11 +7,9 @@ #include 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(inFile)), std::istreambuf_iterator()); 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) { diff --git a/code/sdk/application/wearable/fd230app/nativeui/heart_blood/src/BloodCardView.cpp b/code/sdk/application/wearable/fd230app/nativeui/heart_blood/src/BloodCardView.cpp index 467a93042..0bf068761 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/heart_blood/src/BloodCardView.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/heart_blood/src/BloodCardView.cpp @@ -47,8 +47,8 @@ namespace OHOS std::string bloodoxygenStr = std::to_string(spo2_); bloodLabel->SetText(bloodoxygenStr.c_str()); // 更新时间 - uint64_t time = BloodOxygenPresenter::GetInstance()->GetCurrentTime(); - uint64_t check_time = BloodOxygenModel::GetInstance()->GetCheckTime(); + uint32_t time = BloodOxygenPresenter::GetInstance()->GetCurrentTime(); + uint32_t check_time = BloodOxygenModel::GetInstance()->GetCheckTime(); std::string TimeStr = BloodOxygenPresenter::GetInstance()->GetTDOA(time, check_time) + "检测"; timeLabel->SetText(TimeStr.c_str()); } @@ -118,8 +118,8 @@ namespace OHOS if (bloodoxygen != 0) { // 更新时间 - uint64_t time = BloodOxygenPresenter::GetInstance()->GetCurrentTime(); - uint64_t check_time = BloodOxygenModel::GetInstance()->GetCheckTime(); + uint32_t time = BloodOxygenPresenter::GetInstance()->GetCurrentTime(); + uint32_t check_time = BloodOxygenModel::GetInstance()->GetCheckTime(); std::string TimeStr = BloodOxygenPresenter::GetInstance()->GetTDOA(time, check_time) + "检测"; timeLabel->SetText(TimeStr.c_str()); } diff --git a/code/sdk/application/wearable/fd230app/nativeui/heartrate/include/HeartPresenter.h b/code/sdk/application/wearable/fd230app/nativeui/heartrate/include/HeartPresenter.h index 970ae3e75..d0916d20e 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/heartrate/include/HeartPresenter.h +++ b/code/sdk/application/wearable/fd230app/nativeui/heartrate/include/HeartPresenter.h @@ -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); diff --git a/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartModel.cpp index dd5f1e993..62bdf93e7 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartModel.cpp @@ -630,7 +630,7 @@ namespace OHOS HeartRateData *data = new HeartRateData; data->Heart = heart; data->timestamp = HeartPresenter::GetInstance()->GetCurrentTime(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d %lld", heart, data->timestamp); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d %d", heart, data->timestamp); SetCheckTime(data->timestamp); HealthService::GetInstance()->UpdateHeartRateData(*data); // 更新卡片心率值 diff --git a/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp index 323581252..42f7da653 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/heartrate/src/HeartPresenter.cpp @@ -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() -- 2.17.1