WIP
This commit is contained in:
parent
4b5b460d12
commit
e42bde7b63
1 changed files with 59 additions and 68 deletions
127
lib/notifier.js
127
lib/notifier.js
|
|
@ -1,13 +1,14 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
require('es6-promise').polyfill()
|
||||||
|
|
||||||
var R = require('ramda')
|
var R = require('ramda')
|
||||||
|
|
||||||
var db = null
|
var db = null
|
||||||
var getBalance = null
|
var getBalances = null
|
||||||
|
|
||||||
function init (_db, _getBalance) {
|
function init (_db, _getBalances) {
|
||||||
db = _db
|
db = _db
|
||||||
getBalance = _getBalance
|
getBalances = _getBalances
|
||||||
}
|
}
|
||||||
|
|
||||||
function toInt10 (str) { return parseInt(str, 10) }
|
function toInt10 (str) { return parseInt(str, 10) }
|
||||||
|
|
@ -20,13 +21,16 @@ function sameState (a, b) {
|
||||||
return a.note.sessionId === b.note.sessionId && a.note.state === b.note.state
|
return a.note.sessionId === b.note.sessionId && a.note.state === b.note.state
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkBalance () {
|
function checkBalance (rec) {
|
||||||
var LOW_BALANCE_THRESHOLD = 10
|
var LOW_BALANCE_THRESHOLD = 10
|
||||||
if (getBalance() < LOW_BALANCE_THRESHOLD) {
|
return rec.fiatBalance < LOW_BALANCE_THRESHOLD
|
||||||
return [{code: 'lowBitcoinBalance'}]
|
? {code: 'lowBalance', cryptoCode: rec.cryptoCode, fiatBalance: rec.fiatBalance}
|
||||||
}
|
: null
|
||||||
|
}
|
||||||
|
|
||||||
return []
|
function checkBalances () {
|
||||||
|
var balances = getBalances()
|
||||||
|
return R.reject(R.isNil, balances.map(checkBalance))
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkPing (deviceEvents) {
|
function checkPing (deviceEvents) {
|
||||||
|
|
@ -38,11 +42,9 @@ function checkPing (deviceEvents) {
|
||||||
return [{code: 'ping'}]
|
return [{code: 'ping'}]
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('DEBUG6')
|
var age = Math.floor(lastEvent.age)
|
||||||
console.log(lastEvent)
|
if (age > NETWORK_DOWN_TIME) {
|
||||||
|
return [{code: 'ping', age: age}]
|
||||||
if (lastEvent.age > NETWORK_DOWN_TIME) {
|
|
||||||
return [{code: 'ping'}]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return []
|
return []
|
||||||
|
|
@ -65,78 +67,67 @@ function checkStuckScreen (deviceEvents) {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(lastEvent.age)
|
var age = Math.floor(lastEvent.age)
|
||||||
if (lastEvent.age > STALE_STATE) {
|
if (age > STALE_STATE) {
|
||||||
return [{code: 'stale', state: state}]
|
return [{code: 'stale', state: state, age: age}]
|
||||||
}
|
}
|
||||||
|
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkStatus (cb) {
|
function devicesAndEvents () {
|
||||||
/*
|
return new Promise(function (resolve, reject) {
|
||||||
- Fetch devices from devices table
|
db.devices(function (err, devices) {
|
||||||
- Fetch all machine_events into memory
|
if (err) return reject(err)
|
||||||
- For each device, verify the following:
|
|
||||||
v stuck on screen
|
|
||||||
- last screen is >5m stale and is not idle screen
|
|
||||||
- report stuck on screen and name of screen
|
|
||||||
- not scanning qr codes?
|
|
||||||
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 [next release]
|
|
||||||
*/
|
|
||||||
var alerts = []
|
|
||||||
|
|
||||||
alerts = R.concat(alerts, checkBalance())
|
db.machineEvents(function (err, events) {
|
||||||
|
if (err) return reject(err)
|
||||||
|
|
||||||
db.devices(function (err, devices) {
|
return resolve({devices: devices, events: events})
|
||||||
if (err) return cb(err)
|
|
||||||
|
|
||||||
db.machineEvents(function (err, events) {
|
|
||||||
if (err) return cb(err)
|
|
||||||
|
|
||||||
devices.rows.forEach(function (deviceRow) {
|
|
||||||
var deviceFingerprint = deviceRow.fingerprint
|
|
||||||
var deviceEvents = events.rows.filter(function (eventRow) {
|
|
||||||
return eventRow.device_fingerprint === deviceFingerprint
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log('DEVICE: ' + deviceRow.fingerprint)
|
|
||||||
alerts = R.concat(alerts, checkStuckScreen(deviceEvents))
|
|
||||||
alerts = R.concat(alerts, checkPing(deviceEvents))
|
|
||||||
|
|
||||||
console.log(deviceFingerprint)
|
|
||||||
console.log(alerts)
|
|
||||||
|
|
||||||
// TODO: Use promises to do this right
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkStatus () {
|
||||||
|
var alerts = {devices: {}}
|
||||||
|
alerts.general = checkBalances()
|
||||||
|
|
||||||
|
return devicesAndEvents()
|
||||||
|
.then(function (rec) {
|
||||||
|
var devices = rec.devices
|
||||||
|
var events = rec.events
|
||||||
|
|
||||||
|
devices.rows.forEach(function (deviceRow) {
|
||||||
|
var deviceFingerprint = deviceRow.fingerprint
|
||||||
|
var deviceEvents = events.rows.filter(function (eventRow) {
|
||||||
|
return eventRow.device_fingerprint === deviceFingerprint
|
||||||
|
})
|
||||||
|
|
||||||
|
var deviceAlerts = []
|
||||||
|
deviceAlerts = R.concat(deviceAlerts, checkStuckScreen(deviceEvents))
|
||||||
|
deviceAlerts = R.concat(deviceAlerts, checkPing(deviceEvents))
|
||||||
|
|
||||||
|
alerts.devices[deviceRow.fingerprint] = deviceAlerts
|
||||||
|
})
|
||||||
|
|
||||||
|
return alerts
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var _db = require('./postgresql_interface')
|
var _db = require('./postgresql_interface')
|
||||||
var connectionString = 'postgres://lamassu:lamassu@localhost/lamassu'
|
var connectionString = 'postgres://lamassu:lamassu@localhost/lamassu'
|
||||||
|
|
||||||
var _getBalance = function () {
|
var _getBalances = function () {
|
||||||
return 12
|
return [{cryptoCode: 'BTC', fiatBalance: 12}, {cryptoCode: 'ETH', fiatBalance: 8}]
|
||||||
}
|
}
|
||||||
|
|
||||||
_db.init(connectionString)
|
_db.init(connectionString)
|
||||||
init(_db, _getBalance)
|
init(_db, _getBalances)
|
||||||
|
|
||||||
checkStatus(function (err, alerts) {
|
checkStatus()
|
||||||
if (err) console.log(err)
|
.then(function (alerts) {
|
||||||
console.log('DEBUG1')
|
console.log('DEBUG1')
|
||||||
console.log(alerts)
|
console.log('%j', alerts)
|
||||||
|
process.exit(0)
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: How to know which alerts have been sent?
|
|
||||||
// Send alert every 10m while alert state
|
|
||||||
// Remember last sent alert in memory
|
|
||||||
|
|
||||||
/*
|
|
||||||
- check balances for all cryptos
|
|
||||||
- update idle screens
|
|
||||||
*/
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue