Files
digital-pilates/utils/stress.ts
richarjiang 21e57634e0 feat(hrv): 添加心率变异性监控和压力评估功能
- 新增 HRV 监听服务,实时监控心率变异性数据
- 实现 HRV 到压力指数的转换算法和压力等级评估
- 添加智能通知服务,在压力偏高时推送健康建议
- 优化日志系统,修复日志丢失问题并增强刷新机制
- 改进个人页面下拉刷新,支持并行数据加载
- 优化勋章数据缓存策略,减少不必要的网络请求
- 重构应用初始化流程,优化权限服务和健康监听服务的启动顺序
- 移除冗余日志输出,提升应用性能
2025-11-18 14:08:20 +08:00

66 lines
1.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* HRV 与压力指数转换工具
*
* 提供统一的 HRV -> 压力指数映射及压力等级判定
*/
export type StressLevel = 'low' | 'moderate' | 'high';
export interface StressLevelInfo {
level: StressLevel;
label: string;
description: string;
}
const MIN_HRV = 30;
const MAX_HRV = 130;
/**
* 将 HRVms转换为压力指数0-100数值越高表示压力越大
*/
export function convertHrvToStressIndex(hrv: number | null | undefined): number | null {
if (hrv == null || hrv <= 0) {
return null;
}
const clamped = Math.max(MIN_HRV, Math.min(MAX_HRV, hrv));
const normalized = 100 - ((clamped - MIN_HRV) / (MAX_HRV - MIN_HRV)) * 100;
return Math.round(normalized);
}
/**
* 根据压力指数获取压力等级信息
*/
export function getStressLevelInfo(stressIndex: number | null): StressLevelInfo {
if (stressIndex == null) {
return {
level: 'moderate',
label: '压力未知',
description: '暂无有效的 HRV 数据',
};
}
if (stressIndex >= 70) {
return {
level: 'high',
label: '压力偏高',
description: '建议立即放松,关注呼吸与休息',
};
}
if (stressIndex >= 40) {
return {
level: 'moderate',
label: '压力适中',
description: '保持当前节奏,注意劳逸结合',
};
}
return {
level: 'low',
label: '状态放松',
description: '身心良好,继续保持',
};
}