feat: 更新健康数据权限描述,添加HRV数据获取测试功能,优化后台任务配置,调整压力计显示单位
This commit is contained in:
@@ -452,22 +452,43 @@ async function fetchSleepDuration(options: HealthDataOptions): Promise<number> {
|
||||
|
||||
async function fetchHeartRateVariability(options: HealthDataOptions): Promise<number | null> {
|
||||
return new Promise((resolve) => {
|
||||
console.log('=== 开始获取HRV数据 ===');
|
||||
console.log('查询选项:', options);
|
||||
|
||||
AppleHealthKit.getHeartRateVariabilitySamples(options, (err, res) => {
|
||||
console.log('HRV API调用结果:', { err, res });
|
||||
|
||||
if (err) {
|
||||
logError('HRV数据', err);
|
||||
console.error('HRV获取错误详情:', err);
|
||||
return resolve(null);
|
||||
}
|
||||
|
||||
if (!res || !Array.isArray(res) || res.length === 0) {
|
||||
logWarning('HRV', '为空或格式错误');
|
||||
console.warn('HRV数据为空,原始响应:', res);
|
||||
return resolve(null);
|
||||
}
|
||||
|
||||
logSuccess('HRV', res);
|
||||
console.log('HRV数据样本数量:', res.length);
|
||||
|
||||
// 打印最新的几个样本用于调试
|
||||
const latestSamples = res.slice(-3);
|
||||
console.log('最新的HRV样本:', latestSamples.map(sample => ({
|
||||
value: sample.value,
|
||||
startDate: sample.startDate,
|
||||
endDate: sample.endDate
|
||||
})));
|
||||
|
||||
const latestHrv = res[res.length - 1];
|
||||
if (latestHrv && latestHrv.value) {
|
||||
if (latestHrv && latestHrv.value !== undefined && latestHrv.value !== null) {
|
||||
// HealthKit 中的 HRV 数据已经是毫秒单位,无需转换
|
||||
resolve(Math.round(latestHrv.value));
|
||||
const hrvValue = Math.round(latestHrv.value);
|
||||
console.log('最终HRV值:', hrvValue);
|
||||
resolve(hrvValue);
|
||||
} else {
|
||||
console.warn('HRV样本值无效:', latestHrv);
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
@@ -681,6 +702,41 @@ export async function fetchRecentHRV(hoursBack: number = 2): Promise<number | nu
|
||||
return fetchHeartRateVariability(options);
|
||||
}
|
||||
|
||||
// 测试HRV数据获取功能
|
||||
export async function testHRVDataFetch(date: Date = dayjs().toDate()): Promise<void> {
|
||||
console.log('=== 开始测试HRV数据获取 ===');
|
||||
|
||||
try {
|
||||
// 首先确保权限
|
||||
const hasPermission = await ensureHealthPermissions();
|
||||
if (!hasPermission) {
|
||||
console.error('没有健康数据权限,无法测试HRV');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('权限检查通过,开始获取HRV数据...');
|
||||
|
||||
// 测试不同时间范围的HRV数据
|
||||
const options = createDateRange(date);
|
||||
|
||||
// 获取今日HRV
|
||||
const todayHRV = await fetchHeartRateVariability(options);
|
||||
console.log('今日HRV结果:', todayHRV);
|
||||
|
||||
// 获取最近2小时HRV
|
||||
const recentHRV = await fetchRecentHRV(2);
|
||||
console.log('最近2小时HRV结果:', recentHRV);
|
||||
|
||||
// 获取指定日期HRV
|
||||
const dateHRV = await fetchHRVForDate(date);
|
||||
console.log('指定日期HRV结果:', dateHRV);
|
||||
|
||||
console.log('=== HRV数据测试完成 ===');
|
||||
} catch (error) {
|
||||
console.error('HRV测试过程中出现错误:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新healthkit中的体重
|
||||
export async function updateWeight(weight: number) {
|
||||
return new Promise((resolve) => {
|
||||
|
||||
Reference in New Issue
Block a user