272:压力界面优化,使用随机数模拟传感器出值 2591/2 2591/head 2592/target/1
authorluozheng <1732036298@qq.com>
Thu, 23 Oct 2025 06:25:14 +0000 (14:25 +0800)
committerluozheng <1732036298@qq.com>
Thu, 23 Oct 2025 06:25:14 +0000 (14:25 +0800)
code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestModel.h
code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestStandbyView.h
code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestModel.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestPresenter.cpp
code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestResultView.cpp

index d61adef..eb41966 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef STRESS_TEST_MODEL_H
 #define STRESS_TEST_MODEL_H
 
+#include <cstdint>
 #include <inttypes.h>
 #include "string"
 #include "iostream"
@@ -39,7 +40,7 @@ namespace OHOS
         void LoadResultFromFile(void);
 
         // 定时器相关
-        void StressTest_StartTimer(uint32_t interval = 60 * 1000, int type = 0);
+        void StressTest_StartTimer(uint32_t interval = 1 * 1000, int type = 0);
         void StressTest_StopTimer(void);
 
     private:
index b999cde..f3e5bb1 100644 (file)
@@ -7,7 +7,7 @@
 #include "components/ui_label_button_ext.h"
 namespace OHOS
 {
-    static constexpr const char *STRESSTESTSTANDBYVIEW_ID = "STRESSTESTSTANDBYVIEW_ID";
+    static constexpr const char *STRESSTESTSTANDBYVIEW_ID = "STRESSTESTSTANDBYVIEW_ID";//待机页面
     static constexpr const char *START_TEST_BUTTON_ID = "START_TEST_BUTTON_ID";
     class StressTestStandbyView
     {
index 7b35e40..5bd7020 100644 (file)
@@ -1,5 +1,6 @@
 #include "stress/include/stressTestModel.h"
 #include "stress/include/stressTestUIConfig.h"
+#include "stress/include/stressTestPresenter.h"
 #include "wearable_log.h"
 #include "graphic_timer.h"
 #include "ohos_timer.h"
@@ -7,6 +8,8 @@
 #include "custom_utils.h"
 #include <vibrator_service.h>
 #include <stdio.h>
+#include <stdlib.h> // 包含 rand()、srand() 函数
+#include <time.h>   // 包含 time() 函数,用于生成随机种子
 
 namespace OHOS
 {
@@ -156,13 +159,106 @@ namespace OHOS
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "StressTestModel::HandleResultValue: %dms", currentResultValue);
     }
 
+    static uint16_t StressAlgo_FetchOneSecondData(void)
+    {
+        // 初始化随机种子(仅需初始化一次,放在函数外更高效)
+        static int initialized = 0;
+        if (!initialized)
+        {
+            srand((unsigned int)time(NULL)); // 以当前时间作为种子,确保每次运行结果不同
+            initialized = 1;
+        }
+
+        // rand() 生成 [0, RAND_MAX] 的随机整数,取模 101 后得到 [0, 100]
+        return (uint16_t)(rand() % 101);
+    }
+
+    GraphicTimer *g_stressTestTimer = nullptr;
+    static int g_stressTestTimerCount = 0;  // 压力测试定时器计数
+    static int g_invalidConsecutiveSec = 0; // 无效连续次数计数
+    static uint8_t validCount{0};           // 有效数据个数
+    static uint16_t ringBuffer[60]{0};      // 压力测试结果环形缓冲区
+
+    static void resetStressTestTimerData(void)
+    {
+        g_stressTestTimerCount = 0;
+        g_invalidConsecutiveSec = 0;
+        validCount = 0;
+        for (int i = 0; i < 60; i++)
+        {
+            ringBuffer[i] = 0;
+        }
+    }
+
     static void StressTestTimerCallbackProc(void *data)
     {
         (void)data;
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "StressTestModel::StressTestTimerCallbackProc:60s压力测试结束");
-        StressTestModel::GetInstance().StressTest_StopTimer();
-        // 震动提醒
-        custom_VibratorFunc_int(0);
+        //秒
+        g_stressTestTimerCount++;
+        // 模拟出压力测试值
+        uint16_t rawValueMs = StressAlgo_FetchOneSecondData();
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "当前秒数: %d, 压力测试值: %dms", g_stressTestTimerCount, rawValueMs);
+
+        bool isValueRange = (rawValueMs >= 0 && rawValueMs <= 100);
+        if (!isValueRange)
+        {
+            g_invalidConsecutiveSec++;
+        }
+        else
+        {
+            g_invalidConsecutiveSec = 0;
+            ringBuffer[validCount % 60] = rawValueMs;
+            validCount++;
+        }
+
+        /* 无效连续次数超过10次,停止压力测试 */
+        if (g_invalidConsecutiveSec >= 10)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "StressTestModel::StressTestTimerCallbackProc: 无效连续次数超过10次,停止压力测试");
+            StressTestModel::GetInstance().StressTest_StopTimer();
+            // 震动提醒
+            custom_VibratorFunc_int(0);
+            if (StressTestPresenter::GetInstance())
+            {
+                StressTestPresenter::GetInstance()->ChangeViewOperation(StressTestModel::GetInstance().GetCurrentView(), STRESSTESTFAILVIEW);
+            }
+            return;
+        }
+        // 完成判定
+        if (g_stressTestTimerCount >= 60 && validCount >= 40)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "StressTestModel::StressTestTimerCallbackProc: 完成判定,计算最终结果");
+            // 计算最终结果,有效值的均值
+            uint16_t sum = 0;
+            for (int i = 0; i < validCount; i++)
+            {
+                sum += ringBuffer[i];
+            }
+            uint16_t finalMs = sum / validCount;
+            StressTestModel &m = StressTestModel::GetInstance();
+            m.HandleResultValue(finalMs); // 分档上报
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "StressTestModel::StressTestTimerCallbackProc: 最终结果: %dms", finalMs);
+            // 切换到结果页面
+            if (StressTestPresenter::GetInstance())
+            {
+                StressTestPresenter::GetInstance()->ChangeViewOperation(StressTestModel::GetInstance().GetCurrentView(), STRESSTESTRESULTVIEW);
+            }
+            //停止压力测试定时器
+            StressTestModel::GetInstance().StressTest_StopTimer();
+        }
+        else if (g_stressTestTimerCount >= 60 && validCount < 40)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "StressTestModel::StressTestTimerCallbackProc: 完成判定,计算最终结果");
+            //停止压力测试定时器
+            StressTestModel::GetInstance().StressTest_StopTimer();
+            // 震动提醒
+            custom_VibratorFunc_int(0);
+            // 切换到结果页面
+            if (StressTestPresenter::GetInstance())
+            {
+                StressTestPresenter::GetInstance()->ChangeViewOperation(StressTestModel::GetInstance().GetCurrentView(), STRESSTESTFAILVIEW);
+            }
+        }
     }
 
     static void StressTestTimerCallback(void *data)
@@ -170,7 +266,6 @@ namespace OHOS
         GraphicService::GetInstance()->PostGraphicEvent(std::bind(StressTestTimerCallbackProc, data));
     }
 
-    GraphicTimer *g_stressTestTimer = nullptr;
     // 开启压力测试定时器
     void StressTestModel::StressTest_StartTimer(uint32_t interval, int type)
     {
@@ -216,6 +311,8 @@ namespace OHOS
             g_stressTestTimer = nullptr;
             // 取消常亮屏
             ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON);
+            // 重置压力测试定时器数据
+            resetStressTestTimerData();
         }
     }
 }
index b20c077..c80a3ee 100644 (file)
@@ -109,7 +109,7 @@ namespace OHOS
             }
             else if (view.GetViewId() == STRESSTESTINGVIEW_ID)
             {
-                ChangeViewOperation(StressTestModel::GetInstance().GetCurrentView(), STRESSTESTIDLEVIEW);
+                // ChangeViewOperation(StressTestModel::GetInstance().GetCurrentView(), STRESSTESTIDLEVIEW);
             }
             else if (view.GetViewId() == STRESS_TEST_RESULT_VIEW_ID)
             {
index 1a1717b..a84f847 100644 (file)
@@ -187,7 +187,6 @@ namespace OHOS
                 return nullptr;
             }
             ValueNamesLabel->SetPosition(53, 261, 360, 35);
-            ValueNamesLabel->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
             ValueNamesLabel->SetStyle(STYLE_TEXT_FONT, 30);
             ValueNamesLabel->SetStyle(STYLE_LINE_HEIGHT, 35);
             ValueNamesLabel->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION);
@@ -205,7 +204,6 @@ namespace OHOS
             }
             descriptionLabel->SetPosition(53, 315, 360, 70);
             descriptionLabel->SetText("压力在健康范围,建议继续保持规律作息,状态良好");
-            descriptionLabel->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
             descriptionLabel->SetStyle(STYLE_TEXT_FONT, 30);
             descriptionLabel->SetStyle(STYLE_LINE_HEIGHT, 35);
             descriptionLabel->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);