pbc/libs/api.js

94 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-02-07 15:41:27 +01:00
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');
2022-02-07 15:41:27 +01:00
const endpoints = new Map();
2022-02-08 11:11:49 +01:00
function setEndpoint(url, get, post) {
if (get == undefined && post == undefined) {
return;
2022-02-07 15:41:27 +01:00
}
2022-02-08 11:11:49 +01:00
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));
2022-02-07 15:41:27 +01:00
}
function getEndpoints() {
return endpoints;
}
function setupEndpoints() {
return new Promise(function (resolve, reject) {
2022-02-08 13:46:02 +01:00
var startTime = new Date();
logger.debug('setting up endpoints...');
2022-02-07 15:41:27 +01:00
modep.getBanks()
2022-02-07 23:50:16 +01:00
.then(function (banks) {
2022-02-08 11:11:49 +01:00
setEndpoint(constants.API_BANKS, { method: modep.getBanks });
2022-02-07 23:50:16 +01:00
for (var index = 0; index < banks.length; index++) {
var id = banks[index].id;
2022-02-08 13:46:02 +01:00
setEndpoint(
constants.API_BANKS + '/' + id,
{ method: modep.getBankById, id: id }
);
2022-02-07 23:50:16 +01:00
}
2022-02-07 15:41:27 +01:00
})
.then(modep.getPedalboards)
2022-02-07 23:50:16 +01:00
.then(function (pedalboards) {
2022-02-08 11:11:49 +01:00
setEndpoint(constants.API_PEDALBOARDS, { method: modep.getPedalboards });
2022-02-07 23:50:16 +01:00
for (var index = 0; index < pedalboards.length; index++) {
var id = pedalboards[index].id;
2022-02-08 11:11:49 +01:00
setEndpoint(
constants.API_PEDALBOARDS + '/' + id,
2022-02-08 13:46:02 +01:00
{ method: modep.getPedalboardById, id: id },
{ method: modep.setPedalboardById, id: id }
2022-02-08 11:11:49 +01:00
);
2022-02-07 23:50:16 +01:00
}
2022-02-07 15:41:27 +01:00
})
2022-02-08 11:11:49 +01:00
.then(function () {
2022-02-08 13:46:02 +01:00
setEndpoint(
constants.API_PEDALBOARDS_DEFAULT,
{ method: modep.getDefaultPedalboard }
);
2022-02-08 11:11:49 +01:00
})
.then(function () {
2022-02-08 13:46:02 +01:00
setEndpoint(
constants.API_PEDALBOARDS_CURRENT,
{ method: modep.getCurrentPedalboard }
);
2022-02-08 11:11:49 +01:00
})
2022-02-07 15:41:27 +01:00
.then(modep.getCurrentPedals)
2022-02-07 23:50:16 +01:00
.then(function (pedals) {
2022-02-08 13:46:02 +01:00
setEndpoint(
constants.API_PEDALS,
{ method: modep.getCurrentPedals }
);
2022-02-07 23:50:16 +01:00
for (var index = 0; index < pedals.length; index++) {
var id = pedals[index].id;
2022-02-08 13:46:02 +01:00
setEndpoint(
constants.API_PEDALS + '/' + id,
{ method: modep.getCurrentPedalById, id: id },
{ method: modep.setControlValue, id: id }
);
2022-02-07 23:50:16 +01:00
}
2022-02-07 15:41:27 +01:00
})
.then(setEndpoint(constants.API_INFO, { method: info.getHostInfo }))
2022-02-08 13:46:02 +01:00
.then(function () {
logger.debug('setting up ' + endpoints.size + ' endpoints took ' + util.timeDiff(startTime) + 'ms');
resolve();
})
2022-02-07 15:41:27 +01:00
.catch(reject);
});
}
module.exports = {
setupEndpoints,
getEndpoints
}