163 lines
5.6 KiB
JavaScript
163 lines
5.6 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Health Auto Export (iOS) REST automation adapter.
|
|
*
|
|
* Payload shape:
|
|
* { "data": { "metrics": [ { name: "step_count", data: [{ qty, date }] } ],
|
|
* "workouts": [ { id, name, start, end, duration, activeEnergyBurned } ] } }
|
|
*
|
|
* Dates use "yyyy-MM-dd HH:mm:ss Z"; the date part is already local to the
|
|
* phone, so it can be grouped directly. Workout `duration` is seconds in the
|
|
* v2 format (v1 omits it, so fall back to start/end).
|
|
*/
|
|
|
|
const WORKOUT_TYPES = {
|
|
running: { name: 'Running', met: 9.0 },
|
|
walking: { name: 'Walking', met: 4.3 },
|
|
cycling: { name: 'Cycling', met: 7.5 },
|
|
swimming: { name: 'Swimming', met: 6.0 },
|
|
hiking: { name: 'Hiking', met: 6.0 },
|
|
yoga: { name: 'Yoga', met: 2.5 },
|
|
rowing: { name: 'Rowing', met: 7.0 },
|
|
elliptical: { name: 'Elliptical', met: 5.0 },
|
|
dance: { name: 'Dance', met: 5.5 },
|
|
'cardio dance': { name: 'Dance', met: 5.5 },
|
|
hiit: { name: 'HIIT', met: 8.0 },
|
|
'high intensity interval training': { name: 'HIIT', met: 8.0 },
|
|
'traditional strength training': { name: 'Strength training', met: 5.0 },
|
|
'functional strength training': { name: 'Strength training', met: 5.0 },
|
|
'strength training': { name: 'Strength training', met: 5.0 },
|
|
'core training': { name: 'Core training', met: 3.5 },
|
|
flexibility: { name: 'Stretching', met: 2.3 },
|
|
pilates: { name: 'Pilates', met: 3.0 },
|
|
soccer: { name: 'Soccer', met: 7.0 },
|
|
basketball: { name: 'Basketball', met: 6.5 },
|
|
tennis: { name: 'Tennis', met: 7.3 },
|
|
golf: { name: 'Golf', met: 4.8 },
|
|
skiing: { name: 'Skiing', met: 7.0 },
|
|
snowboarding: { name: 'Snowboarding', met: 5.3 },
|
|
surfing: { name: 'Surfing', met: 5.0 },
|
|
climbing: { name: 'Climbing', met: 8.0 },
|
|
'stair climbing': { name: 'Stair climbing', met: 8.8 },
|
|
'jump rope': { name: 'Jump rope', met: 12.3 },
|
|
boxing: { name: 'Boxing', met: 7.8 },
|
|
'martial arts': { name: 'Martial arts', met: 10.3 },
|
|
'team sports': { name: 'Team sport', met: 7.0 },
|
|
other: { name: 'Workout', met: 6.0 },
|
|
};
|
|
|
|
function normalizeName(name) {
|
|
return String(name || '').toLowerCase().replace(/[^a-z0-9]+/g, ' ').trim();
|
|
}
|
|
|
|
function titleCase(name) {
|
|
return String(name || '')
|
|
.replace(/\w\S*/g, (word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
|
|
}
|
|
|
|
function localDay(value) {
|
|
const match = /^(\d{4}-\d{2}-\d{2})/.exec(String(value || ''));
|
|
return match ? match[1] : null;
|
|
}
|
|
|
|
function appleDate(value) {
|
|
if (typeof value !== 'string') return NaN;
|
|
const iso = value.trim().replace(' ', 'T').replace(/ ([+-]\d{2})(\d{2})$/, '$1:$2');
|
|
return Date.parse(iso);
|
|
}
|
|
|
|
function workoutMeta(name) {
|
|
const key = normalizeName(name);
|
|
if (WORKOUT_TYPES[key]) return WORKOUT_TYPES[key];
|
|
if (key.includes('strength')) return { name: 'Strength training', met: 5.0 };
|
|
if (key.includes('run')) return { name: 'Running', met: 9.0 };
|
|
if (key.includes('walk')) return { name: 'Walking', met: 4.3 };
|
|
if (key.includes('cycl') || key.includes('bike')) return { name: 'Cycling', met: 7.5 };
|
|
return { name: titleCase(name) || 'Workout', met: 6.0 };
|
|
}
|
|
|
|
function durationMinutes(workout) {
|
|
const seconds = Number(workout.duration);
|
|
if (Number.isFinite(seconds) && seconds > 0) return seconds / 60;
|
|
|
|
const start = appleDate(workout.start);
|
|
const end = appleDate(workout.end);
|
|
if (Number.isFinite(start) && Number.isFinite(end) && end > start) {
|
|
return (end - start) / 60000;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
function activeKcal(workout) {
|
|
const fields = [workout.activeEnergyBurned, workout.activeEnergy, workout.totalEnergy];
|
|
for (const field of fields) {
|
|
const qty = Number(field && field.qty);
|
|
if (Number.isFinite(qty) && qty > 0) return qty;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
function intensityMet(workout) {
|
|
const qty = Number(workout.intensity && workout.intensity.qty);
|
|
return Number.isFinite(qty) && qty >= 1 && qty <= 25 ? qty : null;
|
|
}
|
|
|
|
function parseHealthAutoExport(payload) {
|
|
const root = payload && typeof payload === 'object' ? payload : {};
|
|
const data = root.data && typeof root.data === 'object' ? root.data : root;
|
|
|
|
const stepDays = new Map();
|
|
const metrics = Array.isArray(data.metrics) ? data.metrics : [];
|
|
|
|
for (const metric of metrics) {
|
|
const name = String((metric && metric.name) || '').toLowerCase().replace(/[^a-z0-9]+/g, '_');
|
|
if (name !== 'step_count') continue;
|
|
|
|
const points = Array.isArray(metric.data) ? metric.data : [];
|
|
for (const point of points) {
|
|
const day = localDay(point && point.date);
|
|
const qty = Number(point && point.qty);
|
|
if (!day || !Number.isFinite(qty) || qty <= 0) continue;
|
|
stepDays.set(day, (stepDays.get(day) || 0) + qty);
|
|
}
|
|
}
|
|
|
|
const rawWorkouts = Array.isArray(data.workouts) ? data.workouts : [];
|
|
const workouts = [];
|
|
const seen = new Set();
|
|
|
|
for (const workout of rawWorkouts) {
|
|
if (!workout || typeof workout !== 'object') continue;
|
|
|
|
const day = localDay(workout.start);
|
|
if (!day) continue;
|
|
|
|
const minutes = durationMinutes(workout);
|
|
if (!(minutes > 0)) continue;
|
|
|
|
const meta = workoutMeta(workout.name);
|
|
const externalId = workout.id
|
|
? `hae:${String(workout.id).slice(0, 120)}`
|
|
: `hae:${normalizeName(workout.name)}:${workout.start}`;
|
|
if (seen.has(externalId)) continue;
|
|
seen.add(externalId);
|
|
|
|
workouts.push({
|
|
externalId,
|
|
date: day,
|
|
name: meta.name,
|
|
minutes: Math.round(minutes * 10) / 10,
|
|
kcal: Math.round(activeKcal(workout)),
|
|
met: intensityMet(workout) || meta.met,
|
|
});
|
|
}
|
|
|
|
return {
|
|
days: Array.from(stepDays, ([date, steps]) => ({ date, steps: Math.round(steps) })),
|
|
workouts,
|
|
};
|
|
}
|
|
|
|
module.exports = { parseHealthAutoExport, workoutMeta };
|