增加开始结束训练时间戳打印
authorCaisking <3449750381@qq.com>
Sat, 13 Apr 2024 06:13:19 +0000 (14:13 +0800)
committerCaisking <3449750381@qq.com>
Sat, 13 Apr 2024 06:13:19 +0000 (14:13 +0800)
code/sdk/application/wearable/fd230app/nativeui/traing/include/TraingModel.h
code/sdk/application/wearable/fd230app/nativeui/traing/src/TraingModel.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/training_card/include/TraingCardView.h
code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardPresenter.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardView.cpp
code/sdk/application/wearable/service/msg_center/src/msg_center_customer.cpp

index 94d4105..8da6adf 100644 (file)
@@ -41,6 +41,7 @@
 #include "displaybrightness/include/DisplayBrightnessModel.h"
 
 #include <string>
+#include <memory>
 
 struct QuickTraining
 {
@@ -149,8 +150,8 @@ namespace OHOS
     };
     struct SportData
     {
-        uint32 starttime;         // 开始时间时间戳
-        uint32 endtime;           // 结束时间时间戳
+        uint64 starttime;         // 开始时间时间戳
+        uint64 endtime;           // 结束时间时间戳
         int goaldegree;           // 目标完成度
         int calories;             // 训练卡路里
         int duration;             // 训练时长
@@ -198,6 +199,10 @@ namespace OHOS
         SportData *GetSportdata();
         /*获取运动卡路里*/
         int GetCal();
+        /*设置时间戳*/
+        void SetTimestamp();
+        /*解析时间戳*/
+        std::string AnalysisTimestamp(uint64_t timestamp);
 
     private:
         TraingModel(const TraingModel &) = delete;
@@ -206,6 +211,7 @@ namespace OHOS
         std::string motionDuration;                         // 训练时长
         UIResourceTextId sportId_ = STR_EXTREME_SPORTS;     // 训练项目
         ExerciseState sportstate_ = ExerciseState::General; // 训练状态
+        /*std::unique_ptr<SportData> sportdata;*/           // 使用 std::unique_ptr 管理 SportData 对象
     };
 }
 
index 8890dc4..5264620 100644 (file)
 #include "broadcast_feature.h"
 #include "broadcast_service.h"
 #include "samgr_lite.h"
+#include <mutex>
+#include <memory>
 
 #define TIMER 1
 #define CHECK 0
 #define HARD_OFF 1
 #define TRAINING_PATH "user/userdata/sportdata/"
+extern "C" errcode_t uapi_get_timestampe_ms(uint64_t *tm);
 
 #ifdef __cplusplus
 extern "C"
@@ -427,6 +430,8 @@ namespace OHOS
     ExerciseClass TraingModel::GetSportType()
     {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " GetSportType %d", sportId_);
+        // 设置结构体里面的运动类型
+        sportdata->sportId = sportId_;
         switch (sportId_)
         {
             // 室外跑步
@@ -486,6 +491,39 @@ namespace OHOS
     {
         return sportdata->calories;
     }
+    /*设置时间戳*/
+    void TraingModel::SetTimestamp()
+    {
+        // std::lock_guard<std::mutex> lock(mutex); // 使用互斥锁确保线程安全
+        uint64_t time;
+        uapi_get_timestampe_ms(&time);
+        ExerciseState currentState = GetSportState(); // 获取当前运动状态
+
+        if (currentState == ExerciseState::Start)
+        {
+            sportdata->starttime = time;
+        }
+        else if (currentState == ExerciseState::End)
+        {
+            sportdata->endtime = time;
+        }
+
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " SetTimestamp [%d  %lu]", currentState, time);
+    }
+    /*解析时间戳 返回例如:13:00:22*/
+    std::string TraingModel::AnalysisTimestamp(uint64_t timestamp)
+    {
+        // 将时间戳转换为小时、分钟、秒
+        int hours = timestamp / 3600000;             // 1小时 = 3600000毫秒
+        int minutes = (timestamp % 3600000) / 60000; // 1分钟 = 60000毫秒
+        int seconds = (timestamp % 60000) / 1000;    // 1秒 = 1000毫秒
+
+        // 格式化输出为 HH:MM:SS
+        std::stringstream ss;
+        ss << std::setfill('0') << std::setw(2) << hours << ":" << std::setw(2) << minutes << ":" << std::setw(2) << seconds;
+
+        return ss.str();
+    }
 /* -------------------------------------------------------------------------- */
 #if TIMER
     /* 时间显示 */
index bc893f9..98ff791 100644 (file)
@@ -74,7 +74,7 @@ namespace OHOS
 \r
         void SetEndViewStatus(bool status);\r
         void ShowEndView();\r
-        void RefreshEndView();\r
+        bool RefreshEndView();\r
         UIView *GetTraingCrad();\r
         int GetCurrentPage();\r
 \r
index e9366d6..87be72b 100644 (file)
@@ -204,7 +204,7 @@ namespace OHOS
         if (strcmp(view.GetViewId(), BUTTONSTOP) == 0)\r
         {\r
             // 开始状态\r
-            if (TraingModel::GetInstance()->GetSportState() == ExerciseState::Start)\r
+            if (TraingModel::GetInstance()->GetSportState() == ExerciseState::Start || TraingModel::GetInstance()->GetSportState() == ExerciseState::Continue)\r
             {\r
                 TrainingControlView::GetInstance()->GetLabel()->SetText("继续");\r
                 TrainingControlView::GetInstance()->GetButton()->SetImageSrc(SPORT_CONTINUE_IMAGE, SPORT_CONTINUE_IMAGE);\r
@@ -221,8 +221,8 @@ namespace OHOS
             {\r
                 TrainingControlView::GetInstance()->GetLabel()->SetText("暂停");\r
                 TrainingControlView::GetInstance()->GetButton()->SetImageSrc(SPORT_PAUSE_IMAGE, SPORT_PAUSE_IMAGE);\r
-                // 设置为开始状态\r
-                TraingModel::GetInstance()->SetSportState(ExerciseState::Start);\r
+                // 设置为继续状态\r
+                TraingModel::GetInstance()->SetSportState(ExerciseState::Continue);\r
                 // 恢复计时器\r
                 ResumeTimer();\r
                 // 恢复运动库\r
@@ -241,8 +241,32 @@ namespace OHOS
         // 退出按键\r
         if (strcmp(view.GetViewId(), BUTTONEXIT) == 0)\r
         {\r
+            TraingModel::GetInstance()->SetSportState(ExerciseState::End);\r
             ControlSensor(ExerciseState::End);\r
-            NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGWIN);\r
+            if (TraingCardView::GetInstance()->RefreshEndView())\r
+            {\r
+                NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGWIN);\r
+            }\r
+            else\r
+            {\r
+                NativeAbility::GetInstance().ChangeSlice(VIEW_TRAININGMORE);\r
+            }\r
+#if 0\r
+            // 在点击事件中打印 SportData 对象之前,确保结束时间戳已经被设置\r
+            if (TraingModel::GetInstance()->GetSportdata()->endtime == 0 && TraingModel::GetInstance()->GetSportState() == ExerciseState::End)\r
+            {\r
+                PRINT("RESET ENDTIMESTAMP\r\n");\r
+                TraingModel::GetInstance()->SetTimestamp(); // 如果结束时间戳为0且当前为结束状态,再次调用 SetTimestamp 设置结束时间戳\r
+            }\r
+            // 打印 SportData 中的数据\r
+            SportData *sportData = TraingModel::GetInstance()->GetSportdata();\r
+\r
+            std::string formattedStartTime = TraingModel::GetInstance()->AnalysisTimestamp(sportData->starttime);\r
+            std::string formattedEndTime = TraingModel::GetInstance()->AnalysisTimestamp(sportData->endtime);\r
+            std::string formattedTimeRange = formattedStartTime + "-" + formattedEndTime;\r
+\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "【SportData - StartTime: [%lu], EndTime: [%lu]  %s】", sportData->starttime, sportData->endtime, formattedTimeRange.c_str());\r
+#endif\r
         }\r
         // 返回按键\r
         if (strcmp(view.GetViewId(), BUTTONBACK) == 0)\r
index 8857a41..d8cffaf 100644 (file)
@@ -38,8 +38,7 @@ namespace OHOS
         InitTimer();\r
         // 设置运动状态为开始\r
         TraingModel::GetInstance()->SetSportState(ExerciseState::Start);\r
-        // 设置常亮\r
-        ScreenAlwaysOnCtr(true);\r
+        TraingModel::GetInstance()->SetTimestamp();\r
     }\r
 \r
     TraingCardView::~TraingCardView()\r
@@ -50,8 +49,7 @@ namespace OHOS
         DisableTimer();\r
         // 设置运动状态为结束\r
         TraingModel::GetInstance()->SetSportState(ExerciseState::End);\r
-        // 设置为正常\r
-        ScreenAlwaysOnCtr(false);\r
+        TraingModel::GetInstance()->SetTimestamp();\r
     }\r
 \r
     TraingCardView *TraingCardView::GetInstance()\r
@@ -121,7 +119,7 @@ namespace OHOS
             delete MusicView;\r
             MusicView = nullptr;\r
         }\r
-        if (MusicView != nullptr)\r
+        if (endView != nullptr)\r
         {\r
             endView->RemoveAll();\r
             delete endView;\r
@@ -286,7 +284,7 @@ namespace OHOS
         return;\r
     }\r
     /*获取运动量结束弹窗界面刷新*/\r
-    void TraingCardView::RefreshEndView()\r
+    bool TraingCardView::RefreshEndView()\r
     {\r
         // 获取训练卡路里判断运动量\r
         int caldata = TraingModel::GetInstance()->GetCal();\r
@@ -306,6 +304,7 @@ namespace OHOS
             endtitle->SetTextId(STR_LOOK_OUT);\r
             notelabel->SetTextId(STR_INSUFFICIENT_EXERCISE_AMOUNT);\r
         }\r
+        return momentumFlag;\r
     }\r
     /*获取卡片View单例*/\r
     UIView *TraingCardView::GetTraingCrad()\r
index 53c69a2..f3756e7 100755 (executable)
@@ -126,6 +126,7 @@ void MsgCenterSubscribe()
     MsgCenterSubscribeTopic(TOPIC_EVENT_HFP_FINISHED);
     MsgCenterSubscribeTopic(TOPIC_EVENT_HFP_PHONE_TO_WATCH);
     MsgCenterSubscribeTopic(TOPIC_EVENT_HFP_WATCH_TO_PHONE);
+#ifdef CYWEE_SPORT
     MsgCenterSubscribeTopic(TOPIC_EVENT_NORMAL_UPDATE);
     MsgCenterSubscribeTopic(TOPIC_EVENT_TREADMILL_UPDATE);
     MsgCenterSubscribeTopic(TOPIC_EVENT_OUTDOOR_RUNNING_UPDATE);
@@ -134,6 +135,7 @@ void MsgCenterSubscribe()
     MsgCenterSubscribeTopic(TOPIC_EVENT_FREE_TRAINING_UPDATE);
     MsgCenterSubscribeTopic(TOPIC_EVENT_AR_ALERT_UPDATE);
     MsgCenterSubscribeTopic(TOPIC_EVENT_EXTRA_INFO_UPDATE);
+#endif
 #ifdef SUPPORT_CHARGER_PLUG_DETECTION_NOTIFICATION
     MsgCenterSubscribeTopic(TOPIC_EVENT_CHARGE_PLUG);
     MsgCenterSubscribeTopic(TOPIC_EVENT_CHARGE_UNPLUG);
@@ -166,7 +168,6 @@ void MsgCenterSubscribe()
 #endif 
     MsgCenterSubscribeTopic(TOPIC_EVENT_FIND_WATCH);
     MsgCenterSubscribeTopic(TOPIC_EVENT_CANCEL_FIND_WATCH);
-
 }
 
 static OHOS::UIViewGroup* showAlarmNotify(void)