diff --git a/migrations/20180629144344_initial.js b/migrations/20180629144344_initial.js index 4a15ec1..23559db 100644 --- a/migrations/20180629144344_initial.js +++ b/migrations/20180629144344_initial.js @@ -31,7 +31,7 @@ exports.up = async function (knex) { left_sidebar_visibility: true, left_sidebar_extended: true, right_sidebar_visibility: false, - temperature_unit: 'f' + temperature_unit: 'c' }) // pools diff --git a/src/configurator.js b/src/configurator.js new file mode 100644 index 0000000..d9aeb26 --- /dev/null +++ b/src/configurator.js @@ -0,0 +1,89 @@ +const fs = require('fs'); +const _ = require('lodash'); +const { knex } = require('./db') + +const generate = async function (pools = null, settings = null ) { + if (!settings) { + [ settings ] = await knex('settings').select([ + 'miner_mode as minerMode', + 'voltage', + 'frequency', + 'fan', + 'connected_wifi as connectedWifi', + 'left_sidebar_visibility as leftSidebarVisibility', + 'left_sidebar_extended as leftSidebarExtended', + 'right_sidebar_visibility as rightSidebarVisibility', + 'temperature_unit as temperatureUnit', + ]) + .orderBy('created_at', 'desc') + .orderBy('id', 'desc') + .limit(1) + } + + if (!pools) { + pools = await knex('pools').select([ + 'id', + 'enabled', + 'url', + 'username', + 'password', + 'proxy', + 'index' + ]) + .where('enabled', 1) + .orderBy('index', 'asc') + } + + pools = _.chain(pools) + .filter(function (pool) { + if (pool.enabled) return pool + }) + .map(function (pool) { + let newPool = { + url: pool.url, + user: pool.username, + pass: pool.password, + 'pool-priority': pool.index, + } + if (pool.proxy && pool.proxy.length) newPool['pool-proxy'] = pool.proxy + return newPool; + }).value() + + let configuration = { + 'pools': pools, + 'api-listen': true, + 'api-allow': 'W:127.0.0.1', + 'api-mcast-port' : '4028', + 'api-port' : '4028', + 'expiry' : '120', + 'expiry-lp' : '3600', + 'failover-switch-delay' : '300', + 'log' : '20', + 'no-pool-disable' : true, + 'no-client-reconnect' : true, + 'no-show-processors' : true, + 'no-show-procs' : true, + 'queue' : '1', + 'quiet-work-updates' : true, + 'quiet-work-update' : true, + 'scan-time' : '60', + 'skip-security-checks' : '0', + 'submit-stale' : true, + 'scan' : [ + 'ALL' + ], + 'set-device' : [ + 'APL:clock=' + settings.frequency, + 'APL:voltage=' + settings.voltage, + 'APL:mode=' + settings.minerMode, + 'APL:fan=' + settings.fan + ] + }; + + fs.writeFile('/opt/bfgminer.conf', JSON.stringify(configuration, null, 4), (err) => { + // console.log(configuration); + console.log('Configuration saved'); + }); +} + +module.exports = generate; \ No newline at end of file diff --git a/src/store/api/pools/collection/poolsUpdate.js b/src/store/api/pools/collection/poolsUpdate.js index afb3b31..5bd4185 100644 --- a/src/store/api/pools/collection/poolsUpdate.js +++ b/src/store/api/pools/collection/poolsUpdate.js @@ -1,8 +1,8 @@ module.exports = ({ define }) => { - define('updateAll', async (data = {}, { dispatch, knex, errors, utils }) => { - return await knex.transaction(async function(trx) { - await trx.delete().from('pools') - await trx.insert(data).into('pools') - }); - }) + define('updateAll', async (data = {}, { dispatch, knex, errors, utils }) => { + return await knex.transaction(async function(trx) { + await trx.delete().from('pools') + await trx.insert(data).into('pools') + }); + }) } diff --git a/src/store/api/pools/poolsUpdate.js b/src/store/api/pools/poolsUpdate.js index 8a5e9db..9260ced 100644 --- a/src/store/api/pools/poolsUpdate.js +++ b/src/store/api/pools/poolsUpdate.js @@ -1,7 +1,12 @@ +const generateConf = require('../../../configurator'); + module.exports = ({ define }) => { define('updateAll', async (payload, { dispatch, errors, utils }) => { await dispatch('api/pools/collection/updateAll', payload) const { items: pools } = await dispatch('api/pools/collection/read', {}) + + await generateConf(pools); + return { pools } diff --git a/src/store/api/settings/settingsUpdate.js b/src/store/api/settings/settingsUpdate.js index de73091..a9edd22 100644 --- a/src/store/api/settings/settingsUpdate.js +++ b/src/store/api/settings/settingsUpdate.js @@ -1,7 +1,12 @@ +const generateConf = require('../../../configurator'); + module.exports = ({ define }) => { define('update', async (settings, { dispatch, errors, utils }) => { await dispatch('api/settings/collection/update', settings) const newSettings = await dispatch('api/settings/collection/read') + + await generateConf(null, newSettings); + return { settings: newSettings }