Skip to content

Commit

Permalink
Merge pull request #59 from tidepool-org/kjq/addDeviceData
Browse files Browse the repository at this point in the history
Add device data
  • Loading branch information
Kent Quirk committed Jan 30, 2015
2 parents 30ee07d + 377a547 commit 6d2f9c7
Show file tree
Hide file tree
Showing 39 changed files with 325 additions and 24 deletions.
64 changes: 64 additions & 0 deletions lib/carelink/getDeviceInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* == BSD2 LICENSE ==
* Copyright (c) 2014, Tidepool Project
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the associated License, which is identical to the BSD 2-Clause
* License as published by the Open Source Initiative at opensource.org.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the License for more details.
*
* You should have received a copy of the License along with this program; if
* not, you can obtain one from Tidepool Project at tidepool.org.
* == BSD2 LICENSE ==
*/

var _ = require('lodash');

var common = require('./common.js');
var parsing = require('./parsing.js');

module.exports = function(rows, type) {
var serials = [], multiple = false;
for (var i = 0; i < rows.length; ++i) {
if (rows[i][0].search(type) != -1) {
var model = rows[i][1].replace(' -', '');
var serial = rows[i][2].replace('#', '');
serials.push({
deviceModel: model,
deviceSerialNumber: serial
});
}
}
if (serials.length > 1) {
multiple = true;
}
return {
getDeviceModel: function() {
if (serials.length === 1) {
return serials[0].deviceModel;
}
else if (serials.length > 1) {
return 'multiple';
}
else {
throw new Error('No devices! Cannot retrieve deviceModel :(');
}
},
getDeviceSerialNumber: function() {
if (serials.length === 1) {
return serials[0].deviceSerialNumber;
}
else if (serials.length > 1) {
return 'multiple';
}
else {
throw new Error('No devices! Cannot retrieve deviceSerialNumber :(');
}
},
hasMultiple: function() { return multiple; },
getPayload: function() { return {devices: serials}; }
};
};
41 changes: 27 additions & 14 deletions lib/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

var _ = require('lodash');
var async = require('async');
var md5 = require('blueimp-md5');

var bows = require('../bows');
var builder = require('../objectBuilder')();
var localStore = require('./localStore');

// Wrapper around the Tidepool client library
Expand Down Expand Up @@ -169,27 +171,38 @@ api.upload.toPlatform = function(data, sessionInfo, progress, cb) {
return postBlockToPlatform(data, callback);
};

var decorate_uploadid = function (data, uploadSessionMeta) {
var decorate_uploadid = function (data, uploadItem) {
var deviceRecords = _.map(data, function(item) {
return _.extend({}, item, {uploadId: uploadSessionMeta.uploadId});
return _.extend({}, item, {uploadId: uploadItem.uploadId});
});
deviceRecords.unshift(uploadSessionMeta);
deviceRecords.unshift(uploadItem);
return deviceRecords;
};

tidepool.startUploadSession(sessionInfo, function(err, uploadSessionMeta){
if (err) {
return cb(err);
}
data = decorate_uploadid(data, uploadSessionMeta);

for (var i = 0; i < data.length; i += BLOCKSIZE) {
blocks.push(data.slice(i, i + BLOCKSIZE));
}
var uploadId = 'upid_' + md5(sessionInfo.deviceId + '_' + sessionInfo.start).slice(0, 12);
var uploadItem = builder.makeUpload()
.with_time(sessionInfo.start)
.with_timezone(sessionInfo.tzName)
.with_version(sessionInfo.version)
.with_uploadId(uploadId)
.with_source(sessionInfo.source)
.with_byUser(tidepool.getUserId())
.with_deviceTags(sessionInfo.deviceTags)
.with_deviceManufacturers(sessionInfo.deviceManufacturers)
.with_deviceModel(sessionInfo.deviceModel)
.with_deviceSerialNumber(sessionInfo.deviceSerialNumber)
.with_deviceId(sessionInfo.deviceId)
.with_payload(sessionInfo.payload)
.done();

data = decorate_uploadid(data, uploadItem);

for (var i = 0; i < data.length; i += BLOCKSIZE) {
blocks.push(data.slice(i, i + BLOCKSIZE));
}

async.mapSeries(blocks, post_and_progress, cb);
async.mapSeries(blocks, post_and_progress, cb);

});
};

// `payload` contains:
Expand Down
6 changes: 5 additions & 1 deletion lib/drivers/abbottFreeStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ module.exports = function (config) {
_.assign(obj, _.pick(result, 'serialNumber', 'softwareVersion', 'deviceTime'));
// we should add a serial number identification routine
// that returns a better model ID (when we generalize this driver)
obj.model = 'Abbott Freestyle Precision Xtra';
obj.model = 'AbbFreePrecXtra';
obj.id = obj.model + ' ' + obj.serialNumber;
cb(null, obj);
}
Expand Down Expand Up @@ -493,6 +493,10 @@ module.exports = function (config) {
uploadData: function (progress, data, cb) {
progress(0);
var sessionInfo = {
deviceTags: ['bgm'],
deviceManufacturers: ['Abbott'],
deviceModel: 'FreeStyle Precision Xtra',
deviceSerialNumber: data.serialNumber,
deviceId: data.id,
start: sundial.utcDateString(),
tzName : cfg.timezone,
Expand Down
21 changes: 18 additions & 3 deletions lib/drivers/carelinkDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var sundial = require('sundial');
var common = require('../carelink/common');
var debug = require('../bows')('CarelinkDriver');
var removeOverlaps = require('../carelink/removeOverlapping');
var getDeviceInfo = require('../carelink/getDeviceInfo');
var CARELINK_TS_FORMAT = 'MM/DD/YY HH:mm:ss';

function convertRawValues(e) {
Expand Down Expand Up @@ -163,6 +164,7 @@ module.exports = function(simulatorMaker, api){
var endOfPreamble = cfg.fileData.indexOf('Index');
// Setup the preamble to have everything up to the header line
payload.preamble = csv.parse(cfg.fileData.substr(0, endOfPreamble), {});
cfg.deviceInfo = getDeviceInfo(payload.preamble.data, /Pump/);
// Store the rest of the data
payload.theData = csv.parse(cfg.fileData.substr(endOfPreamble), {
header: true,
Expand Down Expand Up @@ -209,7 +211,7 @@ module.exports = function(simulatorMaker, api){
device.simulator = simulatorMaker.make(
{
autoGenScheduleds: common.autoGenModels[key] ? true : false,
defaults: { }
defaults: { source: 'carelink' }
});
device.processors = initializeProcessors(cfg.timezone, key);

Expand Down Expand Up @@ -241,11 +243,24 @@ module.exports = function(simulatorMaker, api){
debug('Carelink UploadData!');
payload.post_records = [];
async.map(Object.keys(payload.devices), function(key, done) {

var deviceRecords = payload.devices[key].simulator.getEvents();
var deviceIds = _.uniq(_.pluck(deviceRecords, 'deviceId'));
var sessionInfo = {
deviceTags: ['insulin-pump'],
deviceManufacturers: ['Medtronic'],
deviceModel: cfg.deviceInfo.getDeviceModel(),
deviceSerialNumber: cfg.deviceInfo.getDeviceSerialNumber(),
deviceId: deviceIds.length > 1 ? 'multiple': deviceIds[0],
start: sundial.utcDateString(),
tzName : cfg.timezone,
version: cfg.version
};
if (cfg.deviceInfo.hasMultiple()) {
sessionInfo.payload = cfg.deviceInfo.getPayload();
}
api.upload.toPlatform(
deviceRecords,
{deviceId: Object.keys(payload.devices).toString(), start: sundial.utcDateString(), tzName : cfg.timezone, version: cfg.version},
sessionInfo,
progress,
function (err, result) {
if (err) {
Expand Down
12 changes: 10 additions & 2 deletions lib/drivers/dexcomDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,13 +572,16 @@ module.exports = function (config) {
};

var getDeviceId = function (data) {
return data.firmwareHeader.attrs.ProductName + ' ' + data.manufacturing_data.attrs.SerialNumber;
var names = data.firmwareHeader.attrs.ProductName.split(' ');
var shortname = _.map(names, function(name) { return name.slice(0,3); }).join('');
return shortname + '_' + data.manufacturing_data.attrs.SerialNumber;
};

var prepCBGData = function (data) {
dexcomDeviceId = getDeviceId(data);

cfg.builder.setDefaults({ deviceId: dexcomDeviceId });

var dataToPost = [];
for (var i = 0; i < data.cbg_data.length; ++i) {
var datum = data.cbg_data[i];
Expand Down Expand Up @@ -691,7 +694,8 @@ module.exports = function (config) {
fetchManufacturingData(0, function (err, result) {
data.manufacturing_data = result;
data.serialNumber = result.attrs.SerialNumber;
data.id = data.model + ' ' + data.serialNumber;
dexcomDeviceId = getDeviceId(data);
data.id = dexcomDeviceId;
progress(100);
data.getConfigInfo = true;
cb(null, data);
Expand Down Expand Up @@ -752,6 +756,10 @@ module.exports = function (config) {
debug('STEP: uploadData');
progress(0);
var sessionInfo = {
deviceTags: ['cgm'],
deviceManufacturers: ['Dexcom'],
deviceModel: data.model,
deviceSerialNumber: data.SerialNumber,
deviceId: dexcomDeviceId,
start: sundial.utcDateString(),
tzName : cfg.timezone,
Expand Down
23 changes: 22 additions & 1 deletion lib/objectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = function () {
set: function set(k, v) {
if (v == null && this[k]) {
delete this[k];
} else {
} else if (v != null) {
this[k] = v;
}
return this;
Expand Down Expand Up @@ -218,6 +218,7 @@ module.exports = function () {
type: 'deviceMeta',
subType: 'calibration',
value: REQUIRED,
units: REQUIRED
});
rec._bindProps();
return rec;
Expand Down Expand Up @@ -284,6 +285,25 @@ module.exports = function () {
return rec;
}

function makeUpload() {
var rec = _.assign(_createObject(), {
type: 'upload',
time: REQUIRED,
timezone: REQUIRED,
version: REQUIRED,
source: OPTIONAL,
uploadId: REQUIRED,
byUser: REQUIRED,
deviceTags: REQUIRED,
deviceManufacturers: OPTIONAL,
deviceModel: OPTIONAL,
deviceSerialNumber: OPTIONAL,
deviceId: REQUIRED,
payload: OPTIONAL
});
rec._bindProps();
return rec;
}

return {
makeCBG: makeCBG,
Expand All @@ -300,6 +320,7 @@ module.exports = function () {
makeSquareBolus: makeSquareBolus,
makeSuspendBasal: makeSuspendBasal,
makeTempBasal: makeTempBasal,
makeUpload: makeUpload,
makeWizard: makeWizard,
setDefaults: setDefaults
};
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Tidepool Uploader",
"short_name": "Uploader",
"version": "0.100.0",
"version": "0.102.0",
"description": "The Tidepool Uploader helps you get your data from insulin pumps, CGMs and BG meters into Tidepool’s secure cloud platform.",
"minimum_chrome_version": "38",
"icons": {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tidepool-uploader",
"version": "0.100.0",
"version": "0.102.0",
"description": "Tidepool Project Universal Uploader",
"private": true,
"main": "main.js",
Expand All @@ -18,12 +18,13 @@
"dependencies": {
"async": "0.9.0",
"babyparse": "0.2.1",
"blueimp-md5": "1.1.0",
"commander": "2.5.0",
"lodash": "2.4.1",
"moment-timezone": "0.2.4",
"react": "0.12.0",
"sundial": "1.1.1",
"tidepool-platform-client": "0.15.0"
"tidepool-platform-client": "0.16.1"
},
"devDependencies": {
"css-loader": "0.7.1",
Expand Down
3 changes: 3 additions & 0 deletions test/carelink/basal/scheduled/normal/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"rate": 0.8,
"deviceId": "Paradigm Revel - 723-=-53021863",
"deviceTime": "2014-03-13T00:29:03",
"source": "carelink",
"time": "2014-03-13T10:29:03.000Z",
"timezoneOffset": -600,
"type": "basal-scheduled",
Expand All @@ -16,6 +17,7 @@
"rate": 0.9,
"deviceId": "Paradigm Revel - 723-=-53021863",
"deviceTime": "2014-03-13T05:27:15",
"source": "carelink",
"time": "2014-03-13T15:27:15.000Z",
"timezoneOffset": -600,
"type": "basal-scheduled",
Expand All @@ -27,6 +29,7 @@
"rate": 0.65,
"deviceId": "Paradigm Revel - 723-=-53021863",
"deviceTime": "2014-03-14T02:00:00",
"source": "carelink",
"time": "2014-03-14T12:00:00.000Z",
"timezoneOffset": -600,
"type": "basal-scheduled",
Expand Down
3 changes: 3 additions & 0 deletions test/carelink/basal/scheduled/outOfSeq/output.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"type": "basal-scheduled",
"source": "carelink",
"deviceTime": "2014-11-24T11:09:57",
"time": "2014-11-24T21:09:57.000Z",
"timezoneOffset": -600,
Expand All @@ -12,6 +13,7 @@
},
{
"type": "basal-scheduled",
"source": "carelink",
"deviceTime": "2014-11-24T11:12:27",
"time": "2014-11-24T21:12:27.000Z",
"timezoneOffset": -600,
Expand All @@ -23,6 +25,7 @@
},
{
"type": "basal-scheduled",
"source": "carelink",
"deviceTime": "2014-11-24T12:00:00",
"time": "2014-11-24T22:00:00.000Z",
"timezoneOffset": -600,
Expand Down
3 changes: 3 additions & 0 deletions test/carelink/basal/temp/percent/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"deviceId": "Paradigm Revel - 723-=-53021863",
"deviceTime": "2014-03-13T00:00:00",
"scheduleName": "standard",
"source": "carelink",
"timezoneOffset": -600,
"time": "2014-03-13T10:00:00.000Z",
"type": "basal-scheduled",
Expand All @@ -16,6 +17,7 @@
"deviceTime": "2014-03-13T00:28:56",
"duration": 7200000,
"percent": 0.7,
"source": "carelink",
"timezoneOffset": -600,
"time": "2014-03-13T10:28:56.000Z",
"type": "basal-temp"
Expand All @@ -25,6 +27,7 @@
"deviceId": "Paradigm Revel - 723-=-53021863",
"deviceTime": "2014-03-13T00:29:03",
"scheduleName": "standard",
"source": "carelink",
"timezoneOffset": -600,
"time": "2014-03-13T10:29:03.000Z",
"type": "basal-scheduled",
Expand Down
Loading

0 comments on commit 6d2f9c7

Please sign in to comment.