cleaned up the code mess a bit

This commit is contained in:
Daniel Sommer 2022-02-11 01:40:47 +01:00
parent 3528e8e146
commit 86f202da0b
4 changed files with 42 additions and 45 deletions

View file

@ -7,18 +7,22 @@ const info = require('./info.js');
const endpoints = new Map(); const endpoints = new Map();
function setEndpoint(url, get, post) { function setEndpoint(url, get, post) {
if (get == undefined && post == undefined) { return new Promise(function (resolve, reject) {
return; if (get == undefined && post == undefined) {
} return resolve();
var endpoint = {}; }
if (get != undefined) { var endpoint = {};
endpoint.GET = get; if (get != undefined) {
} endpoint.GET = get;
if (post != undefined) { }
endpoint.POST = post; if (post != undefined) {
} endpoint.POST = post;
endpoints.set(url, endpoint); }
logger.debug('set up endpoint \'' + url + '\' > ' + JSON.stringify(endpoint)); endpoints.set(url, endpoint);
logger.debug('set up endpoint \'' + url + '\' > ' + JSON.stringify(endpoint));
return resolve();
});
} }
function getEndpoints() { function getEndpoints() {
@ -29,7 +33,9 @@ function setupEndpoints() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var startTime = new Date(); var startTime = new Date();
logger.debug('setting up endpoints...'); logger.debug('setting up endpoints...');
modep.getBanks() setEndpoint(constants.API_INFO, { method: info.getHostInfo })
.then(setEndpoint(constants.API_TEMPERATURE, { method: info.getTemperature }))
.then(modep.getBanks)
.then(function (banks) { .then(function (banks) {
setEndpoint(constants.API_BANKS, { method: modep.getBanks }); setEndpoint(constants.API_BANKS, { method: modep.getBanks });
for (var index = 0; index < banks.length; index++) { for (var index = 0; index < banks.length; index++) {
@ -52,18 +58,14 @@ function setupEndpoints() {
); );
} }
}) })
.then(function () { .then(setEndpoint(
setEndpoint( constants.API_PEDALBOARDS_DEFAULT,
constants.API_PEDALBOARDS_DEFAULT, { method: modep.getDefaultPedalboard }
{ method: modep.getDefaultPedalboard } ))
); .then(setEndpoint(
}) constants.API_PEDALBOARDS_CURRENT,
.then(function () { { method: modep.getCurrentPedalboard }
setEndpoint( ))
constants.API_PEDALBOARDS_CURRENT,
{ method: modep.getCurrentPedalboard }
);
})
.then(modep.getCurrentPedals) .then(modep.getCurrentPedals)
.then(function (pedals) { .then(function (pedals) {
setEndpoint( setEndpoint(
@ -79,7 +81,6 @@ function setupEndpoints() {
); );
} }
}) })
.then(setEndpoint(constants.API_INFO, { method: info.getHostInfo }))
.then(function () { .then(function () {
logger.debug('setting up ' + endpoints.size + ' endpoints took ' + util.timeDiff(startTime) + 'ms'); logger.debug('setting up ' + endpoints.size + ' endpoints took ' + util.timeDiff(startTime) + 'ms');
resolve(); resolve();

View file

@ -1,7 +1,8 @@
exports.HTTP_GET = 'GET'; exports.HTTP_GET = 'GET';
exports.HTTP_POST = 'POST'; exports.HTTP_POST = 'POST';
exports.API_INFO = '/info' exports.API_INFO = '/info';
exports.API_TEMPERATURE = '/temperature';
exports.API_BANKS = '/banks'; exports.API_BANKS = '/banks';
exports.API_PEDALBOARDS = '/pedalboards'; exports.API_PEDALBOARDS = '/pedalboards';
exports.API_PEDALBOARDS_DEFAULT = '/pedalboards/default'; exports.API_PEDALBOARDS_DEFAULT = '/pedalboards/default';

View file

@ -1,5 +1,6 @@
const logger = require('./logger.js'); const logger = require('./logger.js');
const cache = require('./cache'); const cache = require('./cache.js');
const commands = require('./commands.js');
const os = require('os'); const os = require('os');
function getHostname() { function getHostname() {
@ -52,6 +53,14 @@ function getMemoryInfo() {
} }
} }
function getTemperature() {
return new Promise(function (resolve, reject) {
commands.execute("vcgencmd", ["measure_temp"])
.then(resolve)
.catch(reject);
});
}
function getHostInfo() { function getHostInfo() {
var info = cache.getInfo(); var info = cache.getInfo();
if (info != undefined) { if (info != undefined) {
@ -67,7 +76,7 @@ function getHostInfo() {
platform: getPlatform(), platform: getPlatform(),
kernel: getKernelVersion(), kernel: getKernelVersion(),
cpu: getCpuInfo(), cpu: getCpuInfo(),
memory: getMemoryInfo() memory: getMemoryInfo(),
} }
cache.setInfo(info); cache.setInfo(info);
return info; return info;
@ -84,5 +93,6 @@ module.exports = {
getKernelVersion, getKernelVersion,
getCpuInfo, getCpuInfo,
getMemoryInfo, getMemoryInfo,
getTemperature,
getHostInfo getHostInfo
} }

View file

@ -329,22 +329,7 @@ function setControlValue(pedalId, requestParams) {
value = '00' + util.toHex(value) + '0' + util.toHex(control.midi.controller) + 'b' + util.toHex(control.midi.channel); value = '00' + util.toHex(value) + '0' + util.toHex(control.midi.controller) + 'b' + util.toHex(control.midi.channel);
var cmd = 'oscsend'; var cmd = 'oscsend';
var args = [config.osc.host, config.osc.port, config.osc.address, 'm', value]; var args = [config.osc.host, config.osc.port, config.osc.address, 'm', value];
logger.debug('executing command \'' + cmd + '\' with args \'' + args + '\'...'); commands.execute(cmd, args);
var spawned = spawn(cmd, args);
spawned.stdout.on('data', function (data) {
logger.debug(data);
});
spawned.stderr.on('data', function (data) {
logger.error(data);
});
spawned.on('close', function (code) {
logger.debug('command \'' + cmd + '\' with args \'' + args + '\' finished with exit code ' + code);
resolve();
});
spawned.on('error', function (err) {
logger.error('command \'' + cmd + '\' with args \'' + args + '\' encountered an error >>> ' + err);
reject(err);
});
}) })
.catch(reject); .catch(reject);
}); });