forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.js
323 lines (269 loc) · 10 KB
/
settings.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict';
var _ = require('lodash');
var levels = require('./levels');
function init ( ) {
var settings = {
units: 'mg/dL'
, timeFormat: 12
, nightMode: false
, editMode: true
, showRawbg: 'never'
, customTitle: 'Nightscout'
, theme: 'default'
, alarmUrgentHigh: true
, alarmUrgentHighMins: [30, 60, 90, 120]
, alarmHigh: true
, alarmHighMins: [30, 60, 90, 120]
, alarmLow: true
, alarmLowMins: [15, 30, 45, 60]
, alarmUrgentLow: true
, alarmUrgentLowMins: [15, 30, 45]
, alarmUrgentMins: [30, 60, 90, 120]
, alarmWarnMins: [30, 60, 90, 120]
, alarmTimeagoWarn: true
, alarmTimeagoWarnMins: 15
, alarmTimeagoUrgent: true
, alarmTimeagoUrgentMins: 30
, alarmPumpBatteryLow: false
, language: 'en'
, scaleY: 'log'
, showPlugins: ''
, showForecast: 'ar2'
, focusHours: 3
, heartbeat: 60
, baseURL: ''
, authDefaultRoles: 'readable'
, thresholds: {
bgHigh: 260
, bgTargetTop: 180
, bgTargetBottom: 80
, bgLow: 55
},
insecureUseHttp: false,
secureHstsHeader: true,
secureHstsHeaderIncludeSubdomains: false,
secureHstsHeaderPreload: false,
secureCsp: false
};
var valueMappers = {
nightMode: mapTruthy
, alarmUrgentHigh: mapTruthy
, alarmUrgentHighMins: mapNumberArray
, alarmHigh: mapTruthy
, alarmHighMins: mapNumberArray
, alarmLow: mapTruthy
, alarmLowMins: mapNumberArray
, alarmUrgentLow: mapTruthy
, alarmUrgentLowMins: mapNumberArray
, alarmUrgentMins: mapNumberArray
, alarmTimeagoWarn: mapTruthy
, alarmTimeagoUrgent: mapTruthy
, alarmWarnMins: mapNumberArray
, timeFormat: mapNumber
, insecureUseHttp: mapTruthy
, secureHstsHeader: mapTruthy
, secureCsp: mapTruthy
};
function mapNumberArray (value) {
if (!value || _.isArray(value)) {
return value;
}
if (isNaN(value)) {
var rawValues = value && value.split(' ') || [];
return _.map(rawValues, function (num) {
return isNaN(num) ? null : Number(num);
});
} else {
return [Number(value)];
}
}
function mapNumber (value) {
if (!value) {
return value;
}
if (isNaN(value)) {
return value;
} else {
return Number(value);
}
}
function mapTruthy (value) {
if (typeof value === 'string' && (value.toLowerCase() === 'on' || value.toLowerCase() === 'true')) { value = true; }
if (typeof value === 'string' && (value.toLowerCase() === 'off' || value.toLowerCase() === 'false')) { value = false; }
return value;
}
//TODO: getting sent in status.json, shouldn't be
settings.DEFAULT_FEATURES = ['bgnow', 'delta', 'direction', 'timeago', 'devicestatus', 'upbat', 'errorcodes', 'profile'];
var wasSet = [];
function isSimple (value) {
return _.isArray(value) || (typeof value !== 'function' && typeof value !== 'object');
}
function nameFromKey (key, nameType) {
return nameType === 'env' ? _.snakeCase(key).toUpperCase() : key;
}
function eachSettingAs (nameType) {
function mapKeys (accessor, keys) {
_.forIn(keys, function each (value, key) {
if (isSimple(value)) {
var newValue = accessor(nameFromKey(key, nameType));
if (newValue !== undefined) {
var mapper = valueMappers[key];
wasSet.push(key);
keys[key] = mapper ? mapper(newValue) : newValue;
}
}
});
}
return function allKeys (accessor) {
mapKeys(accessor, settings);
mapKeys(accessor, settings.thresholds);
enableAndDisableFeatures(accessor, nameType);
};
}
function enableAndDisableFeatures (accessor, nameType) {
function getAndPrepare (key) {
var raw = accessor(nameFromKey(key, nameType)) || '';
var cleaned = decodeURIComponent(raw).toLowerCase();
return cleaned ? cleaned.split(' ') : [];
}
function enableIf (feature, condition) {
if (condition) {
enable.push(feature);
}
}
function anyEnabled (features) {
return _.findIndex(features, function (feature) {
return enable.indexOf(feature) > -1;
}) > -1;
}
function prepareAlarmTypes ( ) {
var alarmTypes = _.filter(getAndPrepare('alarmTypes'), function onlyKnownTypes (type) {
return type === 'predict' || type === 'simple';
});
if (alarmTypes.length === 0) {
var thresholdWasSet = _.findIndex(wasSet, function (name) {
return name.indexOf('bg') === 0;
}) > -1;
alarmTypes = thresholdWasSet ? ['simple'] : ['predict'];
}
return alarmTypes;
}
var enable = getAndPrepare('enable');
var disable = getAndPrepare('disable');
settings.alarmTypes = prepareAlarmTypes();
//don't require pushover to be enabled to preserve backwards compatibility if there are extendedSettings for it
enableIf('pushover', accessor(nameFromKey('pushoverApiToken', nameType)));
enableIf('treatmentnotify', anyEnabled(['careportal', 'pushover', 'maker']));
_.each(settings.DEFAULT_FEATURES, function eachDefault (feature) {
enableIf(feature, enable.indexOf(feature) < 0);
});
//TODO: maybe get rid of ALARM_TYPES and only use enable?
enableIf('simplealarms', settings.alarmTypes.indexOf('simple') > -1);
enableIf('ar2', settings.alarmTypes.indexOf('predict') > -1);
if (disable.length > 0) {
console.info('disabling', disable);
}
//all enabled feature, without any that have been disabled
settings.enable = _.difference(enable, disable);
var thresholds = settings.thresholds;
thresholds.bgHigh = Number(thresholds.bgHigh);
thresholds.bgTargetTop = Number(thresholds.bgTargetTop);
thresholds.bgTargetBottom = Number(thresholds.bgTargetBottom);
thresholds.bgLow = Number(thresholds.bgLow);
verifyThresholds();
adjustShownPlugins();
}
function verifyThresholds() {
var thresholds = settings.thresholds;
if (thresholds.bgTargetBottom >= thresholds.bgTargetTop) {
console.warn('BG_TARGET_BOTTOM(' + thresholds.bgTargetBottom + ') was >= BG_TARGET_TOP(' + thresholds.bgTargetTop + ')');
thresholds.bgTargetBottom = thresholds.bgTargetTop - 1;
console.warn('BG_TARGET_BOTTOM is now ' + thresholds.bgTargetBottom);
}
if (thresholds.bgTargetTop <= thresholds.bgTargetBottom) {
console.warn('BG_TARGET_TOP(' + thresholds.bgTargetTop + ') was <= BG_TARGET_BOTTOM(' + thresholds.bgTargetBottom + ')');
thresholds.bgTargetTop = thresholds.bgTargetBottom + 1;
console.warn('BG_TARGET_TOP is now ' + thresholds.bgTargetTop);
}
if (thresholds.bgLow >= thresholds.bgTargetBottom) {
console.warn('BG_LOW(' + thresholds.bgLow + ') was >= BG_TARGET_BOTTOM(' + thresholds.bgTargetBottom + ')');
thresholds.bgLow = thresholds.bgTargetBottom - 1;
console.warn('BG_LOW is now ' + thresholds.bgLow);
}
if (thresholds.bgHigh <= thresholds.bgTargetTop) {
console.warn('BG_HIGH(' + thresholds.bgHigh + ') was <= BG_TARGET_TOP(' + thresholds.bgTargetTop + ')');
thresholds.bgHigh = thresholds.bgTargetTop + 1;
console.warn('BG_HIGH is now ' + thresholds.bgHigh);
}
}
function adjustShownPlugins ( ) {
var showPluginsUnset = settings.showPlugins && 0 === settings.showPlugins.length;
settings.showPlugins += ' delta direction upbat';
if (settings.showRawbg === 'always' || settings.showRawbg === 'noise') {
settings.showPlugins += ' rawbg';
}
if (showPluginsUnset) {
//assume all enabled features are plugins and they should be shown for now
//it would be better to use the registered plugins, but it's not loaded yet...
_.forEach(settings.enable, function showFeature(feature) {
if (isEnabled(feature)) {
settings.showPlugins += ' ' + feature;
}
});
}
}
function isEnabled (feature) {
var enabled = false;
if (settings.enable && typeof feature === 'object' && feature.length !== undefined) {
enabled = _.find(feature, function eachFeature (f) {
return settings.enable.indexOf(f) > -1;
}) !== undefined;
} else {
enabled = settings.enable && settings.enable.indexOf(feature) > -1;
}
return enabled;
}
function isAlarmEventEnabled (notify) {
var enabled = false;
if ('high' !== notify.eventName && 'low' !== notify.eventName) {
enabled = true;
} else if (notify.eventName === 'high' && notify.level === levels.URGENT && settings.alarmUrgentHigh) {
enabled = true;
} else if (notify.eventName === 'high' && settings.alarmHigh) {
enabled = true;
} else if (notify.eventName === 'low' && notify.level === levels.URGENT && settings.alarmUrgentLow) {
enabled = true;
} else if (notify.eventName === 'low' && settings.alarmLow) {
enabled = true;
}
return enabled;
}
function snoozeMinsForAlarmEvent (notify) {
var snoozeTime;
if (notify.eventName === 'high' && notify.level === levels.URGENT && settings.alarmUrgentHigh) {
snoozeTime = settings.alarmUrgentHighMins;
} else if (notify.eventName === 'high' && settings.alarmHigh) {
snoozeTime = settings.alarmHighMins;
} else if (notify.eventName === 'low' && notify.level === levels.URGENT && settings.alarmUrgentLow) {
snoozeTime = settings.alarmUrgentLowMins;
} else if (notify.eventName === 'low' && settings.alarmLow) {
snoozeTime = settings.alarmLowMins;
} else if (notify.level === levels.URGENT) {
snoozeTime = settings.alarmUrgentMins;
} else {
snoozeTime = settings.alarmWarnMins;
}
return snoozeTime;
}
function snoozeFirstMinsForAlarmEvent (notify) {
return _.first(snoozeMinsForAlarmEvent(notify));
}
settings.eachSetting = eachSettingAs();
settings.eachSettingAsEnv = eachSettingAs('env');
settings.isEnabled = isEnabled;
settings.isAlarmEventEnabled = isAlarmEventEnabled;
settings.snoozeMinsForAlarmEvent = snoozeMinsForAlarmEvent;
settings.snoozeFirstMinsForAlarmEvent = snoozeFirstMinsForAlarmEvent;
return settings;
}
module.exports = init;