From: Caisking <3449750381@qq.com> Date: Sat, 12 Oct 2024 08:00:09 +0000 (+0800) Subject: 优化训练文件存储。增加错误存储提醒 X-Git-Tag: release_V1.2.70_20241020_135402~60^2~1^2~11 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=5a5a72c77755d80a52487b01d6f532fd83969380;p=mirrors%2FFD230.git 优化训练文件存储。增加错误存储提醒 --- 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 921cb2c75..cf111bee3 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp @@ -1063,6 +1063,9 @@ bool TraingModel::SaveSportData() { data.std = static_cast(sportdata->std); data.cdc = static_cast(sportdata->cdc); + // 创建sport文件夹 + CreateDirectoryIfNotExists(TRAINING_DATA_DIRECTORY); + // 保存数据到 bin if (write_Trainingdata_data(data) != 1) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "write_Trainingdata_data is error!"); @@ -1623,38 +1626,53 @@ static std::vector LoadTrainingDataFromTodayFile() { #ifdef __cplusplus extern "C" { #endif + +#include +#include +#include +#include +#include + // 获得数据个数 static int get_Trainingdata_data_num(FILE *fp) { - long size = 0; - if (fp != NULL) { - fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾 - size = ftell(fp); // 获取当前文件指针位置,即文件大小 - fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头 + if (fp == NULL) { + return 0; // 如果文件指针为空,返回0 } - int num = size / sizeof(Trainingdata); - return num; + + fseek(fp, 0, SEEK_END); // 将文件指针移动到文件末尾 + long size = ftell(fp); // 获取当前文件指针位置,即文件大小 + fseek(fp, 0, SEEK_SET); // 将文件指针移回文件开头 + + return (size / sizeof(Trainingdata)); } + +// 写训练bin文件 int write_Trainingdata_data(Trainingdata data) { char file_path[MAX_FILE_PATH_LENGTH] = {0}; char timeday[12] = {0}; uapi_get_YYMMDD_Time(timeday, '_'); - sprintf_s(file_path, sizeof(file_path), "%s%s.bin", TRAINING_PATH, - timeday); // 防止越界 + snprintf(file_path, sizeof(file_path), "%s%s.bin", TRAINING_PATH, timeday); - // Check if the directory exists, if not, create it + // 检查目录是否存在,如果不存在则创建 struct stat st = {0}; if (stat(TRAINING_PATH, &st) == -1) { - mkdir(TRAINING_PATH, 0700); + if (mkdir(TRAINING_PATH, 0700) != 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create directory: %s", + strerror(errno)); + return -1; // 返回错误码 + } } WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%s", file_path); FILE *fp = fopen(file_path, "ab+"); if (fp == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for append"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for append: %s", + strerror(errno)); fp = fopen(file_path, "wb+"); if (fp == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for write"); - return -1; // Return an error code to indicate file open failure + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to open file for write: %s", strerror(errno)); + return -1; // 返回错误码 } } @@ -1662,46 +1680,52 @@ int write_Trainingdata_data(Trainingdata data) { if (ret != 1) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret); fclose(fp); - return -1; // Return an error code on write failure + return -1; // 返回错误码 } - fclose(fp); // Close the file after writing data - return 1; // Return success code + fclose(fp); // 关闭文件 + return 1; // 返回成功码 } + // 读数据 Trainingdata *read_Trainingdata_data(char *timedata, uint16_t offset) { char file_path[128] = {0}; - sprintf_s(file_path, sizeof(file_path), "%s%s.bin", TRAINING_PATH, - timedata); // 防止越界 + snprintf(file_path, sizeof(file_path), "%s%s.bin", TRAINING_PATH, timedata); - FILE *fp = fopen(file_path, "rb+"); + FILE *fp = fopen(file_path, "rb"); if (fp == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for read: %s", + strerror(errno)); return NULL; } + Trainingdata *data = (Trainingdata *)malloc(sizeof(Trainingdata)); if (data == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Trainingdata malloc is error"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Failed to allocate memory for Trainingdata"); fclose(fp); return NULL; } - if (get_Trainingdata_data_num(fp) <= offset) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL %d", - get_Trainingdata_data_num(fp)); - free(data); // 修复内存泄漏 + + int num_data = get_Trainingdata_data_num(fp); + if (num_data <= offset) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Offset %d exceeds number of data %d", offset, num_data); + free(data); // 释放内存 fclose(fp); return NULL; } + fseek(fp, sizeof(Trainingdata) * offset, SEEK_SET); - fread((void *)data, sizeof(Trainingdata), 1, fp); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "%d", data->src); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "%d %d ", offset, + fread(data, sizeof(Trainingdata), 1, fp); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Data src: %d", data->src); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Offset: %d, Size: %zu", offset, sizeof(Trainingdata)); - fseek(fp, 0, SEEK_SET); - fclose(fp); - return data; + fclose(fp); // 关闭文件 + return data; // 返回读取的数据 } + #ifdef __cplusplus } #endif @@ -1743,8 +1767,6 @@ bool TraingModel::CheckDataJson() { bool TraingModel::SaveDataToJson(SportData *data) { FILE *fp; char *jsonStr = NULL; - // 创建sport文件夹 - CreateDirectoryIfNotExists(TRAINING_DATA_DIRECTORY); // 读取现有的 JSON 数据 fp = fopen(TRAINING_RECORD_FILE, "r");