From: 袁清梦 <3449750381@qq.com> Date: Sat, 11 May 2024 07:40:29 +0000 (+0000) Subject: Merge #161 into xts_origin from refs/changes/161/head X-Git-Tag: release_V1.2.70_20241020_135402~1121^2~4^2~1 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=498fe77b337557ebed2f48eb6398f32f922dd00b;p=mirrors%2FFD230.git Merge #161 into xts_origin from refs/changes/161/head 增加训练记录json服务 * refs/changes/161/head: (1 commits squashed) - 增加训练记录json服务 Signed-off-by: Caisking <3449750381@qq.com> Reviewed-by: snickersun@163.com Merged-by: snickersun@163.com CR-link: https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230/change/161 --- diff --git a/code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h b/code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h index 4e13437f4..21bfd0226 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h +++ b/code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h @@ -282,6 +282,10 @@ namespace OHOS /*平均心率计算*/ int CalculateAverageHeartRate(int newHeartRate); + /*保存训练记录到json文件*/ + bool SaveDataToJson(SportData*data); + void LoadDateFeomJson(); + private: TraingModel(const TraingModel &) = delete; TraingModel &operator=(const TraingModel &) = delete; diff --git a/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp index 4863ae3df..4dc0247b3 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp @@ -44,11 +44,13 @@ #define MAX_FILE_PATH_LENGTH 128 #define TRAINING_DATA_DIRECTORY "user/userdata/sport/" +#define TRAINING_RECORD_FILE "user/userdata/sport/Record.json" +#define MAX_DATA_COUNT 10 #define TIMER 1 #define BLETEST 0 #define HARD_OFF 1 -#define READTEST 0 +#define READTEST 1 #define MAX_HEART_RATE 220 #define AGE_THRESHOLD 59 @@ -660,13 +662,20 @@ namespace OHOS data.cir = 0; data.alt = 0; - if (write_Trainingdata_data(data) != true) + // 保存数据到bin + if (write_Trainingdata_data(data) != 1) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "write_Trainingdata_data is error!"); + // return false; + } + // 保存数据到json + if (SaveDataToJson(sportdata) != true) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SaveDataToJson is error!"); return false; } -#if READTEST +#if 0 Trainingdata *readdata{nullptr}; int i = 0; while (readdata = read_Trainingdata_data("2024_04_25", i)) @@ -947,6 +956,7 @@ namespace OHOS return trainingRecords; } + #ifdef __cplusplus extern "C" { @@ -1000,7 +1010,7 @@ namespace OHOS } fclose(fp); // Close the file after writing data - return 0; // Return success code + return 1; // Return success code } // 读数据 Trainingdata *read_Trainingdata_data(char *timedata, uint16_t offset) @@ -1028,4 +1038,89 @@ namespace OHOS #ifdef __cplusplus } #endif + + /* -------------------------------------------------------------------------- */ + /* 保存训练数据到 json 文件 */ + bool TraingModel::SaveDataToJson(SportData *data) + { + cJSON *root = cJSON_CreateObject(); + cJSON *dataArray = cJSON_CreateArray(); + + // 1.获取record.json文件,不存在则创建 + FILE *fp = fopen(TRAINING_RECORD_FILE, "r+"); + if (fp == NULL) + { + fp = fopen(TRAINING_RECORD_FILE, "w"); + if (fp == NULL) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fopen file is error"); + return false; + } + } + + fseek(fp, 0, SEEK_END); + long fileSize = ftell(fp); + fseek(fp, 0, SEEK_SET); + + char *jsonStr = (char *)malloc(fileSize + 1); + fread(jsonStr, 1, fileSize, fp); + jsonStr[fileSize] = '\0'; + + // 2.读取已有数据 + cJSON *existingData = cJSON_Parse(jsonStr); + cJSON *existingArray = cJSON_GetObjectItem(existingData, "data"); + if (existingArray != NULL && cJSON_GetArraySize(existingArray) > 0) + { + int existingCount = cJSON_GetArraySize(existingArray); + for (int i = 0; i < existingCount; i++) + { + cJSON *item = cJSON_GetArrayItem(existingArray, i); + cJSON_AddItemToArray(dataArray, cJSON_Duplicate(item, 1)); + } + } + cJSON_Delete(existingData); + free(jsonStr); + + // 3.添加新数据 + cJSON *sportData = cJSON_CreateObject(); + cJSON_AddNumberToObject(sportData, "sportTypeId", int(data->sportType)); // 训练项目类 Id + cJSON_AddNumberToObject(sportData, "sportId", data->sportId); // 训练项目 Id + cJSON_AddStringToObject(sportData, "startDate", (data->start_date).c_str()); // 训练开始日期字符串 + cJSON_AddNumberToObject(sportData, "startTime", data->starttime); // 训练开始时间戳 + cJSON_AddNumberToObject(sportData, "endTime", data->endtime); // 训练结束时间戳 + cJSON_AddNumberToObject(sportData, "goalDegree", data->goaldegree); // 训练目标完成度 + cJSON_AddNumberToObject(sportData, "duration", data->duration); // 训练时长(秒) + cJSON_AddNumberToObject(sportData, "distance", data->distance); // 训练距离 + cJSON_AddNumberToObject(sportData, "avgHeartRate", data->avgheart); // 训练平均心率 + cJSON_AddNumberToObject(sportData, "steps", data->steps); // 训练步数 + cJSON_AddNumberToObject(sportData, "intensity", data->intensity); // 训练强度 + cJSON_AddItemToArray(dataArray, sportData); + + // 4.限制存储数量,只保存最新的10条数据 + int dataCount = cJSON_GetArraySize(dataArray); + if (dataCount > MAX_DATA_COUNT) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "The training data has reached its maximum "); + cJSON_DeleteItemFromArray(dataArray, 0); + } + + cJSON_AddItemToObject(root, "data", dataArray); + + char *jsonStrNew = cJSON_Print(root); + cJSON_Delete(root); + + fseek(fp, 0, SEEK_SET); + fprintf(fp, "%s\n", jsonStrNew); + + fclose(fp); + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SaveDataToJson is succeed"); + return true; + } + /*读取json文件*/ + SportData *LoadDateFeomJson() + { + // 1.读取一条结构体信息 + // 2.在外部可以循环读取10条数据 + } } \ No newline at end of file