debug: add console.log at every step to trace execution

This commit is contained in:
Anthony Merlo 2026-08-21 00:14:41 +01:00
parent aa05c9c8d4
commit e01c0b92e3

View file

@ -16,10 +16,15 @@ async function readLatest(type, unit) {
h.setLimit(1); h.setLimit(1);
h.setDescendingSorting(); h.setDescendingSorting();
const samples = await h.quantitySamples(); 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]; const s = samples[0];
console.log(`readLatest(${type}): value=${s.value} date=${s.startDate}`);
return { value: s.value, startDate: new Date(s.startDate) }; return { value: s.value, startDate: new Date(s.startDate) };
} catch (e) { } catch (e) {
console.log(`readLatest(${type}): error ${e}`);
return null; return null;
} }
} }
@ -33,14 +38,19 @@ async function readTodaySum(type, unit) {
h.setTypeIdentifier(type); h.setTypeIdentifier(type);
h.setUnit(unit); h.setUnit(unit);
const samples = await h.quantitySamples(); 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; let total = 0;
for (const s of samples) { for (const s of samples) {
const sd = new Date(s.startDate); const sd = new Date(s.startDate);
if (sd >= start) total += s.value; if (sd >= start) total += s.value;
} }
console.log(`readTodaySum(${type}): total=${total}`);
return total; return total;
} catch (e) { } catch (e) {
console.log(`readTodaySum(${type}): error ${e}`);
return null; return null;
} }
} }
@ -50,6 +60,7 @@ function todayDate() {
} }
async function notify(body) { async function notify(body) {
console.log(`notify: ${body}`);
const n = new Notification(); const n = new Notification();
n.title = "Health Sync"; n.title = "Health Sync";
n.body = body; n.body = body;
@ -57,6 +68,8 @@ async function notify(body) {
} }
// --- Read all health metrics --- // --- Read all health metrics ---
console.log("=== Health Sync starting ===");
const weight = await readLatest("bodyMass", "kg"); const weight = await readLatest("bodyMass", "kg");
const bpSys = await readLatest("bloodPressureSystolic", "mmHg"); const bpSys = await readLatest("bloodPressureSystolic", "mmHg");
const bpDia = await readLatest("bloodPressureDiastolic", "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 steps = await readTodaySum("stepCount", "count");
const spo2 = await readLatest("oxygenSaturation", "%"); 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 --- // --- Build samples array — only include metrics that have data ---
const samples = []; const samples = [];
@ -124,6 +139,8 @@ if (spo2) {
}); });
} }
console.log(`Total samples to send: ${samples.length}`);
// --- POST to server --- // --- POST to server ---
if (samples.length === 0) { if (samples.length === 0) {
await notify("No health data found today"); await notify("No health data found today");
@ -139,9 +156,12 @@ req.body = JSON.stringify({ samples: samples });
try { try {
const res = await req.loadJSON(); 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) { } catch (e) {
await notify(`✗ Error: ${e.message || e}`); console.log(`POST error: ${e}`);
await notify(`Error: ${e.message || e}`);
} }
} // end else } // end else
console.log("=== Health Sync done ===");