#include <memory>
#include "bleEvent.h"
#include "power_display_service.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <filesystem>
+#include <algorithm>
+#include <fstream>
+#include <limits.h>
+#include <list>
+#include "samgr_lite.h"
+
+#define MAX_FILE_PATH_LENGTH 128
+#define TRAINING_DATA_DIRECTORY "user/userdata/sport/"
#define TIMER 1
#define BLETEST 0
}
#endif
#endif
-/* ---------------------------------- 数据存储 ---------------------------------- */
+ /* ---------------------------------- 数据存储 ---------------------------------- */
+ static int32 SaveTraingDataToFile(void)
+ {
+ time_t now = time(nullptr);
+ struct tm *timeinfo = localtime(&now);
+ char filename[20];
+ strftime(filename, sizeof(filename), "%Y%m%d.bin", timeinfo);
+
+ uint32 size = sizeof(Trainingdata);
+
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "SaveTraingDataToFile !!");
+
+ char filePath[20];
+ sprintf(filePath, "%s%s", TRAINING_DATA_DIRECTORY, filename);
+
+ std::ofstream fd(filePath, std::ios::binary);
+ if (!fd.is_open())
+ {
+ if (mkdir(TRAINING_DATA_DIRECTORY, S_IRWXU) != 0)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to create directory");
+ return 0;
+ }
+
+ fd.open(filePath, std::ios::binary);
+ if (!fd.is_open())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for write");
+ return 0;
+ }
+
+ WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Directory created successfully!!");
+ }
+
+ fd.write(reinterpret_cast<const char *>(&sportdata), size);
+ fd.close();
+ return 1;
+ }
+ static std::vector<Trainingdata> LoadTrainingDataFromTodayFile()
+ {
+ std::vector<Trainingdata> trainingRecords;
+
+ time_t now = time(nullptr);
+ struct tm *timeinfo = localtime(&now);
+ char filename[20];
+ strftime(filename, sizeof(filename), "%Y%m%d.bin", timeinfo);
+ // 找到训练文件夹里面的当天文件
+ char filePath[20];
+ sprintf(filePath, "%s%s", TRAINING_DATA_DIRECTORY, filename);
+
+ std::ifstream fd(filePath, std::ios::binary);
+ if (!fd.is_open())
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open today's file");
+ return trainingRecords; // Return an empty vector if file open fails
+ }
+
+ // 读取训练信息
+ while (!fd.eof())
+ {
+ Trainingdata record;
+ fd.read(reinterpret_cast<char *>(&record), sizeof(Trainingdata));
+ if (fd.gcount() == sizeof(Trainingdata))
+ {
+ trainingRecords.push_back(record);
+ }
+ }
+
+ fd.close();
+
+ return trainingRecords;
+ }
#ifdef __cplusplus
extern "C"
{
int num = size / 64;
return num;
}
-
- // 写数据
int write_Trainingdata_data(Trainingdata data)
{
- char file_path[128] = {0};
+ char file_path[MAX_FILE_PATH_LENGTH] = {0};
char timeday[12] = {0};
uapi_get_YYMMDD_Time(timeday, '_');
sprintf(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);
+ }
+
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%s", file_path);
FILE *fp = fopen(file_path, "ab+");
if (fp == NULL)
{
- // 未存储分配函数funcName的返回值
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file for append");
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 failur
+ return -1; // Return an error code to indicate file open failure
}
}
-#if HARD_OFF
int ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
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
}
-#else
- Trainingdata_hard hard = {0};
- fseek(fp, 0, SEEK_SET);
- int ret = fread((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
- if (ret == 0)
- {
- // Write new header if it doesn't exist
- hard.num = 1;
- hard.st = timeday;
- fseek(fp, 0, SEEK_SET);
- ret = fwrite((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
- if (ret != 1)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard error! %d", ret);
- }
- }
- else
- {
- fseek(fp, 0, SEEK_END);
- hard.num++;
- ret = fwrite((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
- if (ret != 1)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard error! %d", ret);
- }
- }
- fseek(fp, 0, SEEK_END);
- ret = fwrite((void *)&data, sizeof(Trainingdata), 1, fp);
- if (ret != 1)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_data error! %d", ret);
- }
-#endif
fclose(fp); // Close the file after writing data
return 0; // Return success code
free(data); // 修复内存泄漏
return NULL;
}
-#if HARD_OFF
fseek(fp, sizeof(Trainingdata) * offset, SEEK_SET);
fread((void *)data, sizeof(Trainingdata), 1, fp);
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d", data->src);
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d %d ", offset, sizeof(Trainingdata));
-#else
- fseek(fp, 0, SEEK_SET);
- Trainingdata_hard hard;
- int ret = fread((void *)&hard, sizeof(Trainingdata_hard), 1, fp);
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d", hard.num);
- if (ret == 0)
- {
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL");
- return NULL;
- }
- else
- {
- fread((void *)data, sizeof(Trainingdata), 1, fp);
- WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d", data->src);
- }
-#endif
fseek(fp, 0, SEEK_SET);
+
fclose(fp);
return data;
}