return false;
}
+ // 解析json
+ std::vector<SportData> sportDataVector;
+ if(!LoadDataFormJson(sportDataVector))
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadDataFormJson is error");
+ file.close();
+ return false;
+ }
+
// 关闭文件
file.close();
-
- // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CheckDataJson is ok");
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CheckDataJson is ok");
return true;
}
#if 1
return true;
}
#endif
- /* 加载训练数据函数 */
- std::vector<SportData> TraingModel::LoadDataFormJson()
+ /* 加载训练数据函数 */
+ bool TraingModel::LoadDataFormJson(std::vector<SportData>& sportDataVector)
{
std::ifstream file(TRAINING_RECORD_FILE);
if (!file.is_open())
{
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to open file");
- return std::vector<SportData>();
+ return false;
}
std::string jsonData((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
cJSON *root = cJSON_Parse(jsonData.c_str());
+ if (root == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Failed to parse JSON: %s", cJSON_GetErrorPtr());
+ return false;
+ }
+
cJSON *dataArray = cJSON_GetObjectItem(root, "data");
+ if (dataArray == NULL ||!cJSON_IsArray(dataArray))
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid or missing 'data' array in JSON");
+ cJSON_Delete(root);
+ return false;
+ }
int dataCount = cJSON_GetArraySize(dataArray);
+ if (dataCount == 0)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "error dataCount is 0");
+ cJSON_Delete(root);
+ return false;
+ }
+
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "dataCount is %d", dataCount);
- std::vector<SportData> sportDataVector;
for (int i = 0; i < dataCount; i++)
{
cJSON *item = cJSON_GetArrayItem(dataArray, i);
+ if (item == NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid item at index %d", i);
+ continue; // 跳过无效项目
+ }
+
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.start_time = cJSON_GetObjectItem(item, "startTime")->valuestring;
- sportData.end_time = cJSON_GetObjectItem(item, "endTime")->valuestring;
- 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;
+ bool isValid = true; // 用于标记 sportData 是否有效
+
+ // 检查并获取每个字段,确保其存在且类型正确
+ if (cJSON *sportTypeIdItem = cJSON_GetObjectItem(item, "sportTypeId"); sportTypeIdItem && cJSON_IsNumber(sportTypeIdItem))
+ sportData.sportType = (Exercisetype)sportTypeIdItem->valueint;
+ else
+ isValid = false;
- sportData.apc = cJSON_GetObjectItem(item, "apc")->valueint;
- sportData.cdc = cJSON_GetObjectItem(item, "cdc")->valueint;
- sportData.std = cJSON_GetObjectItem(item, "std")->valueint;
+ if (cJSON *sportIdItem = cJSON_GetObjectItem(item, "sportId"); sportIdItem && cJSON_IsNumber(sportIdItem))
+ sportData.sportId = (UIResourceTextId)sportIdItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *startDateItem = cJSON_GetObjectItem(item, "startDate"); startDateItem && cJSON_IsString(startDateItem))
+ sportData.start_date = startDateItem->valuestring;
+ else
+ isValid = false;
- sportDataVector.push_back(sportData);
+ if (cJSON *startTimeItem = cJSON_GetObjectItem(item, "startTime"); startTimeItem && cJSON_IsString(startTimeItem))
+ sportData.start_time = startTimeItem->valuestring;
+ else
+ isValid = false;
+
+ if (cJSON *endTimeItem = cJSON_GetObjectItem(item, "endTime"); endTimeItem && cJSON_IsString(endTimeItem))
+ sportData.end_time = endTimeItem->valuestring;
+ else
+ isValid = false;
+
+ if (cJSON *goalDegreeItem = cJSON_GetObjectItem(item, "goalDegree"); goalDegreeItem && cJSON_IsNumber(goalDegreeItem))
+ sportData.goaldegree = goalDegreeItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *durationItem = cJSON_GetObjectItem(item, "duration"); durationItem && cJSON_IsNumber(durationItem))
+ sportData.duration = durationItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *distanceItem = cJSON_GetObjectItem(item, "distance"); distanceItem && cJSON_IsNumber(distanceItem))
+ sportData.distance = distanceItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *avgHeartRateItem = cJSON_GetObjectItem(item, "avgHeartRate"); avgHeartRateItem && cJSON_IsNumber(avgHeartRateItem))
+ sportData.avgheart = avgHeartRateItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *stepsItem = cJSON_GetObjectItem(item, "steps"); stepsItem && cJSON_IsNumber(stepsItem))
+ sportData.steps = stepsItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *intensityItem = cJSON_GetObjectItem(item, "intensity"); intensityItem && cJSON_IsNumber(intensityItem))
+ sportData.intensity = intensityItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *apcItem = cJSON_GetObjectItem(item, "apc"); apcItem && cJSON_IsNumber(apcItem))
+ sportData.apc = apcItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *cdcItem = cJSON_GetObjectItem(item, "cdc"); cdcItem && cJSON_IsNumber(cdcItem))
+ sportData.cdc = cdcItem->valueint;
+ else
+ isValid = false;
+
+ if (cJSON *stdItem = cJSON_GetObjectItem(item, "std"); stdItem && cJSON_IsNumber(stdItem))
+ sportData.std = stdItem->valueint;
+ else
+ isValid = false;
+
+ // 只有在所有字段都有效的情况下才添加 sportData 到向量中
+ if (isValid)
+ {
+ sportDataVector.push_back(sportData);
+ }
+ else
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Invalid data in item %d", i);
+ }
}
cJSON_Delete(root);
WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadDataFormJson is ok");
- return sportDataVector;
+ return true;
}
+
}
\ No newline at end of file