diff --git a/dev/notify.js b/dev/notify.js index db09461a..1edb5276 100644 --- a/dev/notify.js +++ b/dev/notify.js @@ -11,7 +11,7 @@ function getBalances () { } db.init('psql://lamassu:lamassu@localhost/lamassu') -notifier.init(db, getBalances) +notifier.init(db, getBalances, {lowBalanceThreshold: 10}) console.log('DEBUG0') notifier.checkStatus() .then(function (alertRec) { diff --git a/lib/notifier.js b/lib/notifier.js index a8cb0506..9297be5a 100644 --- a/lib/notifier.js +++ b/lib/notifier.js @@ -5,9 +5,18 @@ var numeral = require('numeral') var db = null var getBalances = null -function init (_db, _getBalances) { +var LOW_BALANCE_THRESHOLD = 1000 +var STALE_STATE = 2 * 60 * 1000 +var NETWORK_DOWN_TIME = 60 * 1000 + +function init (_db, _getBalances, config) { db = _db getBalances = _getBalances + + console.log(config) + if (config && config.lowBalanceThreshold) { + LOW_BALANCE_THRESHOLD = config.lowBalanceThreshold + } } exports.init = init @@ -22,7 +31,6 @@ function sameState (a, b) { } function checkBalance (rec) { - var LOW_BALANCE_THRESHOLD = 10 return rec.fiatBalance < LOW_BALANCE_THRESHOLD ? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance, fiatCode: rec.fiatCode} : null @@ -36,7 +44,6 @@ function checkBalances () { function checkPing (deviceEvents) { var sortedEvents = R.sortBy(R.compose(toInt10, R.prop('device_time')), R.map(jsonParse, deviceEvents)) var lastEvent = R.last(sortedEvents) - var NETWORK_DOWN_TIME = 2 * 60 * 1000 if (!lastEvent) { return [{code: 'ping'}] @@ -54,7 +61,6 @@ function checkStuckScreen (deviceEvents) { var sortedEvents = R.sortBy(R.compose(toInt10, R.prop('device_time')), R.map(jsonParse, deviceEvents)) var noRepeatEvents = R.dropRepeatsWith(sameState, sortedEvents) var lastEvent = R.last(noRepeatEvents) - var STALE_STATE = 60 * 1000 if (!lastEvent) { return [] diff --git a/lib/plugins.js b/lib/plugins.js index 26d8bfc9..4aab69ae 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -347,8 +347,6 @@ function reapTxs () { // TODO: Run these in parallel and return success exports.trade = function trade (session, rawTrade, cb) { - logger.debug('DEBUG2') - // TODO: move this to DB, too // add bill to trader queue (if trader is enabled) var cryptoCode = rawTrade.cryptoCode || 'BTC' @@ -363,16 +361,12 @@ exports.trade = function trade (session, rawTrade, cb) { }) } - logger.debug('DEBUG3') - if (!rawTrade.toAddress) { var newRawTrade = _.cloneDeep(rawTrade) newRawTrade.toAddress = 'remit' return db.recordBill(session, newRawTrade, cb) } - logger.debug('DEBUG1') - async.parallel([ async.apply(db.addOutgoingPending, session, rawTrade.currency, rawTrade.cryptoCode, rawTrade.toAddress), async.apply(db.recordBill, session, rawTrade) @@ -391,7 +385,6 @@ exports.stateChange = function stateChange (session, rec, cb) { } exports.recordPing = function recordPing (session, rec, cb) { - console.log('DEBUG4') var event = { id: uuid.v4(), fingerprint: session.fingerprint, @@ -640,11 +633,7 @@ exports.getcryptoCodes = function getcryptoCodes () { } function sendMessage (rec) { - console.log('DEBUG50') - console.log('%j', rec) - console.log(cachedConfig.exchanges.plugins.current.notify) var pluginTypes = JSON.parse(cachedConfig.exchanges.plugins.current.notify) - console.log(pluginTypes) var pluginPromises = pluginTypes.map(function (pluginType) { if (pluginType === 'email') return emailPlugin.sendMessage(rec) if (pluginType === 'sms') return smsPlugin.sendMessage(rec) @@ -669,27 +658,20 @@ function sendNoAlerts () { } function checkNotification () { - console.log('DEBUG39 ******************************') return notifier.checkStatus() .then(function (alertRec) { - console.log('DEBUG41 ******************************') var fingerprint = notifier.alertFingerprint(alertRec) if (!fingerprint) { - console.log('DEBUG40 ******************************') var inAlert = !!alertFingerprint alertFingerprint = null lastAlertTime = null if (inAlert) return sendNoAlerts() } - console.log('DEBUG42 ******************************') - var alertChanged = fingerprint === alertFingerprint && lastAlertTime - Date.now() < ALERT_SEND_INTERVAL if (alertChanged) return - console.log('DEBUG43 ******************************') - var subject = notifier.alertSubject(alertRec) var rec = { sms: { @@ -702,6 +684,7 @@ function checkNotification () { } alertFingerprint = fingerprint lastAlertTime = Date.now() + return sendMessage(rec) }) .then(function () { @@ -727,7 +710,8 @@ function checkBalances () { } exports.startCheckingNotification = function startCheckingNotification () { - notifier.init(db, checkBalances) + var config = cachedConfig.exchanges.plugins.settings.notifier + notifier.init(db, checkBalances, config) checkNotification() setInterval(checkNotification, CHECK_NOTIFICATION_INTERVAL) } diff --git a/lib/routes.js b/lib/routes.js index f6de2e03..ae21e6ae 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -167,7 +167,6 @@ function dispenseAck (req, res) { } function deviceEvent (req, res) { - console.log('DEBUG5') plugins.logEvent(session(req), req.body) res.json({err: null}) } diff --git a/todo.txt b/todo.txt index e79a9233..9676b2f9 100644 --- a/todo.txt +++ b/todo.txt @@ -1,6 +1 @@ -- configure email, sms or both for notification -- 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 +- configure LOW_BALANCE_THRESHOLD, STALE_STATE, NETWORK_DOWN_TIME