From 7e0dcf77df95261e281e8d2e9f4d473648cab0c1 Mon Sep 17 00:00:00 2001 From: Caisking <3449750381@qq.com> Date: Sat, 30 Nov 2024 17:54:45 +0800 Subject: [PATCH] =?utf8?q?=E5=BF=83=E7=8E=87=EF=BC=9A=E4=BC=98=E5=8C=96?= =?utf8?q?=E5=BF=83=E7=8E=87=E6=95=B0=E6=8D=AE=E7=9A=84=E5=AD=98=E5=82=A8?= =?utf8?q?=E4=B8=8E=E8=AF=BB=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../health_service/health_service.cpp | 120 +++++++++++++----- .../health_service/health_service.h | 6 + .../debug_output_notification.cpp | 17 ++- 3 files changed, 105 insertions(+), 38 deletions(-) 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 5a7f32400..6ca39a1db 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 @@ -4,6 +4,7 @@ #include "heartrate/include/HeartModel.h" #include "kv_store.h" #include "wearable_log.h" + #include #include #include @@ -458,28 +459,12 @@ bool HealthService::UpdateHealthData(const HeartRateData &data) { return false; } - /* 存储 bin 文件 [协议使用] */ - std::string binFilePath = GetBinFilePath(HEALTH_TYPE_HEART); - FILE *fp = fopen(binFilePath.c_str(), "ab+"); - if (fp == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Failed to open binary file for writing"); - uapi_dev_runtime_set_heartrate_storage_status(0); // 恢复状态 - return false; - } - - // 写入数据 - if (fwrite(&data.timestamp, sizeof(data.timestamp), 1, fp) != 1 || - fwrite(&data.Heart, sizeof(data.Heart), 1, fp) != 1) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Failed to write data to binary file"); - fclose(fp); + if (!save_bin_heart(data)) { // 存储bin文件[协议使用] + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SaveDailyHeartRate failed"); uapi_dev_runtime_set_heartrate_storage_status(0); // 恢复状态 return false; } - fclose(fp); - uapi_dev_runtime_set_heartrate_storage_status(0); // 恢复状态 return true; } @@ -693,46 +678,113 @@ void HealthService::send_blood_save() { CUSTOM_FILEOPERATE_MSG_TYPE_BLOODXYGEN_SAVE, nullptr, 0); } +uint32_t HealthService::GetListTime() { + return lastLoadTime; +} + std::string HealthService::formatTime(uint32_t time) { char labelTime[20]; - if (uapi_get_TIME_STR(time, labelTime, '.', ':', DATE_Y_M_D_H_M) != + if (uapi_get_TIME_STR(time, labelTime, '.', ':', DATE_TIME_DEFULL) != ERRCODE_SUCC) { return "--"; // 默认值 - } else { - return labelTime; // 成功获取时间 } + return labelTime; // 成功获取时间 } -// 读取心率数据 + +bool HealthService::save_bin_heart(const HeartRateData &data) { + /* 存储 bin 文件 [协议使用] */ + std::string binFilePath = GetBinFilePath(HEALTH_TYPE_HEART); + FILE *fp = fopen(binFilePath.c_str(), "ab+"); + if (fp == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to open binary file for writing"); + return false; + } + + // 检查时间戳是否有效 + if (data.timestamp < 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid timestamp: %d", + data.timestamp); + fclose(fp); + return false; // 返回错误 + } + + // 写入数据 + if (fwrite(&data, sizeof(HeartRateData), 1, fp) != 1) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to write data to binary file"); + fclose(fp); + return false; + } + + fclose(fp); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "Saved heart data: timestamp = %d, Heart = %d", data.timestamp, + data.Heart); + return true; +} + 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) { + + std::ifstream ifs(binFilePath, std::ios::binary); // 以二进制读取模式打开文件 + if (!ifs.is_open()) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open binary file for reading"); return "Error: Unable to open file \n"; // 返回错误信息 } - std::string result; + std::stringstream result; + result << "file: " << binFilePath << "\n"; HeartRateData data; - result += "file:" + binFilePath + "\n"; - // 读取数据直到文件结束 - while (fread(&data, sizeof(HeartRateData), 1, fp) == 1) { + while (ifs.read(reinterpret_cast(&data), sizeof(HeartRateData))) { + // 检查时间戳的有效性 + if (data.timestamp < 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid timestamp read: %d", + data.timestamp); + continue; // 跳过无效的数据 + } + // 将数据格式化为字符串,拼接到结果中 - result += - formatTime(data.timestamp) + "," + std::to_string(data.Heart) + "\n"; + result << formatTime(data.timestamp) << "," << std::to_string(data.Heart) << "\n"; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "get_heart_data %d %d", + data.timestamp, data.Heart); } - if (ferror(fp)) { + if (ifs.bad()) { 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; // 检查是否读取到数据 + return result.str().empty() ? "No data found \n" + : result.str(); // 检查是否读取到数据 } + +// 存储血氧数据 +// bool HealthService::save_bin_blood(const BloodOxygenData &bloodoxygen) { +// /* 存储bin文件[协议使用] */ +// std::string binFilePath = GetBinFilePath(HEALTH_TYPE_HEART); +// FILE *fp = fopen(binFilePath.c_str(), "ab+"); +// if (fp == NULL) { +// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, +// "Failed to open binary file for writing"); +// return false; +// } +// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "binFilePath %s", +// binFilePath.c_str()); + +// if (fwrite(&bloodoxygen, sizeof(bloodoxygen), 1, fp) != 1) { +// WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, +// "Failed to write data to binary file"); +// fclose(fp); +// return false; +// } +// fclose(fp); +// WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "save_bin_blood is ok"); +// return true; +// } } // namespace OHOS diff --git a/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.h b/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.h index f7df7d823..a2fa5d268 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.h +++ b/code/sdk/application/wearable/fd230app/nativeui/heart_blood/health_service/health_service.h @@ -10,6 +10,7 @@ #include "stdio.h" #include "string" + #include #include #include @@ -94,6 +95,7 @@ namespace OHOS time_t GetHourlyTimestamp(uint16_t hour); void ClearHourlyHeartRates(); uint16_t GetCurrentHour(); + uint32_t GetListTime(); bool SaveHeartMaxMin(uint16_t max, uint16_t min); void LoadHeartMaxMin(uint16_t &max, uint16_t &min); @@ -105,12 +107,16 @@ namespace OHOS std::string get_blood_data(); std::string formatTime(uint32_t time); + bool save_bin_heart(const HeartRateData &heart); + bool save_bin_blood(const BloodOxygenData &bloodoxygen); + private: static const int MAX_HOURS = 24; HeartRateData dailyHeartRates[MAX_HOURS]; // 存储心率值,检测时间戳?使用结构体? HealthState healthState_; time_t lastLoadTime; // 记录最后加载的时间 uint16_t currenthour = 0; + }; } #endif // HEALTH_MODEL_H \ No newline at end of file diff --git a/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp b/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp index 7fd0c5f6c..7d27dd37b 100644 --- a/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp +++ b/code/sdk/application/wearable/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp @@ -28,6 +28,7 @@ #include "vibrator_service.h" #include "wearable_log.h" #include + #include #include #include @@ -92,7 +93,7 @@ namespace OHOS // 格式化时间的辅助函数 std::string formatTime(uint32_t time) { char labelTime[20]; - if (uapi_get_TIME_STR(time, labelTime, '.', ':', DATE_Y_M_D_H_M) != ERRCODE_SUCC) { + if (uapi_get_TIME_STR(time, labelTime, '.', ':', DATE_TIME_DEFULL) != ERRCODE_SUCC) { return "--"; // 默认值 } else { return labelTime; // 成功获取时间 @@ -582,10 +583,18 @@ namespace OHOS // 3.读取文件里面的心率数据 auto healthService = HealthService::GetInstance(); result << healthService->get_heart_data(); + result << "=====================\n"; - // 4. 获取一天的心率列表 - result << "Today's HeartRate list\n"; + // 4. 获取一天的心率列表 + uint32_t listtime = healthService->GetListTime(); + std::string listtimestr; + if (listtime == 0) { + listtimestr = "--"; // 默认值 + } else { + listtimestr = formatTime(listtime); + } + result << "listt_ime: " << listtimestr << "\n"; for (int serialIndex = 0; serialIndex < 24; ++serialIndex) { uint32_t heartRate = HealthService::GetInstance()->GetHourlyHeartRate(serialIndex); uint32_t time = HealthService::GetInstance()->GetHourlyTimestamp(serialIndex); @@ -597,7 +606,7 @@ namespace OHOS timestr = formatTime(time); } - result << "time " << timestr << ""<<"\n" <<"heart " << heartRate <<""<< "\n"; // 输出每小时的心率 + result << timestr <<" "<< heartRate << "\n"; // 输出每小时的心率 } result << "=====================\n"; -- 2.17.1