Skip to content

Commit

Permalink
Gather CGM data!
Browse files Browse the repository at this point in the history
  • Loading branch information
mddub committed Oct 6, 2015
1 parent 7f1aed5 commit a6e22f0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 18 deletions.
3 changes: 2 additions & 1 deletion config.js.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
exports.CARELINK_USERNAME = 'username';
exports.CARELINK_PASSWORD = 'password';
exports.PUMP_TIMEZONE = '-0700'; // UTC time offset
exports.CARELINK_REQUEST_INTERVAL = 60 * 1000; // ms
exports.CARELINK_REQUEST_INTERVAL = 60 * 1000; // milliseconds
exports.NUM_RECORDS_TO_SUBMIT = 20; // carelink gives ~300 sgv records at a time, but there's no need to send them all to nightscout

exports.NIGHTSCOUT_HOST = 'https://yoursitename.azurewebsites.net';
exports.NIGHTSCOUT_API_SECRET = 'your api secret';
80 changes: 63 additions & 17 deletions scraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,78 @@ var isNewData = (function() {
};
})();

function addTimeToEntry(pumpTimeString, entry) {
var timeUTC = Date.parse(pumpTimeString + ' ' + config.PUMP_TIMEZONE);
entry['date'] = timeUTC;
entry['dateString'] = new Date(timeUTC).toISOString();
return entry;
}

function transformForNightscout(data) {
var entries = [];

if(data['activeInsulin']) {
entries.push(
addTimeToEntry(
data['activeInsulin']['datetime'],
{
'type': 'reported_active_insulin',
'activeInsulin': data['activeInsulin']['amount']
}
)
);
}

if(data['sgs'] && data['sgs'].length) {
var sgvs = data['sgs'].filter(function(entry) {
return entry['kind'] === 'SG' && entry['sg'] !== 0;
});
// TODO: don't assume minimed will continue giving sensor glucose values ordered by date ascending
for(var i = Math.max(0, sgvs.length - config.NUM_RECORDS_TO_SUBMIT); i < sgvs.length; i++) {
var sgv = sgvs[i];
entries.push(
addTimeToEntry(
sgv['datetime'],
{
'type': 'sgv',
'sgv': sgv['sg'],
}
)
);
}
}

entries.forEach(function(entry) {
entry['device'] = 'MiniMed Connect ' + data['medicalDeviceFamily'] + ' ' + data['medicalDeviceSerialNumber'];
});

var activeEntry = entries.filter(function(e) { return e['type'] === 'reported_active_insulin'; })[0];
var activeIns = activeEntry ? activeEntry['activeInsulin'] + ' at ' + activeEntry['dateString'] : 'unknown';
var sgvEntries = entries.filter(function(e) { return e['type'] === 'sgv'; });
var recentSgv = sgvEntries.length ? sgvEntries[sgvEntries.length - 1] + ' at ' + activeEntry['dateString'] : 'unknown';
casper.log('active insulin ' + activeIns, 'info');
casper.log('sensor glucose ' + recentSgv, 'info');

return entries;
}

function sendToNightscout(data, callback) {
var activeIns, timeReportedUTC;
try {
activeIns = data['activeInsulin']['amount'];
timeReportedUTC = Date.parse(data['activeInsulin']['datetime'] + ' ' + config.PUMP_TIMEZONE);
} catch(e) {
casper.log('Error parsing JSON data: ' + e.message + ' ' + JSON.stringify(data), 'error');
var endpoint = config.NIGHTSCOUT_HOST + '/api/v1/entries.json';
var entries = transformForNightscout(data);
if(entries.length === 0) {
casper.log('No data found in CareLink JSON: ' + JSON.stringify(data), 'error');
return callback.apply(casper);
}

casper.log('SENDING TO NIGHTSCOUT: active insulin ' + activeIns + ', reported at ' + new Date(timeReportedUTC).toString(), 'info');

casper.open(config.NIGHTSCOUT_HOST + '/api/v1/entries.json', {
casper.log('POST ' + endpoint + ' ' + JSON.stringify(entries), 'info');
casper.open(endpoint, {
method: 'post',
data: JSON.stringify(entries),
headers: {
'api-secret': new Rusha().digest(config.NIGHTSCOUT_API_SECRET),
'Accept': 'application/json',
'Content-Type': 'application/json'
},
data: JSON.stringify({
'type': 'reported_active_insulin',
'device': 'MiniMed Connect ' + data['medicalDeviceFamily'] + ' ' + data['medicalDeviceSerialNumber'],
'date': timeReportedUTC,
'dateString': new Date(timeReportedUTC).toISOString(),
'activeInsulin': activeIns
})
}
}).then(function(response) {
if(!response.status) {
casper.log("Error uploading to Nightscout: can't connect to Nightscout host", 'error');
Expand Down

0 comments on commit a6e22f0

Please sign in to comment.