Skip to content

Commit

Permalink
pull the ticker and notifications into the client
Browse files Browse the repository at this point in the history
align client and server sandboxe inits some more
update tests
  • Loading branch information
jasoncalabrese committed Oct 29, 2015
1 parent daeb067 commit 75b84dd
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 146 deletions.
2 changes: 1 addition & 1 deletion lib/bootevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function boot (env) {
ctx.devicestatus = require('./devicestatus')(env.devicestatus_collection, ctx);
ctx.profile = require('./profile')(env.profile_collection, ctx);
ctx.pebble = require('./pebble')(env, ctx);
ctx.bus = require('./bus')(env, ctx);
ctx.bus = require('./bus')(env.settings, ctx);
ctx.data = require('./data')(env, ctx);
ctx.notifications = require('./notifications')(env, ctx);

Expand Down
4 changes: 2 additions & 2 deletions lib/bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

var Stream = require('stream');

function init (env) {
function init (settings) {
var beats = 0;
var started = new Date( );
var interval = env.settings.heartbeat * 1000;
var interval = settings.heartbeat * 1000;

var stream = new Stream;

Expand Down
68 changes: 42 additions & 26 deletions lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ client.init = function init(serverSettings, plugins) {
, isInitialData = false
, SGVdata = []
, MBGdata = []
, latestUpdateTime
, prevSGV
, devicestatusData
, opacity = {current: 1, DAY: 1, NIGHT: 0.5}
Expand All @@ -47,11 +46,34 @@ client.init = function init(serverSettings, plugins) {
client.forecastTime = times.mins(30).msecs;
client.data = [];
client.browserUtils = require('./browser-utils')($);
client.settings = require('./browser-settings')(client, plugins, serverSettings, $);
client.utils = require('../utils')(client.settings);
client.ticks = require('./ticks');

client.sbx = sandbox.clientInit(client.settings, client.now);
//containers
var container = $('.container')
, bgStatus = $('.bgStatus')
, currentBG = $('.bgStatus .currentBG')
, majorPills = $('.bgStatus .majorPills')
, minorPills = $('.bgStatus .minorPills')
, statusPills = $('.status .statusPills')
;

client.tooltip = d3.select('body').append('div')
.attr('class', 'tooltip')
.style('opacity', 0);

client.settings = require('./browser-settings')(client, plugins, serverSettings, $);

client.ctx = {
data: {}
, bus: require('../bus')(client.settings, client.ctx)
, settings: client.settings
, notifications: require('../notifications')(client.settings, client.ctx)
, pluginBase: plugins.base(majorPills, minorPills, statusPills, bgStatus, client.tooltip, $.localStorage)
};

client.utils = require('../utils')(client.ctx.settings);

client.sbx = sandbox.clientInit(client.ctx, client.now);
client.rawbg = plugins('rawbg');
client.delta = plugins('delta');
client.timeago = plugins('timeago');
Expand All @@ -64,10 +86,6 @@ client.init = function init(serverSettings, plugins) {
client.hashauth = require('../hashauth');
client.hashauth.init(client, $).initAuthentication();

client.tooltip = d3.select('body').append('div')
.attr('class', 'tooltip')
.style('opacity', 0);

client.foucusRangeMS = times.hours(3).msecs;
client.brushed = brushed;
client.formatTime = formatTime;
Expand All @@ -76,13 +94,11 @@ client.init = function init(serverSettings, plugins) {
client.renderer = require('./renderer')(client, d3, $);
client.careportal = require('./careportal')(client, $);

var container = $('.container')
, bgStatus = $('.bgStatus')
, currentBG = $('.bgStatus .currentBG')
, majorPills = $('.bgStatus .majorPills')
, minorPills = $('.bgStatus .minorPills')
, statusPills = $('.status .statusPills')
;
client.ctx.bus.on('tick', function timedReload (tick) {
console.info('tick', tick.now);
});

client.ctx.bus.uptime( );

client.dataExtent = function dataExtent ( ) {
return client.data.length > 0 ?
Expand Down Expand Up @@ -271,19 +287,19 @@ client.init = function init(serverSettings, plugins) {
}

function updatePlugins (sgvs, time) {
var pluginBase = plugins.base(majorPills, minorPills, statusPills, bgStatus, client.tooltip, $.localStorage);

client.sbx = sandbox.clientInit(
client.settings
client.ctx
, new Date(time).getTime() //make sure we send a timestamp
, pluginBase, {
sgvs: sgvs
, cals: [client.cal]
, treatments: client.treatments
, profile: profile
, uploaderBattery: devicestatusData && devicestatusData.uploaderBattery
, inRetroMode: inRetroMode()
});
, {
sgvs: sgvs
, cals: [client.cal]
, treatments: client.treatments
, profile: profile
, uploaderBattery: devicestatusData && devicestatusData.uploaderBattery
, inRetroMode: inRetroMode()
}
);

//all enabled plugins get a chance to set properties, even if they aren't shown
plugins.setProperties(client.sbx);
Expand Down Expand Up @@ -835,7 +851,7 @@ client.init = function init(serverSettings, plugins) {

if (d.sgvs) {
// change the next line so that it uses the prediction if the signal gets lost (max 1/2 hr)
latestUpdateTime = Date.now();
client.ctx.data.lastUpdated = Date.now();
client.latestSGV = SGVdata[SGVdata.length - 1];
prevSGV = SGVdata[SGVdata.length - 2];
}
Expand Down
25 changes: 17 additions & 8 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ function init ( ) {
return sbx2;
}

/**
* A view into the safe notification functions for plugins
*
* @param ctx
* @returns {{notification}}
*/
function safeNotifications (ctx) {
return _.pick(ctx.notifications, ['requestNotify', 'requestSnooze', 'requestClear']);
}

/**
* Initialize the sandbox using server state
*
Expand All @@ -37,9 +47,7 @@ function init ( ) {
sbx.time = Date.now();
sbx.settings = env.settings;
sbx.data = ctx.data.clone();

//don't expose all of notifications, ctx.notifications will decide what to do after all plugins chime in
sbx.notifications = _.pick(ctx.notifications, ['levels', 'requestNotify', 'requestSnooze', 'requestClear']);
sbx.notifications = safeNotifications(ctx);

var profile = require('./profilefunctions')();
//Plugins will expect the right profile based on time
Expand Down Expand Up @@ -67,22 +75,23 @@ function init ( ) {
* @param data - svgs, treatments, profile, etc
* @returns {{sbx}}
*/
sbx.clientInit = function clientInit (settings, time, pluginBase, data) {
sbx.clientInit = function clientInit (ctx, time, data) {
reset();

sbx.settings = settings;
sbx.showPlugins = settings.showPlugins;
sbx.settings = ctx.settings;
sbx.showPlugins = ctx.showPlugins;
sbx.time = time;
sbx.data = data;
sbx.pluginBase = pluginBase;
sbx.pluginBase = ctx.pluginBase;
sbx.notifications = safeNotifications(ctx);

if (sbx.pluginBase) {
sbx.pluginBase.forecastPoints = [];
}

sbx.extendedSettings = {empty: true};
sbx.withExtendedSettings = function getPluginExtendedSettingsOnly (plugin) {
return withExtendedSettings(plugin, settings.extendedSettings, sbx);
return withExtendedSettings(plugin, sbx.settings.extendedSettings, sbx);
};

extend();
Expand Down
15 changes: 8 additions & 7 deletions tests/basalprofileplugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,21 @@ describe('basalprofile', function ( ) {
var profile = require('../lib/profilefunctions')([profileData]);

it('update basal profile pill', function (done) {

var clientSettings = {};
var data = {};

var pluginBase = {
updatePillText: function mockedUpdatePillText (plugin, options) {
options.value.should.equal('0.175U');
done();
var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.value.should.equal('0.175U');
done();
}
}
};

var time = new Date('2015-06-21T00:00:00').getTime();

var sbx = sandbox.clientInit(clientSettings, time, pluginBase, data);
var sbx = sandbox.clientInit(ctx, time, data);
sbx.data.profile = profile;
basal.updateVisualisation(sbx);

Expand Down
42 changes: 23 additions & 19 deletions tests/boluswizardpreview.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,16 @@ describe('boluswizardpreview', function ( ) {
};

var sandbox = require('../lib/sandbox')();
var pluginBase = {};
var clientSettings = { units: 'mmol' };
var ctx = {
settings: {
units: 'mmol'
}
, pluginBase: {}
};
var data = {sgvs: [{mills: before, mgdl: 100}, {mills: now, mgdl: 100}]};
data.treatments = [{mills: now, insulin: '1.0'}];
data.profile = require('../lib/profilefunctions')([profileData]);
var sbx = sandbox.clientInit(clientSettings, Date.now(), pluginBase, data);
var sbx = sandbox.clientInit(ctx, Date.now(), data);
var iob = require('../lib/plugins/iob')();
sbx.properties.iob = iob.calcTotal(data.treatments, data.profile, now);

Expand Down Expand Up @@ -165,12 +169,16 @@ describe('boluswizardpreview', function ( ) {
};

var sandbox = require('../lib/sandbox')();
var pluginBase = {};
var clientSettings = { units: 'mmol' };
var ctx = {
settings: {
units: 'mmol'
}
, pluginBase: {}
};
var data = {sgvs: [{mills: before, mgdl: 175}, {mills: now, mgdl: 153}]};
data.treatments = [{mills: now, insulin: '0.45'}];
data.profile = require('../lib/profilefunctions')([profileData]);
var sbx = sandbox.clientInit(clientSettings, Date.now(), pluginBase, data);
var sbx = sandbox.clientInit(ctx, Date.now(), data);
var iob = require('../lib/plugins/iob')();
sbx.properties.iob = iob.calcTotal(data.treatments, data.profile, now);

Expand Down Expand Up @@ -257,16 +265,17 @@ describe('boluswizardpreview', function ( ) {
});

it('set a pill to the BWP with infos', function (done) {
var pluginBase = {
updatePillText: function mockedUpdatePillText (plugin, options) {
options.label.should.equal('BWP');
options.value.should.equal('0.50U');
done();
var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.label.should.equal('BWP');
options.value.should.equal('0.50U');
done();
}
}
};

var clientSettings = {};

var loadedProfile = require('../lib/profilefunctions')();
loadedProfile.loadData([profile]);

Expand All @@ -276,16 +285,11 @@ describe('boluswizardpreview', function ( ) {
, profile: loadedProfile
};

var sbx = require('../lib/sandbox')().clientInit(clientSettings, Date.now(), pluginBase, data);
var sbx = require('../lib/sandbox')().clientInit(ctx, Date.now(), data);

iob.setProperties(sbx);
boluswizardpreview.setProperties(sbx);
boluswizardpreview.updateVisualisation(sbx);

ctx.notifications.resetStateForTests();
ctx.notifications.initRequests();
ctx.data.profiles = [profile];

});

});
32 changes: 18 additions & 14 deletions tests/cannulaage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,25 @@ describe('cage', function ( ) {

it('set a pill to the current cannula age', function (done) {

var clientSettings = {};

var data = {
treatments: [
{eventType: 'Site Change', notes: 'Foo', mills: Date.now() - 48 * 60 * 60000}
, {eventType: 'Site Change', notes: 'Bar', mills: Date.now() - 24 * 60 * 60000}
]
};

var pluginBase = {
updatePillText: function mockedUpdatePillText (plugin, options) {
options.value.should.equal('24h');
options.info[1].value.should.equal('Bar');
done();
var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.value.should.equal('24h');
options.info[1].value.should.equal('Bar');
done();
}
}
};

var sbx = sandbox.clientInit(clientSettings, Date.now(), pluginBase, data);
var sbx = sandbox.clientInit(ctx, Date.now(), data);
cage.updateVisualisation(sbx);

});
Expand All @@ -54,15 +55,18 @@ describe('cage', function ( ) {
]
};

var pluginBase = {
updatePillText: function mockedUpdatePillText (plugin, options) {
options.value.should.equal('0h');
options.info.length.should.equal(1);
done();
var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText(plugin, options) {
options.value.should.equal('0h');
options.info.length.should.equal(1);
done();
}
}
};

var sbx = sandbox.clientInit(clientSettings, Date.now(), pluginBase, data);
var sbx = sandbox.clientInit(ctx, Date.now(), data);
cage.updateVisualisation(sbx);

});
Expand Down
16 changes: 8 additions & 8 deletions tests/cob.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ describe('COB', function ( ) {
});

it('set a pill to the current COB', function (done) {

var clientSettings = {};

var data = {
treatments: [{
carbs: '8'
Expand All @@ -75,15 +72,18 @@ describe('COB', function ( ) {
, profile: profile
};

var pluginBase = {
updatePillText: function mockedUpdatePillText (plugin, options) {
options.value.should.equal('8g');
done();
var ctx = {
settings: {}
, pluginBase: {
updatePillText: function mockedUpdatePillText (plugin, options) {
options.value.should.equal('8g');
done();
}
}
};

var sandbox = require('../lib/sandbox')();
var sbx = sandbox.clientInit(clientSettings, Date.now(), pluginBase, data);
var sbx = sandbox.clientInit(ctx, Date.now(), data);
cob.setProperties(sbx);
cob.updateVisualisation(sbx);

Expand Down
Loading

0 comments on commit 75b84dd

Please sign in to comment.