1
0
Fork 0
BangleApps/apps/health/boot.js

39 lines
1.3 KiB
JavaScript

Bangle.on("health", health => {
// ensure we write health info for *last* block
var d = new Date(Date.now() - 590000);
const DB_RECORD_LEN = 4;
const DB_RECORDS_PER_HR = 6;
const DB_RECORDS_PER_DAY = DB_RECORDS_PER_HR*24;
const DB_RECORDS_PER_MONTH = DB_RECORDS_PER_DAY*31;
const DB_HEADER_LEN = 8;
const DB_FILE_LEN = DB_HEADER_LEN + DB_RECORDS_PER_MONTH*DB_RECORD_LEN;
function getRecordFN(d) {
return "health-"+d.getFullYear()+"-"+d.getMonth()+".raw";
}
function getRecordIdx(d) {
return (DB_RECORDS_PER_DAY*(d.getDate()-1)) +
(DB_RECORDS_PER_HR*d.getHours()) +
(0|(d.getMinutes()*DB_RECORDS_PER_HR/60));
}
var rec = getRecordIdx(d);
var fn = getRecordFN(d);
var f = require("Storage").read(fn);
if (f) {
var dt = f.substr(DB_HEADER_LEN+(rec*DB_RECORD_LEN), DB_RECORD_LEN);
if (dt!="\xFF\xFF\xFF\xFF") {
print("HEALTH ERR: Already written!");
return;
}
} else {
require("Storage").write(fn, "HEALTH1\0", 0, DB_FILE_LEN); // header
}
var recordData = String.fromCharCode(
health.steps>>8,health.steps&255, // 16 bit steps
health.bpm, // 8 bit bpm
Math.min(health.movement / 8, 255)); // movement
require("Storage").write(fn, recordData, DB_HEADER_LEN+(rec*DB_RECORD_LEN), DB_FILE_LEN);
});