This commit is contained in:
Josh Harvey 2016-10-10 14:53:01 +01:00
parent c057fe3df8
commit 259b527214
6 changed files with 68 additions and 81 deletions

View file

@ -4,15 +4,14 @@ var http = require('http')
var https = require('https')
var express = require('express')
var bodyParser = require('body-parser')
var LamassuConfig = require('lamassu-config')
var routes = require('./routes')
var plugins = require('./plugins')
var logger = require('./logger')
var configManager = require('./config-manager')
module.exports = function (options) {
var app = express()
var server
var lamassuConfig
const psqlUrl = options.postgresql
if (!psqlUrl) {
@ -20,33 +19,19 @@ module.exports = function (options) {
process.exit(1)
}
lamassuConfig = new LamassuConfig(psqlUrl)
const seedPath = options.seedPath || './seeds/seed.txt'
plugins.init(psqlUrl, seedPath)
lamassuConfig.load(function (err, config) {
if (err) {
logger.error('Loading config failed')
throw err
}
console.log('DEBUG6')
configManager.load()
.then(config => {
console.log('DEBUG5: %j', config)
plugins.configure(config)
plugins.startPolling()
plugins.startCheckingNotification()
})
lamassuConfig.on('configUpdate', function () {
lamassuConfig.load(function (err, config) {
if (err) {
return logger.error('Error while reloading config')
}
plugins.configure(config)
logger.info('Config reloaded')
})
})
app.use(bodyParser.json())
var authMiddleware
@ -64,24 +49,15 @@ module.exports = function (options) {
server = https.createServer(serverOptions, app)
authMiddleware = function (req, res, next) {
lamassuConfig.isAuthorized(routes.getDeviceId(req), function (err,
device) {
if (err) {
res.json({err: 'Internal Server Error'})
return next(err)
}
if (!device) {
res.json(404, {err: 'Not Found'})
return next(new Error('Device is unpaired'))
}
next()
})
next() // TODO: authentication
}
} else {
server = http.createServer(app)
authMiddleware = function (req, res, next) {
req.device = {}
console.log('DEBUG2')
console.log(req.route)
return next()
}
}
@ -96,7 +72,6 @@ module.exports = function (options) {
routes.init({
app: app,
localApp: localApp,
lamassuConfig: lamassuConfig,
plugins: plugins,
authMiddleware: authMiddleware,
// reloadConfigMiddleware: reloadConfigMiddleware,

View file

@ -1,8 +1,9 @@
var pgp = require('pg-promise')()
var psqlUrl = require('../lib/options').postgres
var psqlUrl = require('../lib/options').postgresql
var R = require('ramda')
function connect () {
console.log(psqlUrl)
return pgp(psqlUrl)
}
exports.connect = connect
@ -37,8 +38,8 @@ module.exports = {
}
function matchesValue (crypto, machine, instance) {
instance.fieldScope.crypto === crypto &&
instance.fieldScope.machine === machine
return instance.fieldLocator.fieldScope.crypto === crypto &&
instance.fieldLocator.fieldScope.machine === machine
}
function permutations (crypto, machine) {
@ -50,37 +51,43 @@ function permutations (crypto, machine) {
])
}
function fallbackValue (arr, instances) {
const crypto = arr[0]
const machine = arr[1]
function fallbackValue (crypto, machine, instances) {
const notNil = R.pipe(R.isNil, R.not)
const pickValue = (crypto, machine) => R.find(matchesValue, instances)
console.log('DEBUG10: %j', instances)
const pickValue = arr => R.find(instance => matchesValue(arr[0], arr[1], instance), instances)
console.log('DEBUG11: %j', permutations(crypto, machine))
console.log('DEBUG14: %j', R.map(pickValue, permutations(crypto, machine)))
return R.find(notNil, R.map(pickValue, permutations(crypto, machine)))
}
function generalScoped (crypto, machine, config) {
const machineScopedCluster = fieldCluster =>
[fieldCluster.code, fallbackValue(crypto, machine, fieldCluster.fieldInstances)]
const scopedValue = (key, instances) =>
[key, fallbackValue(crypto, machine, keyedValues(key, instances))]
const scopedGroups = group =>
[group.code, R.fromPairs(group.fieldClusters.map(machineScopedCluster))]
const keyedValues = (key, instances) => R.filter(r => r.fieldLocator.code === key, instances)
const keys = instances => R.uniq(R.map(r => r.fieldLocator.code, instances))
const scopedGroups = group => {
const instances = group.values
console.log('DEBUG66: %j', keys(instances))
return [group.code, R.fromPairs(keys(instances).map(key => scopedValue(key, instances)))]
}
return R.fromPairs(config.groups.map(scopedGroups))
}
function machineScoped (machine, config) {
generalScoped('global', machine, config)
return generalScoped('global', machine, config)
}
function unscoped (config) {
generalScoped('global', 'global', config)
return generalScoped('global', 'global', config)
}
function cryptoScoped (crypto, config) {
generalScoped(crypto, 'global', config)
return generalScoped(crypto, 'global', config)
}
function scoped (crypto, machine, config) {
generalScoped(crypto, machine, config)
return generalScoped(crypto, machine, config)
}

View file

@ -13,6 +13,7 @@ try {
} catch (err) {
try {
configPath = path.resolve(os.homedir(), '.lamassu', 'lamassu.json')
console.log(configPath)
options = JSON.parse(fs.readFileSync(configPath))
} catch (err2) {
console.log('Missing configuration file -- exiting.')

View file

@ -11,7 +11,7 @@ const db = require('./postgresql_interface')
const logger = require('./logger')
const notifier = require('./notifier')
const T = require('./time')
const config = require('./config')
const configManager = require('./config-manager')
const tradeIntervals = {}
@ -166,13 +166,12 @@ function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, cfg, accounts
exports.loadOrConfigPlugin = loadOrConfigPlugin
// Note: this whole function gets called every time there's a config update
exports.configure = function configure (cfg) {
if (config.exchanges.settings.lowBalanceMargin < 1) {
throw new Error('\'settings.lowBalanceMargin\' has to be >= 1')
}
exports.configure = function configure (config) {
console.log('DEBUG4: %j', config)
cachedConfig = cfg
const accounts = config.loadAccounts()
cachedConfig = config
const accounts = configManager.loadAccounts()
deviceCurrency = config.fiat.fiatCurrency
cryptoCodes = config.crypto.cryptoCurrencies
@ -255,8 +254,10 @@ exports.configure = function configure (cfg) {
)
}
exports.getConfig = function getConfig () {
return cachedConfig
exports.getConfig = function getConfig (machineId) {
console.log('DEBUG3: %j', cachedConfig)
console.log('DEBUG55: %j', configManager.machineScoped(machineId, cachedConfig))
return configManager.machineScoped(machineId, cachedConfig)
}
exports.logEvent = db.recordDeviceEvent

View file

@ -2,11 +2,11 @@
const BigNumber = require('bignumber.js')
const logger = require('./logger')
const configManager = require('./config-manager')
let mock = false
let plugins
let lamassuConfig
module.exports = {
init,
@ -56,6 +56,7 @@ function buildBalances () {
}
function poll (req, res) {
console.log('DEBUG1')
const deviceId = getDeviceId(req)
const deviceTime = getDeviceTime(req)
const pid = req.query.pid
@ -209,22 +210,11 @@ function verifyTx (req, res) {
}
function pair (req, res) {
const token = req.body.token
const name = req.body.name
lamassuConfig.pair(
token,
getDeviceId(req),
name,
err => {
if (err) {
logger.error(err)
return res.json({err: err.message})
}
// const token = req.body.token
// const name = req.body.name
// TODO: Pair
res.json({success: true})
}
)
}
function phoneCode (req, res) {
@ -323,14 +313,13 @@ function dispense (req, res) {
.catch(err => logger.error(err))
}
function init (localConfig) {
lamassuConfig = localConfig.lamassuConfig
plugins = localConfig.plugins
mock = localConfig.mock
function init (opts) {
plugins = opts.plugins
mock = opts.mock
const authMiddleware = localConfig.authMiddleware
const app = localConfig.app
const localApp = localConfig.localApp
const authMiddleware = opts.authMiddleware
const app = opts.app
const localApp = opts.localApp
app.get('/poll', authMiddleware, poll)
@ -371,6 +360,15 @@ function init (localConfig) {
res.sendStatus(200)
})
localApp.post('/dbChange', (req, res) => {
return configManager.load()
.then(config => {
plugins.configure(config)
logger.info('Config reloaded')
})
.catch(logger.error)
})
return app
}

View file

@ -89,3 +89,8 @@ options: configure per machine; configure per crypto/fiat
currencies now. (passed in on trade -- we assume that we're using machine currency, which may not be true)
- consider defining exchange fiat currency in exchange account config, we don't pass in fiat amount, anyway
-----------------------------
- convert result to actual value, not record
- default values