fixed POSTs and other stuff

This commit is contained in:
Daniel Sommer 2022-02-08 13:46:02 +01:00
parent 13becac482
commit 54d73e1530
3 changed files with 89 additions and 114 deletions

View file

@ -26,12 +26,17 @@ function getEndpoints() {
function setupEndpoints() { function setupEndpoints() {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var startTime = new Date();
logger.debug('setting up endpoints...');
modep.getBanks() 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++) {
var id = banks[index].id; 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) .then(modep.getPedalboards)
@ -41,26 +46,42 @@ function setupEndpoints() {
var id = pedalboards[index].id; var id = pedalboards[index].id;
setEndpoint( setEndpoint(
constants.API_PEDALBOARDS + '/' + id, constants.API_PEDALBOARDS + '/' + id,
{ method: modep.getPedalboardById, args: id }, { method: modep.getPedalboardById, id: id },
{ method: modep.setPedalboardById, args: id } { method: modep.setPedalboardById, id: id }
); );
} }
}) })
.then(function () { .then(function () {
setEndpoint(constants.API_PEDALBOARDS_DEFAULT, { method: modep.getDefaultPedalboard }); setEndpoint(
constants.API_PEDALBOARDS_DEFAULT,
{ method: modep.getDefaultPedalboard }
);
}) })
.then(function () { .then(function () {
setEndpoint(constants.API_PEDALBOARDS_CURRENT, { method: modep.getCurrentPedalboard }); setEndpoint(
constants.API_PEDALBOARDS_CURRENT,
{ method: modep.getCurrentPedalboard }
);
}) })
.then(modep.getCurrentPedals) .then(modep.getCurrentPedals)
.then(function (pedals) { .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++) { for (var index = 0; index < pedals.length; index++) {
var id = pedals[index].id; 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); .catch(reject);
}); });
} }

View file

@ -227,8 +227,9 @@ function parseCurrentPedalboard(currentPedalboard) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
getDefaultPedalboard() getDefaultPedalboard()
.then(function (defaultPedalboard) { .then(function (defaultPedalboard) {
currentPedals = [];
if (defaultPedalboard.id == currentPedalboard.id) { if (defaultPedalboard.id == currentPedalboard.id) {
return resolve([]); return resolve(currentPedals);
} }
var startTime = new Date(); var startTime = new Date();
logger.debug('parsing current pedalboard...'); logger.debug('parsing current pedalboard...');
@ -236,13 +237,12 @@ function parseCurrentPedalboard(currentPedalboard) {
reject('error: could not determine current pedalboard config file'); reject('error: could not determine current pedalboard config file');
} }
// FAKE DATA // 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://', '')); // var file = path.resolve(currentPedalboard.uri.replace('file://', ''));
fs.readFile(file, function (err, data) { fs.readFile(file, function (err, data) {
if (err) { if (err) {
return reject('error: could not parse current pedalboard file \'' + file + '\' >>> ' + err); return reject('error: could not parse current pedalboard file \'' + file + '\' >>> ' + err);
} }
currentPedals = [];
var json = ttl2jsonld(data.toString())['@graph']; var json = ttl2jsonld(data.toString())['@graph'];
var id = 0; var id = 0;
for (var index = 0; index < json.length; index++) { 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) { return new Promise(function (resolve, reject) {
if (!control || !control.midi) { var controlId = requestParams.get('id');
return reject('error: control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings'); if (controlId == undefined) {
reject('error: could not handle POST - missing parameter \'id\'', 400);
} }
if (value > 127) { var value = parseInt(requestParams.get('value'));
value = 127; if (value == undefined) {
} else if (value < 0) { reject('error: could not handle POST - missing parameter \'value\'', 400);
value = 0; } 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); getPedalControlById(pedalId, controlId)
var cmd = 'oscsend'; .then(function (control) {
var args = [config.osc.host, config.osc.port, config.osc.address, 'm', value]; if (!control || !control.midi) {
logger.debug('executing command \'' + cmd + '\' with args \'' + args + '\'...'); return reject('error: control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings');
var spawned = spawn(cmd, args); }
spawned.stdout.on('data', function (data) { if (value > 127) {
logger.debug(data); value = 127;
}); } else if (value < 0) {
spawned.stderr.on('data', function (data) { value = 0;
logger.error(data); }
}); value = '00' + util.toHex(value) + '0' + util.toHex(control.midi.controller) + 'b' + util.toHex(control.midi.channel);
spawned.on('close', function (code) { var cmd = 'oscsend';
logger.debug('command \'' + cmd + '\' with args \'' + args + '\' finished with exit code ' + code); var args = [config.osc.host, config.osc.port, config.osc.address, 'm', value];
resolve(); logger.debug('executing command \'' + cmd + '\' with args \'' + args + '\'...');
}); var spawned = spawn(cmd, args);
spawned.on('error', function (err) { spawned.stdout.on('data', function (data) {
logger.error('command \'' + cmd + '\' with args \'' + args + '\' encountered an error >>> ' + err); logger.debug(data);
reject(err); });
}); 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, getCurrentPedals,
getCurrentPedalById, getCurrentPedalById,
getPedalControlById, getPedalControlById,
sendValueToControl,
setPedalboard, setPedalboard,
setPedalboardById, setPedalboardById,
setControlValue,
} }

View file

@ -33,44 +33,6 @@ function handleRequests() {
logger.debug(msg); logger.debug(msg);
return; 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]; endpoint = endpoint[request.method];
if (endpoint == undefined || endpoint.method == undefined) { if (endpoint == undefined || endpoint.method == undefined) {
var msg = 'error: endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests'; var msg = 'error: endpoint \'' + request.url + '\' does not have any handlers for ' + request.method + ' requests';
@ -79,7 +41,10 @@ function handleRequests() {
logger.debug(msg); logger.debug(msg);
return; return;
} }
endpoint.method(endpoint.args) getRequestParams(request)
.then(function (params) {
return endpoint.method(endpoint.id, params);
})
.then(function (result) { .then(function (result) {
response.writeHead(200); response.writeHead(200);
try { try {
@ -87,59 +52,28 @@ function handleRequests() {
} catch (err) { } catch (err) {
response.end(result); response.end(result);
} }
return;
}) })
.catch(function (err, code) { .catch(function (err, code) {
logger.error(err);
if (code == undefined) { if (code == undefined) {
code = 500; code = 500;
} }
response.writeHead(500); response.writeHead(500);
response.end(err); response.end(err);
return;
}); });
}); });
} }
function handleGET(endpoint) { function getRequestParams(request) {
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) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
var params = ""; var params = "";
request.on("data", function (data) { request.on("data", function (data) {
params += data; params += data;
}); });
request.on("end", function () { request.on("end", function () {
if (params == undefined || params.length == 0) {
return resolve();
}
return resolve(new URLSearchParams(params)); return resolve(new URLSearchParams(params));
}); });
}); });
@ -147,7 +81,7 @@ function getPOSTParams(request) {
function handlePOSTPedals(request) { function handlePOSTPedals(request) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
getPOSTParams(request) getRequestParams(request)
.then(function (params) { .then(function (params) {
var pedalId = request.url.substring(request.url.lastIndexOf('/') + 1); var pedalId = request.url.substring(request.url.lastIndexOf('/') + 1);
var controlId = params.get('id'); var controlId = params.get('id');