From 86f202da0b75cc0e0785a4d607f4f39ef58b4628 Mon Sep 17 00:00:00 2001 From: velvettear Date: Fri, 11 Feb 2022 01:40:47 +0100 Subject: [PATCH] cleaned up the code mess a bit --- libs/api.js | 53 ++++++++++++++++++++++++----------------------- libs/constants.js | 3 ++- libs/info.js | 14 +++++++++++-- libs/modep.js | 17 +-------------- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/libs/api.js b/libs/api.js index 532f0bf..25375e3 100644 --- a/libs/api.js +++ b/libs/api.js @@ -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(); diff --git a/libs/constants.js b/libs/constants.js index b008d89..ec17a3e 100644 --- a/libs/constants.js +++ b/libs/constants.js @@ -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'; diff --git a/libs/info.js b/libs/info.js index 9809bfc..35d0e62 100644 --- a/libs/info.js +++ b/libs/info.js @@ -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 } \ No newline at end of file diff --git a/libs/modep.js b/libs/modep.js index 3f43b09..92d2411 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -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); });