From 7447481322cc876eea205294d44b43cdee9cb5ff Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Thu, 23 Oct 2025 14:25:14 +0800 Subject: [PATCH] =?utf8?q?272=EF=BC=9A=E5=8E=8B=E5=8A=9B=E7=95=8C=E9=9D=A2?= =?utf8?q?=E4=BC=98=E5=8C=96=EF=BC=8C=E4=BD=BF=E7=94=A8=E9=9A=8F=E6=9C=BA?= =?utf8?q?=E6=95=B0=E6=A8=A1=E6=8B=9F=E4=BC=A0=E6=84=9F=E5=99=A8=E5=87=BA?= =?utf8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/stress/include/stressTestModel.h | 3 +- .../stress/include/stressTestStandbyView.h | 2 +- .../nativeui/stress/src/stressTestModel.cpp | 107 +++++++++++++++++- .../stress/src/stressTestPresenter.cpp | 2 +- .../stress/src/stressTestResultView.cpp | 2 - 5 files changed, 106 insertions(+), 10 deletions(-) diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestModel.h b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestModel.h index d61adef3a..eb41966fc 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestModel.h +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestModel.h @@ -1,6 +1,7 @@ #ifndef STRESS_TEST_MODEL_H #define STRESS_TEST_MODEL_H +#include #include #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: diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestStandbyView.h b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestStandbyView.h index b999cdeac..f3e5bb15a 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestStandbyView.h +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/include/stressTestStandbyView.h @@ -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 { diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestModel.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestModel.cpp index 7b35e40e8..5bd70207d 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestModel.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestModel.cpp @@ -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 #include +#include // 包含 rand()、srand() 函数 +#include // 包含 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(); } } } diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestPresenter.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestPresenter.cpp index b20c0776d..c80a3ee4f 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestPresenter.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestPresenter.cpp @@ -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) { diff --git a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestResultView.cpp b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestResultView.cpp index 1a1717b8c..a84f8470a 100644 --- a/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestResultView.cpp +++ b/code/sdk/application/wearable/fd272/nativeapp/nativeui/stress/src/stressTestResultView.cpp @@ -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); -- 2.17.1