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

View file

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

View file

@ -1,5 +1,6 @@
const logger = require('./logger.js');
const cache = require('./cache');
const cache = require('./cache.js');
const commands = require('./commands.js');
const os = require('os');
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() {
var info = cache.getInfo();
if (info != undefined) {
@ -67,7 +76,7 @@ function getHostInfo() {
platform: getPlatform(),
kernel: getKernelVersion(),
cpu: getCpuInfo(),
memory: getMemoryInfo()
memory: getMemoryInfo(),
}
cache.setInfo(info);
return info;
@ -84,5 +93,6 @@ module.exports = {
getKernelVersion,
getCpuInfo,
getMemoryInfo,
getTemperature,
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);
var cmd = 'oscsend';
var args = [config.osc.host, config.osc.port, config.osc.address, 'm', value];
logger.debug('executing command \'' + cmd + '\' with args \'' + 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);
});
commands.execute(cmd, args);
})
.catch(reject);
});