-
Notifications
You must be signed in to change notification settings - Fork 71.3k
/
sandbox.js
263 lines (207 loc) · 6.73 KB
/
sandbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
'use strict';
var _ = require('lodash');
var units = require('./units')();
var profile = require('./profilefunctions')();
function init ( ) {
var sbx = {};
function reset () {
sbx.properties = [];
sbx.scaleBg = scaleBg;
sbx.displayBg = displayBg;
sbx.roundInsulinForDisplayFormat = roundInsulinForDisplayFormat;
sbx.roundBGToDisplayFormat = roundBGToDisplayFormat;
}
function extend () {
sbx.unitsLabel = unitsLabel();
sbx.data = sbx.data || {};
//default to prevent adding checks everywhere
sbx.extendedSettings = {empty: true};
}
function withExtendedSettings(plugin, allExtendedSettings, sbx) {
var sbx2 = _.extend({}, sbx);
sbx2.extendedSettings = allExtendedSettings[plugin.name] || {};
return sbx2;
}
/**
* Initialize the sandbox using server state
*
* @param env - .js
* @param ctx - created from bootevent
* @returns {{sbx}}
*/
sbx.serverInit = function serverInit (env, ctx) {
reset();
sbx.time = Date.now();
sbx.units = env.DISPLAY_UNITS;
sbx.defaults = env.defaults;
sbx.enable = env.enable;
sbx.thresholds = env.thresholds;
sbx.alarm_types = env.alarm_types;
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']);
//Plugins will expect the right profile based on time
profile.loadData(ctx.data.profiles);
sbx.data.profile = profile;
delete sbx.data.profiles;
sbx.properties = [];
sbx.withExtendedSettings = function getPluginExtendedSettingsOnly (plugin) {
return withExtendedSettings(plugin, env.extendedSettings, sbx);
};
extend();
return sbx;
};
/**
* Initialize the sandbox using client state
*
* @param app - app settings
* @param clientSettings - specific settings from the client, starting with the defaults
* @param time - could be a retro time
* @param pluginBase - used by visualization plugins to update the UI
* @param data - svgs, treatments, profile, etc
* @returns {{sbx}}
*/
sbx.clientInit = function clientInit (app, clientSettings, time, pluginBase, data) {
reset();
sbx.units = clientSettings.units;
sbx.defaults = clientSettings; //TODO: strip out extra stuff
sbx.thresholds = app.thresholds;
sbx.enable = app.enabledOptions;
sbx.alarm_types = clientSettings.alarm_types;
sbx.showPlugins = clientSettings.showPlugins;
sbx.time = time;
sbx.data = data;
sbx.pluginBase = pluginBase;
sbx.extendedSettings = {empty: true};
sbx.withExtendedSettings = function getPluginExtendedSettingsOnly (plugin) {
return withExtendedSettings(plugin, app.extendedSettings, sbx);
};
extend();
return sbx;
};
/**
* Properties are immutable, first plugin to set it wins, plugins should be in the correct order
*
* @param name
* @param setter
*/
sbx.offerProperty = function offerProperty (name, setter) {
if (!sbx.properties.hasOwnProperty(name)) {
var value = setter();
if (value) {
sbx.properties[name] = value;
}
}
};
sbx.lastSGVEntry = function lastSGVEntry ( ) {
return _.findLast(sbx.data.sgvs, function notInTheFuture (entry) {
return entry.x <= sbx.time;
});
};
sbx.lastSGV = function lastSGV ( ) {
var last = sbx.lastSGVEntry();
return last && last.y;
};
sbx.lastSGVMills = function lastSGVMills ( ) {
var last = sbx.lastSGVEntry();
return (last && last.x) || (last && last.date && (new Date(last.date)).getTime()) || 0;
};
sbx.lastScaledSGV = function lastScaledSVG ( ) {
return sbx.scaleBg(sbx.lastSGV());
};
sbx.lastDisplaySVG = function lastDisplaySVG ( ) {
return sbx.displayBg(sbx.lastSGV());
};
sbx.buildBGNowLine = function buildBGNowLine ( ) {
var line = 'BG Now: ' + sbx.lastDisplaySVG();
var delta = sbx.properties.delta && sbx.properties.delta.display;
if (delta) {
line += ' ' + delta;
}
var direction = sbx.properties.direction && sbx.properties.direction.label;
if (direction) {
line += ' ' + direction;
}
line += ' ' + sbx.unitsLabel;
return line;
};
sbx.buildForecastLine = function buildForecastLine (usingRawForecast) {
var prop = sbx.properties.ar2;
var predicted = prop && (usingRawForecast ? prop.rawForecast && prop.rawForecast.predicted : prop.forecast && prop.forecast.predicted);
if (!predicted) {
return;
}
var scaled = _.map(predicted, function(p) { return sbx.scaleBg(p.y) } );
var line;
if (scaled.length >= 3) {
line = (usingRawForecast ? 'Raw BG' : 'BG') + ' 15m: ' + scaled[2] + ' ' + sbx.unitsLabel;
}
return line;
};
sbx.appendPropertyLine = function appendPropertyLine (propertyName, lines) {
lines = lines || [];
var displayLine = sbx.properties[propertyName] && sbx.properties[propertyName].displayLine;
if (displayLine) {
lines.push(displayLine);
}
return lines;
};
sbx.prepareDefaultLines = function prepareDefaultLines(usingRawForecast) {
var lines = [sbx.buildBGNowLine()];
sbx.appendPropertyLine('rawbg', lines);
var forecastLine = sbx.buildForecastLine(usingRawForecast);
if (forecastLine) {
lines.push(forecastLine);
}
sbx.appendPropertyLine('bwp', lines);
sbx.appendPropertyLine('iob', lines);
sbx.appendPropertyLine('cob', lines);
return lines;
};
sbx.buildDefaultMessage = function buildDefaultMessage(usingRawForecast) {
return sbx.prepareDefaultLines(usingRawForecast).join('\n');
};
function displayBg (bg) {
if (Number(bg) === 39) {
return 'LOW';
} else if (Number(bg) === 401) {
return 'HIGH';
} else {
return scaleBg(bg);
}
}
function scaleBg (bg) {
if (sbx.units === 'mmol' && bg) {
return Number(units.mgdlToMMOL(bg));
} else {
return Number(bg);
}
}
function roundInsulinForDisplayFormat (insulin) {
if (insulin === 0) {
return '0';
}
if (sbx.properties.roundingStyle === 'medtronic') {
var denominator = 0.1;
var digits = 1;
if (insulin > 0.5 && iob < 1) {
denominator = 0.05;
digits = 2;
}
if (insulin <= 0.5) {
denominator = 0.025;
digits = 3;
}
return (Math.floor(insulin / denominator) * denominator).toFixed(digits);
}
return (Math.floor(insulin / 0.01) * 0.01).toFixed(2);
}
function unitsLabel ( ) {
return sbx.units === 'mmol' ? 'mmol/L' : 'mg/dl';
}
function roundBGToDisplayFormat (bg) {
return sbx.units === 'mmol' ? Math.round(bg * 10) / 10 : Math.round(bg);
}
return sbx;
}
module.exports = init;