优化训练记录存储 270/2 270/head
authorCaisking <3449750381@qq.com>
Thu, 13 Jun 2024 04:00:35 +0000 (12:00 +0800)
committerCaisking <3449750381@qq.com>
Thu, 13 Jun 2024 04:00:35 +0000 (12:00 +0800)
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp
code/sdk/application/wearable/fd230app/nativeui/traingrecord/src/TraingRecordModel.cpp

index 9583194..e9b8b7f 100644 (file)
@@ -1154,9 +1154,16 @@ namespace OHOS
         // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CheckDataJson is ok");
         return true;
     }
-
+#if 0
     bool TraingModel::SaveDataToJson(SportData *data)
     {
+        if (data == nullptr)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "data is error");
+            return false;
+        }
+        printf_data(data);
+
         // 获取 JSON 文件,不存在则创建
         FILE *fp = fopen(TRAINING_RECORD_FILE, "r");
         if (fp == NULL)
@@ -1181,7 +1188,6 @@ namespace OHOS
             fclose(fp);
             return false;
         }
-
         fread(jsonStr, 1, fileSize, fp);
         fclose(fp);
 
@@ -1245,6 +1251,73 @@ namespace OHOS
         WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "SaveDataToJson succeeded");
         return true;
     }
+#endif
+    bool TraingModel::SaveDataToJson(SportData *data)
+    {
+        if (data == nullptr)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Data is null");
+            return false;
+        }
+
+        printf_data(data);
+
+        cJSON *root = cJSON_CreateObject(); // 创建一个新的 JSON 对象
+
+        // 创建或更新数据节点
+        cJSON *dataArray = cJSON_AddArrayToObject(root, "data");
+        cJSON *sportData = cJSON_CreateObject();
+        cJSON_AddNumberToObject(sportData, "sportTypeId", static_cast<int>(data->sportType));
+        cJSON_AddNumberToObject(sportData, "sportId", static_cast<int>(data->sportId));
+        cJSON_AddStringToObject(sportData, "startDate", data->start_date.c_str());
+        cJSON_AddStringToObject(sportData, "startTime", data->start_time.c_str());
+        cJSON_AddStringToObject(sportData, "endTime", data->end_time.c_str());
+        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);
+
+        // 限制存储数量并删除多余数据
+        while (cJSON_GetArraySize(dataArray) > MAX_DATA_COUNT)
+        {
+            cJSON_DeleteItemFromArray(dataArray, 0);
+        }
+
+        // 将更新后的 JSON 数据写入文件
+        char *jsonStrNew = cJSON_Print(root);
+        if (jsonStrNew == nullptr)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to serialize JSON data");
+            cJSON_Delete(root);
+            return false;
+        }
+
+        printf("jsonStrNew:%s\r\n", jsonStrNew);
+
+        FILE *fp = fopen(TRAINING_RECORD_FILE, "w");
+        if (fp == nullptr)
+        {
+            perror("fopen");
+            cJSON_Delete(root);
+            cJSON_free(jsonStrNew);
+            return false;
+        }
+
+        fwrite(jsonStrNew, strlen(jsonStrNew), 1, fp);
+        fclose(fp);
+
+        // 释放资源
+        cJSON_Delete(root);
+        cJSON_free(jsonStrNew);
+
+        WEARABLE_LOGI(WEARABLE_LOG_MODULE_APP, "SaveDataToJson succeeded");
+        return true;
+    }
+
     /* 加载训练数据函数 */
     std::vector<SportData> TraingModel::LoadDataFormJson()
     {
index 9bfcb29..9ede7dc 100644 (file)
@@ -1,3 +1,9 @@
+/*** 
+ * @Date: 2024-05-20 19:40:41
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-06-13 11:01:17
+ * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traingrecord/src/TraingRecordModel.cpp
+ */
 /***
  * @Date: 2024-05-20 19:40:41
  * @LastEditors: Caisking 3449750381@qq.com
@@ -58,15 +64,15 @@ namespace OHOS
         {
             ArrayNum = sportDataVector.size();
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "the ArrayNum is %d", ArrayNum);
-            for (int i = 0; i < ArrayNum && i < TraingRecordListMaxNum; i++) // 最多只有10条数据
+            for (int i = ArrayNum - 1, count = 0; i >= 0 && count < TraingRecordListMaxNum; i--, count++)
             {
                 SportData sportData = sportDataVector[i];
-                // 2.获取训练类
+                // 获取训练类图标
                 std::string imageicon = TraingModel::GetInstance()->GetSportIcon(sportData.sportType);
-                // 3.获取日期
-                // 4.获取时间
+                // 获取日期
+                // 获取时间
                 std::string Time = sportData.start_time + std::string("-") + sportData.end_time;
-                TraingRecords[i] = {i, imageicon.c_str(), sportData.start_date, Time};
+                TraingRecords[count] = {count, imageicon.c_str(), sportData.start_date, Time};
             }
         }
     }