Skip to content

Commit

Permalink
Move creation of upload object into the uploader.
Browse files Browse the repository at this point in the history
  • Loading branch information
kentquirk committed Jan 29, 2015
1 parent cf0ba08 commit 377a547
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
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
22 changes: 21 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 @@ -285,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 @@ -301,6 +320,7 @@ module.exports = function () {
makeSquareBolus: makeSquareBolus,
makeSuspendBasal: makeSuspendBasal,
makeTempBasal: makeTempBasal,
makeUpload: makeUpload,
makeWizard: makeWizard,
setDefaults: setDefaults
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"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",
Expand Down

0 comments on commit 377a547

Please sign in to comment.