mirror of
https://github.com/bckelley/cgm-remote-monitor.git
synced 2026-08-24 03:14:12 -05:00
Add server teardown ability (#5410)
* adding teardown event to ctx.bus * adding teardown support for mmconnect and bridge plugins
This commit is contained in:
committed by
Sulka Haro
parent
ee0b1e8183
commit
0def1f225e
+8
-2
@@ -1,11 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
var Stream = require('stream');
|
||||
|
||||
function init (settings) {
|
||||
var beats = 0;
|
||||
var started = new Date( );
|
||||
var interval = settings.heartbeat * 1000;
|
||||
let busInterval;
|
||||
|
||||
var stream = new Stream;
|
||||
|
||||
@@ -24,9 +24,15 @@ function init (settings) {
|
||||
stream.emit('tick', ictus( ));
|
||||
}
|
||||
|
||||
stream.teardown = function ( ) {
|
||||
console.log('Initiating server teardown');
|
||||
clearInterval(busInterval);
|
||||
stream.emit('teardown');
|
||||
};
|
||||
|
||||
stream.readable = true;
|
||||
stream.uptime = repeat;
|
||||
setInterval(repeat, interval);
|
||||
busInterval = setInterval(repeat, interval);
|
||||
return stream;
|
||||
}
|
||||
module.exports = init;
|
||||
|
||||
+12
-6
@@ -5,9 +5,9 @@ var engine = require('share2nightscout-bridge');
|
||||
// Track the most recently seen record
|
||||
var mostRecentRecord;
|
||||
|
||||
function init (env) {
|
||||
function init (env, bus) {
|
||||
if (env.extendedSettings.bridge && env.extendedSettings.bridge.userName && env.extendedSettings.bridge.password) {
|
||||
return create(env);
|
||||
return create(env, bus);
|
||||
} else {
|
||||
console.info('Dexcom bridge not enabled');
|
||||
}
|
||||
@@ -64,26 +64,32 @@ function options (env) {
|
||||
};
|
||||
}
|
||||
|
||||
function create (env) {
|
||||
function create (env, bus) {
|
||||
|
||||
var bridge = { };
|
||||
|
||||
var opts = options(env);
|
||||
var interval = opts.interval;
|
||||
|
||||
mostRecentRecord = new Date().getTime() - opts.fetch.minutes * 60000
|
||||
mostRecentRecord = new Date().getTime() - opts.fetch.minutes * 60000;
|
||||
|
||||
bridge.startEngine = function startEngine (entries) {
|
||||
|
||||
opts.callback = bridged(entries);
|
||||
|
||||
setInterval(function () {
|
||||
let timer = setInterval(function () {
|
||||
opts.fetch.minutes = parseInt((new Date() - mostRecentRecord) / 60000);
|
||||
opts.fetch.maxCount = parseInt((opts.fetch.minutes / 5) + 1);
|
||||
opts.firstFetchCount = opts.fetch.maxCount
|
||||
opts.firstFetchCount = opts.fetch.maxCount;
|
||||
console.log("Fetching Share Data: ", 'minutes', opts.fetch.minutes, 'maxCount', opts.fetch.maxCount);
|
||||
engine(opts);
|
||||
}, interval);
|
||||
|
||||
if (bus) {
|
||||
bus.on('teardown', function serverTeardown () {
|
||||
clearInterval(timer);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return bridge;
|
||||
|
||||
@@ -4,16 +4,16 @@
|
||||
var _ = require('lodash'),
|
||||
connect = require('minimed-connect-to-nightscout');
|
||||
|
||||
function init (env, entries, devicestatus) {
|
||||
function init (env, entries, devicestatus, bus) {
|
||||
if (env.extendedSettings.mmconnect && env.extendedSettings.mmconnect.userName && env.extendedSettings.mmconnect.password) {
|
||||
return {run: makeRunner(env, entries, devicestatus)};
|
||||
return {run: makeRunner(env, entries, devicestatus, bus)};
|
||||
} else {
|
||||
console.info('MiniMed Connect not enabled');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function makeRunner (env, entries, devicestatus) {
|
||||
function makeRunner (env, entries, devicestatus, bus) {
|
||||
var options = getOptions(env);
|
||||
|
||||
var client = connect.carelink.Client(options);
|
||||
@@ -22,9 +22,15 @@ function makeRunner (env, entries, devicestatus) {
|
||||
var handleData = makeHandler_(entries, devicestatus, options.sgvLimit, options.storeRawData);
|
||||
|
||||
return function run () {
|
||||
setInterval(function() {
|
||||
let timer = setInterval(function() {
|
||||
client.fetch(handleData);
|
||||
}, options.interval);
|
||||
|
||||
if (bus) {
|
||||
bus.on('teardown', function serverTeardown () {
|
||||
clearInterval(timer);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -247,7 +247,7 @@ function boot (env, language) {
|
||||
return next();
|
||||
}
|
||||
|
||||
ctx.bridge = require('../plugins/bridge')(env);
|
||||
ctx.bridge = require('../plugins/bridge')(env, ctx.bus);
|
||||
if (ctx.bridge) {
|
||||
ctx.bridge.startEngine(ctx.entries);
|
||||
}
|
||||
@@ -259,7 +259,7 @@ function boot (env, language) {
|
||||
return next();
|
||||
}
|
||||
|
||||
ctx.mmconnect = require('../plugins/mmconnect').init(env, ctx.entries, ctx.devicestatus);
|
||||
ctx.mmconnect = require('../plugins/mmconnect').init(env, ctx.entries, ctx.devicestatus, ctx.bus);
|
||||
if (ctx.mmconnect) {
|
||||
ctx.mmconnect.run();
|
||||
}
|
||||
|
||||
@@ -73,6 +73,13 @@ function init (env, ctx, server) {
|
||||
'browser client etag': true,
|
||||
'browser client gzip': false
|
||||
});
|
||||
|
||||
ctx.bus.on('teardown', function serverTeardown () {
|
||||
Object.keys(io.sockets.sockets).forEach(function(s) {
|
||||
io.sockets.sockets[s].disconnect(true);
|
||||
});
|
||||
io.close();
|
||||
});
|
||||
}
|
||||
|
||||
function verifyAuthorization (message, callback) {
|
||||
|
||||
@@ -54,6 +54,12 @@ require('./lib/server/bootevent')(env, language).boot(function booted (ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.bus.on('teardown', function serverTeardown () {
|
||||
server.close();
|
||||
clearTimeout(sendStartupAllClearTimer);
|
||||
ctx.store.client.close();
|
||||
});
|
||||
|
||||
///////////////////////////////////////////////////
|
||||
// setup socket io for data and message transmission
|
||||
///////////////////////////////////////////////////
|
||||
@@ -68,7 +74,7 @@ require('./lib/server/bootevent')(env, language).boot(function booted (ctx) {
|
||||
});
|
||||
|
||||
//after startup if there are no alarms send all clear
|
||||
setTimeout(function sendStartupAllClear () {
|
||||
let sendStartupAllClearTimer = setTimeout(function sendStartupAllClear () {
|
||||
var alarm = ctx.notifications.findHighestAlarm();
|
||||
if (!alarm) {
|
||||
ctx.bus.emit('notification', {
|
||||
|
||||
@@ -19,7 +19,7 @@ describe('Basic REST API3', function() {
|
||||
|
||||
|
||||
after(function after () {
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ describe('API3 CREATE', function() {
|
||||
|
||||
|
||||
after(() => {
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ describe('API3 UPDATE', function() {
|
||||
|
||||
|
||||
after(() => {
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ describe('Generic REST API3', function() {
|
||||
|
||||
|
||||
after(() => {
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ describe('API3 PATCH', function() {
|
||||
|
||||
|
||||
after(() => {
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ describe('API3 READ', function() {
|
||||
|
||||
|
||||
after(() => {
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ describe('API3 SEARCH', function() {
|
||||
|
||||
|
||||
after(() => {
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ const request = require('supertest')
|
||||
, moment = require('moment')
|
||||
;
|
||||
require('should');
|
||||
|
||||
|
||||
describe('Security of REST API3', function() {
|
||||
const self = this
|
||||
, instance = require('./fixtures/api3/instance')
|
||||
@@ -26,10 +26,10 @@ describe('Security of REST API3', function() {
|
||||
self.token = authResult.token;
|
||||
});
|
||||
|
||||
|
||||
|
||||
after(() => {
|
||||
self.http.server.close();
|
||||
self.https.server.close();
|
||||
self.http.ctx.bus.teardown();
|
||||
self.https.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ describe('Socket.IO in REST API3', function() {
|
||||
if(self.instance && self.instance.clientSocket && self.instance.clientSocket.connected) {
|
||||
self.instance.clientSocket.disconnect();
|
||||
}
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ describe('API3 UPDATE', function() {
|
||||
|
||||
|
||||
after(() => {
|
||||
self.instance.server.close();
|
||||
self.instance.ctx.bus.teardown();
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user