2021-10-14 16:14:10 +00:00
|
|
|
const DB_RECORD_LEN = 4;
|
|
|
|
const DB_RECORDS_PER_HR = 6;
|
2021-10-29 09:34:14 +00:00
|
|
|
const DB_RECORDS_PER_DAY = DB_RECORDS_PER_HR*24 + 1/*summary*/;
|
2021-10-14 16:14:10 +00:00
|
|
|
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) {
|
2021-12-17 09:09:33 +00:00
|
|
|
return "health-"+d.getFullYear()+"-"+(d.getMonth()+1)+".raw";
|
2021-10-14 16:14:10 +00:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all records from the given month
|
|
|
|
exports.readAllRecords = function(d, cb) {
|
|
|
|
var fn = getRecordFN(d);
|
|
|
|
var f = require("Storage").read(fn);
|
2021-10-29 09:34:14 +00:00
|
|
|
if (f===undefined) return;
|
2021-10-14 16:14:10 +00:00
|
|
|
var idx = DB_HEADER_LEN;
|
|
|
|
for (var day=0;day<31;day++) {
|
2021-10-29 09:34:14 +00:00
|
|
|
for (var hr=0;hr<24;hr++) { // actually 25, see below
|
2021-10-14 16:14:10 +00:00
|
|
|
for (var m=0;m<DB_RECORDS_PER_HR;m++) {
|
|
|
|
var h = f.substr(idx, DB_RECORD_LEN);
|
|
|
|
if (h!="\xFF\xFF\xFF\xFF") {
|
|
|
|
cb({
|
|
|
|
day:day+1, hr : hr, min:m*10,
|
|
|
|
steps : (h.charCodeAt(0)<<8) | h.charCodeAt(1),
|
|
|
|
bpm : h.charCodeAt(2),
|
|
|
|
movement : h.charCodeAt(3)
|
|
|
|
});
|
|
|
|
}
|
2021-10-29 09:34:14 +00:00
|
|
|
idx += DB_RECORD_LEN;
|
2021-10-14 16:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-29 09:34:14 +00:00
|
|
|
idx += DB_RECORD_LEN; // +1 because we have an extra record with totals for the end of the day
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read daily summaries from the given month
|
|
|
|
exports.readDailySummaries = function(d, cb) {
|
|
|
|
var rec = getRecordIdx(d);
|
|
|
|
var fn = getRecordFN(d);
|
|
|
|
var f = require("Storage").read(fn);
|
|
|
|
if (f===undefined) return;
|
|
|
|
var idx = DB_HEADER_LEN + (DB_RECORDS_PER_DAY-1)*DB_RECORD_LEN; // summary is at the end of each day
|
|
|
|
for (var day=0;day<31;day++) {
|
|
|
|
var h = f.substr(idx, DB_RECORD_LEN);
|
|
|
|
if (h!="\xFF\xFF\xFF\xFF") {
|
|
|
|
cb({
|
|
|
|
day:day+1,
|
|
|
|
steps : (h.charCodeAt(0)<<8) | h.charCodeAt(1),
|
|
|
|
bpm : h.charCodeAt(2),
|
|
|
|
movement : h.charCodeAt(3)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
idx += DB_RECORDS_PER_DAY*DB_RECORD_LEN;
|
2021-10-14 16:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 07:15:37 +00:00
|
|
|
// Read all records from the given day
|
2021-10-14 16:14:10 +00:00
|
|
|
exports.readDay = function(d, cb) {
|
|
|
|
var rec = getRecordIdx(d);
|
|
|
|
var fn = getRecordFN(d);
|
|
|
|
var f = require("Storage").read(fn);
|
2021-10-29 09:34:14 +00:00
|
|
|
if (f===undefined) return;
|
2021-10-14 16:14:10 +00:00
|
|
|
var idx = DB_HEADER_LEN + (DB_RECORD_LEN*DB_RECORDS_PER_DAY*(d.getDate()-1));
|
|
|
|
for (var hr=0;hr<24;hr++) {
|
|
|
|
for (var m=0;m<DB_RECORDS_PER_HR;m++) {
|
|
|
|
var h = f.substr(idx, DB_RECORD_LEN);
|
|
|
|
if (h!="\xFF\xFF\xFF\xFF") {
|
|
|
|
cb({
|
|
|
|
hr : hr, min:m*10,
|
|
|
|
steps : (h.charCodeAt(0)<<8) | h.charCodeAt(1),
|
|
|
|
bpm : h.charCodeAt(2),
|
|
|
|
movement : h.charCodeAt(3)
|
|
|
|
});
|
|
|
|
}
|
2021-10-29 09:34:14 +00:00
|
|
|
idx += DB_RECORD_LEN;
|
2021-10-14 16:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|