增加训练json文件读取
authorCaisking <3449750381@qq.com>
Tue, 14 May 2024 10:05:54 +0000 (18:05 +0800)
committerCaisking <3449750381@qq.com>
Tue, 14 May 2024 10:05:54 +0000 (18:05 +0800)
code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp

index 21bfd02..fe8691c 100644 (file)
@@ -283,8 +283,8 @@ namespace OHOS
         int CalculateAverageHeartRate(int newHeartRate);
 
         /*保存训练记录到json文件*/
-        bool SaveDataToJson(SportData*data);
-        void LoadDateFeomJson();
+        bool SaveDataToJson(SportData *data);
+        SportData *LoadDateFeomJson();
 
     private:
         TraingModel(const TraingModel &) = delete;
index 38c20f8..a57bd35 100644 (file)
@@ -50,7 +50,7 @@
 #define TIMER 1
 #define BLETEST 0
 #define HARD_OFF 1
-#define READTEST 1
+#define READTEST 0
 
 #define MAX_HEART_RATE 220
 #define AGE_THRESHOLD 59
@@ -674,7 +674,34 @@ namespace OHOS
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SaveDataToJson is error!");
             return false;
         }
+/*json读取测试*/
+#if READTEST
+        SportData *sportDataArray = LoadDateFeomJson();
+        if (sportDataArray != nullptr)
+        {
+            int dataCount = 0;
+            for (int i = 0; i < MAX_DATA_COUNT; i++) // 最多只有10条数据
+            {
+                SportData sportData = sportDataArray[i];
+                printf("Sport type: %d\n", sportData.sportType);
+                printf("Sport ID: %d\n", sportData.sportId);
+                printf("Start date: %s\n", sportData.start_date.c_str());
+                printf("Start time: %d\n", sportData.starttime);
+                printf("End time: %d\n", sportData.endtime);
+                printf("Goal degree: %d\n", sportData.goaldegree);
+                printf("Duration: %d\n", sportData.duration);
+                printf("Distance: %d\n", sportData.distance);
+                printf("Average heart rate: %d\n", sportData.avgheart);
+                printf("Steps: %d\n", sportData.steps);
+                printf("Intensity: %d\n", sportData.intensity);
+
+                dataCount++;
+            }
 
+            delete[] sportDataArray; // 记得释放内存
+        }
+#endif
+/*bin 读取测试*/
 #if 0
         Trainingdata *readdata{nullptr};
         int i = 0;
@@ -1040,6 +1067,7 @@ namespace OHOS
 #endif
 
     /* -------------------------------------------------------------------------- */
+
     /* 保存训练数据到 json 文件 */
     bool TraingModel::SaveDataToJson(SportData *data)
     {
@@ -1117,10 +1145,45 @@ namespace OHOS
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SaveDataToJson  is succeed");
         return true;
     }
-    /*读取json文件*/
-    SportData *LoadDateFeomJson()
+    /* 加载训练数据函数 */
+    SportData *TraingModel::LoadDateFeomJson()
     {
-        // 1.读取一条结构体信息
-        // 2.在外部可以循环读取10条数据
+        std::ifstream file(TRAINING_RECORD_FILE);
+        if (!file.is_open())
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file");
+            return nullptr;
+        }
+
+        std::string jsonData((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
+        file.close();
+
+        cJSON *root = cJSON_Parse(jsonData.c_str());
+        cJSON *dataArray = cJSON_GetObjectItem(root, "data");
+
+        int dataCount = cJSON_GetArraySize(dataArray); //获取数组大小
+        SportData *sportDataArray = new SportData[dataCount];
+
+        for (int i = 0; i < dataCount; i++)
+        {
+            cJSON *item = cJSON_GetArrayItem(dataArray, i);
+            SportData sportData;
+            sportData.sportType = (Exercisetype)cJSON_GetObjectItem(item, "sportTypeId")->valueint;
+            sportData.sportId = (UIResourceTextId)cJSON_GetObjectItem(item, "sportId")->valueint;
+            sportData.start_date = cJSON_GetObjectItem(item, "startDate")->valuestring;
+            sportData.starttime = cJSON_GetObjectItem(item, "startTime")->valueint;
+            sportData.endtime = cJSON_GetObjectItem(item, "endTime")->valueint;
+            sportData.goaldegree = cJSON_GetObjectItem(item, "goalDegree")->valueint;
+            sportData.duration = cJSON_GetObjectItem(item, "duration")->valueint;
+            sportData.distance = cJSON_GetObjectItem(item, "distance")->valueint;
+            sportData.avgheart = cJSON_GetObjectItem(item, "avgHeartRate")->valueint;
+            sportData.steps = cJSON_GetObjectItem(item, "steps")->valueint;
+            sportData.intensity = cJSON_GetObjectItem(item, "intensity")->valueint;
+
+            sportDataArray[i] = sportData;
+        }
+
+        cJSON_Delete(root);
+        return sportDataArray;
     }
 }
\ No newline at end of file