From 54d73e1530eff1cee5c5b5fab809e7d98d3708d7 Mon Sep 17 00:00:00 2001 From: velvettear Date: Tue, 8 Feb 2022 13:46:02 +0100 Subject: [PATCH] fixed POSTs and other stuff --- libs/api.js | 37 +++++++++++++++++----- libs/modep.js | 80 ++++++++++++++++++++++++++++------------------ libs/server.js | 86 ++++++-------------------------------------------- 3 files changed, 89 insertions(+), 114 deletions(-) diff --git a/libs/api.js b/libs/api.js index 55d1495..52b6682 100644 --- a/libs/api.js +++ b/libs/api.js @@ -26,12 +26,17 @@ function getEndpoints() { function setupEndpoints() { return new Promise(function (resolve, reject) { + var startTime = new Date(); + logger.debug('setting up endpoints...'); modep.getBanks() .then(function (banks) { setEndpoint(constants.API_BANKS, { method: modep.getBanks }); for (var index = 0; index < banks.length; index++) { var id = banks[index].id; - setEndpoint(constants.API_BANKS + '/' + id, { method: modep.getBankById, args: id }); + setEndpoint( + constants.API_BANKS + '/' + id, + { method: modep.getBankById, id: id } + ); } }) .then(modep.getPedalboards) @@ -41,26 +46,42 @@ function setupEndpoints() { var id = pedalboards[index].id; setEndpoint( constants.API_PEDALBOARDS + '/' + id, - { method: modep.getPedalboardById, args: id }, - { method: modep.setPedalboardById, args: id } + { method: modep.getPedalboardById, id: id }, + { method: modep.setPedalboardById, id: id } ); } }) .then(function () { - setEndpoint(constants.API_PEDALBOARDS_DEFAULT, { method: modep.getDefaultPedalboard }); + setEndpoint( + constants.API_PEDALBOARDS_DEFAULT, + { method: modep.getDefaultPedalboard } + ); }) .then(function () { - setEndpoint(constants.API_PEDALBOARDS_CURRENT, { method: modep.getCurrentPedalboard }); + setEndpoint( + constants.API_PEDALBOARDS_CURRENT, + { method: modep.getCurrentPedalboard } + ); }) .then(modep.getCurrentPedals) .then(function (pedals) { - setEndpoint(constants.API_PEDALS, { method: modep.getCurrentPedals }); + setEndpoint( + constants.API_PEDALS, + { method: modep.getCurrentPedals } + ); for (var index = 0; index < pedals.length; index++) { var id = pedals[index].id; - setEndpoint(constants.API_PEDALS + '/' + id, { method: modep.getCurrentPedalById, args: id }); + setEndpoint( + constants.API_PEDALS + '/' + id, + { method: modep.getCurrentPedalById, id: id }, + { method: modep.setControlValue, id: id } + ); } }) - .then(resolve) + .then(function () { + logger.debug('setting up ' + endpoints.size + ' endpoints took ' + util.timeDiff(startTime) + 'ms'); + resolve(); + }) .catch(reject); }); } diff --git a/libs/modep.js b/libs/modep.js index 5f1616d..cb0c5e7 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -227,8 +227,9 @@ function parseCurrentPedalboard(currentPedalboard) { return new Promise(function (resolve, reject) { getDefaultPedalboard() .then(function (defaultPedalboard) { + currentPedals = []; if (defaultPedalboard.id == currentPedalboard.id) { - return resolve([]); + return resolve(currentPedals); } var startTime = new Date(); logger.debug('parsing current pedalboard...'); @@ -236,13 +237,12 @@ function parseCurrentPedalboard(currentPedalboard) { reject('error: could not determine current pedalboard config file'); } // FAKE DATA - var file = path.resolve('./dev/FUZZ.ttl'); + var file = path.resolve('./dev/' + currentPedalboard.uri.substring(currentPedalboard.uri.lastIndexOf('/') + 1)); // var file = path.resolve(currentPedalboard.uri.replace('file://', '')); fs.readFile(file, function (err, data) { if (err) { return reject('error: could not parse current pedalboard file \'' + file + '\' >>> ' + err); } - currentPedals = []; var json = ttl2jsonld(data.toString())['@graph']; var id = 0; for (var index = 0; index < json.length; index++) { @@ -295,35 +295,55 @@ function parseCurrentPedalboard(currentPedalboard) { }); } -function sendValueToControl(value, control) { +function setControlValue(pedalId, requestParams) { return new Promise(function (resolve, reject) { - if (!control || !control.midi) { - return reject('error: control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings'); + var controlId = requestParams.get('id'); + if (controlId == undefined) { + reject('error: could not handle POST - missing parameter \'id\'', 400); } - if (value > 127) { - value = 127; - } else if (value < 0) { - value = 0; + var value = parseInt(requestParams.get('value')); + if (value == undefined) { + reject('error: could not handle POST - missing parameter \'value\'', 400); + } else if (Number.isNaN(value)) { + reject('error: parameter \'value\' is not a number', 400); } - 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); - }); + getPedalControlById(pedalId, controlId) + .then(function (control) { + if (!control || !control.midi) { + return reject('error: control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings'); + } + if (value > 127) { + value = 127; + } else if (value < 0) { + value = 0; + } + 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); + }); + }) + .catch(reject); + }); +} + +function sendValueToControl(pedalId, controlId, value) { + return new Promise(function (resolve, reject) { + }); } @@ -375,7 +395,7 @@ module.exports = { getCurrentPedals, getCurrentPedalById, getPedalControlById, - sendValueToControl, setPedalboard, setPedalboardById, + setControlValue, } \ No newline at end of file diff --git a/libs/server.js b/libs/server.js index f73d08b..ca5fa9a 100644 --- a/libs/server.js +++ b/libs/server.js @@ -33,44 +33,6 @@ function handleRequests() { logger.debug(msg); return; } - - // if (!endpoint.types.includes(request.method)) { - // var msg = 'endpoint \'' + request.url + '\' does not support ' + request.method + ' requests'; - // response.writeHead(405); - // response.end(msg); - // logger.debug(msg); - // return; - // } - // if (request.method == constants.HTTP_GET) { - // handleGET(endpoint[constants.HTTP_GET]) - // .then(function (result) { - // response.writeHead(200); - // response.end(result); - // }) - // .catch(function (err, code) { - // if (!code) { - // code = 500; - // } - // response.writeHead(code); - // response.end(err); - // }); - // return; - // } - // if (request.method == constants.HTTP_POST) { - // handlePOST(request, response) - // .then(function (result) { - // response.writeHead(200); - // response.end(result); - // }) - // .catch(function (err, code) { - // if (!code) { - // code = 500; - // } - // response.writeHead(code); - // response.end(err); - // }); - // return; - // } endpoint = endpoint[request.method]; if (endpoint == undefined || endpoint.method == undefined) { var msg = 'error: endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests'; @@ -79,7 +41,10 @@ function handleRequests() { logger.debug(msg); return; } - endpoint.method(endpoint.args) + getRequestParams(request) + .then(function (params) { + return endpoint.method(endpoint.id, params); + }) .then(function (result) { response.writeHead(200); try { @@ -87,59 +52,28 @@ function handleRequests() { } catch (err) { response.end(result); } - return; }) .catch(function (err, code) { + logger.error(err); if (code == undefined) { code = 500; } response.writeHead(500); response.end(err); - return; }); }); } -function handleGET(endpoint) { - return new Promise(function (resolve, reject) { - if (!endpoint.method) { - return reject('error: no handler defined for endpoint \'' + endpoint.url + '\''); - } - endpoint.method(endpoint.args) - .then(function (data) { - return resolve(JSON.stringify(data)) - }) - .catch(reject); - }); -} - -function handlePOST(endpoint) { - return new Promise(function (resolve, reject) { - if (request.url.startsWith(constants.API_PEDALS)) { - handlePOSTPedals(request) - .then(resolve) - .catch(reject); - return; - } - if (request.url.startsWith(constants.API_PEDALBOARDS)) { - handlePOSTPedalboards(request) - .then(resolve) - .catch(reject); - return; - } - var msg = 'endpoint \'' + request.url + '\' is not yet implemented for ' + request.method + ' requests'; - response.writeHead(405); - response.end(msg); - }); -} - -function getPOSTParams(request) { +function getRequestParams(request) { return new Promise(function (resolve, reject) { var params = ""; request.on("data", function (data) { params += data; }); request.on("end", function () { + if (params == undefined || params.length == 0) { + return resolve(); + } return resolve(new URLSearchParams(params)); }); }); @@ -147,7 +81,7 @@ function getPOSTParams(request) { function handlePOSTPedals(request) { return new Promise(function (resolve, reject) { - getPOSTParams(request) + getRequestParams(request) .then(function (params) { var pedalId = request.url.substring(request.url.lastIndexOf('/') + 1); var controlId = params.get('id');