Skip to content

Commit bcd6b27

Browse files
ctx.bus
1 parent 055e6ad commit bcd6b27

File tree

4 files changed

+17
-14
lines changed

4 files changed

+17
-14
lines changed

lib/bootevent.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ function boot (env) {
3030
store.ensureIndexes(ctx.devicestatus( ), ctx.devicestatus.indexedFields);
3131
store.ensureIndexes(ctx.profile( ), ctx.profile.indexedFields);
3232

33-
ctx.heartbeat = require('./ticker')(env, ctx);
33+
ctx.bus = require('./bus')(env, ctx);
3434

3535
ctx.data = require('./data')(env, ctx);
3636
ctx.notifications = require('./notifications')(env, ctx);
3737

38-
ctx.heartbeat.on('tick', function(tick) {
38+
ctx.bus.on('tick', function(tick) {
3939
console.info('tick', tick.now);
4040
ctx.data.update(function dataUpdated () {
41-
ctx.heartbeat.emit('data-loaded');
41+
ctx.bus.emit('data-loaded');
4242
});
4343
});
4444

45-
ctx.heartbeat.on('data-loaded', function() {
45+
ctx.bus.on('data-loaded', function() {
4646
ctx.notifications.processData(env, ctx);
4747
});
4848

49-
ctx.heartbeat.uptime( );
49+
ctx.bus.uptime( );
5050

5151
next( );
5252
})

lib/ticker.js lib/bus.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
2-
var es = require('event-stream');
31
var Stream = require('stream');
42

5-
function heartbeat (env, ctx) {
3+
function init (env, ctx) {
64
var beats = 0;
75
var started = new Date( );
86
var id;
97
var interval = env.HEARTBEAT || 20000;
8+
9+
var stream = new Stream;
10+
1011
function ictus ( ) {
1112
var tick = {
1213
now: new Date( )
@@ -18,18 +19,20 @@ function heartbeat (env, ctx) {
1819
};
1920
return tick;
2021
}
22+
2123
function repeat ( ) {
2224
stream.emit('tick', ictus( ));
2325
}
26+
2427
function ender ( ) {
2528
if (id) cancelInterval(id);
2629
stream.emit('end');
2730
}
28-
var stream = new Stream;
31+
2932
stream.readable = true;
3033
stream.uptime = repeat;
3134
id = setInterval(repeat, interval);
3235
return stream;
3336
}
34-
module.exports = heartbeat;
37+
module.exports = init;
3538

lib/notifications.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ function init (env, ctx) {
3838
}
3939
}
4040
if (sendClear) {
41-
ctx.heartbeat.emit('notification', {clear: true});
41+
ctx.bus.emit('notification', {clear: true});
4242
console.info('emitted notification clear');
4343
}
4444
}
4545

4646
function emitAlarm (type) {
4747
var alarm = alarms[type];
4848
if (ctx.data.lastUpdated > alarm.lastAckTime + alarm.silenceTime) {
49-
ctx.heartbeat.emit('notification', {type: type});
49+
ctx.bus.emit('notification', {type: type});
5050
alarm.lastEmitTime = ctx.data.lastUpdated;
5151
console.info('emitted notification:' + type);
5252
} else {

server.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ bootevent(env).boot(function booted (ctx) {
6060
///////////////////////////////////////////////////
6161
var websocket = require('./lib/websocket')(env, ctx, server);
6262

63-
ctx.heartbeat.on('data-loaded', function() {
63+
ctx.bus.on('data-loaded', function() {
6464
websocket.processData();
6565
});
6666

67-
ctx.heartbeat.on('notification', function(info) {
67+
ctx.bus.on('notification', function(info) {
6868
websocket.emitNotification(info);
6969
});
7070

0 commit comments

Comments
 (0)