feat(hrv): 添加心率变异性监控和压力评估功能
- 新增 HRV 监听服务,实时监控心率变异性数据 - 实现 HRV 到压力指数的转换算法和压力等级评估 - 添加智能通知服务,在压力偏高时推送健康建议 - 优化日志系统,修复日志丢失问题并增强刷新机制 - 改进个人页面下拉刷新,支持并行数据加载 - 优化勋章数据缓存策略,减少不必要的网络请求 - 重构应用初始化流程,优化权限服务和健康监听服务的启动顺序 - 移除冗余日志输出,提升应用性能
This commit is contained in:
@@ -5,10 +5,13 @@
|
||||
*/
|
||||
|
||||
import { logger } from '@/utils/logger';
|
||||
import AsyncStorage from '@/utils/kvStore';
|
||||
import dayjs from 'dayjs';
|
||||
import { NativeEventEmitter, NativeModules } from 'react-native';
|
||||
import { analyzeSleepAndSendNotification } from './sleepNotificationService';
|
||||
|
||||
const { HealthKitManager } = NativeModules;
|
||||
const SLEEP_ANALYSIS_SENT_KEY = '@sleep_analysis_sent';
|
||||
|
||||
|
||||
// 睡眠阶段类型
|
||||
@@ -138,6 +141,12 @@ class SleepMonitorService {
|
||||
this.lastProcessedTime = now;
|
||||
|
||||
try {
|
||||
const alreadySentToday = await this.hasSentSleepAnalysisToday();
|
||||
if (alreadySentToday) {
|
||||
console.log('[SleepMonitor] Sleep analysis already sent today, skipping notification');
|
||||
return;
|
||||
}
|
||||
|
||||
// 分析最近的睡眠数据
|
||||
const analysis = await this.analyzeSleepData();
|
||||
|
||||
@@ -150,6 +159,7 @@ class SleepMonitorService {
|
||||
|
||||
// 发送睡眠分析通知
|
||||
await this.notifySleepAnalysis(analysis);
|
||||
await this.markSleepAnalysisSent();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[SleepMonitor] Failed to analyze sleep data:', error);
|
||||
@@ -507,7 +517,30 @@ class SleepMonitorService {
|
||||
|
||||
return recommendations;
|
||||
}
|
||||
|
||||
private getTodayKey(): string {
|
||||
return dayjs().format('YYYY-MM-DD');
|
||||
}
|
||||
|
||||
private async hasSentSleepAnalysisToday(): Promise<boolean> {
|
||||
try {
|
||||
const todayKey = this.getTodayKey();
|
||||
const value = await AsyncStorage.getItem(SLEEP_ANALYSIS_SENT_KEY);
|
||||
return value === todayKey;
|
||||
} catch (error) {
|
||||
logger.warn('[SleepMonitor] Failed to check sleep analysis sent flag:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async markSleepAnalysisSent(): Promise<void> {
|
||||
try {
|
||||
await AsyncStorage.setItem(SLEEP_ANALYSIS_SENT_KEY, this.getTodayKey());
|
||||
} catch (error) {
|
||||
logger.warn('[SleepMonitor] Failed to mark sleep analysis as sent:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例
|
||||
export const sleepMonitorService = new SleepMonitorService();
|
||||
export const sleepMonitorService = new SleepMonitorService();
|
||||
|
||||
Reference in New Issue
Block a user