7ca3a2d5274bb1ee87f6004bdfd73979919f9e28
[mirrors/FD230.git] /
1 /***
2  * @Date: 2024-07-06 16:27:31
3  * @LastEditors: Caisking 3449750381@qq.com
4  * @LastEditTime: 2024-08-02 10:21:14
5  * @FilePath:
6  * /FD230/code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp
7  */
8 /***
9  * @Date: 2024-04-26 09:23:18
10  * @LastEditors: Caisking 3449750381@qq.com
11  * @LastEditTime: 2024-04-26 10:25:05
12  * @FilePath:
13  * /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp
14  */
15
16 #include "AppViewIDs.h"
17 #include "NativeAbility.h"
18 #include "AbilityNavigationApi.h"
19 #include "components/ui_label_ext.h"
20 #include "ui_resource_string.h"
21
22 #include "ConfigModel.h"
23 #include "custom_utils.h"
24 #include "graphic_service.h"
25 #include "traing/include/TraingModel.h"
26 #include "traing/training_card/include/TraingCardPresenter.h"
27 #include "traing/training_card/include/TrainingControlView.h"
28 #include "uicomponents/UIComponents.h"
29 #include "vibrator_service.h"
30 #include "UiConfig.h"
31  
32  #include <chrono>
33  #include <iomanip>
34  #include <iostream>
35  #include <sstream>
36  #include <thread>
37  #include <unordered_map>
38  #include "custom_runtime.h"
39  
40  extern "C" void do_cywee_service_check_msg(int cmd, int data[]);
41  
42  #define MAXSPORTID 176
43  
44  #define DEFAULT_DASH "--"
45  #define DATATEST 0
46  
47  extern "C" {
48  void StartTimer();
49  void StopAndResetTimer();
50  void PauseTimer();
51  void ResumeTimer();
52  }
53  #include "NativeRegisterManager.h"
54  
55  #ifdef SUPPORT_CYWEE_SERVICE
56  #include "cywee_service.h"
57  #endif
58  namespace OHOS {
59  
60  REGIST_SLICE(VIEW_TRAININGCRAD, TraingCardView, TraingCardPresenter);
61  
62  // 当前Presenter属性实例化的内存指针
63  static TraingCardPresenter *g_pTraingCardPresenter = nullptr;
64  
65  TraingCardPresenter::TraingCardPresenter() { g_pTraingCardPresenter = this; }
66  
67  TraingCardPresenter::~TraingCardPresenter() {
68    // tag_ = 0;
69    g_pTraingCardPresenter = nullptr;
70  }
71  
72  TraingCardPresenter *TraingCardPresenter::GetInstance() {
73    static TraingCardPresenter instance;
74    return &instance;
75  }
76  
77  void TraingCardPresenter::OnStart() {
78    Presenter<TraingCardView>::OnStart();
79    // ScreenAlwaysOnCtr(OPEN_SCREEN_ALWAYS_ON); // 长亮屏
80  }
81  
82  void TraingCardPresenter::OnPause(void) {
83  
84    Presenter<TraingCardView>::OnPause();
85  }
86  
87  void TraingCardPresenter::OnResume(void) {
88  
89    Presenter<TraingCardView>::OnResume();
90    RootView::GetInstance()->SetOnKeyActListener(this);
91  }
92  
93  void TraingCardPresenter::OnStop(void) {
94  
95    Presenter<TraingCardView>::OnStop();
96    // ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON); // 长亮屏
97  }
98  
99 bool TraingCardPresenter::OnDragStart(UIView &view, const DragEvent &event) {
100   UNUSED(view);
101   UNUSED(event);
102   // 重置标志
103   Y_DRAG_FLAG = false;
104   return false;
105 }
106
107 bool TraingCardPresenter::OnDrag(UIView &view, const DragEvent &event) {
108   UNUSED(view);
109   // 拖动过程中判断是否是垂直滑动:垂直距离大于阈值,且垂直距离大于水平距离的2倍
110   if ((event.GetDeltaY() > X_DRAG_OFFSET || event.GetDeltaY() < -X_DRAG_OFFSET) && 
111       (MATH_ABS(event.GetDeltaY()) > (DOUBLES * MATH_ABS(event.GetDeltaX())))) {
112     Y_DRAG_FLAG = true; // 标记为垂直滑动
113     WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Vertical drag detected (deltaY=%d, deltaX=%d), blocking horizontal swipe", 
114                   event.GetDeltaY(), event.GetDeltaX());
115   }
116   return true;
117 }
118
119 bool TraingCardPresenter::OnDragEnd(UIView &view, const DragEvent &event) {
120   UNUSED(view);
121   // 如果检测到垂直滑动,阻止水平滑动
122   if (Y_DRAG_FLAG) {
123     WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Vertical drag ended, blocking horizontal swipe");
124     Y_DRAG_FLAG = false;
125     // 返回true阻止水平滑动事件
126     return true;
127   }
128   Y_DRAG_FLAG = false;
129   return false;
130 }
131  
132  // 计时器更新timer
133  void TraingCardPresenter::ModifiesTime() {
134    WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ModifiesTime begin");
135    std ::string TimeStr = TraingModel::GetInstance()->DisplayMotionDuration();
136  
137    /*灭屏状态不刷新UI*/
138    if (uapi_get_screen_state() == 1) {
139      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uapi_get_screen_state is off");
140      return;
141    }
142  
143    if (TimeStr.empty()) {
144      WEARABLE_LOGE(
145          WEARABLE_LOG_MODULE_APP,
146          "TraingModel::GetInstance()->DisplayMotionDuration() nullptr");
147      return;
148    }
149    if (TrainingControlView::GetInstance() != nullptr &&
150        TrainingDataView::GetInstance() != nullptr) {
151      if (TrainingControlView::GetInstance()->GetTimeLabel() != nullptr) {
152        TrainingControlView::GetInstance()->GetTimeLabel()->SetText(
153            TimeStr.c_str());
154      }
155      if (TrainingDataView::GetInstance()->GetTimeLabel() != nullptr) {
156        TrainingDataView::GetInstance()->GetTimeLabel()->SetText(TimeStr.c_str());
157      }
158      WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "ModifiesTime end");
159    }
160  }
161  
162  //计时器
163  void TraingCardPresenter::UpdateTimeOfControl() {
164    std ::string TimeStr = TraingModel::GetInstance()->DisplayMotionDuration();
165    if (TimeStr.empty()) {
166      WEARABLE_LOGE(
167          WEARABLE_LOG_MODULE_APP,
168          "TraingModel::GetInstance()->DisplayMotionDuration() nullptr");
169      return;
170    }
171    if (TrainingControlView::GetInstance()->GetTimeLabel() != nullptr) {
172      TrainingControlView::GetInstance()->GetTimeLabel()->SetText(
173          TimeStr.c_str());
174    }
175    //需要判断是否需要运动是否需要gps
176    bool isNeedGPS = TraingModel::GetInstance()->IsNeedGPS(TraingModel::GetInstance()->GetSportEvent());
177    if(isNeedGPS == false)
178    {
179      WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "该运动项不需要GPS");
180      return;
181    }
182    
183    if (TrainingControlView::GetInstance()->GetGpsStatusLabel() != nullptr) {
184      //获取GPS状态
185      bool gpsStatus = TraingModel::GetInstance()->GetGPSConnectState();
186      std::string gpsStatusStr;
187      if (gpsStatus) {
188        if(ConfigModel::GetInstance()->GetLanguage() == 0)
189          gpsStatusStr = "GPS定位成功";
190        else
191          gpsStatusStr = "GPS Success";
192        TrainingControlView::GetInstance()->GetGpsStatusLabel()->SetTextColor(
193            Color::Green());
194      } else {
195        if(ConfigModel::GetInstance()->GetLanguage() == 0)
196          gpsStatusStr = "GPS未定位成功";
197        else
198          gpsStatusStr = "GPS not found";
199        TrainingControlView::GetInstance()->GetGpsStatusLabel()->SetTextColor(
200            Color::Red());
201      }
202      TrainingControlView::GetInstance()->GetGpsStatusLabel()->SetText(
203          gpsStatusStr.c_str());
204    }
205  }
206  void TraingCardPresenter::UpdateTimeOfData() {
207    std ::string TimeStr = TraingModel::GetInstance()->DisplayMotionDuration();
208    if (TimeStr.empty()) {
209      WEARABLE_LOGE(
210          WEARABLE_LOG_MODULE_APP,
211          "TraingModel::GetInstance()->DisplayMotionDuration() nullptr");
212      return;
213    }
214    if (TrainingDataView::GetInstance()->GetTimeLabel() != nullptr) {
215      TrainingDataView::GetInstance()->GetTimeLabel()->SetText(TimeStr.c_str());
216    }
217  }
218  
219  /* --------------------------------- cywee库操作
220   * -------------------------------- */
221  /*开启cywee*/
222  bool TraingCardPresenter::OpenSensor(ExerciseClass type) {
223    // 防止重复开启
224    if (opensensorstuats) {
225      WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "OpenSensor is opening!!");
226      return false;
227    }
228    // 根据不同的运动项目进行不同的运动
229    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "TraingCardPresenter::OpenSensor!!");
230  #ifdef SUPPORT_CYWEE_SERVICE
231    switch (type) {
232    case ExerciseClass::NORMAL:
233      cywee_sport_mode_change(CYWEE_SERVICE_CMD_NORMAL_MODE);
234      break;
235    case ExerciseClass::TREADMILL:
236      cywee_sport_mode_change(CYWEE_SERVICE_CMD_TREADMILL_MODE);
237      break;
238    case ExerciseClass::OUTDOOR:
239      cywee_sport_mode_change(CYWEE_SERVICE_CMD_OUTDOOR_RUNNING_MODE);
240      break;
241    case ExerciseClass::INDOOR:
242      cywee_sport_mode_change(CYWEE_SERVICE_CMD_INDOOR_RUNNING_MODE);
243      break;
244    case ExerciseClass::FITNESS_PEDOMETER:
245      cywee_sport_mode_change(CYWEE_SERVICE_CMD_FITNESS_PEDOMETER_MODE);
246      break;
247    case ExerciseClass::FREE_TRAINING:
248      cywee_sport_mode_change(CYWEE_SERVICE_CMD_FREE_TRAINING_MODE);
249      break;
250    default:
251      break;
252    }
253  #endif
254    opensensorstuats = true;
255    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "OpenSensor--%d!!", type);
256    return true;
257  }
258  
259  /*控制cywee*/
260  bool TraingCardPresenter::ControlSensor(ExerciseState state) {
261  #ifdef SUPPORT_PPG_SERVICE
262    switch (state) {
263    case ExerciseState::End:
264      // 退出进入普通模式
265      do_cywee_service_check_msg(CYWEE_SERVICE_CMD_NORMAL_MODE, NULL);
266      opensensorstuats = false; // 恢复标志位
267      StopAndResetTimer();      // 关闭计时器
268      break;
269    case ExerciseState::Pause:
270      do_cywee_service_check_msg(CYWEE_SERVICE_CMD_PAUSE, 0);
271      break;
272    case ExerciseState::Continue:
273      do_cywee_service_check_msg(CYWEE_SERVICE_CMD_RESUME, 0);
274      break;
275    default:
276      break;
277    }
278  #endif
279    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ControlSensor[%d]!!", state);
280    return true;
281  }
282  
283  void TraingCardPresenter::RefreshData(SportData *sportdata) {
284    if (sportdata == nullptr) {
285      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "sportdata is null");
286      return;
287    }
288  
289    if (TrainingDetailView::GetInstance() == nullptr ||
290        TrainingDataView::GetInstance() == nullptr)
291      return;
292  
293    std::string distanceStr;
294    if (sportdata->distance != 0) {
295      double distance_km =
296          sportdata->distance / 1000.0; // 转换为公里并保留两位小数
297      std::stringstream ss;
298      ss << std::fixed << std::setprecision(2) << distance_km;
299      distanceStr = ss.str();
300    } else {
301      distanceStr = DEFAULT_DASH;
302    }
303  
304    std::string heartStr =
305        (sportdata->heart != 0) ? std::to_string(sportdata->heart) : DEFAULT_DASH;
306    // 配速 pc 单位为秒/公里,显示为 分钟'秒"(如 5'30");慢于 60 分钟/公里时显示 --,避免异常递增
307    const int PACE_MAX_SEC_PER_KM = 60 * 60;  // 60 分钟/公里 = 3600 秒/公里
308    std::string paceStr;
309    if (sportdata->pc > 0 && sportdata->pc <= PACE_MAX_SEC_PER_KM) {
310      int min = sportdata->pc / 60;
311      int sec = sportdata->pc % 60;
312      std::stringstream ss;
313      ss << min << "'" << std::setfill('0') << std::setw(2) << sec << "\"";
314      paceStr = ss.str();
315    } else {
316      paceStr = DEFAULT_DASH;
317    }
318    std::string calorieStr = (sportdata->calories != 0)
319                                 ? std::to_string(sportdata->calories)
320                                 : DEFAULT_DASH;
321    // 训练强度刷新
322    UIResourceTextId intensityid = TraingModel::GetInstance()->GetIntensity();
323    TrainingDetailView::GetInstance()->Get_Intensity_Button()->SetTextId(
324        intensityid);
325
326    TrainingDataView::GetInstance()->GetProgress()->SetValue(
327        sportdata->goaldegree); // 目标完成度
328    TrainingDataView::GetInstance()->GetHeartLabel()->SetText(
329        heartStr.c_str()); // 心率
330    TrainingDetailView::GetInstance()->GetCalorieLabel()->SetText(
331        calorieStr.c_str()); // 卡路里
332    TrainingDetailView::GetInstance()->GetMileLabel()->SetText(
333        distanceStr.c_str()); // 距离
334    TrainingDetailView::GetInstance()->GetStepLabel()->SetText(
335        paceStr.c_str()); // 配速(原步数位置)
336
337    WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,
338                  "[Calories: %s, Distance: %s, Pace: %s Intensityid: %d]",
339                  calorieStr.c_str(), distanceStr.c_str(), paceStr.c_str(),
340                  intensityid);
341    // 释放sportdata内存
342  }
343  #if 1
344  /*刷新 time 和 data */
345  void TraingCardPresenter::updataview(void) {
346    // 如果未开启训练,直接退出
347    if (TraingModel::GetInstance()->GetSportState() == ExerciseState::General) {
348      WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP,
349                    "TraingModel::GetInstance()->GetSportState() == "
350                    "ExerciseState::General ");
351      return;
352    }
353  
354    if (TraingCardView::GetInstance() == nullptr) {
355      WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "view_ == nullptr");
356      return;
357    }
358  
359    Control_Card cardId = static_cast<Control_Card>(
360        TraingCardView::GetInstance()->GetCurrentPage());
361    WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "cardId %d", (int)cardId);
362  
363    switch (cardId) {
364    case Control_Card::CONTROL: {
365      UpdateTimeOfControl(); // 更新控制界面的时间
366      break;
367    }
368    case Control_Card::DATA: {
369      UpdateDataView(); // 更新训练数据视图
370      break;
371    }
372    default:
373      break; // 其他情况不处理
374    }
375  }
376  #else
377  void TraingCardPresenter::Notify(void) {
378    // 如果未开启训练,直接退出
379    if (TraingModel::GetInstance()->GetSportState() == ExerciseState::General ||
380        view_ == nullptr) {
381      return;
382    }
383  
384    Control_Card cardId = static_cast<Control_Card>(view_->GetCurrentPage());
385  
386    switch (cardId) {
387    case Control_Card::CONTROL: {
388      UpdateTimeOfControl(); // 更新控制界面的时间
389      break;
390    }
391    case Control_Card::DATA: {
392      UpdateDataView(); // 更新训练数据视图
393      break;
394    }
395    default:
396      break; // 其他情况不处理
397    }
398  }
399  #endif
400  void TraingCardPresenter::UpdateDataView() {
401    // 刷新计时
402    UpdateTimeOfData();
403  
404    // 刷新训练数据视图,显示当前时间
405    GraphicService::GetInstance()->PostGraphicEvent(std::bind(
406        &TrainingDataView::UpdataTime, TrainingDataView::GetInstance()));
407  
408    TraingModel::GetInstance()->updata_goal();
409  
410    // 刷新训练数据:心率、卡路里、距离、配速、训练强度、目标完成度
411    SportData *sportdata = TraingModel::GetInstance()->GetSportdata();
412    RefreshData(sportdata);
413  }
414  /* --------------------------------- 点击事件处理
415   * --------------------------------- */
416  bool TraingCardPresenter::OnClick(UIView &view, const ClickEvent &event) {
417    const std::string viewId = view.GetViewId();
418  
419    // 定义点击事件映射
420    static const std::unordered_map<std::string, std::function<void()>>
421        actionMap = {
422            // 暂停按键
423            {BUTTONSTOP,
424             [&]() {
425               auto currentState = TraingModel::GetInstance()->GetSportState();
426               if (currentState == ExerciseState::Continue ||
427                   currentState == ExerciseState::Start) {
428                 handlePauseState();
429               } else if (currentState == ExerciseState::Pause) {
430                 handleContinueState();
431               }
432             }},
433            // 结束按键
434            {BUTTONEND,
435             [&]() {
436               //  ControlSensor(ExerciseState::Pause);
437               TraingCardView::GetInstance()->SetPopupViewStatus(EndPopup, true);
438               TraingCardView::GetInstance()->RefreshEndView();
439             }},
440            // 确定结束
441            {BUTTONEXIT, [&]() { handleEndState(); }},
442            // 返回
443            {BUTTONBACK,
444             [&]() {
445               //  ControlSensor(ExerciseState::Continue);
446               TraingCardView::GetInstance()->SetPopupViewStatus(EndPopup, false);
447             }},
448            // 确定达到目标
449            {BUTTONWINOK,
450             [&]() {
451               TraingCardView::GetInstance()->GetAnimator()->Stop();
452               TraingCardView::GetInstance()->SetPopupViewStatus(GetTagPopup,
453                                                                 false);
454             }},
455            // 确定心率提醒
456            {BUTTONNOTE,
457             [&]() {
458               TraingCardView::GetInstance()->SetPopupViewStatus(HeartPopup,
459                                                                 false);
460             }},
461        };
462  
463    // 查找并执行对应的操作
464    auto it = actionMap.find(viewId);
465    if (it != actionMap.end()) {
466      it->second(); // 执行操作
467      return true;
468    }
469  
470    return false; // 没有匹配的操作
471  }
472  void TraingCardPresenter::OnSwipe(UISwipeView &view) {
473    WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "TempPresenter::OnSwipe ");
474    UNUSED(view);
475    return;
476  }
477  bool TraingCardPresenter::OnKeyAct(UIView &view, const KeyEvent &event) {
478    /*消耗按键事件*/
479    return true;
480  }
481  /* -------------------------------- 协议发起运动处理
482   * -------------------------------- */
483  #ifdef SUPPORT_BLE_SERVICE
484  bool TraingCardPresenter::send_sport_cmd(training_ble *cmd) {
485    if (cmd == nullptr) {
486      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "无效的运动命令 cmd 为空!");
487      return false;
488    }
489  
490    // 获取当前的运动状态
491    ExerciseState currentState = TraingModel::GetInstance()->GetSportState();
492    ExerciseState cmdstate = static_cast<ExerciseState>(cmd->controlmd);
493    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "cmdstate:%d currentState:%d",
494                  cmdstate, currentState);
495    int m_sportTag = 0;
496    switch (cmd->controlmd) {
497    case SETGOAL:
498      // 仅在一般状态下允许设置目标
499      if (currentState == ExerciseState::General) {
500        if (cmd->sportTagType == 0) {
501          //训练目标->m 需要转换 km
502          m_sportTag = cmd->sportTag / 1000;
503        } else {
504          m_sportTag = cmd->sportTag;
505        }
506        TraingModel::GetInstance()->SetSportTag(
507            static_cast<ExerciseTag>(cmd->sportTagType), m_sportTag);
508        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "目标设置成功!\r\n");
509        return true;
510      }
511      break;
512  
513    case SENDDATA:
514      // 随时允许发送实时数据
515      TraingModel::GetInstance()->SendLiveData();
516      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "实时数据发送成功!\r\n");
517      return true;
518  
519    default:
520      break;
521    }
522  
523    // 仅在一般状态下允许开始运动
524    if (cmdstate == ExerciseState::Start) {
525      if ((currentState == ExerciseState::General)) {
526        if (TraingCardView::GetInstance() == nullptr) {
527          WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ble 开启运动");
528          // 设置目标类型和项目有且只有一次
529          switch (cmd->sportType) {
530          case 0:
531            TraingModel::GetInstance()->SetSportEvent(STR_EXERCISE_OUTDOORWALKING);
532            OpenSensor(ExerciseClass::OUTDOOR);
533            break;
534          case 1:
535            TraingModel::GetInstance()->SetSportEvent(STR_EXERCISE_OUTDOORRUNNING);
536            OpenSensor(ExerciseClass::OUTDOOR);
537            break;
538          case 2:
539            TraingModel::GetInstance()->SetSportEvent(STR_EXERCISE_OUTDOORBIKING);
540            OpenSensor(ExerciseClass::FREE_TRAINING);
541            break;
542          default:
543            break;
544          }
545        }
546        reply_APP_OpenSportModeCmd(true);
547        // 打印app开启的运动的数据
548        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"*************app开启运动******************");
549        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"sportType:%d",cmd->sportType);
550        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"sportTagType:%d",cmd->sportTagType);
551        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"sportTag:%d",cmd->sportTag);
552        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"*******************************");
553        handleStartState();
554        uapi_set_screen_on_api(); // 亮屏
555        NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGCRAD);
556        return true;
557      } else {
558  
559        reply_APP_OpenSportModeCmd(false);
560        // return false;
561      }
562    }
563    // 仅在运动状态为开始或继续时允许暂停
564    if (cmdstate == ExerciseState::Pause) {
565      if (currentState == ExerciseState::Start ||
566          currentState == ExerciseState::Continue) {
567        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ble 暂停运动");
568        reply_APP_SportPasueResetCmd(TraingModel::GetInstance()->GetSportTime());
569        handlePauseState();
570        return true;
571      }
572    }
573  
574    // 仅在暂停状态下允许恢复运动
575    if (cmdstate == ExerciseState::Continue &&
576        currentState == ExerciseState::Pause) {
577      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ble 恢复运动");
578      reply_APP_SportPasueResetCmd(TraingModel::GetInstance()->GetSportTime());
579      handleContinueState();
580      return true;
581    }
582  
583    // 仅在运动状态为开始、继续或暂停时允许结束运动
584    if (cmdstate == ExerciseState::End &&
585        (currentState == ExerciseState::Start ||
586         currentState == ExerciseState::Continue ||
587         currentState == ExerciseState::Pause)) {
588      WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "ble 结束运动");
589      uint32_t sport_time = TraingModel::GetInstance()->GetSportTime();
590      reply_APP_SportStopModeCmd(sport_time);
591      handleEndState();
592      return true;
593    }
594  
595    // 如果到达这里,命令未执行
596    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
597                  "cmd->sportType[%d],cmd->controlmd[%d],cmd->sportTagType[%d],"
598                  "cmd->sportTag[%d]\r\n",
599                  cmd->sportType, cmd->controlmd, cmd->sportTagType,
600                  cmd->sportTag);
601  
602    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,
603                  "由于状态限制,命令未执行 当前状态 %d。\r\n", currentState);
604    return false; // 没有执行任何命令
605  }
606  #endif
607  /*训练结束*/
608  void TraingCardPresenter::handleEndState() {
609    TraingModel::GetInstance()->SetSportState(ExerciseState::End);
610    ControlSensor(ExerciseState::End);
611    uapi_set_screen_on_api(); // 亮屏
612  #if DATATEST
613    // NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGENDDATA);
614    // 回退
615    NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGNOMAL);
616    return;
617  #endif
618    // 是否达到训练目标
619    if (TraingModel::GetInstance()->GetMomentum()) {
620      // 进入训练详情
621      NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGENDDATA);
622      OHOS::AbilityNavigation::ClearSliceIdStack(); // 清空所有SLiceId栈元素
623      uapi_dev_runtime_set_train_status_is_end_flag(1);
624    } else {
625      // 进入训练记录
626      // NativeAbility::GetInstance().ChangeSlice(VIEW_TRAINING_RECORD);
627      if (TraingModel::GetInstance()->GetEntryType() == APPLIST_ENTER)
628        NativeAbility::GetInstance().ChangeSlice(
629            VIEW_TRAINING, TransitionType::TRANSITION_BACK_TO_HEXAGONS);
630      else {
631        NativeAbility::GetInstance().ChangeSlice(
632            VIEW_MAIN, TransitionType::TRANSITION_BACK_TO_HEXAGONS);
633        OHOS::AbilityNavigation::ClearSliceIdStack(); // 清空所有SLiceId栈元素
634      }
635      OHOS::AbilityNavigation::ClearSliceIdStack(); // 清空所有SLiceId栈元素
636    }
637  }
638  
639  /*训练暂停*/
640  void TraingCardPresenter::handlePauseState() {
641    uapi_set_screen_on_api(); // 亮屏
642    TrainingControlView::GetInstance()->GetLabel()->SetTextId(STR_RESUME);
643    TrainingControlView::GetInstance()->GetButton()->SetImageSrc(
644        SPORT_CONTINUE_IMAGE, SPORT_CONTINUE_IMAGE);
645    TraingModel::GetInstance()->SetSportState(ExerciseState::Pause);
646    PauseTimer();
647    ControlSensor(ExerciseState::Pause);
648  }
649  
650  /*训练继续*/
651  void TraingCardPresenter::handleContinueState() {
652    uapi_set_screen_on_api(); // 亮屏
653    TrainingControlView::GetInstance()->GetLabel()->SetTextId(STR_PAUSE);
654    TrainingControlView::GetInstance()->GetButton()->SetImageSrc(
655        SPORT_PAUSE_IMAGE, SPORT_PAUSE_IMAGE);
656    TraingModel::GetInstance()->SetSportState(ExerciseState::Continue);
657    ResumeTimer();
658    ControlSensor(ExerciseState::Continue);
659  }
660  
661  /*训练开始*/
662  void TraingCardPresenter::handleStartState() {
663    ControlSensor(ExerciseState::Start);
664    TraingModel::GetInstance()->SetSportOpenWay(true);
665    //开启心率
666    TraingModel::GetInstance()->start_training_heart();
667  #if defined(SUPPORT_GNSS)
668    WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "开启gps定时器,频率:2秒/点");
669    TraingModel::GetInstance()->gnss_start_timer(2 * 1000); // 2秒一个GPS点
670  #endif
671    // 快捷训练都属于健身类
672    TraingModel::GetInstance()->SetSportType(Exercisetype::FITNESS);
673  }
674  
675  } // namespace OHOS
676