pbc/libs/api.js

94 lines
3.2 KiB
JavaScript

const logger = require('./logger.js');
const util = require('./util.js');
const constants = require('./constants.js');
const modep = require('./modep.js');
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));
}
function getEndpoints() {
return endpoints;
}
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, id: id }
);
}
})
.then(modep.getPedalboards)
.then(function (pedalboards) {
setEndpoint(constants.API_PEDALBOARDS, { method: modep.getPedalboards });
for (var index = 0; index < pedalboards.length; index++) {
var id = pedalboards[index].id;
setEndpoint(
constants.API_PEDALBOARDS + '/' + id,
{ method: modep.getPedalboardById, id: id },
{ method: modep.setPedalboardById, id: id }
);
}
})
.then(function () {
setEndpoint(
constants.API_PEDALBOARDS_DEFAULT,
{ method: modep.getDefaultPedalboard }
);
})
.then(function () {
setEndpoint(
constants.API_PEDALBOARDS_CURRENT,
{ method: modep.getCurrentPedalboard }
);
})
.then(modep.getCurrentPedals)
.then(function (pedals) {
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, id: id },
{ method: modep.setControlValue, id: id }
);
}
})
.then(setEndpoint(constants.API_INFO, { method: info.getHostInfo }))
.then(function () {
logger.debug('setting up ' + endpoints.size + ' endpoints took ' + util.timeDiff(startTime) + 'ms');
resolve();
})
.catch(reject);
});
}
module.exports = {
setupEndpoints,
getEndpoints
}