修改训练存储以及读取
authorCaisking <3449750381@qq.com>
Thu, 9 May 2024 08:03:11 +0000 (16:03 +0800)
committerCaisking <3449750381@qq.com>
Thu, 9 May 2024 08:03:11 +0000 (16:03 +0800)
code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp

index 4b99506..de3667a 100644 (file)
 #ifndef Traing_MODEL_H
 #define Traing_MODEL_H
 
-//#include "BaseView.h"
-//#include "View.h"
+// #include "BaseView.h"
+// #include "View.h"
 #include "string.h"
 #include "wearable_log.h"
 
-//#include "traing/include/TraingModel.h"
+// #include "traing/include/TraingModel.h"
 #include "traing/Training_UiConfig.h"
 #include "language/font/ui_resource_string.h"
 
@@ -30,7 +30,7 @@
 #define GILROY_BOLD_FONT "./font_library/gilroy-bold-4.ttf"
 #define GILROY_MEDIUM_FONT "./font_library/gilroy-medium-2.ttf"
 
-#define TRAINING_PATH "user/userdata/" //sport/TrainingData/"
+#define TRAINING_PATH "user/userdata/sport/TrainingData/"
 
 #ifdef __cplusplus
 extern "C"
@@ -279,7 +279,7 @@ namespace OHOS
         UIResourceTextId sportId_ = STR_EXTREME_SPORTS;     // 训练项目
         Exercisetype sportType_ = Exercisetype::FITNESS;    // 训练类型
         ExerciseState sportState_ = ExerciseState::General; // 训练状态
-        ExerciseTag sportTagType_ = ExerciseTag::UNKNOW;  // 训练目标类型
+        ExerciseTag sportTagType_ = ExerciseTag::UNKNOW;    // 训练目标类型
 
         int sportTag_ = 0; // 训练目标
         int totalHeartRate = 0;
index 4ca91e2..43fa100 100644 (file)
 #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
@@ -859,7 +873,78 @@ namespace OHOS
     }
 #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"
     {
@@ -877,68 +962,40 @@ namespace OHOS
             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
@@ -957,28 +1014,12 @@ namespace OHOS
                 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;
         }