diff --git a/scripts/health-sync.js b/scripts/health-sync.js index f72423a..0bcd549 100644 --- a/scripts/health-sync.js +++ b/scripts/health-sync.js @@ -16,10 +16,15 @@ async function readLatest(type, unit) { h.setLimit(1); h.setDescendingSorting(); const samples = await h.quantitySamples(); - if (!samples || samples.length === 0) return null; + if (!samples || samples.length === 0) { + console.log(`readLatest(${type}): no samples`); + return null; + } const s = samples[0]; + console.log(`readLatest(${type}): value=${s.value} date=${s.startDate}`); return { value: s.value, startDate: new Date(s.startDate) }; } catch (e) { + console.log(`readLatest(${type}): error ${e}`); return null; } } @@ -33,14 +38,19 @@ async function readTodaySum(type, unit) { h.setTypeIdentifier(type); h.setUnit(unit); const samples = await h.quantitySamples(); - if (!samples || samples.length === 0) return null; + if (!samples || samples.length === 0) { + console.log(`readTodaySum(${type}): no samples`); + return null; + } let total = 0; for (const s of samples) { const sd = new Date(s.startDate); if (sd >= start) total += s.value; } + console.log(`readTodaySum(${type}): total=${total}`); return total; } catch (e) { + console.log(`readTodaySum(${type}): error ${e}`); return null; } } @@ -50,6 +60,7 @@ function todayDate() { } async function notify(body) { + console.log(`notify: ${body}`); const n = new Notification(); n.title = "Health Sync"; n.body = body; @@ -57,6 +68,8 @@ async function notify(body) { } // --- Read all health metrics --- +console.log("=== Health Sync starting ==="); + const weight = await readLatest("bodyMass", "kg"); const bpSys = await readLatest("bloodPressureSystolic", "mmHg"); const bpDia = await readLatest("bloodPressureDiastolic", "mmHg"); @@ -65,6 +78,8 @@ const restingHr = await readLatest("restingHeartRate", "count/min"); const steps = await readTodaySum("stepCount", "count"); const spo2 = await readLatest("oxygenSaturation", "%"); +console.log(`Results: weight=${weight?.value} bpSys=${bpSys?.value} bpDia=${bpDia?.value} hr=${hr?.value} rhr=${restingHr?.value} steps=${steps} spo2=${spo2?.value}`); + // --- Build samples array — only include metrics that have data --- const samples = []; @@ -124,6 +139,8 @@ if (spo2) { }); } +console.log(`Total samples to send: ${samples.length}`); + // --- POST to server --- if (samples.length === 0) { await notify("No health data found today"); @@ -139,9 +156,12 @@ req.body = JSON.stringify({ samples: samples }); try { const res = await req.loadJSON(); - await notify(`✓ Sent ${res.ingested || samples.length} samples`); + console.log(`Server response: ${JSON.stringify(res)}`); + await notify(`Sent ${res.ingested || samples.length} samples`); } catch (e) { - await notify(`✗ Error: ${e.message || e}`); + console.log(`POST error: ${e}`); + await notify(`Error: ${e.message || e}`); } -} // end else \ No newline at end of file +} // end else +console.log("=== Health Sync done ==="); \ No newline at end of file