优化计步保存
authorluozheng <1732036298@qq.com>
Mon, 25 May 2026 07:56:52 +0000 (15:56 +0800)
committerluozheng <1732036298@qq.com>
Mon, 25 May 2026 07:56:52 +0000 (15:56 +0800)
code/sdk/application/wearable/fd272/nativeapp/nativeui/goal/src/goalmodel.cpp
code/sdk/application/wearable/fd272/service/msg_center/src/notify/debug_output_notification/debug_output_notification.cpp
code/sdk/zhihui/drivers/cywee/dml/cywee_data.c
code/sdk/zhihui/drivers/cywee/dml/cywee_data.h

index 33149f4..38cf55e 100644 (file)
@@ -634,28 +634,37 @@ namespace OHOS
    //步数
    uint32_t GoalModel::show_data_step_refresh(void)
    {
-      int32_t value=0;
+      int32_t value = 0;
       uint32_t toal_date;
-      
-      toal_date=show_step;
+      uint32_t raw_ui;
+
+      toal_date = show_step;
 #ifdef SUPPORT_CYWEE_SERVICE
-      cywee_get_now_hour_data(&value,STEP);
+      cywee_get_now_hour_data(&value, STEP);
+      toal_date = cywee_step_calc_ui_total(show_step, value);
+      raw_ui = show_step + (value > 0 ? (uint32_t)value : 0);
 #if STEP_DEBUG
       {
          uint32_t hist = cywee_get_sport_data(HISTORY_STEP);
          uint32_t sc = cywee_get_sport_data(STEP);
          uint32_t hist_ago = cywee_get_sport_data(HISTORY_STEP_);
-         uint32_t total = toal_date + (value > 0 ? (uint32_t)value : 0);
+         uint32_t algo = cywee_step_get_algo_daily_total();
          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
-            "[步数调试] 活动界面 合计=%u(文件%u+本小时%d) 历史=%u 当前=%u 一小时前=%u",
-            total, show_step, value, hist, sc, hist_ago);
+            "[步数调试] 活动界面 展示=%u 原始(文件%u+本小时%d)=%u 算法=%u 历史=%u 当前=%u 快照=%u",
+            toal_date, show_step, value, raw_ui, algo, hist, sc, hist_ago);
+         if (raw_ui != toal_date) {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
+               "[步数纠正] 展示兜底: %u -> %u", raw_ui, toal_date);
+         }
       }
 #endif
-#endif
-      if(value>0)
-      toal_date+=value;
       return toal_date;
-      return 0;
+#else
+      if (value > 0) {
+         toal_date += (uint32_t)value;
+      }
+      return toal_date;
+#endif
    }
 
    float GoalModel::show_data_calories_refresh(void)
index fa71006..fae7209 100644 (file)
@@ -979,15 +979,20 @@ namespace OHOS
             fclose(fp);
         }
 
-        uint32_t ui_total = file_sum;
-        if (hour_steps > 0) {
-            ui_total += (uint32_t)hour_steps;
-        }
+        uint32_t ui_raw = file_sum + (hour_steps > 0 ? (uint32_t)hour_steps : 0);
+        uint32_t ui_total = cywee_step_calc_ui_total(file_sum, hour_steps);
+
+        cywee_step_simulate_abnormal_correct_print();
 
         oss << "【UI今日步数】\n";
-        oss << "UI今日总数(文件+本小时):" << ui_total << "\n";
+        oss << "UI今日总数(兜底后):" << ui_total << "\n";
+        oss << "  原始(文件+本小时):" << ui_raw << "\n";
         oss << "  文件各小时合计:" << file_sum << "\n";
         oss << "  本小时步数:" << hour_steps << "\n";
+        oss << "  算法当日(历史+当前):" << mem_total << "\n";
+        if (ui_raw != ui_total && mem_total > 0) {
+            oss << "  [已兜底] " << ui_raw << " -> " << ui_total << "\n";
+        }
 
         oss << "【cywee内存(算本小时)】\n";
         oss << "历史步数:" << hist << "\n";
index ded9363..246bd43 100644 (file)
 
 #if STEP_DEBUG
 #define STEP_DBG_PRINT(fmt, ...) PRINT("[步数调试] " fmt, ##__VA_ARGS__)
+#define STEP_CORRECT_PRINT(fmt, ...) PRINT("[步数纠正] " fmt, ##__VA_ARGS__)
 #else
 #define STEP_DBG_PRINT(fmt, ...) ((void)0)
+#define STEP_CORRECT_PRINT(fmt, ...) ((void)0)
 #endif
 
+/* 单小时增量合理上限(超过则视为误存了全天累计) */
+#define STEP_MAX_HOURLY_DELTA 12000
+
+typedef enum {
+    STEP_SAVE_OK = 0,
+    STEP_SAVE_CORRECT_SNAP,
+    STEP_SAVE_CORRECT_CUMULATIVE,
+    STEP_SAVE_SKIP_NEG,
+} step_save_fix_reason_t;
+
+static uint32_t cywee_parse_today_step_file_sum(void);
+
+uint32_t cywee_step_get_algo_daily_total(void)
+{
+    return cywee_get_sport_data(HISTORY_STEP) + cywee_get_sport_data(STEP);
+}
+
+uint32_t cywee_step_calc_ui_total(uint32_t file_sum, int32_t hour_delta)
+{
+    uint32_t algo = cywee_step_get_algo_daily_total();
+    uint32_t hour_part = (hour_delta > 0) ? (uint32_t)hour_delta : 0;
+    uint32_t raw = file_sum + hour_part;
+
+    if (algo > 0 && raw > algo) {
+        STEP_CORRECT_PRINT("展示兜底: 文件%u + 本小时%d = %u > 算法%u -> UI=%u\r\n",
+            file_sum, hour_delta, raw, algo, algo);
+        return algo;
+    }
+    return raw;
+}
+
+void cywee_step_simulate_abnormal_correct_print(void)
+{
+#if STEP_DEBUG
+    /* 复现用户反馈:快照失效时把全天累计当每小时增量写入并相加 */
+    const int demo_records[] = {2200, 3664, 4174, 16999};
+    const int demo_record_cnt = 4;
+    const int demo_algo_total = 16999;
+    int file_sum_wrong = 0;
+    int i;
+
+    for (i = 0; i < demo_record_cnt; i++) {
+        file_sum_wrong += demo_records[i];
+    }
+
+    int ui_wrong = file_sum_wrong;
+    int ui_fixed = demo_algo_total;
+    int snap_bad = 0;
+    int total_at_19 = demo_algo_total;
+    int raw_delta_at_19 = total_at_19 - snap_bad;
+    int file_before_19 = 2200 + 3664 + 4174;
+    int save_fixed_delta = total_at_19 - file_before_19;
+
+    STEP_CORRECT_PRINT("======== 步数异常纠正模拟(典型场景) ========\r\n");
+    STEP_CORRECT_PRINT("误存每小时记录: %d+%d+%d+%d = %d (当作增量相加)\r\n",
+        demo_records[0], demo_records[1], demo_records[2], demo_records[3], file_sum_wrong);
+    STEP_CORRECT_PRINT("活动页错误展示(文件+本小时0): %d\r\n", ui_wrong);
+    STEP_CORRECT_PRINT("展示兜底后(以算法当日合计为准): %d\r\n", ui_fixed);
+    STEP_CORRECT_PRINT("19:00整点保存纠正: total=%d snap=%d 原增量=%d -> 纠正增量=%d\r\n",
+        total_at_19, snap_bad, raw_delta_at_19, save_fixed_delta);
+    STEP_CORRECT_PRINT("================================================\r\n");
+#else
+    (void)0;
+#endif
+}
+
+static int cywee_step_fix_hourly_delta(int total, int snap, int *delta_out,
+    step_save_fix_reason_t *reason_out)
+{
+    int raw = total - snap;
+    uint32_t file_sum = cywee_parse_today_step_file_sum();
+    int effective_snap = snap;
+    step_save_fix_reason_t reason = STEP_SAVE_OK;
+
+    if (delta_out == NULL || reason_out == NULL) {
+        return 0;
+    }
+
+    if (total <= 0) {
+        *delta_out = 0;
+        *reason_out = STEP_SAVE_OK;
+        return 0;
+    }
+
+    if (raw < 0) {
+        STEP_CORRECT_PRINT("保存纠正[负增量]: total=%d snap=%d raw=%d -> 跳过写入, 快照推进到%d\r\n",
+            total, snap, raw, total);
+        *delta_out = 0;
+        *reason_out = STEP_SAVE_SKIP_NEG;
+        return 0;
+    }
+
+    bool snap_bad = (snap <= 0 || snap > total);
+    bool looks_cumulative = (snap_bad && raw > 0 && raw >= total);
+    bool delta_too_large = (raw > STEP_MAX_HOURLY_DELTA);
+
+    if (snap_bad || looks_cumulative || delta_too_large) {
+        effective_snap = (int)file_sum;
+        if (effective_snap > total) {
+            effective_snap = total;
+        }
+        int corrected = total - effective_snap;
+        if (corrected < 0) {
+            corrected = 0;
+        }
+        if (delta_too_large && corrected > STEP_MAX_HOURLY_DELTA) {
+            corrected = STEP_MAX_HOURLY_DELTA;
+        }
+        reason = snap_bad ? STEP_SAVE_CORRECT_SNAP : STEP_SAVE_CORRECT_CUMULATIVE;
+        STEP_CORRECT_PRINT(
+            "保存纠正[%s]: total=%d 原快照=%d 文件已存=%u 原增量=%d -> 纠正增量=%d\r\n",
+            snap_bad ? "快照异常" : "增量过大", total, snap, file_sum, raw, corrected);
+        *delta_out = corrected;
+        *reason_out = reason;
+        return corrected;
+    }
+
+    *delta_out = raw;
+    *reason_out = STEP_SAVE_OK;
+    return raw;
+}
+
 /* 读取今日 hourly 步数文件,累加各小时增量(与活动页 file_sum 一致) */
 static uint32_t cywee_parse_today_step_file_sum(void)
 {
@@ -364,6 +488,13 @@ void cywee_restoreData(void)
 
     CWM_OS_restoreData(&history_step,sizeof(int),STEP);
     CWM_OS_OneHourAgo_restoreData(&history_step_,sizeof(int),STEP);
+    if (history_step_ <= 0 && history_step > 0) {
+        uint32_t file_sum = cywee_parse_today_step_file_sum();
+        history_step_ = (file_sum > 0 && file_sum <= (uint32_t)history_step) ?
+            (int)file_sum : history_step;
+        STEP_CORRECT_PRINT("恢复纠正: 快照丢失 用文件累计=%u 或主文件=%d -> 快照=%d\r\n",
+            file_sum, history_step, history_step_);
+    }
     cywee_set_sport_data(history_step,HISTORY_STEP);
     cywee_set_sport_data(history_step_,HISTORY_STEP_);
     cywee_set_sport_data(history_step,STEP);
@@ -458,17 +589,18 @@ static void CWM_OS_saveData_history(void* new_data,void *history ,uint32_t len,u
         break;
     case STEP:
        sprintf_s(path_,64,"%s%s.bin",CWM_HISTORY_DATA_PATH,timeday);
-        STEP_DBG_PRINT("整点历史步数 日期后缀=%s 文件=%s 整点unix时间=%u 本小时增量=%d\r\n",
-            timeday, path_, time, *(int*)new_data - *(int*)history);
-
-        i_data= *(int*)new_data-*(int*)history;//新数据减去旧数据,作为这一个小时的值
-        // {   
-        //     FILE *fp= fopen("/user/userdata/i_data_test.txt","ab+");
-        //     fwrite("x",1,1,fp);
-        //     fclose(fp);
-        // }
-       
-
+        {
+            int total = *(int*)new_data;
+            int snap = *(int*)history;
+            step_save_fix_reason_t fix_reason = STEP_SAVE_OK;
+            cywee_step_fix_hourly_delta(total, snap, &i_data, &fix_reason);
+            if (fix_reason == STEP_SAVE_SKIP_NEG) {
+                *(int*)history = total;
+                return;
+            }
+            STEP_DBG_PRINT("整点历史步数 日期=%s unix=%u total=%d snap=%d 写入增量=%d\r\n",
+                timeday, time, total, snap, i_data);
+        }
         break;
     case DISTANCE:
        sprintf_s(path_,64,"%s%s.bin",CWM_HISTORY_DATA_DISTANCE_PATH,timeday);
@@ -487,11 +619,14 @@ static void CWM_OS_saveData_history(void* new_data,void *history ,uint32_t len,u
    
     PRINT(" save path %s \n",path_);
 
-    if(i_data<0 || f_data<0 ||i_data_16<0)
-    {
-        i_data=0;
-        f_data=0;
-        *(int*)history=0;
+    if (type == STEP) {
+        if (i_data < 0) {
+            i_data = 0;
+        }
+    } else if (i_data < 0 || f_data < 0 || i_data_16 < 0) {
+        i_data = 0;
+        f_data = 0;
+        *(int*)history = 0;
         return;
     }
 
@@ -689,12 +824,15 @@ static void CWM_OS_saveData_history(void* new_data,void *history ,uint32_t len,u
     }
     
 
-     if(i_data)
-    *(int*)history=*(int*)new_data;
-     else if(i_data_16)
-    *(int16_t*)history=*(int16_t*)new_data;
-    else if(f_data)
-    *(float*)history=*(float*)new_data;
+    if (type == STEP) {
+        *(int*)history = *(int*)new_data;
+    } else if (i_data) {
+        *(int*)history = *(int*)new_data;
+    } else if (i_data_16) {
+        *(int16_t*)history = *(int16_t*)new_data;
+    } else if (f_data) {
+        *(float*)history = *(float*)new_data;
+    }
 
     // printf("CWM_OS_saveData_history %d %d\n",*(int*)history,*(int*)new_data);
 }
@@ -823,7 +961,15 @@ int32_t cywee_get_now_hour_data(void*value, uint8_t type)
 
             if (snap <= 0 || snap > total) {
                 int file_sum = (int)cywee_parse_today_step_file_sum();
-                data = total - file_sum;
+                if (file_sum > total && total > 0) {
+                    data = 0;
+                    STEP_CORRECT_PRINT("本小时纠正: 文件合计%d>total%d -> 本小时=0\r\n",
+                        file_sum, total);
+                } else {
+                    data = total - file_sum;
+                    STEP_CORRECT_PRINT("本小时纠正: 快照异常 合计=%d 快照=%d 文件=%d -> 本小时=%d\r\n",
+                        total, snap, file_sum, data);
+                }
                 STEP_DBG_PRINT("本小时(快照异常) 合计=%d 快照=%d 文件合计=%d 本小时=%d\r\n",
                     total, snap, file_sum, data);
             } else {
index 91810eb..c76eff4 100644 (file)
@@ -86,6 +86,13 @@ void cywee_save_data(void);
 int32_t cywee_get_now_hour_data(void*value, uint8_t type);
 void cywee_fat_history_save(uint32_t time);
 
+/* 算法侧当日总步数(历史 + 当前) */
+uint32_t cywee_step_get_algo_daily_total(void);
+/* 活动页展示:文件累计 + 本小时,异常时以算法总步数兜底 */
+uint32_t cywee_step_calc_ui_total(uint32_t file_sum, int32_t hour_delta);
+/* 模拟典型异常场景并输出纠正说明(STEP_DEBUG 时有效) */
+void cywee_step_simulate_abnormal_correct_print(void);
+
 #ifdef __cplusplus
 #if __cplusplus
 }