diff --git a/lib/app.js b/lib/app.js index 8fdc83f5..55fe94dc 100644 --- a/lib/app.js +++ b/lib/app.js @@ -31,6 +31,7 @@ module.exports = function (options) { plugins.configure(config) plugins.startPolling() + plugins.startCheckingNotification() }) lamassuConfig.on('configUpdate', function () { diff --git a/lib/plugins.js b/lib/plugins.js index 77949856..296a9a5b 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -2,15 +2,19 @@ var _ = require('lodash') var async = require('async') +var R = require('ramda') var BigNumber = require('bignumber.js') var logger = require('./logger') +var notifier = require('./notifier') + var argv = require('minimist')(process.argv.slice(2)) var uuid = require('node-uuid') var tradeIntervals = {} +var CHECK_NOTIFICATION_INTERVAL = 60 * 1000 var POLLING_RATE = 60 * 1000 // poll each minute var REAP_RATE = 2 * 1000 var PENDING_TIMEOUT = 70 * 1000 @@ -630,7 +634,7 @@ exports.getcryptoCodes = function getcryptoCodes () { return cryptoCodes } -exports.sendMessage = function sendMessage (rec, cb) { +function sendMessage (rec, cb) { console.log('DEBUG5') cb = cb || function () {} console.log(cachedConfig.exchanges.plugins.current.notify) @@ -644,3 +648,35 @@ exports.sendMessage = function sendMessage (rec, cb) { }) return Promise.all(pluginPromises) } + +function checkNotification () { + notifier.checkStatus() + .then(function (alerts) { + if (alerts.length === 0) return + + // var devices = R.keys(alerts.devices) + var alertRecs = alerts.general + R.keys(alerts.devices).forEach(function (device) { + alertRecs = R.append(alerts.devices[device], alertRecs) + }) + var alertTypes = alertRecs.pluck('code', alerts) + var body = '' + var rec = { + sms: { + body: '[Lamassu] Errors reported: ' + alertTypes.join(', ') + }, + email: { + subject: '[Lamassu] Errors reported: ' + alertTypes.join(', '), + body: body + } + } + sendMessage(rec) + }) + .catch(function (err) { + logger.error(err) + }) +} + +exports.startCheckingNotification = function startCheckingNotification () { + setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL) +} diff --git a/todo.txt b/todo.txt index e56ee262..e79a9233 100644 --- a/todo.txt +++ b/todo.txt @@ -2,3 +2,5 @@ - integrate email and sms into notifications - run notifications from server - don't keep sending notifications when state persists -- keep variable in memory +- add toEmail, toNumber to db +- finish up notification emails