From 5e6314eee963ceedd0e59c59ee958cc7fdd9cc5b Mon Sep 17 00:00:00 2001 From: velvettear Date: Thu, 24 Mar 2022 12:50:16 +0100 Subject: [PATCH] replaced requiring config.json with a global config object --- .vscode/launch.json | 5 ++++- libs/cache.js | 11 ++++++++--- libs/modep.js | 11 +++++------ libs/osc.js | 5 ++--- libs/server.js | 5 ++--- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 3d28bc0..06a306d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,10 @@ "skipFiles": [ "/**" ], - "program": "${workspaceFolder}/pbc.js" + "program": "${workspaceFolder}/pbc.js", + "args": [ + "${workspaceFolder}/example_config.json" + ] } ] } \ No newline at end of file diff --git a/libs/cache.js b/libs/cache.js index 8e8a609..e58b51b 100644 --- a/libs/cache.js +++ b/libs/cache.js @@ -1,20 +1,25 @@ -const config = require('../config.json'); const logger = require('./logger.js'); const constants = require('./constants.js'); const timeDiff = require('./util.js').timeDiff; -const active = config.cache.active; -const lifetime = config.cache.lifetime * 60000; +let active; +let lifetime; let modep; const cache = new Map(); function isActive() { + if (active === undefined) { + active = global.config.cache.active; + } return active; } function getLifetime() { + if (lifetime === undefined) { + lifetime = global.config.cache.lifetime * 60000 + } return lifetime; } diff --git a/libs/modep.js b/libs/modep.js index 73f765a..e0614d5 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -1,4 +1,3 @@ -const config = require('../config.json'); const util = require('./util.js'); const constants = require('./constants.js'); const logger = require('./logger.js'); @@ -14,7 +13,7 @@ let pedalboardIdBypassOrigin; function reset() { return new Promise(function (resolve, reject) { - util.httpGET(config.modep.host, config.modep.port, '/reset') + util.httpGET(global.config.modep.host, global.config.modep.port, '/reset') .then(resolve) .catch(reject); }); @@ -35,7 +34,7 @@ function getBanks() { // banks = util.sortById(fake); // return resolve(fake); - util.httpGET(config.modep.host, config.modep.port, '/banks') + util.httpGET(global.config.modep.host, global.config.modep.port, '/banks') .then(function (banks) { for (let index = 0; index < banks.length; index++) { let bank = banks[index]; @@ -87,7 +86,7 @@ function getPedalboards() { // pedalboards = util.sortById(fake); // return resolve(fake); - util.httpGET(config.modep.host, config.modep.port, '/pedalboard/list') + util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/list') .then(function (pedalboards) { let id = 1; for (let index = 0; index < pedalboards.length; index++) { @@ -139,7 +138,7 @@ function getCurrentPedalboard() { // .catch(reject); // PRODUCTION - util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current') + util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/current') .then(getPedalboardByBundle) .then(function (currentPedalboard) { cache.setCurrentPedalboard(currentPedalboard); @@ -359,7 +358,7 @@ function setPedalboard(pedalboard) { return reset() }) .then(function () { - return util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle) + return util.httpPOST(global.config.modep.host, global.config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle) }) .then(function () { cache.setCurrentPedalboard(pedalboard); diff --git a/libs/osc.js b/libs/osc.js index 5a04da8..1dc48a4 100644 --- a/libs/osc.js +++ b/libs/osc.js @@ -1,7 +1,6 @@ const logger = require('./logger.js'); const util = require('./util.js'); const commands = require('./commands.js'); -const config = require('../config.json').osc; const CMD = 'oscsend'; @@ -16,8 +15,8 @@ async function send(controller, channel, value) { } logger.debug('sending value \'' + value + '\' to controller \'' + controller + '\' on channel \'' + channel + '\'...'); let args = [ - config.host, config.port, - config.address, + global.config.osc.host, global.config.osc.port, + global.config.osc.address, 'm', '00' + util.toHex(value) + '0' + util.toHex(controller) + 'b' + util.toHex(channel) ]; diff --git a/libs/server.js b/libs/server.js index fd52f0f..f435abe 100644 --- a/libs/server.js +++ b/libs/server.js @@ -1,4 +1,3 @@ -const config = require('../config.json'); const logger = require('./logger.js'); const api = require('./api.js'); const blinky = require('./blinky.js'); @@ -12,8 +11,8 @@ async function start() { if (!server) { server = http.createServer(); } - server.listen(config.server.port, config.server.listen).on('listening', function () { - logger.debug('server listening on ' + config.server.listen + ':' + config.server.port + '...'); + server.listen(global.config.server.port, global.config.server.listen).on('listening', function () { + logger.debug('server listening on ' + global.config.server.listen + ':' + global.config.server.port + '...'); blinky.setActive(true); handleRequests(); });