diff --git a/lib/notifier.js b/lib/notifier.js index 61c74a23..65a57e3a 100644 --- a/lib/notifier.js +++ b/lib/notifier.js @@ -23,10 +23,10 @@ function sameState (a, b) { function checkBalance () { var LOW_BALANCE_THRESHOLD = 10 if (getBalance() < LOW_BALANCE_THRESHOLD) { - return console.log('Bitcoin balance is low [ALERT]') + return [{code: 'lowBitcoinBalance'}] } - console.log('Bitcoin balance ok [OK]') + return [] } function checkPing (deviceEvents) { @@ -35,15 +35,14 @@ function checkPing (deviceEvents) { var NETWORK_DOWN_TIME = 2 * 60 * 1000 if (!lastEvent) { - console.log('No data for device') - return + return [] } if (lastEvent.age > NETWORK_DOWN_TIME) { - return console.log('Device not reachable [ALERT]') + return [{code: 'ping'}] } - console.log('Device reachable [OK]') + return [] } function checkStuckScreen (deviceEvents) { @@ -54,26 +53,23 @@ function checkStuckScreen (deviceEvents) { var STALE_STATE = 60 * 1000 if (!lastEvent) { - console.log('No data for device') - return + return [] } var state = lastEvent.note.state if (R.contains(state, IDLE_STATES)) { - console.log('Machine is idle [OK]') - return + return [] } console.log(lastEvent.age) if (lastEvent.age > STALE_STATE) { - console.log('Stale state: ' + state + ' [ALERT]') - return + return [{code: 'stale', state: state}] } - console.log('[OK]') + return [] } -function checkStatus () { +function checkStatus (cb) { /* - Fetch devices from devices table - Fetch all machine_events into memory @@ -82,18 +78,20 @@ function checkStatus () { - last screen is >5m stale and is not idle screen - report stuck on screen and name of screen - not scanning qr codes? - - low bitcoins -- need a separate strategy, but server has this info + v low bitcoins -- need a separate strategy, but server has this info - var fiatBalance = plugins.fiatBalance(); v machine isn't pinging server - - jam checking, need report from l-m + - jam checking, need report from l-m [next release] */ - checkBalance() + var alerts = [] + + alerts = R.concat(alerts, checkBalance()) db.devices(function (err, devices) { - if (err) return console.error(err) + if (err) return cb(err) db.machineEvents(function (err, events) { - if (err) return console.error(err) + if (err) return cb(err) devices.rows.forEach(function (deviceRow) { var deviceFingerprint = deviceRow.fingerprint @@ -102,8 +100,10 @@ function checkStatus () { }) console.log('DEVICE: ' + deviceRow.fingerprint) - checkStuckScreen(deviceEvents) - checkPing(deviceEvents) + alerts = R.concat(alerts, checkStuckScreen(deviceRow, deviceEvents)) + alerts = R.concat(alerts, checkPing(deviceRow, deviceEvents)) + + // TODO: Use promises to do this right }) }) }) @@ -119,7 +119,11 @@ var _getBalance = function () { _db.init(connectionString) init(_db, _getBalance) -checkStatus() +checkStatus(function (err, alerts) { + if (err) console.log(err) + console.log('DEBUG1') + console.log(alerts) +}) // TODO: How to know which alerts have been sent? // Send alert every 10m while alert state