#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"
#include "custom_utils.h"
#include <vibrator_service.h>
#include <stdio.h>
+#include <stdlib.h> // 包含 rand()、srand() 函数
+#include <time.h> // 包含 time() 函数,用于生成随机种子
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)
GraphicService::GetInstance()->PostGraphicEvent(std::bind(StressTestTimerCallbackProc, data));
}
- GraphicTimer *g_stressTestTimer = nullptr;
// 开启压力测试定时器
void StressTestModel::StressTest_StartTimer(uint32_t interval, int type)
{
g_stressTestTimer = nullptr;
// 取消常亮屏
ScreenAlwaysOnCtr(CANCEL_SCREEN_ALWAYS_ON);
+ // 重置压力测试定时器数据
+ resetStressTestTimerData();
}
}
}