fix: rewrite health-sync.js for Scripting app API
- fetch() instead of new Request() + req.loadJSON() (Scriptable API)
- Notification.schedule({title, body}) instead of new Notification() + n.schedule()
- allowInsecureRequest: true for HTTP (Scripting app blocks HTTP by default)
- This was the real reason 'nothing happened' — Scriptable API doesn't exist in Scripting app
This commit is contained in:
parent
067bf7a769
commit
ac2d2ad0ff
1 changed files with 17 additions and 14 deletions
|
|
@ -2,10 +2,12 @@
|
||||||
// Run via Scripting app automation at 9:30am daily
|
// Run via Scripting app automation at 9:30am daily
|
||||||
// Server: http://chronos:9432/ingest
|
// Server: http://chronos:9432/ingest
|
||||||
//
|
//
|
||||||
// Uses Scripting app's Health API:
|
// Uses Scripting app APIs:
|
||||||
// Health.queryQuantitySamples(type, options) — returns samples
|
// Health.queryQuantitySamples(type, options) — returns samples
|
||||||
// sample.quantityValue(unit) — extracts numeric value
|
// sample.quantityValue(unit) — extracts numeric value
|
||||||
// HealthUnit / HealthMetricPrefix — unit construction
|
// HealthUnit / HealthMetricPrefix — unit construction
|
||||||
|
// fetch() — standard Fetch API (with allowInsecureRequest for HTTP)
|
||||||
|
// Notification.schedule({ title, body }) — static method
|
||||||
//
|
//
|
||||||
// Docs: https://scriptingapp.github.io/guide/Device%20Capabilities/Health/
|
// Docs: https://scriptingapp.github.io/guide/Device%20Capabilities/Health/
|
||||||
|
|
||||||
|
|
@ -77,10 +79,10 @@ function todayDate() {
|
||||||
|
|
||||||
async function notify(body) {
|
async function notify(body) {
|
||||||
console.log(`notify: ${body}`);
|
console.log(`notify: ${body}`);
|
||||||
const n = new Notification();
|
await Notification.schedule({
|
||||||
n.title = "Health Sync";
|
title: "Health Sync",
|
||||||
n.body = body;
|
body: body
|
||||||
await n.schedule();
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Read all health metrics ---
|
// --- Read all health metrics ---
|
||||||
|
|
@ -162,16 +164,17 @@ if (samples.length === 0) {
|
||||||
await notify("No health data found today");
|
await notify("No health data found today");
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
const req = new Request(SERVER_URL);
|
try {
|
||||||
req.method = "POST";
|
const response = await fetch(SERVER_URL, {
|
||||||
req.headers = {
|
method: "POST",
|
||||||
|
headers: {
|
||||||
"Authorization": `Bearer ${API_KEY}`,
|
"Authorization": `Bearer ${API_KEY}`,
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
};
|
},
|
||||||
req.body = JSON.stringify({ samples: samples });
|
body: JSON.stringify({ samples: samples }),
|
||||||
|
allowInsecureRequest: true
|
||||||
try {
|
});
|
||||||
const res = await req.loadJSON();
|
const res = await response.json();
|
||||||
console.log(`Server response: ${JSON.stringify(res)}`);
|
console.log(`Server response: ${JSON.stringify(res)}`);
|
||||||
await notify(`Sent ${res.ingested || samples.length} samples`);
|
await notify(`Sent ${res.ingested || samples.length} samples`);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue