From: Caisking Date: Fri, 10 Oct 2025 09:13:59 +0000 (+0800) Subject: 修复运动gps定时器的不停止 X-Git-Tag: V1.0.9_20251128_194606_LN~212^2~40 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=894ffd378d63e30fa2b5447792d0c614b1a0aaeb;p=mirrors%2FFD230.git 修复运动gps定时器的不停止 --- diff --git a/code/sdk/application/wearable/fd230/nativeapp/nativeui/traing/src/TraingModel.cpp b/code/sdk/application/wearable/fd230/nativeapp/nativeui/traing/src/TraingModel.cpp index 388b4a2ad..901ba7bb3 100644 --- a/code/sdk/application/wearable/fd230/nativeapp/nativeui/traing/src/TraingModel.cpp +++ b/code/sdk/application/wearable/fd230/nativeapp/nativeui/traing/src/TraingModel.cpp @@ -590,16 +590,16 @@ void TraingModel::AnalysisData(act_result_t *cyweeInfo) { #endif #if 0 - int avg_step_freq;//平均步频 - int avg_step_len;//平均步长 - int avg_pace;//平均配速 - int max_step_freq;//最大步频 - int max_pace;//最大配速 - int activity_zone;//活动程度区间 0低度 1中度 2高度 - int ativity_zone_moderate_duration;//中度活动时间 - int ativity_zone_high_duration;//高度活动时间 - int MHR_zone;//心率区间 - int distance_motion;//体感距离 + int avg_step_freq;//平均步频 + int avg_step_len;//平均步长 + int avg_pace;//平均配速 + int max_step_freq;//最大步频 + int max_pace;//最大配速 + int activity_zone;//活动程度区间 0低度 1中度 2高度 + int ativity_zone_moderate_duration;//中度活动时间 + int ativity_zone_high_duration;//高度活动时间 + int MHR_zone;//心率区间 + int distance_motion;//体感距离 #endif /*分析activity extra info数据*/ @@ -829,12 +829,13 @@ void TraingModel::SetTimestamp() { Startstate = false; Endstate = true; - stop_heart_gps(); // 停止心率和gps数据采集 + stop_heart_gps(); // 停止心率和gps数据采集 + gnss_stop_timer(); // 停止gps定时器 #if 0 - // 重置训练目标 [发生在训练前] - sportTag_ = 0; - sportTagType_ = ExerciseTag::UNKNOW; + // 重置训练目标 [发生在训练前] + sportTag_ = 0; + sportTagType_ = ExerciseTag::UNKNOW; #endif /*运动结束并且是手表发起的运动*/ if (Openingway == false) { @@ -1171,7 +1172,7 @@ int TraingModel::GetSportTagData() { //获取目标值 uint32_t TraingModel::GetCurrentSportTag() { if (sportdata->tag == 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return 0; } @@ -1205,7 +1206,7 @@ uint32_t TraingModel::GetCurrentSportTag() { //获取是否达到目标 使用比较 bool TraingModel::IsGetGoaldegree() { if (sportdata->tag == 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return false; } @@ -1288,7 +1289,7 @@ void TraingModel::CheckSportTag() { // 校验时间获取目标 void TraingModel::CheckTimeTag() { if (sportdata->tag == 0 || sportdata->sportTagType != ExerciseTag::Duration) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return; } if (sportdata->duration == current_goal && !Gettagstuats) { @@ -1480,16 +1481,16 @@ uint32_t TraingModel::GetSportTime() { return sportdata->duration; } /*获取训练量是否达标*/ bool TraingModel::GetMomentum() { #if 0 - int caldata = TraingModel::GetInstance()->GetCal(); - /*判断是否产生至少1Kcald的运动量*/ - if (caldata >= 1) - { - MomentumFlag = true; - } - else - { - MomentumFlag = false; - } + int caldata = TraingModel::GetInstance()->GetCal(); + /*判断是否产生至少1Kcald的运动量*/ + if (caldata >= 1) + { + MomentumFlag = true; + } + else + { + MomentumFlag = false; + } #endif int duration = sportdata->duration; //判断运动时间是否大于1分钟 @@ -2849,26 +2850,54 @@ void TraingModel::GetPointData() { // 添加经纬度数据集 void TraingModel::AddGPSData(uint32_t timestamp, double latitude, double longitude) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== AddGPSData函数调用开始 ==="); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "输入参数: timestamp=%u, latitude=%.6f, longitude=%.6f", + timestamp, latitude, longitude); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "当前GPS数据点数量: %zu", + gpsData.size()); + // 判断数据是否合理 if (latitude < -90 || latitude > 90 || longitude < -180 || longitude > 180) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Invalid GPS data: lat=%.6f, lon=%.6f", latitude, longitude); + "错误: GPS数据无效 - 纬度=%.6f (范围: -90~90), 经度=%.6f " + "(范围: -180~180)", + latitude, longitude); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== AddGPSData函数调用结束(数据无效) ==="); return; } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GPS数据有效性检查通过"); // 校验时间戳是否递增 if (!gpsData.empty() && timestamp <= gpsData.back().timestamp) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Timestamp not increasing: current=%u, last=%u", timestamp, - gpsData.back().timestamp); + "错误: 时间戳未递增 - 当前=%u, 上一个=%u, 差值=%d", timestamp, + gpsData.back().timestamp, + (int)timestamp - (int)gpsData.back().timestamp); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== AddGPSData函数调用结束(时间戳错误) ==="); return; } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "时间戳递增检查通过"); // 添加数据 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "准备添加GPS数据到容器..."); gpsData.push_back({timestamp, latitude, longitude}); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Added GPS data #%zu: ts=%u, (%.6f, %.6f)", gpsData.size(), - timestamp, latitude, longitude); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "成功添加GPS数据 #%zu: 时间戳=%u, 坐标=(%.6f, %.6f)", + gpsData.size(), timestamp, latitude, longitude); + + // 打印当前GPS数据统计信息 + if (gpsData.size() > 1) { + double lat_diff = latitude - gpsData[gpsData.size() - 2].latitude; + double lon_diff = longitude - gpsData[gpsData.size() - 2].longitude; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "与上一个点的距离差: 纬度差=%.6f, 经度差=%.6f", lat_diff, + lon_diff); + } + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== AddGPSData函数调用结束 ==="); } //保存canvas数据 @@ -3034,40 +3063,69 @@ std::string TraingModel::GetGPSDataPath(uint32_t timestamp) { bool TraingModel::GetGPSConnectState() { return gpsconnect; } //设置GPS连接状态 void TraingModel::SetGPSConnectState(bool state) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS连接状态设置 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前GPS连接状态: %d, 新状态: %d", + gpsconnect, state); + if (gpsconnect != state) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态发生变化: %d -> %d", + gpsconnect, state); gpsconnect = state; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态已更新为: %d", + gpsconnect); + //状态改变记录 // SystemLogModel::GetInstance()->save_gps_status_log_type(gpsconnect); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS状态变化记录完成"); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态未发生变化,保持: %d", + gpsconnect); } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS连接状态设置完成 ==="); } //运动心率、gps开启准备 void TraingModel::start_heart_gps() { if (gpsopen == false) { - // 开启gps -#if defined(SUPPORT_GNSS) - gnss_start(); -#endif + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始启动GPS定位服务..."); //开启心率检测 #ifdef SUPPORT_PPG_SERVICE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "启动心率检测服务"); ppg_hr_start(); +#endif + // 判断是否是需要GPS的运动 + if (IsNeedGPS(GetSportEvent()) == false) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "不需要GPS运动,跳过启动流程"); + return; + } + // 开启gps +#if defined(SUPPORT_GNSS) + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "调用gnss_start()启动GPS服务"); + gnss_start(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gnss_start()调用完成"); #endif gpsopen = true; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开启gps定位"); } } //运动心率、gps关闭 void TraingModel::stop_heart_gps() { if (gpsopen == true) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始关闭GPS定位服务..."); // 关闭gps #if defined(SUPPORT_GNSS) + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "调用gnss_stop()关闭GPS服务"); gnss_stop(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gnss_stop()调用完成"); #endif + //关闭心率检测 #ifdef SUPPORT_PPG_SERVICE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "关闭心率检测服务"); ppg_hr_stop(); #endif + gpsopen = false; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "关闭gps定位"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "GPS定位服务关闭完成,gpsopen设置为false"); } } @@ -3080,25 +3138,38 @@ void TraingModel::stop_heart_gps() { */ static osTimerId_t g_location_timer = nullptr; static int location_timer_interval = 1; // 位置数据获取间隔,单位秒 +static int gps_timer_count = 0; /*位置服务定时器到期时的处理*/ static void gnss_timer_callbackProc(int data) { /* GPS数据获取处理 + 1、目前gps注册有问题 ;需要延迟2s 去开启gps; */ + gps_timer_count++; + if (gps_timer_count < 2) { + return; + } + if (gps_timer_count == 2) { +#if defined(SUPPORT_GNSS) + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开启gps"); + gnss_start(); +#endif + } #if defined(SUPPORT_GNSS) gps_data_t send_gps_data = {0}; gps_data_t *gps_data = getPGPSGlobaldata(); if (gps_data == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gps_data == NULL"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "错误: gps_data == NULL,GPS全局数据获取失败"); return; } - // 判断是否为第一次定位并复制GPS数据 if (gps_data->last_gps_time_ms > 0) { memcpy_s(&send_gps_data, sizeof(gps_data_t), gps_data, sizeof(gps_data_t)); } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS时间戳无效,跳过本次处理"); return; } @@ -3107,8 +3178,12 @@ static void gnss_timer_callbackProc(int data) { TraingModel::GetInstance()->SetGPSConnectState(isGpsConnected); // 判断是否在运动或训练已结束 - if (uapi_dev_runtime_get_train_status() == 0 || - TraingModel::GetInstance()->GetSportState() == ExerciseState::End) { + int train_status = uapi_dev_runtime_get_train_status(); + ExerciseState sport_state = TraingModel::GetInstance()->GetSportState(); + + if (train_status == 0 || sport_state == ExerciseState::End) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "运动已结束或未开始,跳过GPS数据添加"); return; } @@ -3117,6 +3192,8 @@ static void gnss_timer_callbackProc(int data) { uint32_t time = TraingModel::GetInstance()->get_current_time(); TraingModel::GetInstance()->AddGPSData(time, send_gps_data.location.lat, send_gps_data.location.lng); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定位失败,跳过数据添加"); } #endif } @@ -3135,60 +3212,95 @@ static void gnss_timer_callback(int data) { * @param interval 时间间隔,单位ms */ void TraingModel::gnss_start_timer(uint32_t interval) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TraingModel::gnss_start_timer"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器启动流程开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "定时器间隔: %u ms", interval); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前定时器状态: %p", + g_location_timer); + /* -------------------------- 1.检查位置服务定时器是否已经存在,如果存在则返回 * ------------------------- */ if (g_location_timer != nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Location Timer already exists!!"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS定时器已存在,跳过创建"); return; } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器不存在,开始创建..."); /* --------------------------------- 2.创建位置服务定时器 * -------------------------------- */ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "调用osTimerNew创建GPS定时器..."); g_location_timer = osTimerNew((osTimerFunc_t)gnss_timer_callback, osTimerPeriodic, nullptr, nullptr); // 检查定时器创建是否成功 if (g_location_timer == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Location Timer create failed!!"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS定时器创建失败!!"); return; } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器创建成功: %p", + g_location_timer); /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 * ---------------------------- */ int tick = osMs2Tick(interval); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "时间转换: %u ms -> %d ticks", + interval, tick); /* --------------------------------- 4.启动位置服务定时器 * -------------------------------- */ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "启动GPS定时器..."); osStatus_t ret = osTimerStart(g_location_timer, tick); /* -------------------- 5.检查位置服务定时器启动是否成功,如果启动失败,删除定时器并置为 * nullptr -------------------- */ if (ret != osOK) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Location Timer start failed, ret = %lx!!", ret); + "错误: GPS定时器启动失败, ret = %lx!!", ret); if (g_location_timer != nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "删除失败的GPS定时器..."); ret = osTimerDelete(g_location_timer); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器删除结果: %lx", ret); } g_location_timer = nullptr; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== GPS定时器启动流程结束(失败) ==="); return; } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器启动成功"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器启动流程结束(成功) ==="); } /* * @brief 停止位置服务定时器 */ void TraingModel::gnss_stop_timer(void) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器停止流程开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前定时器状态: %p", + g_location_timer); + // 检查位置服务定时器是否存在 if (g_location_timer != nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingModel::gnss_stop_timer"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器存在,开始停止流程..."); + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "停止GPS定时器..."); osTimerStop(g_location_timer); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器已停止"); + // 删除定时器并置为 nullptr + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "删除GPS定时器..."); osStatus_t ret = osTimerDelete(g_location_timer); if (ret != osOK) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Location Timer delete failed, ret = %lx!!", ret); + "错误: GPS定时器删除失败, ret = %lx!!", ret); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器删除成功"); } g_location_timer = nullptr; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器指针已置空"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器停止流程结束 ==="); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器不存在,跳过停止流程"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== GPS定时器停止流程结束(无需停止) ==="); } + gps_timer_count = 0; } ///////////////////////////////////////运动数据采集定时器////////////////////////////////// @@ -3258,7 +3370,7 @@ static void sport_data_time_proc(int data) { TraingModel::GetInstance()->GetSportActivityData(); // 获取并处理运动额外数据 前提是特殊运动 - if (TraingModel::GetInstance()->GetIsSpecial()){ + if (TraingModel::GetInstance()->GetIsSpecial()) { TraingModel::GetInstance()->GetSportExtraData(); } @@ -3358,13 +3470,24 @@ bool get_training_heart_path_(char *path, uint32_t time, uint32_t max_len) { //获取gps轨迹路径 bool get_training_gps_path_(char *path, uint32_t time, uint32_t max_len) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 获取GPS轨迹路径开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "请求时间戳: %u, 最大路径长度: %u", + time, max_len); + std::string str = OHOS::TraingModel::GetInstance()->GetGPSDataPath(time); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get_training_gps:%s\r\n", - str.c_str()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "获取到的GPS路径: %s", str.c_str()); + if (str.length() <= 3) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS路径长度不足,长度: %zu", + str.length()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 获取GPS轨迹路径结束(失败) ==="); return false; } + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS路径长度: %zu", str.length()); memcpy_s(path, max_len, str.c_str(), str.length()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS路径复制完成"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 获取GPS轨迹路径结束(成功) ==="); return true; } @@ -3422,6 +3545,67 @@ char *struct_to_json_buf_malloc_(uint8_t day) { return json_format; } +int read_gps_data_(char *path, uint8_t *out_buf) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 读取GPS数据开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件路径: %s", path); + + FILE *fp = fopen(path, "rb+"); + if (fp == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: 无法打开GPS文件"); + return 0; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件打开成功"); + + uint32_t file_size = 0; + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + fseek(fp, 0, SEEK_SET); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件大小: %u 字节", file_size); + + int len = 0; + int point_count = 0; + OHOS::GPSData_t data = {0}; + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始读取GPS轨迹点..."); + while (1) { + int read_size = fread(&data, 1, sizeof(data), fp); + if (read_size == sizeof(data)) { + point_count++; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "读取GPS点 #%d: 时间戳=%u, 纬度=%.6f, 经度=%.6f", + point_count, data.timestamp, data.latitude, data.longitude); + + int temp_data = data.timestamp; + memcpy_s(out_buf + len, 4, &temp_data, sizeof(data.timestamp)); + len += 4; + + int temp_latitude = (int)(data.latitude * 10000000); + memcpy_s(out_buf + len, 4, &temp_latitude, sizeof(temp_latitude)); + len += 4; + + int temp_longitude = (int)(data.longitude * 10000000); + memcpy_s(out_buf + len, 4, &temp_longitude, sizeof(temp_longitude)); + len += 4; + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "GPS点数据转换: 时间戳=%u, 纬度=%d, 经度=%d", temp_data, + temp_latitude, temp_longitude); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件读取结束,读取大小: %d", + read_size); + break; + } + } + + fclose(fp); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件关闭"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "总共读取 %d 个GPS点,输出数据长度: %d 字节", point_count, len); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 读取GPS数据结束 ==="); + + return len; +} + extern bleInstruct_t_ bleInstruct_t; void ble_training_init(void) { @@ -3429,6 +3613,7 @@ void ble_training_init(void) { bleInstruct_t.get_training_heart_path_cb = get_training_heart_path_; bleInstruct_t.get_training_gps_path_cb = get_training_gps_path_; bleInstruct_t.struct_to_json_buf_malloc = struct_to_json_buf_malloc_; + bleInstruct_t.read_gps_data_cb = read_gps_data_; } #ifdef __cplusplus diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp index 388b4a2ad..901ba7bb3 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/traing/src/TraingModel.cpp @@ -590,16 +590,16 @@ void TraingModel::AnalysisData(act_result_t *cyweeInfo) { #endif #if 0 - int avg_step_freq;//平均步频 - int avg_step_len;//平均步长 - int avg_pace;//平均配速 - int max_step_freq;//最大步频 - int max_pace;//最大配速 - int activity_zone;//活动程度区间 0低度 1中度 2高度 - int ativity_zone_moderate_duration;//中度活动时间 - int ativity_zone_high_duration;//高度活动时间 - int MHR_zone;//心率区间 - int distance_motion;//体感距离 + int avg_step_freq;//平均步频 + int avg_step_len;//平均步长 + int avg_pace;//平均配速 + int max_step_freq;//最大步频 + int max_pace;//最大配速 + int activity_zone;//活动程度区间 0低度 1中度 2高度 + int ativity_zone_moderate_duration;//中度活动时间 + int ativity_zone_high_duration;//高度活动时间 + int MHR_zone;//心率区间 + int distance_motion;//体感距离 #endif /*分析activity extra info数据*/ @@ -829,12 +829,13 @@ void TraingModel::SetTimestamp() { Startstate = false; Endstate = true; - stop_heart_gps(); // 停止心率和gps数据采集 + stop_heart_gps(); // 停止心率和gps数据采集 + gnss_stop_timer(); // 停止gps定时器 #if 0 - // 重置训练目标 [发生在训练前] - sportTag_ = 0; - sportTagType_ = ExerciseTag::UNKNOW; + // 重置训练目标 [发生在训练前] + sportTag_ = 0; + sportTagType_ = ExerciseTag::UNKNOW; #endif /*运动结束并且是手表发起的运动*/ if (Openingway == false) { @@ -1171,7 +1172,7 @@ int TraingModel::GetSportTagData() { //获取目标值 uint32_t TraingModel::GetCurrentSportTag() { if (sportdata->tag == 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return 0; } @@ -1205,7 +1206,7 @@ uint32_t TraingModel::GetCurrentSportTag() { //获取是否达到目标 使用比较 bool TraingModel::IsGetGoaldegree() { if (sportdata->tag == 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return false; } @@ -1288,7 +1289,7 @@ void TraingModel::CheckSportTag() { // 校验时间获取目标 void TraingModel::CheckTimeTag() { if (sportdata->tag == 0 || sportdata->sportTagType != ExerciseTag::Duration) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return; } if (sportdata->duration == current_goal && !Gettagstuats) { @@ -1480,16 +1481,16 @@ uint32_t TraingModel::GetSportTime() { return sportdata->duration; } /*获取训练量是否达标*/ bool TraingModel::GetMomentum() { #if 0 - int caldata = TraingModel::GetInstance()->GetCal(); - /*判断是否产生至少1Kcald的运动量*/ - if (caldata >= 1) - { - MomentumFlag = true; - } - else - { - MomentumFlag = false; - } + int caldata = TraingModel::GetInstance()->GetCal(); + /*判断是否产生至少1Kcald的运动量*/ + if (caldata >= 1) + { + MomentumFlag = true; + } + else + { + MomentumFlag = false; + } #endif int duration = sportdata->duration; //判断运动时间是否大于1分钟 @@ -2849,26 +2850,54 @@ void TraingModel::GetPointData() { // 添加经纬度数据集 void TraingModel::AddGPSData(uint32_t timestamp, double latitude, double longitude) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== AddGPSData函数调用开始 ==="); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "输入参数: timestamp=%u, latitude=%.6f, longitude=%.6f", + timestamp, latitude, longitude); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "当前GPS数据点数量: %zu", + gpsData.size()); + // 判断数据是否合理 if (latitude < -90 || latitude > 90 || longitude < -180 || longitude > 180) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Invalid GPS data: lat=%.6f, lon=%.6f", latitude, longitude); + "错误: GPS数据无效 - 纬度=%.6f (范围: -90~90), 经度=%.6f " + "(范围: -180~180)", + latitude, longitude); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== AddGPSData函数调用结束(数据无效) ==="); return; } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GPS数据有效性检查通过"); // 校验时间戳是否递增 if (!gpsData.empty() && timestamp <= gpsData.back().timestamp) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Timestamp not increasing: current=%u, last=%u", timestamp, - gpsData.back().timestamp); + "错误: 时间戳未递增 - 当前=%u, 上一个=%u, 差值=%d", timestamp, + gpsData.back().timestamp, + (int)timestamp - (int)gpsData.back().timestamp); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== AddGPSData函数调用结束(时间戳错误) ==="); return; } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "时间戳递增检查通过"); // 添加数据 + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "准备添加GPS数据到容器..."); gpsData.push_back({timestamp, latitude, longitude}); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Added GPS data #%zu: ts=%u, (%.6f, %.6f)", gpsData.size(), - timestamp, latitude, longitude); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "成功添加GPS数据 #%zu: 时间戳=%u, 坐标=(%.6f, %.6f)", + gpsData.size(), timestamp, latitude, longitude); + + // 打印当前GPS数据统计信息 + if (gpsData.size() > 1) { + double lat_diff = latitude - gpsData[gpsData.size() - 2].latitude; + double lon_diff = longitude - gpsData[gpsData.size() - 2].longitude; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "与上一个点的距离差: 纬度差=%.6f, 经度差=%.6f", lat_diff, + lon_diff); + } + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== AddGPSData函数调用结束 ==="); } //保存canvas数据 @@ -3034,40 +3063,69 @@ std::string TraingModel::GetGPSDataPath(uint32_t timestamp) { bool TraingModel::GetGPSConnectState() { return gpsconnect; } //设置GPS连接状态 void TraingModel::SetGPSConnectState(bool state) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS连接状态设置 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前GPS连接状态: %d, 新状态: %d", + gpsconnect, state); + if (gpsconnect != state) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态发生变化: %d -> %d", + gpsconnect, state); gpsconnect = state; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态已更新为: %d", + gpsconnect); + //状态改变记录 // SystemLogModel::GetInstance()->save_gps_status_log_type(gpsconnect); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS状态变化记录完成"); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态未发生变化,保持: %d", + gpsconnect); } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS连接状态设置完成 ==="); } //运动心率、gps开启准备 void TraingModel::start_heart_gps() { if (gpsopen == false) { - // 开启gps -#if defined(SUPPORT_GNSS) - gnss_start(); -#endif + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始启动GPS定位服务..."); //开启心率检测 #ifdef SUPPORT_PPG_SERVICE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "启动心率检测服务"); ppg_hr_start(); +#endif + // 判断是否是需要GPS的运动 + if (IsNeedGPS(GetSportEvent()) == false) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "不需要GPS运动,跳过启动流程"); + return; + } + // 开启gps +#if defined(SUPPORT_GNSS) + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "调用gnss_start()启动GPS服务"); + gnss_start(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gnss_start()调用完成"); #endif gpsopen = true; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开启gps定位"); } } //运动心率、gps关闭 void TraingModel::stop_heart_gps() { if (gpsopen == true) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始关闭GPS定位服务..."); // 关闭gps #if defined(SUPPORT_GNSS) + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "调用gnss_stop()关闭GPS服务"); gnss_stop(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gnss_stop()调用完成"); #endif + //关闭心率检测 #ifdef SUPPORT_PPG_SERVICE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "关闭心率检测服务"); ppg_hr_stop(); #endif + gpsopen = false; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "关闭gps定位"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "GPS定位服务关闭完成,gpsopen设置为false"); } } @@ -3080,25 +3138,38 @@ void TraingModel::stop_heart_gps() { */ static osTimerId_t g_location_timer = nullptr; static int location_timer_interval = 1; // 位置数据获取间隔,单位秒 +static int gps_timer_count = 0; /*位置服务定时器到期时的处理*/ static void gnss_timer_callbackProc(int data) { /* GPS数据获取处理 + 1、目前gps注册有问题 ;需要延迟2s 去开启gps; */ + gps_timer_count++; + if (gps_timer_count < 2) { + return; + } + if (gps_timer_count == 2) { +#if defined(SUPPORT_GNSS) + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开启gps"); + gnss_start(); +#endif + } #if defined(SUPPORT_GNSS) gps_data_t send_gps_data = {0}; gps_data_t *gps_data = getPGPSGlobaldata(); if (gps_data == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gps_data == NULL"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "错误: gps_data == NULL,GPS全局数据获取失败"); return; } - // 判断是否为第一次定位并复制GPS数据 if (gps_data->last_gps_time_ms > 0) { memcpy_s(&send_gps_data, sizeof(gps_data_t), gps_data, sizeof(gps_data_t)); } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS时间戳无效,跳过本次处理"); return; } @@ -3107,8 +3178,12 @@ static void gnss_timer_callbackProc(int data) { TraingModel::GetInstance()->SetGPSConnectState(isGpsConnected); // 判断是否在运动或训练已结束 - if (uapi_dev_runtime_get_train_status() == 0 || - TraingModel::GetInstance()->GetSportState() == ExerciseState::End) { + int train_status = uapi_dev_runtime_get_train_status(); + ExerciseState sport_state = TraingModel::GetInstance()->GetSportState(); + + if (train_status == 0 || sport_state == ExerciseState::End) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "运动已结束或未开始,跳过GPS数据添加"); return; } @@ -3117,6 +3192,8 @@ static void gnss_timer_callbackProc(int data) { uint32_t time = TraingModel::GetInstance()->get_current_time(); TraingModel::GetInstance()->AddGPSData(time, send_gps_data.location.lat, send_gps_data.location.lng); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定位失败,跳过数据添加"); } #endif } @@ -3135,60 +3212,95 @@ static void gnss_timer_callback(int data) { * @param interval 时间间隔,单位ms */ void TraingModel::gnss_start_timer(uint32_t interval) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TraingModel::gnss_start_timer"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器启动流程开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "定时器间隔: %u ms", interval); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前定时器状态: %p", + g_location_timer); + /* -------------------------- 1.检查位置服务定时器是否已经存在,如果存在则返回 * ------------------------- */ if (g_location_timer != nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Location Timer already exists!!"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS定时器已存在,跳过创建"); return; } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器不存在,开始创建..."); /* --------------------------------- 2.创建位置服务定时器 * -------------------------------- */ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "调用osTimerNew创建GPS定时器..."); g_location_timer = osTimerNew((osTimerFunc_t)gnss_timer_callback, osTimerPeriodic, nullptr, nullptr); // 检查定时器创建是否成功 if (g_location_timer == nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Location Timer create failed!!"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS定时器创建失败!!"); return; } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器创建成功: %p", + g_location_timer); /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 * ---------------------------- */ int tick = osMs2Tick(interval); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "时间转换: %u ms -> %d ticks", + interval, tick); /* --------------------------------- 4.启动位置服务定时器 * -------------------------------- */ + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "启动GPS定时器..."); osStatus_t ret = osTimerStart(g_location_timer, tick); /* -------------------- 5.检查位置服务定时器启动是否成功,如果启动失败,删除定时器并置为 * nullptr -------------------- */ if (ret != osOK) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Location Timer start failed, ret = %lx!!", ret); + "错误: GPS定时器启动失败, ret = %lx!!", ret); if (g_location_timer != nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "删除失败的GPS定时器..."); ret = osTimerDelete(g_location_timer); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器删除结果: %lx", ret); } g_location_timer = nullptr; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== GPS定时器启动流程结束(失败) ==="); return; } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器启动成功"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器启动流程结束(成功) ==="); } /* * @brief 停止位置服务定时器 */ void TraingModel::gnss_stop_timer(void) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器停止流程开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前定时器状态: %p", + g_location_timer); + // 检查位置服务定时器是否存在 if (g_location_timer != nullptr) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingModel::gnss_stop_timer"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器存在,开始停止流程..."); + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "停止GPS定时器..."); osTimerStop(g_location_timer); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器已停止"); + // 删除定时器并置为 nullptr + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "删除GPS定时器..."); osStatus_t ret = osTimerDelete(g_location_timer); if (ret != osOK) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Location Timer delete failed, ret = %lx!!", ret); + "错误: GPS定时器删除失败, ret = %lx!!", ret); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器删除成功"); } g_location_timer = nullptr; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器指针已置空"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器停止流程结束 ==="); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器不存在,跳过停止流程"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== GPS定时器停止流程结束(无需停止) ==="); } + gps_timer_count = 0; } ///////////////////////////////////////运动数据采集定时器////////////////////////////////// @@ -3258,7 +3370,7 @@ static void sport_data_time_proc(int data) { TraingModel::GetInstance()->GetSportActivityData(); // 获取并处理运动额外数据 前提是特殊运动 - if (TraingModel::GetInstance()->GetIsSpecial()){ + if (TraingModel::GetInstance()->GetIsSpecial()) { TraingModel::GetInstance()->GetSportExtraData(); } @@ -3358,13 +3470,24 @@ bool get_training_heart_path_(char *path, uint32_t time, uint32_t max_len) { //获取gps轨迹路径 bool get_training_gps_path_(char *path, uint32_t time, uint32_t max_len) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 获取GPS轨迹路径开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "请求时间戳: %u, 最大路径长度: %u", + time, max_len); + std::string str = OHOS::TraingModel::GetInstance()->GetGPSDataPath(time); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get_training_gps:%s\r\n", - str.c_str()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "获取到的GPS路径: %s", str.c_str()); + if (str.length() <= 3) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS路径长度不足,长度: %zu", + str.length()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 获取GPS轨迹路径结束(失败) ==="); return false; } + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS路径长度: %zu", str.length()); memcpy_s(path, max_len, str.c_str(), str.length()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS路径复制完成"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 获取GPS轨迹路径结束(成功) ==="); return true; } @@ -3422,6 +3545,67 @@ char *struct_to_json_buf_malloc_(uint8_t day) { return json_format; } +int read_gps_data_(char *path, uint8_t *out_buf) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 读取GPS数据开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件路径: %s", path); + + FILE *fp = fopen(path, "rb+"); + if (fp == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: 无法打开GPS文件"); + return 0; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件打开成功"); + + uint32_t file_size = 0; + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + fseek(fp, 0, SEEK_SET); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件大小: %u 字节", file_size); + + int len = 0; + int point_count = 0; + OHOS::GPSData_t data = {0}; + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始读取GPS轨迹点..."); + while (1) { + int read_size = fread(&data, 1, sizeof(data), fp); + if (read_size == sizeof(data)) { + point_count++; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "读取GPS点 #%d: 时间戳=%u, 纬度=%.6f, 经度=%.6f", + point_count, data.timestamp, data.latitude, data.longitude); + + int temp_data = data.timestamp; + memcpy_s(out_buf + len, 4, &temp_data, sizeof(data.timestamp)); + len += 4; + + int temp_latitude = (int)(data.latitude * 10000000); + memcpy_s(out_buf + len, 4, &temp_latitude, sizeof(temp_latitude)); + len += 4; + + int temp_longitude = (int)(data.longitude * 10000000); + memcpy_s(out_buf + len, 4, &temp_longitude, sizeof(temp_longitude)); + len += 4; + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "GPS点数据转换: 时间戳=%u, 纬度=%d, 经度=%d", temp_data, + temp_latitude, temp_longitude); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件读取结束,读取大小: %d", + read_size); + break; + } + } + + fclose(fp); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件关闭"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "总共读取 %d 个GPS点,输出数据长度: %d 字节", point_count, len); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 读取GPS数据结束 ==="); + + return len; +} + extern bleInstruct_t_ bleInstruct_t; void ble_training_init(void) { @@ -3429,6 +3613,7 @@ void ble_training_init(void) { bleInstruct_t.get_training_heart_path_cb = get_training_heart_path_; bleInstruct_t.get_training_gps_path_cb = get_training_gps_path_; bleInstruct_t.struct_to_json_buf_malloc = struct_to_json_buf_malloc_; + bleInstruct_t.read_gps_data_cb = read_gps_data_; } #ifdef __cplusplus diff --git a/code/sdk/application/wearable/n9/nativeapp/nativeui/traing/src/TraingModel.cpp b/code/sdk/application/wearable/n9/nativeapp/nativeui/traing/src/TraingModel.cpp index 30f0b26d7..901ba7bb3 100644 --- a/code/sdk/application/wearable/n9/nativeapp/nativeui/traing/src/TraingModel.cpp +++ b/code/sdk/application/wearable/n9/nativeapp/nativeui/traing/src/TraingModel.cpp @@ -590,16 +590,16 @@ void TraingModel::AnalysisData(act_result_t *cyweeInfo) { #endif #if 0 - int avg_step_freq;//平均步频 - int avg_step_len;//平均步长 - int avg_pace;//平均配速 - int max_step_freq;//最大步频 - int max_pace;//最大配速 - int activity_zone;//活动程度区间 0低度 1中度 2高度 - int ativity_zone_moderate_duration;//中度活动时间 - int ativity_zone_high_duration;//高度活动时间 - int MHR_zone;//心率区间 - int distance_motion;//体感距离 + int avg_step_freq;//平均步频 + int avg_step_len;//平均步长 + int avg_pace;//平均配速 + int max_step_freq;//最大步频 + int max_pace;//最大配速 + int activity_zone;//活动程度区间 0低度 1中度 2高度 + int ativity_zone_moderate_duration;//中度活动时间 + int ativity_zone_high_duration;//高度活动时间 + int MHR_zone;//心率区间 + int distance_motion;//体感距离 #endif /*分析activity extra info数据*/ @@ -829,12 +829,13 @@ void TraingModel::SetTimestamp() { Startstate = false; Endstate = true; - stop_heart_gps(); // 停止心率和gps数据采集 + stop_heart_gps(); // 停止心率和gps数据采集 + gnss_stop_timer(); // 停止gps定时器 #if 0 - // 重置训练目标 [发生在训练前] - sportTag_ = 0; - sportTagType_ = ExerciseTag::UNKNOW; + // 重置训练目标 [发生在训练前] + sportTag_ = 0; + sportTagType_ = ExerciseTag::UNKNOW; #endif /*运动结束并且是手表发起的运动*/ if (Openingway == false) { @@ -1171,7 +1172,7 @@ int TraingModel::GetSportTagData() { //获取目标值 uint32_t TraingModel::GetCurrentSportTag() { if (sportdata->tag == 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return 0; } @@ -1205,7 +1206,7 @@ uint32_t TraingModel::GetCurrentSportTag() { //获取是否达到目标 使用比较 bool TraingModel::IsGetGoaldegree() { if (sportdata->tag == 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return false; } @@ -1288,7 +1289,7 @@ void TraingModel::CheckSportTag() { // 校验时间获取目标 void TraingModel::CheckTimeTag() { if (sportdata->tag == 0 || sportdata->sportTagType != ExerciseTag::Duration) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "sportdata->tag is 0"); return; } if (sportdata->duration == current_goal && !Gettagstuats) { @@ -1480,16 +1481,16 @@ uint32_t TraingModel::GetSportTime() { return sportdata->duration; } /*获取训练量是否达标*/ bool TraingModel::GetMomentum() { #if 0 - int caldata = TraingModel::GetInstance()->GetCal(); - /*判断是否产生至少1Kcald的运动量*/ - if (caldata >= 1) - { - MomentumFlag = true; - } - else - { - MomentumFlag = false; - } + int caldata = TraingModel::GetInstance()->GetCal(); + /*判断是否产生至少1Kcald的运动量*/ + if (caldata >= 1) + { + MomentumFlag = true; + } + else + { + MomentumFlag = false; + } #endif int duration = sportdata->duration; //判断运动时间是否大于1分钟 @@ -2850,16 +2851,20 @@ void TraingModel::GetPointData() { void TraingModel::AddGPSData(uint32_t timestamp, double latitude, double longitude) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== AddGPSData函数调用开始 ==="); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "输入参数: timestamp=%u, latitude=%.6f, longitude=%.6f", + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "输入参数: timestamp=%u, latitude=%.6f, longitude=%.6f", timestamp, latitude, longitude); - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "当前GPS数据点数量: %zu", gpsData.size()); - + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "当前GPS数据点数量: %zu", + gpsData.size()); + // 判断数据是否合理 if (latitude < -90 || latitude > 90 || longitude < -180 || longitude > 180) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "错误: GPS数据无效 - 纬度=%.6f (范围: -90~90), 经度=%.6f (范围: -180~180)", + "错误: GPS数据无效 - 纬度=%.6f (范围: -90~90), 经度=%.6f " + "(范围: -180~180)", latitude, longitude); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== AddGPSData函数调用结束(数据无效) ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== AddGPSData函数调用结束(数据无效) ==="); return; } WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "GPS数据有效性检查通过"); @@ -2867,10 +2872,11 @@ void TraingModel::AddGPSData(uint32_t timestamp, double latitude, // 校验时间戳是否递增 if (!gpsData.empty() && timestamp <= gpsData.back().timestamp) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "错误: 时间戳未递增 - 当前=%u, 上一个=%u, 差值=%d", - timestamp, gpsData.back().timestamp, + "错误: 时间戳未递增 - 当前=%u, 上一个=%u, 差值=%d", timestamp, + gpsData.back().timestamp, (int)timestamp - (int)gpsData.back().timestamp); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== AddGPSData函数调用结束(时间戳错误) ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== AddGPSData函数调用结束(时间戳错误) ==="); return; } WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "时间戳递增检查通过"); @@ -2879,17 +2885,18 @@ void TraingModel::AddGPSData(uint32_t timestamp, double latitude, WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "准备添加GPS数据到容器..."); gpsData.push_back({timestamp, latitude, longitude}); WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, - "成功添加GPS数据 #%zu: 时间戳=%u, 坐标=(%.6f, %.6f)", + "成功添加GPS数据 #%zu: 时间戳=%u, 坐标=(%.6f, %.6f)", gpsData.size(), timestamp, latitude, longitude); - + // 打印当前GPS数据统计信息 if (gpsData.size() > 1) { - double lat_diff = latitude - gpsData[gpsData.size()-2].latitude; - double lon_diff = longitude - gpsData[gpsData.size()-2].longitude; - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "与上一个点的距离差: 纬度差=%.6f, 经度差=%.6f", - lat_diff, lon_diff); + double lat_diff = latitude - gpsData[gpsData.size() - 2].latitude; + double lon_diff = longitude - gpsData[gpsData.size() - 2].longitude; + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, + "与上一个点的距离差: 纬度差=%.6f, 经度差=%.6f", lat_diff, + lon_diff); } - + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== AddGPSData函数调用结束 ==="); } @@ -3057,84 +3064,68 @@ bool TraingModel::GetGPSConnectState() { return gpsconnect; } //设置GPS连接状态 void TraingModel::SetGPSConnectState(bool state) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS连接状态设置 ==="); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前GPS连接状态: %d, 新状态: %d", gpsconnect, state); - + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前GPS连接状态: %d, 新状态: %d", + gpsconnect, state); + if (gpsconnect != state) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态发生变化: %d -> %d", gpsconnect, state); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态发生变化: %d -> %d", + gpsconnect, state); gpsconnect = state; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态已更新为: %d", gpsconnect); - + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态已更新为: %d", + gpsconnect); + //状态改变记录 // SystemLogModel::GetInstance()->save_gps_status_log_type(gpsconnect); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS状态变化记录完成"); } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态未发生变化,保持: %d", gpsconnect); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态未发生变化,保持: %d", + gpsconnect); } WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS连接状态设置完成 ==="); } //运动心率、gps开启准备 void TraingModel::start_heart_gps() { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS启动流程开始 ==="); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前GPS状态: gpsopen=%d", gpsopen); - if (gpsopen == false) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始启动GPS定位服务..."); - + //开启心率检测 +#ifdef SUPPORT_PPG_SERVICE + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "启动心率检测服务"); + ppg_hr_start(); +#endif + // 判断是否是需要GPS的运动 + if (IsNeedGPS(GetSportEvent()) == false) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "不需要GPS运动,跳过启动流程"); + return; + } // 开启gps #if defined(SUPPORT_GNSS) WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "调用gnss_start()启动GPS服务"); gnss_start(); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gnss_start()调用完成"); -#else - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "警告: SUPPORT_GNSS未定义,GPS服务不可用"); -#endif - - //开启心率检测 -#ifdef SUPPORT_PPG_SERVICE - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "启动心率检测服务"); - ppg_hr_start(); -#else - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SUPPORT_PPG_SERVICE未定义,跳过心率检测"); #endif - gpsopen = true; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定位服务启动完成,gpsopen设置为true"); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS启动流程结束 ==="); - } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS已经处于开启状态,跳过启动流程"); } } //运动心率、gps关闭 void TraingModel::stop_heart_gps() { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS关闭流程开始 ==="); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前GPS状态: gpsopen=%d", gpsopen); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前GPS数据点数量: %zu", gpsData.size()); - if (gpsopen == true) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始关闭GPS定位服务..."); - // 关闭gps #if defined(SUPPORT_GNSS) WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "调用gnss_stop()关闭GPS服务"); gnss_stop(); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "gnss_stop()调用完成"); -#else - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "警告: SUPPORT_GNSS未定义,GPS服务不可用"); #endif - + //关闭心率检测 #ifdef SUPPORT_PPG_SERVICE WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "关闭心率检测服务"); ppg_hr_stop(); -#else - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "SUPPORT_PPG_SERVICE未定义,跳过心率检测关闭"); #endif - + gpsopen = false; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定位服务关闭完成,gpsopen设置为false"); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS关闭流程结束 ==="); - } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS已经处于关闭状态,跳过关闭流程"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "GPS定位服务关闭完成,gpsopen设置为false"); } } @@ -3147,72 +3138,64 @@ void TraingModel::stop_heart_gps() { */ static osTimerId_t g_location_timer = nullptr; static int location_timer_interval = 1; // 位置数据获取间隔,单位秒 +static int gps_timer_count = 0; /*位置服务定时器到期时的处理*/ static void gnss_timer_callbackProc(int data) { /* GPS数据获取处理 + 1、目前gps注册有问题 ;需要延迟2s 去开启gps; */ - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器回调开始 ==="); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "定时器数据: %d", data); - + gps_timer_count++; + if (gps_timer_count < 2) { + return; + } + if (gps_timer_count == 2) { +#if defined(SUPPORT_GNSS) + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开启gps"); + gnss_start(); +#endif + } #if defined(SUPPORT_GNSS) - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始获取GPS全局数据..."); gps_data_t send_gps_data = {0}; gps_data_t *gps_data = getPGPSGlobaldata(); if (gps_data == NULL) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: gps_data == NULL,GPS全局数据获取失败"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "错误: gps_data == NULL,GPS全局数据获取失败"); return; } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS全局数据获取成功"); - // 判断是否为第一次定位并复制GPS数据 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "检查GPS时间戳: last_gps_time_ms=%llu", gps_data->last_gps_time_ms); if (gps_data->last_gps_time_ms > 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS时间戳有效,复制GPS数据"); memcpy_s(&send_gps_data, sizeof(gps_data_t), gps_data, sizeof(gps_data_t)); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS数据复制完成,状态=%d, 纬度=%.6f, 经度=%.6f", - send_gps_data.status, send_gps_data.location.lat, send_gps_data.location.lng); } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS时间戳无效,跳过本次处理"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS时间戳无效,跳过本次处理"); return; } // 判断是否定位成功并设置连接状态 bool isGpsConnected = (send_gps_data.status > 1); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS连接状态检查: status=%d, isGpsConnected=%d", - send_gps_data.status, isGpsConnected); TraingModel::GetInstance()->SetGPSConnectState(isGpsConnected); // 判断是否在运动或训练已结束 int train_status = uapi_dev_runtime_get_train_status(); ExerciseState sport_state = TraingModel::GetInstance()->GetSportState(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "运动状态检查: train_status=%d, sport_state=%d", - train_status, (int)sport_state); - + if (train_status == 0 || sport_state == ExerciseState::End) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "运动已结束或未开始,跳过GPS数据添加"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "运动已结束或未开始,跳过GPS数据添加"); return; } // 如果定位成功,添加经纬度数据 if (isGpsConnected) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定位成功,准备添加轨迹数据"); uint32_t time = TraingModel::GetInstance()->get_current_time(); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前时间戳: %u", time); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "添加GPS数据: 纬度=%.6f, 经度=%.6f", - send_gps_data.location.lat, send_gps_data.location.lng); TraingModel::GetInstance()->AddGPSData(time, send_gps_data.location.lat, send_gps_data.location.lng); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS数据添加完成"); } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定位失败,跳过数据添加"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定位失败,跳过数据添加"); } -#else - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "警告: SUPPORT_GNSS未定义,GPS功能不可用"); #endif - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器回调结束 ==="); } /* @@ -3231,8 +3214,9 @@ static void gnss_timer_callback(int data) { void TraingModel::gnss_start_timer(uint32_t interval) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器启动流程开始 ==="); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "定时器间隔: %u ms", interval); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前定时器状态: %p", g_location_timer); - + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前定时器状态: %p", + g_location_timer); + /* -------------------------- 1.检查位置服务定时器是否已经存在,如果存在则返回 * ------------------------- */ if (g_location_timer != nullptr) { @@ -3251,12 +3235,14 @@ void TraingModel::gnss_start_timer(uint32_t interval) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS定时器创建失败!!"); return; } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器创建成功: %p", g_location_timer); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器创建成功: %p", + g_location_timer); /* ----------------------------- 3.将毫秒转换为系统时钟节拍数 * ---------------------------- */ int tick = osMs2Tick(interval); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "时间转换: %u ms -> %d ticks", interval, tick); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "时间转换: %u ms -> %d ticks", + interval, tick); /* --------------------------------- 4.启动位置服务定时器 * -------------------------------- */ @@ -3273,7 +3259,8 @@ void TraingModel::gnss_start_timer(uint32_t interval) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器删除结果: %lx", ret); } g_location_timer = nullptr; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器启动流程结束(失败) ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== GPS定时器启动流程结束(失败) ==="); return; } WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器启动成功"); @@ -3285,16 +3272,17 @@ void TraingModel::gnss_start_timer(uint32_t interval) { */ void TraingModel::gnss_stop_timer(void) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器停止流程开始 ==="); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前定时器状态: %p", g_location_timer); - + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前定时器状态: %p", + g_location_timer); + // 检查位置服务定时器是否存在 if (g_location_timer != nullptr) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器存在,开始停止流程..."); - + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "停止GPS定时器..."); osTimerStop(g_location_timer); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器已停止"); - + // 删除定时器并置为 nullptr WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "删除GPS定时器..."); osStatus_t ret = osTimerDelete(g_location_timer); @@ -3309,8 +3297,10 @@ void TraingModel::gnss_stop_timer(void) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器停止流程结束 ==="); } else { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS定时器不存在,跳过停止流程"); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== GPS定时器停止流程结束(无需停止) ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "=== GPS定时器停止流程结束(无需停止) ==="); } + gps_timer_count = 0; } ///////////////////////////////////////运动数据采集定时器////////////////////////////////// @@ -3380,7 +3370,7 @@ static void sport_data_time_proc(int data) { TraingModel::GetInstance()->GetSportActivityData(); // 获取并处理运动额外数据 前提是特殊运动 - if (TraingModel::GetInstance()->GetIsSpecial()){ + if (TraingModel::GetInstance()->GetIsSpecial()) { TraingModel::GetInstance()->GetSportExtraData(); } @@ -3481,17 +3471,19 @@ bool get_training_heart_path_(char *path, uint32_t time, uint32_t max_len) { //获取gps轨迹路径 bool get_training_gps_path_(char *path, uint32_t time, uint32_t max_len) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 获取GPS轨迹路径开始 ==="); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "请求时间戳: %u, 最大路径长度: %u", time, max_len); - + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "请求时间戳: %u, 最大路径长度: %u", + time, max_len); + std::string str = OHOS::TraingModel::GetInstance()->GetGPSDataPath(time); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "获取到的GPS路径: %s", str.c_str()); - + if (str.length() <= 3) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS路径长度不足,长度: %zu", str.length()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: GPS路径长度不足,长度: %zu", + str.length()); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 获取GPS轨迹路径结束(失败) ==="); return false; } - + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS路径长度: %zu", str.length()); memcpy_s(path, max_len, str.c_str(), str.length()); WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS路径复制完成"); @@ -3553,67 +3545,65 @@ char *struct_to_json_buf_malloc_(uint8_t day) { return json_format; } -int read_gps_data_(char *path ,uint8_t *out_buf) -{ - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 读取GPS数据开始 ==="); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件路径: %s", path); - - FILE *fp= fopen(path,"rb+"); - if(fp==NULL) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: 无法打开GPS文件"); - return 0; - } - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件打开成功"); - - uint32_t file_size=0; - fseek(fp, 0, SEEK_END); - file_size = ftell(fp); - fseek(fp, 0, SEEK_SET); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件大小: %u 字节", file_size); +int read_gps_data_(char *path, uint8_t *out_buf) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 读取GPS数据开始 ==="); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件路径: %s", path); - int len=0; - int point_count = 0; - OHOS::GPSData_t data={0}; + FILE *fp = fopen(path, "rb+"); + if (fp == NULL) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "错误: 无法打开GPS文件"); + return 0; + } + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件打开成功"); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始读取GPS轨迹点..."); - while (1) - { - int read_size=fread(&data,1,sizeof(data),fp); - if(read_size==sizeof(data) ) - { - point_count++; - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "读取GPS点 #%d: 时间戳=%u, 纬度=%.6f, 经度=%.6f", - point_count, data.timestamp, data.latitude, data.longitude); - - int temp_data=data.timestamp; - memcpy_s(out_buf+len,4,&temp_data,sizeof(data.timestamp)); - len+=4; - - int temp_latitude=(int)(data.latitude *10000000); - memcpy_s(out_buf+len,4,&temp_latitude,sizeof(temp_latitude)); - len+=4; - - int temp_longitude=(int)(data.longitude*10000000); - memcpy_s(out_buf+len,4,&temp_longitude,sizeof(temp_longitude)); - len+=4; - - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS点数据转换: 时间戳=%u, 纬度=%d, 经度=%d", - temp_data, temp_latitude, temp_longitude); - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件读取结束,读取大小: %d", read_size); - break; - } + uint32_t file_size = 0; + fseek(fp, 0, SEEK_END); + file_size = ftell(fp); + fseek(fp, 0, SEEK_SET); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件大小: %u 字节", file_size); + + int len = 0; + int point_count = 0; + OHOS::GPSData_t data = {0}; + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开始读取GPS轨迹点..."); + while (1) { + int read_size = fread(&data, 1, sizeof(data), fp); + if (read_size == sizeof(data)) { + point_count++; + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "读取GPS点 #%d: 时间戳=%u, 纬度=%.6f, 经度=%.6f", + point_count, data.timestamp, data.latitude, data.longitude); + + int temp_data = data.timestamp; + memcpy_s(out_buf + len, 4, &temp_data, sizeof(data.timestamp)); + len += 4; + + int temp_latitude = (int)(data.latitude * 10000000); + memcpy_s(out_buf + len, 4, &temp_latitude, sizeof(temp_latitude)); + len += 4; + + int temp_longitude = (int)(data.longitude * 10000000); + memcpy_s(out_buf + len, 4, &temp_longitude, sizeof(temp_longitude)); + len += 4; + + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "GPS点数据转换: 时间戳=%u, 纬度=%d, 经度=%d", temp_data, + temp_latitude, temp_longitude); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件读取结束,读取大小: %d", + read_size); + break; } + } - fclose(fp); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件关闭"); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "总共读取 %d 个GPS点,输出数据长度: %d 字节", point_count, len); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 读取GPS数据结束 ==="); + fclose(fp); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "GPS文件关闭"); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "总共读取 %d 个GPS点,输出数据长度: %d 字节", point_count, len); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "=== 读取GPS数据结束 ==="); - return len; + return len; } extern bleInstruct_t_ bleInstruct_t;