forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootevent.js
More file actions
396 lines (315 loc) · 11.6 KB
/
bootevent.js
File metadata and controls
396 lines (315 loc) · 11.6 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
'use strict';
const _ = require('lodash');
const UPDATE_THROTTLE = 5000;
function boot (env, language) {
function startBoot(ctx, next) {
console.log('Executing startBoot');
ctx.bootErrors = [ ];
ctx.moment = require('moment-timezone');
ctx.runtimeState = 'booting';
ctx.settings = env.settings;
ctx.bus = require('../bus')(env.settings, ctx);
ctx.adminnotifies = require('../adminnotifies')(ctx);
if (env.notifies) {
for (var i = 0; i < env.notifies.length; i++) {
ctx.adminnotifies.addNotify(env.notifies[i]);
}
}
next();
}
//////////////////////////////////////////////////
// Check Node version.
// Latest Node LTS releases are recommended and supported.
// Current Node releases MAY work, but are not recommended. Will be tested in CI
// Older Node versions or Node versions with known security issues will not work.
///////////////////////////////////////////////////
function checkNodeVersion (ctx, next) {
console.log('Executing checkNodeVersion');
var semver = require('semver');
var nodeVersion = process.version;
const isLTS = process.release.lts ? true : false;
// Node 16+ is supported (overlap with previous release)
// Recommended: Node 20 LTS (until April 2026) or Node 22 LTS (until April 2027)
if (semver.satisfies(nodeVersion, '>=16.x')) {
if (isLTS) {
console.debug('Node LTS version ' + nodeVersion + ' is supported');
} else {
console.debug('Node version ' + nodeVersion + ' is supported (current release)');
}
next();
return;
}
console.log( 'ERROR: Node version ' + nodeVersion + ' is not supported. Please use Node 16 or later.');
process.exit(1);
}
function checkEnv (ctx, next) {
console.log('Executing checkEnv');
ctx.language = language;
if (env.err.length > 0) {
ctx.bootErrors = ctx.bootErrors || [ ];
ctx.bootErrors.push({'desc': 'ENV Error', err: env.err});
}
next();
}
function hasBootErrors(ctx) {
return ctx.bootErrors && ctx.bootErrors.length > 0;
}
function augmentSettings (ctx, next) {
console.log('Executing augmentSettings');
var configURL = env.IMPORT_CONFIG || null;
var url = require('url');
var href = null;
if (configURL) {
try {
href = url.parse(configURL).href;
} catch (e) {
console.error('Parsing config URL from IMPORT_CONFIG failed');
}
}
if(configURL && href) {
var axios_default = { headers: { 'Accept': 'application/json' } };
var axios = require('axios').create(axios_default);
console.log('Getting settings from', href);
return axios.get(href).then(function (resp) {
var body = resp.data;
var settings = body.settings || body;
console.log('extending settings with', settings);
_.merge(env.settings, settings);
if (body.extendedSettings) {
console.log('extending extendedSettings with', body.extendedSettings);
_.merge(env.extendedSettings, body.extendedSettings);
}
next( );
}).catch(function (err) {
var synopsis = ['Attempt to fetch config', href, 'failed.'];
console.log('Attempt to fetch config', href, 'failed.', err.response);
ctx.bootErrors.push({desc: synopsis.join(' '), err});
next( );
});
} else {
next( );
}
}
function checkSettings (ctx, next) {
console.log('Executing checkSettings');
ctx.bootErrors = ctx.bootErrors || [];
console.log('Checking settings');
if (!env.storageURI) {
ctx.bootErrors.push({'desc': 'Mandatory setting missing',
err: 'MONGODB_URI setting is missing, cannot connect to database'});
}
if (!env.enclave.isApiKeySet()) {
ctx.bootErrors.push({'desc': 'Mandatory setting missing',
err: 'API_SECRET setting is missing, cannot enable REST API'});
}
if (env.settings.authDefaultRoles == 'readable') {
const message = {
title: "Nightscout readable by world"
,message: "Your Nightscout installation is readable by anyone who knows the web page URL. Please consider closing access to the site by following the instructions in the <a href=\"http://nightscout.github.io/nightscout/security/#how-to-turn-off-unauthorized-access\" target=\"_new\">Nightscout documentation</a>."
,persistent: true
};
ctx.adminnotifies.addNotify(message);
}
next();
}
function setupStorage (ctx, next) {
console.log('Executing setupStorage');
if (hasBootErrors(ctx)) {
return next();
}
try {
if (_.startsWith(env.storageURI, 'openaps://')) {
require('../storage/openaps-storage')(env, function ready (err, store) {
if (err) {
throw err;
}
ctx.store = store;
console.log('OpenAPS Storage system ready');
next();
});
} else {
//TODO assume mongo for now, when there are more storage options add a lookup
require('../storage/mongo-storage')(env, function ready(err, store) {
// FIXME, error is always null, if there is an error, the index.js will throw an exception
if (err) {
console.info('ERROR CONNECTING TO MONGO', err);
ctx.bootErrors = ctx.bootErrors || [ ];
ctx.bootErrors.push({'desc': 'Unable to connect to Mongo', err: err.message});
}
console.log('Mongo Storage system ready');
ctx.store = store;
next();
});
}
} catch (err) {
console.info('ERROR CONNECTING TO MONGO', err);
ctx.bootErrors = ctx.bootErrors || [ ];
ctx.bootErrors.push({'desc': 'Unable to connect to Mongo', err: err.message});
next();
}
}
function setupAuthorization (ctx, next) {
console.log('Executing setupAuthorization');
if (hasBootErrors(ctx)) {
return next();
}
ctx.authorization = require('../authorization')(env, ctx);
ctx.authorization.storage.ensureIndexes();
ctx.authorization.storage.reload(function loaded (err) {
if (err) {
ctx.bootErrors = ctx.bootErrors || [ ];
ctx.bootErrors.push({'desc': 'Unable to setup authorization', err: err});
}
next();
});
}
function setupInternals (ctx, next) {
console.log('Executing setupInternals');
if (hasBootErrors(ctx)) {
return next();
}
ctx.levels = require('../levels');
ctx.levels.translate = ctx.language.translate;
///////////////////////////////////////////////////
// api and json object variables
///////////////////////////////////////////////////
ctx.plugins = require('../plugins')({
settings: env.settings
, language: ctx.language
, levels: ctx.levels
, moment: ctx.moment
}).registerServerDefaults();
ctx.wares = require('../middleware/')(env);
ctx.pushover = require('../plugins/pushover')(env, ctx);
ctx.maker = require('../plugins/maker')(env);
ctx.pushnotify = require('./pushnotify')(env, ctx);
ctx.loop = require('./loop')(env, ctx);
ctx.activity = require('./activity')(env, ctx);
ctx.entries = require('./entries')(env, ctx);
ctx.treatments = require('./treatments')(env, ctx);
ctx.devicestatus = require('./devicestatus')(env, ctx);
ctx.profile = require('./profile')(env.profile_collection, ctx);
ctx.food = require('./food')(env, ctx);
ctx.pebble = require('./pebble')(env, ctx);
ctx.properties = require('../api2/properties')(env, ctx);
ctx.ddata = require('../data/ddata')();
ctx.cache = require('./cache')(env,ctx);
ctx.dataloader = require('../data/dataloader')(env, ctx);
ctx.notifications = require('../notifications')(env, ctx);
ctx.purifier = require('./purifier')(env,ctx);
if (env.settings.isEnabled('alexa') || env.settings.isEnabled('googlehome')) {
ctx.virtAsstBase = require('../plugins/virtAsstBase')(env, ctx);
}
if (env.settings.isEnabled('alexa')) {
ctx.alexa = require('../plugins/alexa')(env, ctx);
}
if (env.settings.isEnabled('googlehome')) {
ctx.googleHome = require('../plugins/googlehome')(env, ctx);
}
next( );
}
function ensureIndexes (ctx, next) {
console.log('Executing ensureIndexes');
if (hasBootErrors(ctx)) {
return next();
}
console.info('Ensuring indexes');
ctx.store.ensureIndexes(ctx.entries( ), ctx.entries.indexedFields);
ctx.store.ensureIndexes(ctx.treatments( ), ctx.treatments.indexedFields);
ctx.store.ensureIndexes(ctx.devicestatus( ), ctx.devicestatus.indexedFields);
ctx.store.ensureIndexes(ctx.profile( ), ctx.profile.indexedFields);
ctx.store.ensureIndexes(ctx.food( ), ctx.food.indexedFields);
ctx.store.ensureIndexes(ctx.activity( ), ctx.activity.indexedFields);
next( );
}
function setupListeners (ctx, next) {
console.log('Executing setupListeners');
if (hasBootErrors(ctx)) {
return next();
}
var updateData = _.debounce(function debouncedUpdateData ( ) {
ctx.dataloader.update(ctx.ddata, function dataUpdated () {
ctx.bus.emit('data-loaded');
});
}, UPDATE_THROTTLE);
ctx.bus.on('tick', function timedReloadData (tick) {
console.info('tick', tick.now);
updateData();
});
ctx.bus.on('data-received', function forceReloadData ( ) {
console.info('got data-received event, requesting reload');
updateData();
});
ctx.bus.on('data-loaded', function updatePlugins ( ) {
console.info('data loaded: reloading sandbox data and updating plugins');
var sbx = require('../sandbox')().serverInit(env, ctx);
ctx.plugins.setProperties(sbx);
ctx.notifications.initRequests();
ctx.plugins.checkNotifications(sbx);
ctx.notifications.process(sbx);
ctx.sbx = sbx;
ctx.bus.emit('data-processed', sbx);
});
ctx.bus.on('data-processed', function processed ( ) {
ctx.runtimeState = 'loaded';
});
ctx.bus.on('notification', ctx.pushnotify.emitNotification);
next( );
}
function setupConnect (ctx, next) {
console.log('Executing setupConnect');
ctx.nightscoutConnect = require('nightscout-connect')(env, ctx)
// ctx.nightscoutConnect.
return next( );
}
function setupBridge (ctx, next) {
console.log('Executing setupBridge');
if (hasBootErrors(ctx)) {
return next();
}
ctx.bridge = require('../plugins/bridge')(env, ctx.bus);
if (ctx.bridge) {
ctx.bridge.startEngine(ctx.entries);
console.log("DEPRECATION WARNING", "PLEASE CONSIDER nightscout-connect instead.");
}
next( );
}
function setupMMConnect (ctx, next) {
console.log('Executing setupMMConnect');
if (hasBootErrors(ctx)) {
return next();
}
ctx.mmconnect = require('../plugins/mmconnect').init(env, ctx.entries, ctx.devicestatus, ctx.bus);
if (ctx.mmconnect) {
ctx.mmconnect.run();
console.log("DEPRECATION WARNING", "PLEASE CONSIDER nightscout-connect instead.");
}
next( );
}
function finishBoot (ctx, next) {
console.log('Executing finishBoot');
if (hasBootErrors(ctx)) {
return next();
}
ctx.bus.emit('finishBoot');
ctx.runtimeState = 'booted';
ctx.bus.uptime( );
next( );
}
return require('bootevent')( )
.acquire(startBoot)
.acquire(checkNodeVersion)
.acquire(checkEnv)
.acquire(augmentSettings)
.acquire(checkSettings)
.acquire(setupStorage)
.acquire(setupAuthorization)
.acquire(setupInternals)
.acquire(ensureIndexes)
.acquire(setupListeners)
.acquire(setupConnect)
.acquire(setupBridge)
.acquire(setupMMConnect)
.acquire(finishBoot);
}
module.exports = boot;