#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)
{
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);
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);
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;
}
}
- 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);
}
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 {