#include <iomanip>
#include <string>
#include "cmsis_os.h"
+#include "traing/training_card/include/TraingCardPresenter.h"
-#define TIMER 0
+#define TIMER 1
#define CHECK 0
+ #define HARD_OFF 1
+ #define TRAINING_PATH "user/userdata/"
namespace OHOS
{
+ // struct Trainingdata_hard
+ // {
+ // uint32_t st;
+ // uint32_t num;
+ // };
+
+ // struct Trainingdata
+ // {
+ // //0:手表开启的训练1:APP开启的训练
+ // uint32_t src;
+ // //0:健身类1:舞蹈类2:休闲运动3:球类4:水上项目5:冰上项目6:极限运动 7:其他
+ // uint32_t bt;
+ // //训练类型0:健走1:跑步2:骑行
+ // uint32_t ty;
+ // //开始时间戳
+ // uint32_t st;
+ // //结束时间戳
+ // uint32_t sp;
+ // //训练目标类型
+ // uint32_t tgt;
+ // //训练目标
+ // uint32_t tg;
+ // //目标完成度
+ // uint32_t pg;
+ // //训练时长
+ // uint32_t du;
+ // //卡路里
+ // uint32_t ca;
+ // //距离
+ // uint32_t di;
+ // //平均心率
+ // uint32_t hr;
+ // //步数
+ // uint32_t stp;
+ // //训练强度
+ // uint32_t ti;
+ // //圈数
+ // uint32_t cir;
+ // //最高海拔高度
+ // uint32_t alt;
+ // };
+ #ifdef __cplusplus
+ extern "C" {
+ #endif
+ //处理数据头
+ static void write_Trainingdata_hard_data(uint32_t data)
+ {
+ char file_path[128]={0};
+ sprintf(file_path,"%shard_%d.bin",TRAINING_PATH,data);
+
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%s",file_path);
+ FILE *fp= fopen(file_path,"rb+");
+ Trainingdata_hard hard;
+ int ret=0;
+ if(fp==NULL)
+ {
+ hard.num=0;
+ fp= fopen(file_path,"wb+");
+ }
+ else
+ {
+ ret=fread((void *)&hard,sizeof(Trainingdata_hard),1,fp);
+ }
+
+ fseek(fp, 0, SEEK_SET);
+ fclose(fp);
+
+ fp= fopen(file_path,"wb+");
+ hard.num++;
+ ret=fwrite((void *)&hard,sizeof(Trainingdata_hard),1,fp);
+ fseek(fp, 0, SEEK_SET);
+ fclose(fp);
+ }
+ //获得数据个数
+ static int get_Trainingdata_data_num(uint32_t data)
+ {
+
+ char file_path[128]={0};
+ sprintf(file_path,"%shard_%d.bin",TRAINING_PATH,data);
+
+ FILE *fp= fopen(file_path,"ab");
+ if(fp==NULL)
+ {
+ return 0xffffff;
+ }
+ fseek(fp,0,SEEK_SET);
+ Trainingdata_hard hard;
+ int ret=fread((void *)&hard,sizeof(Trainingdata_hard),1,fp);
+ fseek(fp, 0, SEEK_SET);
+ fclose(fp);
+ return hard.num;
+
+ }
+
+ //写数据
+ int write_Trainingdata_data(Trainingdata data)
+ {
+ uint64_t time;
+ uapi_get_timestampe_ms(&time);
+ uint32_t timeday = time/1000/60/60/12;
+ char file_path[128]={0};
+ sprintf(file_path,"%s%d.bin",TRAINING_PATH,timeday);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%s",file_path);
+ write_Trainingdata_hard_data(timeday);
+ FILE *fp= fopen(file_path,"ab");
+ if(fp==NULL)
+ {
+ fopen(file_path,"wb+");
+ }
+ #if HARD_OFF
+ int ret=0;
+
+ ret=fwrite((void *)&data,sizeof(Trainingdata),1,fp);
+ if(ret!=1)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "2 fwrite_data %d",ret);
+ }
+
+
+ #else
+ if(fp==NULL)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL");
+ }
+ Trainingdata_hard hard={0};
+ fseek(fp,0,SEEK_SET);
+ int ret=fread((void *)&hard,sizeof(Trainingdata_hard),1,fp);
+
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d",hard.num);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fread %d",ret);
+ if(ret==0)
+ {
+ hard.num=1;
+ hard.st=timeday;
+ fseek(fp,0,SEEK_SET);
+ ret=fwrite((void *)&hard,sizeof(Trainingdata_hard),1,fp);
+
+ if(ret!=1)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard %d",ret);
+ }
+
+ ret=fwrite((void *)&data,sizeof(Trainingdata),1,fp);
+ if(ret!=1)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "1 fwrite_data %d",ret);
+ }
+ }
+ else
+ {
+ ret=fseek(fp,8,SEEK_END);
+ if(ret!=0)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fseek %d",ret);
+ }
+ hard.num++;
+ ret=fwrite((void *)&(hard),sizeof(Trainingdata_hard),1,fp);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d",hard.num);
+ if(ret)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "fwrite_hard %d",ret);
+ }
+
+ fseek(fp,0,SEEK_END);
+ // fclose(fp);
+
+ // fp= fopen(file_path,"ab+");
+ ret=fwrite((void *)&data,sizeof(Trainingdata),1,fp);
+ if(ret!=1)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "2 fwrite_data %d",ret);
+ }
+ }
+ #endif
+
+ fseek(fp, 0, SEEK_SET);
+ fclose(fp);
+ return 1;
+ }
+ //读数据
+ Trainingdata *read_Trainingdata_data(uint32_t timedata,uint16_t offset)
+ {
+ char file_path[128]={0};
+ sprintf(file_path,"%s%d.bin",TRAINING_PATH,timedata);
+
+ FILE *fp= fopen(file_path,"rb+");
+ Trainingdata* data=(Trainingdata*)malloc(sizeof(Trainingdata));
+ if(fp==NULL || get_Trainingdata_data_num(timedata)<offset)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL %d",get_Trainingdata_data_num(timedata));
+ return NULL;
+ }
+ #if HARD_OFF
+ fseek(fp,sizeof(Trainingdata)*offset,SEEK_SET);
+ fread((void *)data,sizeof(Trainingdata),1,fp);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d",data->src);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d %d ",offset,sizeof(Trainingdata));
+ #else
+ fseek(fp,0,SEEK_SET);
+ Trainingdata_hard hard;
+ int ret=fread((void *)&hard,sizeof(Trainingdata_hard),1,fp);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "hard num %d",hard.num);
+ if(ret==0)
+ {
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "file is NULL");
+ return NULL;
+
+ }
+ else
+ {
+ fread((void *)data,sizeof(Trainingdata),1,fp);
+ WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "%d",data->src);
+ }
+ #endif
+ fseek(fp, 0, SEEK_SET);
+ fclose(fp);
+ return data;
+ }
+ #ifdef __cplusplus
+ }
+ #endif
+
+
#if TIMER
// 声明定时器变量
- static osTimerId_t g_timer = NULL;
- static int g_trainingTimer = {0};
+ static osTimerId_t g_startCounting = NULL; // 定时器
+ static constexpr uint64_t MS_ONE_SECONDS = 1000;
+ static int g_motionTimes{0};
static constexpr int32 NUM_TEN = 10;
static constexpr int16 ERROR = 0;
static constexpr int16 SUCCESS = 1;