Skip to content

Commit

Permalink
Deduce pump timezone from data
Browse files Browse the repository at this point in the history
  • Loading branch information
mddub committed Oct 11, 2015
1 parent beec517 commit 99ec960
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

* `CARELINK_USERNAME` - your username for [CareLink][carelink]
* `CARELINK_PASSWORD` - your password for [CareLink][carelink]
* `CARELINK_PUMP_TIMEZONE` - a timezone offset string like `"-0700"` which represents the pump's offset from UTC (**TODO:** use time zone names like `America/Los_Angeles` instead, since offsets vary depending on DST)
* `API_SECRET` - the value you use for `API_SECRET` on your Nightscout website
* `WEBSITE_HOSTNAME` - the hostname for your Nightscout instance, which looks like `your.host.com`. If you are running this script in the same Azure environment as Nightscout, there is no need to set this, as it will [already be set by Azure][azure-environment]. If you set `NS` (see below), you do not need to set this.

Expand Down
26 changes: 24 additions & 2 deletions nightscout.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ function addTimeToEntry(pumpTimeString, offset, entry) {
return entry;
}

var guessPumpTimezone = (function() {
// From my observations, sMedicalDeviceTime is advanced by the server even when the app is
// not reporting data or the pump is not connected, so its difference from server time is
// always close to a whole number of hours, and can be used to guess the pump's timezone:
// https://gist.github.com/mddub/f673570e6427c93784bf

var lastGuess;

return function guessPumpTimezone(data) {
var timezoneNaivePumpTime = Date.parse(data['sMedicalDeviceTime'] + ' +0');
var serverTimeUTC = data['currentServerTime'];
var hours = Math.round((timezoneNaivePumpTime - serverTimeUTC) / (60*60*1000));
var offset = (hours >= 0 ? '+' : '-') + (Math.abs(hours) < 10 ? '0' : '') + Math.abs(hours) + '00';
if (offset !== lastGuess) {
logger.log('Guessed pump timezone ' + offset + ' (pump time: "' + data['sMedicalDeviceTime'] + '"; server time: ' + new Date(data['currentServerTime']) + ')');
}
lastGuess = offset;
return offset;
};
})();

function pumpStatusEntry(data, offset) {
var entry = {'type': PUMP_STATUS_ENTRY_TYPE};

Expand Down Expand Up @@ -58,12 +79,13 @@ function sgvEntries(data, offset) {
}
}

var transform = module.exports.transform = function(data, offset, sgvLimit) {
var transform = module.exports.transform = function(data, sgvLimit) {
if(sgvLimit === undefined) {
sgvLimit = Infinity;
}

var entries = [];
var entries = [],
offset = guessPumpTimezone(data);

entries.push(pumpStatusEntry(data, offset));

Expand Down
4 changes: 1 addition & 3 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function readEnv(key, defaultVal) {
var config = {
username: readEnv('CARELINK_USERNAME'),
password: readEnv('CARELINK_PASSWORD'),
pumpTimezone: readEnv('CARELINK_PUMP_TIMEZONE'),
nsHost: readEnv('WEBSITE_HOSTNAME'),
nsBaseUrl: readEnv('NS'),
nsSecret: readEnv('API_SECRET'),
Expand All @@ -25,12 +24,11 @@ var config = {
};

var client = carelink.Client({username: config.username, password: config.password});

var endpoint = (config.nsBaseUrl ? config.nsBaseUrl : 'https://' + config.nsHost) + '/api/v1/entries.json';

(function requestLoop() {
client.fetch(function(data) {
var entries = nightscout.transform(data, config.pumpTimezone, config.sgvLimit);
var entries = nightscout.transform(data, config.sgvLimit);
nightscout.upload(entries, endpoint, config.nsSecret, function(response) {
setTimeout(requestLoop, config.interval);
});
Expand Down

0 comments on commit 99ec960

Please sign in to comment.