more refactoring

This commit is contained in:
Josh Harvey 2016-07-28 18:03:51 +03:00
parent 8acbc90dbd
commit 9a9c927801
6 changed files with 30 additions and 30 deletions

14
bin/ssu
View file

@ -17,7 +17,7 @@ if (!cmd) bail()
function bail (error) { function bail (error) {
error = error || 'Command line utility for lamassu-server' error = error || 'Command line utility for lamassu-server'
console.log(chalk.bold(error)) console.log(chalk.bold(error))
console.log('\nssu reboot <machine-fingerprint>') console.log('\nssu reboot <machine-id>')
console.log('This will remotely reboot your lamassu-machine.') console.log('This will remotely reboot your lamassu-machine.')
console.log('\nssu crypto <code> <ticker-plugin> <wallet-plugin> [<trader-plugin>]') console.log('\nssu crypto <code> <ticker-plugin> <wallet-plugin> [<trader-plugin>]')
console.log('This will configure a new cryptocurrency.') console.log('This will configure a new cryptocurrency.')
@ -54,15 +54,15 @@ switch (cmd) {
} }
function reboot () { function reboot () {
var fingerprint = argv[1] var deviceId = argv[1]
if (!fingerprint) { if (!deviceId) {
console.log('Fingerprint required') console.log('Machine ID required')
process.exit(1) process.exit(1)
} }
var opts = {json: true} var opts = {json: true}
wreck.get('http://localhost:7070/pid?fingerprint=' + fingerprint, opts, function (err, res, payload) { wreck.get('http://localhost:7070/pid?device_id=' + deviceId, opts, function (err, res, payload) {
if (err) { if (err) {
console.log('Please make sure that lamassu-server is running on this box.') console.log('Please make sure that lamassu-server is running on this box.')
process.exit(2) process.exit(2)
@ -82,7 +82,7 @@ function reboot () {
var opts2 = { var opts2 = {
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
payload: JSON.stringify({pid: pid, fingerprint: fingerprint}) payload: JSON.stringify({pid: pid, deviceId: deviceId})
} }
wreck.post('http://localhost:7070/reboot', opts2, function (err2, res) { wreck.post('http://localhost:7070/reboot', opts2, function (err2, res) {
@ -111,7 +111,7 @@ function reboot () {
}, 30000) }, 30000)
setInterval(function () { setInterval(function () {
wreck.get('http://localhost:7070/pid?fingerprint=' + fingerprint, opts, function (err3, res, payload) { wreck.get('http://localhost:7070/pid?device_id=' + deviceId, opts, function (err3, res, payload) {
if (err3) { if (err3) {
console.log('lamassu-server appears to be down.') console.log('lamassu-server appears to be down.')
process.exit(2) process.exit(2)

View file

@ -64,7 +64,7 @@ module.exports = function (options) {
server = https.createServer(serverOptions, app) server = https.createServer(serverOptions, app)
authMiddleware = function (req, res, next) { authMiddleware = function (req, res, next) {
lamassuConfig.isAuthorized(routes.getFingerprint(req), function (err, lamassuConfig.isAuthorized(routes.getDeviceId(req), function (err,
device) { device) {
if (err) { if (err) {
res.json({err: 'Internal Server Error'}) res.json({err: 'Internal Server Error'})

View file

@ -95,18 +95,18 @@ function checkStatus () {
var events = rec.events var events = rec.events
devices.forEach(function (deviceRow) { devices.forEach(function (deviceRow) {
var deviceFingerprint = deviceRow.fingerprint var deviceId = deviceRow.device_id
var deviceName = deviceRow.name || deviceFingerprint var deviceName = deviceRow.name || deviceId
var deviceEvents = events.filter(function (eventRow) { var deviceEvents = events.filter(function (eventRow) {
return eventRow.device_fingerprint === deviceFingerprint return eventRow.device_id === deviceId
}) })
var deviceAlerts = [] var deviceAlerts = []
deviceAlerts = R.concat(deviceAlerts, checkStuckScreen(deviceEvents)) deviceAlerts = R.concat(deviceAlerts, checkStuckScreen(deviceEvents))
deviceAlerts = R.concat(deviceAlerts, checkPing(deviceEvents)) deviceAlerts = R.concat(deviceAlerts, checkPing(deviceEvents))
alerts.devices[deviceFingerprint] = deviceAlerts alerts.devices[deviceId] = deviceAlerts
alerts.deviceNames[deviceFingerprint] = deviceName alerts.deviceNames[deviceId] = deviceName
}) })
return alerts return alerts

View file

@ -336,7 +336,7 @@ exports.trade = function trade (deviceId, rawTrade) {
exports.stateChange = function stateChange (deviceId, deviceTime, rec, cb) { exports.stateChange = function stateChange (deviceId, deviceTime, rec, cb) {
const event = { const event = {
id: rec.uuid, id: rec.uuid,
fingerprint: deviceId, deviceId: deviceId,
eventType: 'stateChange', eventType: 'stateChange',
note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, txId: rec.txId}), note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, txId: rec.txId}),
deviceTime: deviceTime deviceTime: deviceTime
@ -347,7 +347,7 @@ exports.stateChange = function stateChange (deviceId, deviceTime, rec, cb) {
exports.recordPing = function recordPing (deviceId, deviceTime, rec, cb) { exports.recordPing = function recordPing (deviceId, deviceTime, rec, cb) {
const event = { const event = {
id: uuid.v4(), id: uuid.v4(),
fingerprint: deviceId, deviceId: deviceId,
eventType: 'ping', eventType: 'ping',
note: JSON.stringify({state: rec.state, isIdle: rec.idle === 'true', txId: rec.txId}), note: JSON.stringify({state: rec.state, isIdle: rec.idle === 'true', txId: rec.txId}),
deviceTime: deviceTime deviceTime: deviceTime
@ -691,15 +691,15 @@ function sendNoAlerts () {
function checkNotification () { function checkNotification () {
return notifier.checkStatus() return notifier.checkStatus()
.then(alertRec => { .then(alertRec => {
const fingerprint = notifier.alertFingerprint(alertRec) const currentAlertFingerprint = notifier.alertFingerprint(alertRec)
if (!fingerprint) { if (!currentAlertFingerprint) {
const inAlert = !!alertFingerprint const inAlert = !!alertFingerprint
alertFingerprint = null alertFingerprint = null
lastAlertTime = null lastAlertTime = null
if (inAlert) return sendNoAlerts() if (inAlert) return sendNoAlerts()
} }
const alertChanged = fingerprint === alertFingerprint && const alertChanged = currentAlertFingerprint === alertFingerprint &&
lastAlertTime - Date.now() < ALERT_SEND_INTERVAL lastAlertTime - Date.now() < ALERT_SEND_INTERVAL
if (alertChanged) return if (alertChanged) return
@ -713,7 +713,7 @@ function checkNotification () {
body: notifier.printEmailAlerts(alertRec) body: notifier.printEmailAlerts(alertRec)
} }
} }
alertFingerprint = fingerprint alertFingerprint = currentAlertFingerprint
lastAlertTime = Date.now() lastAlertTime = Date.now()
return sendMessage(rec) return sendMessage(rec)

View file

@ -44,7 +44,7 @@ exports.init = function init (conString) {
exports.recordBill = function recordBill (deviceId, rec) { exports.recordBill = function recordBill (deviceId, rec) {
const fields = [ const fields = [
'id', 'id',
'device_fingerprint', 'device_id',
'currency_code', 'currency_code',
'crypto_code', 'crypto_code',
'to_address', 'to_address',
@ -246,7 +246,7 @@ exports.addDispense = function addDispense (deviceId, tx, cartridges) {
exports.cartridgeCounts = function cartridgeCounts (deviceId) { exports.cartridgeCounts = function cartridgeCounts (deviceId) {
const sql = 'SELECT id, count1, count2 FROM dispenses ' + const sql = 'SELECT id, count1, count2 FROM dispenses ' +
'WHERE device_fingerprint=$1 AND refill=$2 ' + 'WHERE device_id=$1 AND refill=$2 ' +
'ORDER BY id DESC LIMIT 1' 'ORDER BY id DESC LIMIT 1'
return db.oneOrNone(sql, [deviceId, true]) return db.oneOrNone(sql, [deviceId, true])
.then(row => { .then(row => {
@ -257,9 +257,9 @@ exports.cartridgeCounts = function cartridgeCounts (deviceId) {
exports.machineEvent = function machineEvent (rec) { exports.machineEvent = function machineEvent (rec) {
const TTL = 2 * 60 * 60 * 1000 const TTL = 2 * 60 * 60 * 1000
const fields = ['id', 'device_fingerprint', 'event_type', 'note', 'device_time'] const fields = ['id', 'device_id', 'event_type', 'note', 'device_time']
const sql = getInsertQuery('machine_events', fields) const sql = getInsertQuery('machine_events', fields)
const values = [rec.id, rec.fingerprint, rec.eventType, rec.note, rec.deviceTime] const values = [rec.id, rec.deviceId, rec.eventType, rec.note, rec.deviceTime]
const deleteSql = 'DELETE FROM machine_events WHERE (EXTRACT(EPOCH FROM (now() - created))) * 1000 > $1' const deleteSql = 'DELETE FROM machine_events WHERE (EXTRACT(EPOCH FROM (now() - created))) * 1000 > $1'
const deleteValues = [TTL] const deleteValues = [TTL]
@ -268,7 +268,7 @@ exports.machineEvent = function machineEvent (rec) {
} }
exports.devices = function devices () { exports.devices = function devices () {
const sql = 'SELECT fingerprint, name FROM devices WHERE authorized=$1' const sql = 'SELECT device_id, name FROM devices WHERE authorized=$1'
return db.any(sql, [true]) return db.any(sql, [true])
} }

View file

@ -334,21 +334,21 @@ function init (localConfig) {
app.post('/dispense', authMiddleware, dispense) app.post('/dispense', authMiddleware, dispense)
localApp.get('/pid', (req, res) => { localApp.get('/pid', (req, res) => {
const machineFingerprint = req.query.fingerprint const deviceId = req.query.device_id
const pidRec = pids[machineFingerprint] const pidRec = pids[deviceId]
res.json(pidRec) res.json(pidRec)
}) })
localApp.post('/reboot', (req, res) => { localApp.post('/reboot', (req, res) => {
const pid = req.body.pid const pid = req.body.pid
const fingerprint = req.body.fingerprint const deviceId = req.body.deviceId
console.log('pid: %s, fingerprint: %s', pid, fingerprint) console.log('pid: %s, deviceId: %s', pid, deviceId)
if (!fingerprint || !pid) { if (!deviceId || !pid) {
return res.sendStatus(400) return res.sendStatus(400)
} }
reboots[fingerprint] = pid reboots[deviceId] = pid
res.sendStatus(200) res.sendStatus(200)
}) })