more refactoring
This commit is contained in:
parent
8acbc90dbd
commit
9a9c927801
6 changed files with 30 additions and 30 deletions
14
bin/ssu
14
bin/ssu
|
|
@ -17,7 +17,7 @@ if (!cmd) bail()
|
|||
function bail (error) {
|
||||
error = error || 'Command line utility for lamassu-server'
|
||||
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('\nssu crypto <code> <ticker-plugin> <wallet-plugin> [<trader-plugin>]')
|
||||
console.log('This will configure a new cryptocurrency.')
|
||||
|
|
@ -54,15 +54,15 @@ switch (cmd) {
|
|||
}
|
||||
|
||||
function reboot () {
|
||||
var fingerprint = argv[1]
|
||||
var deviceId = argv[1]
|
||||
|
||||
if (!fingerprint) {
|
||||
console.log('Fingerprint required')
|
||||
if (!deviceId) {
|
||||
console.log('Machine ID required')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
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) {
|
||||
console.log('Please make sure that lamassu-server is running on this box.')
|
||||
process.exit(2)
|
||||
|
|
@ -82,7 +82,7 @@ function reboot () {
|
|||
|
||||
var opts2 = {
|
||||
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) {
|
||||
|
|
@ -111,7 +111,7 @@ function reboot () {
|
|||
}, 30000)
|
||||
|
||||
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) {
|
||||
console.log('lamassu-server appears to be down.')
|
||||
process.exit(2)
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ module.exports = function (options) {
|
|||
server = https.createServer(serverOptions, app)
|
||||
|
||||
authMiddleware = function (req, res, next) {
|
||||
lamassuConfig.isAuthorized(routes.getFingerprint(req), function (err,
|
||||
lamassuConfig.isAuthorized(routes.getDeviceId(req), function (err,
|
||||
device) {
|
||||
if (err) {
|
||||
res.json({err: 'Internal Server Error'})
|
||||
|
|
|
|||
|
|
@ -95,18 +95,18 @@ function checkStatus () {
|
|||
var events = rec.events
|
||||
|
||||
devices.forEach(function (deviceRow) {
|
||||
var deviceFingerprint = deviceRow.fingerprint
|
||||
var deviceName = deviceRow.name || deviceFingerprint
|
||||
var deviceId = deviceRow.device_id
|
||||
var deviceName = deviceRow.name || deviceId
|
||||
var deviceEvents = events.filter(function (eventRow) {
|
||||
return eventRow.device_fingerprint === deviceFingerprint
|
||||
return eventRow.device_id === deviceId
|
||||
})
|
||||
|
||||
var deviceAlerts = []
|
||||
deviceAlerts = R.concat(deviceAlerts, checkStuckScreen(deviceEvents))
|
||||
deviceAlerts = R.concat(deviceAlerts, checkPing(deviceEvents))
|
||||
|
||||
alerts.devices[deviceFingerprint] = deviceAlerts
|
||||
alerts.deviceNames[deviceFingerprint] = deviceName
|
||||
alerts.devices[deviceId] = deviceAlerts
|
||||
alerts.deviceNames[deviceId] = deviceName
|
||||
})
|
||||
|
||||
return alerts
|
||||
|
|
|
|||
|
|
@ -336,7 +336,7 @@ exports.trade = function trade (deviceId, rawTrade) {
|
|||
exports.stateChange = function stateChange (deviceId, deviceTime, rec, cb) {
|
||||
const event = {
|
||||
id: rec.uuid,
|
||||
fingerprint: deviceId,
|
||||
deviceId: deviceId,
|
||||
eventType: 'stateChange',
|
||||
note: JSON.stringify({state: rec.state, isIdle: rec.isIdle, txId: rec.txId}),
|
||||
deviceTime: deviceTime
|
||||
|
|
@ -347,7 +347,7 @@ exports.stateChange = function stateChange (deviceId, deviceTime, rec, cb) {
|
|||
exports.recordPing = function recordPing (deviceId, deviceTime, rec, cb) {
|
||||
const event = {
|
||||
id: uuid.v4(),
|
||||
fingerprint: deviceId,
|
||||
deviceId: deviceId,
|
||||
eventType: 'ping',
|
||||
note: JSON.stringify({state: rec.state, isIdle: rec.idle === 'true', txId: rec.txId}),
|
||||
deviceTime: deviceTime
|
||||
|
|
@ -691,15 +691,15 @@ function sendNoAlerts () {
|
|||
function checkNotification () {
|
||||
return notifier.checkStatus()
|
||||
.then(alertRec => {
|
||||
const fingerprint = notifier.alertFingerprint(alertRec)
|
||||
if (!fingerprint) {
|
||||
const currentAlertFingerprint = notifier.alertFingerprint(alertRec)
|
||||
if (!currentAlertFingerprint) {
|
||||
const inAlert = !!alertFingerprint
|
||||
alertFingerprint = null
|
||||
lastAlertTime = null
|
||||
if (inAlert) return sendNoAlerts()
|
||||
}
|
||||
|
||||
const alertChanged = fingerprint === alertFingerprint &&
|
||||
const alertChanged = currentAlertFingerprint === alertFingerprint &&
|
||||
lastAlertTime - Date.now() < ALERT_SEND_INTERVAL
|
||||
if (alertChanged) return
|
||||
|
||||
|
|
@ -713,7 +713,7 @@ function checkNotification () {
|
|||
body: notifier.printEmailAlerts(alertRec)
|
||||
}
|
||||
}
|
||||
alertFingerprint = fingerprint
|
||||
alertFingerprint = currentAlertFingerprint
|
||||
lastAlertTime = Date.now()
|
||||
|
||||
return sendMessage(rec)
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ exports.init = function init (conString) {
|
|||
exports.recordBill = function recordBill (deviceId, rec) {
|
||||
const fields = [
|
||||
'id',
|
||||
'device_fingerprint',
|
||||
'device_id',
|
||||
'currency_code',
|
||||
'crypto_code',
|
||||
'to_address',
|
||||
|
|
@ -246,7 +246,7 @@ exports.addDispense = function addDispense (deviceId, tx, cartridges) {
|
|||
|
||||
exports.cartridgeCounts = function cartridgeCounts (deviceId) {
|
||||
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'
|
||||
return db.oneOrNone(sql, [deviceId, true])
|
||||
.then(row => {
|
||||
|
|
@ -257,9 +257,9 @@ exports.cartridgeCounts = function cartridgeCounts (deviceId) {
|
|||
|
||||
exports.machineEvent = function machineEvent (rec) {
|
||||
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 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 deleteValues = [TTL]
|
||||
|
||||
|
|
@ -268,7 +268,7 @@ exports.machineEvent = function machineEvent (rec) {
|
|||
}
|
||||
|
||||
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])
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -334,21 +334,21 @@ function init (localConfig) {
|
|||
app.post('/dispense', authMiddleware, dispense)
|
||||
|
||||
localApp.get('/pid', (req, res) => {
|
||||
const machineFingerprint = req.query.fingerprint
|
||||
const pidRec = pids[machineFingerprint]
|
||||
const deviceId = req.query.device_id
|
||||
const pidRec = pids[deviceId]
|
||||
res.json(pidRec)
|
||||
})
|
||||
|
||||
localApp.post('/reboot', (req, res) => {
|
||||
const pid = req.body.pid
|
||||
const fingerprint = req.body.fingerprint
|
||||
console.log('pid: %s, fingerprint: %s', pid, fingerprint)
|
||||
const deviceId = req.body.deviceId
|
||||
console.log('pid: %s, deviceId: %s', pid, deviceId)
|
||||
|
||||
if (!fingerprint || !pid) {
|
||||
if (!deviceId || !pid) {
|
||||
return res.sendStatus(400)
|
||||
}
|
||||
|
||||
reboots[fingerprint] = pid
|
||||
reboots[deviceId] = pid
|
||||
res.sendStatus(200)
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue