Skip to content

Commit

Permalink
fixes and improvements to the all clear notification
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Jun 30, 2015
1 parent ad8a551 commit 6093f9e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
13 changes: 11 additions & 2 deletions lib/maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ function init (env) {

var lastAllClear = 0;

maker.sendAllClear = function sendAllClear (callback) {
maker.sendAllClear = function sendAllClear (notify, callback) {
if (Date.now() - lastAllClear > TIME_30_MINS_MS) {
lastAllClear = Date.now();
maker.makeRequest({}, 'allclear', function allClearCallback (err) {

//can be used to prevent maker/twitter deduping (add to IFTTT tweet text)
var shortTimestamp = Math.round(Date.now() / 1000 / 60);

maker.makeRequest({
value1: (notify && notify.title) || 'All Clear'
, value2: notify && notify.message && '\n' + notify.message
, value3: '\n' + shortTimestamp
}, 'ns-allclear', function allClearCallback (err) {
if (err) {
lastAllClear = 0;
callback(err);
Expand Down Expand Up @@ -72,6 +80,7 @@ function init (env) {
request
.get(url)
.on('response', function (response) {
console.info('sent maker request: ', url);
if (callback) { callback(null, response); }
})
.on('error', function (err) {
Expand Down
10 changes: 7 additions & 3 deletions lib/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function init (env, ctx) {
}

//should only be used when auto acking the alarms after going back in range or when an error corrects
//setting the silence time to 1ms so the alarm will be retriggered as soon as the condition changes
//setting the silence time to 1ms so the alarm will be re-triggered as soon as the condition changes
//since this wasn't ack'd by a user action
function autoAckAlarms() {

Expand All @@ -75,7 +75,7 @@ function init (env, ctx) {
}

if (sendClear) {
ctx.bus.emit('notification', {clear: true});
ctx.bus.emit('notification', {clear: true, title: 'All Clear', message: 'Auto ack\'d alarm(s)'});
console.info('emitted notification clear');
}
}
Expand Down Expand Up @@ -182,7 +182,11 @@ function init (env, ctx) {
}

if (sendClear) {
ctx.bus.emit('notification', {clear: true});
ctx.bus.emit('notification', {
clear: true
, title: 'All Clear'
, message: notifications.levels.toString(level) + ' was ack\'d'
});
}

};
Expand Down
8 changes: 3 additions & 5 deletions lib/pushnotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ function init(env, ctx) {

pushnotify.emitNotification = function emitNotification (notify) {
if (notify.clear) {
console.info('got a notify clear');
if (ctx.pushover) { cancelPushoverNotifications(); }
if (ctx.maker) { sendMakerAllClear(); }

if (ctx.maker) { sendMakerAllClear(notify); }
return;
}

Expand Down Expand Up @@ -118,8 +116,8 @@ function init(env, ctx) {
});
}

function sendMakerAllClear ( ) {
ctx.maker.sendAllClear(function makerCallback (err, result) {
function sendMakerAllClear (notify) {
ctx.maker.sendAllClear(notify, function makerCallback (err, result) {
if (err) {
console.error('unable to send maker allclear', err);
} else if (result && result.sent) {
Expand Down
2 changes: 1 addition & 1 deletion lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function init (env, ctx, server) {
io.emit('clients', ++watchers);
socket.on('ack', function(alarmType, silenceTime) {
var level = alarmType == 'urgent_alarm' ? 2 : 1;
ctx.notifications.ack(level, silenceTime);
ctx.notifications.ack(level, silenceTime, true);
});
socket.on('disconnect', function () {
io.emit('clients', --watchers);
Expand Down
12 changes: 12 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ require('./lib/bootevent')(env).boot(function booted (ctx) {
ctx.bus.on('notification', function(info) {
websocket.emitNotification(info);
});

//after startup if there are no alarms send all clear
setTimeout(function sendStartupAllClear () {
var alarm = ctx.notifications.findHighestAlarm();
if (!alarm) {
ctx.bus.emit('notification', {
clear: true
, title: 'All Clear'
, message: 'Server started without alarms'
});
}
}, 20000);
});
10 changes: 7 additions & 3 deletions tests/maker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ describe('maker', function ( ) {
var maker = require('../lib/maker')({extendedSettings: {maker: {key: '12345'}}});

//prevent any calls to iftt
maker.makeRequest = function noOpMakeRequest (event, eventName, callback) { callback && callback()};
function noOpMakeRequest (event, eventName, callback) {
if (callback) { callback(); }
}

maker.makeRequest = noOpMakeRequest;

it('turn values to a query', function (done) {
maker.valuesToQuery({
Expand All @@ -27,13 +31,13 @@ describe('maker', function ( ) {
}

maker.makeRequest = mockedToTestSingleDone;
maker.sendAllClear(function sendCallback (err, result) {
maker.sendAllClear({}, function sendCallback (err, result) {
should.not.exist(err);
result.sent.should.equal(true);
});

//send again, if done is called again test will fail
maker.sendAllClear(function sendCallback (err, result) {
maker.sendAllClear({}, function sendCallback (err, result) {
should.not.exist(err);
result.sent.should.equal(false);
});
Expand Down

0 comments on commit 6093f9e

Please sign in to comment.