health 0.33: Ensure readAllRecordsSince always includes the current day

Speed improvements (put temporary functions in RAM where possible)

Fixes https://github.com/orgs/espruino/discussions/7800#discussioncomment-13618010
master
Gordon Williams 2025-06-30 14:53:32 +01:00
parent a63b7b5139
commit 4de0fca8be
3 changed files with 26 additions and 25 deletions

View File

@ -34,4 +34,6 @@
0.30: Minor code improvements 0.30: Minor code improvements
0.31: Add support for new health format (storing more data) 0.31: Add support for new health format (storing more data)
Added graphs for Temperature and Battery Added graphs for Temperature and Battery
0.32: If getting HRM every 3/10 minutes, don't turn it on if the Bangle is charging or hasn't moved and is face down/up 0.32: If getting HRM every 3/10 minutes, don't turn it on if the Bangle is charging or hasn't moved and is face down/up
0.33: Ensure readAllRecordsSince always includes the current day
Speed improvements (put temporary functions in RAM where possible)

View File

@ -41,23 +41,23 @@ exports.getDecoder = function(fileContents) {
return { return {
r : 10, // record length r : 10, // record length
clr : "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", clr : "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
decode : h => { var v = { decode : h => { "ram"; var d = h.charCodeAt.bind(h), v = {
steps : (h.charCodeAt(0)<<8) | h.charCodeAt(1), steps : (d(0)<<8) | d(1),
bpmMin : h.charCodeAt(2), bpmMin : d(2),
bpmMax : h.charCodeAt(3), bpmMax : d(3),
movement : h.charCodeAt(4)*8, movement : d(4)*8,
battery : h.charCodeAt(5)&127, battery : d(5)&127,
isCharging : !!(h.charCodeAt(5)&128), isCharging : !!(d(5)&128),
temperature : h.charCodeAt(6)/2, // signed? temperature : d(6)/2, // signed?
altitude : ((h.charCodeAt(7)&31)<<8)|h.charCodeAt(8), // signed? altitude : ((d(7)&31)<<8)|d(8), // signed?
activity : exports.ACTIVITY[h.charCodeAt(7)>>5] activity : exports.ACTIVITY[d(7)>>5]
}; };
if (v.temperature>80) v.temperature-=128; if (v.temperature>80) v.temperature-=128;
v.bpm = (v.bpmMin+v.bpmMax)/2; v.bpm = (v.bpmMin+v.bpmMax)/2;
if (v.altitude > 7500) v.altitude-=8192; if (v.altitude > 7500) v.altitude-=8192;
return v; return v;
}, },
encode : health => {var alt=health.altitude&8191;return String.fromCharCode( encode : health => { "ram"; var alt=health.altitude&8191;return String.fromCharCode(
health.steps>>8,health.steps&255, // 16 bit steps health.steps>>8,health.steps&255, // 16 bit steps
health.bpmMin || health.bpm, // 8 bit bpm health.bpmMin || health.bpm, // 8 bit bpm
health.bpmMax || health.bpm, // 8 bit bpm health.bpmMax || health.bpm, // 8 bit bpm
@ -66,23 +66,24 @@ exports.getDecoder = function(fileContents) {
0|Math.round(health.temperature*2), 0|Math.round(health.temperature*2),
(alt>>8)|(Math.max(0,exports.ACTIVITY.indexOf(health.activity))<<5),alt&255, (alt>>8)|(Math.max(0,exports.ACTIVITY.indexOf(health.activity))<<5),alt&255,
0 // tbd 0 // tbd
)} );}
}; };
} else { // HEALTH1 } else { // HEALTH1
return { return {
r : 4, // record length r : 4, // record length
clr : "\xFF\xFF\xFF\xFF", clr : "\xFF\xFF\xFF\xFF",
decode : h => ({ decode : h => { "ram"; return {
steps : (h.charCodeAt(0)<<8) | h.charCodeAt(1), steps : (h.charCodeAt(0)<<8) | h.charCodeAt(1),
bpm : h.charCodeAt(2), bpm : h.charCodeAt(2),
bpmMin : h.charCodeAt(2), bpmMin : h.charCodeAt(2),
bpmMax : h.charCodeAt(2), bpmMax : h.charCodeAt(2),
movement : h.charCodeAt(3)*8 movement : h.charCodeAt(3)*8
}), };},
encode : health => String.fromCharCode( encode : health => { "ram"; return String.fromCharCode(
health.steps>>8,health.steps&255, // 16 bit steps health.steps>>8,health.steps&255, // 16 bit steps
health.bpm, // 8 bit bpm health.bpm, // 8 bit bpm
Math.min(health.movement, 255)) Math.min(health.movement, 255));
}
}; };
} }
}; };
@ -111,12 +112,10 @@ exports.readAllRecords = function(d, cb) {
// Read the entire database. There is no guarantee that the months are read in order. // Read the entire database. There is no guarantee that the months are read in order.
exports.readFullDatabase = function(cb) { exports.readFullDatabase = function(cb) {
require("Storage").list(/health-[0-9]+-[0-9]+.raw/).forEach(val => { require("Storage").list(/health-[0-9]+-[0-9]+.raw/).forEach(val => {
console.log(val);
var parts = val.split('-'); var parts = val.split('-');
var y = parseInt(parts[1],10); var y = parseInt(parts[1],10);
var mo = parseInt(parts[2].replace('.raw', ''),10); var mo = parseInt(parts[2].replace('.raw', ''),10);
exports.readAllRecords(new Date(y, mo, 1), (r) => {"ram";
exports.readAllRecords(new Date(y, mo, 1), (r) => {
r.date = new Date(y, mo, r.day, r.hr, r.min); r.date = new Date(y, mo, r.day, r.hr, r.min);
cb(r); cb(r);
}); });
@ -127,9 +126,9 @@ exports.readFullDatabase = function(cb) {
// There may be some records for the day of the timestamp previous to the timestamp // There may be some records for the day of the timestamp previous to the timestamp
exports.readAllRecordsSince = function(d, cb) { exports.readAllRecordsSince = function(d, cb) {
var currentDate = new Date().getTime(); var currentDate = new Date().getTime();
var di = d; var di = new Date(d.toISOString().substr(0,10)); // copy date (ignore time)
while (di.getTime() <= currentDate) { while (di.getTime() <= currentDate) {
exports.readDay(di, (r) => { exports.readDay(di, (r) => {"ram";
r.date = new Date(di.getFullYear(), di.getMonth(), di.getDate(), r.hr, r.min); r.date = new Date(di.getFullYear(), di.getMonth(), di.getDate(), r.hr, r.min);
cb(r); cb(r);
}); });
@ -149,7 +148,7 @@ exports.readDailySummaries = function(d, cb) {
if (h!=inf.clr) cb(Object.assign(inf.decode(h), {day:day+1})); if (h!=inf.clr) cb(Object.assign(inf.decode(h), {day:day+1}));
idx += DB_RECORDS_PER_DAY*inf.r; idx += DB_RECORDS_PER_DAY*inf.r;
} }
} };
// Read all records from the given day // Read all records from the given day
exports.readDay = function(d, cb) { exports.readDay = function(d, cb) {
@ -167,4 +166,4 @@ exports.readDay = function(d, cb) {
idx += inf.r; idx += inf.r;
} }
} }
} };

View File

@ -2,7 +2,7 @@
"id": "health", "id": "health",
"name": "Health Tracking", "name": "Health Tracking",
"shortName": "Health", "shortName": "Health",
"version": "0.32", "version": "0.33",
"description": "Logs health data and provides an app to view it", "description": "Logs health data and provides an app to view it",
"icon": "app.png", "icon": "app.png",
"tags": "tool,system,health", "tags": "tool,system,health",