forked from nightscout/cgm-remote-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbus.js
39 lines (32 loc) · 789 Bytes
/
bus.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
'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;
function ictus ( ) {
return {
now: new Date( )
, type: 'heartbeat'
, sig: 'internal://' + ['heartbeat', beats ].join('/')
, beat: beats++
, interval: interval
, started: started
};
}
function repeat ( ) {
stream.emit('tick', ictus( ));
}
stream.teardown = function ( ) {
console.log('Initiating server teardown');
clearInterval(busInterval);
stream.emit('teardown');
};
stream.readable = true;
stream.uptime = repeat;
busInterval = setInterval(repeat, interval);
return stream;
}
module.exports = init;