heavy code optimization; async/await, arrow functions, etc.
This commit is contained in:
parent
b0c55b9e86
commit
823598aeb3
6 changed files with 227 additions and 321 deletions
|
@ -1,4 +1,3 @@
|
|||
const cache = require('./cache.js');
|
||||
const commands = require('./commands.js');
|
||||
const os = require('os');
|
||||
|
||||
|
|
459
libs/modep.js
459
libs/modep.js
|
@ -11,189 +11,134 @@ const ttl2jsonld = require('@frogcat/ttl2jsonld').parse;
|
|||
|
||||
let pedalboardIdBypassOrigin;
|
||||
|
||||
function reset() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
util.httpGET(global.config.modep.host, global.config.modep.port, '/reset')
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
async function reset() {
|
||||
await util.httpGET(global.config.modep.host, global.config.modep.port, '/reset');
|
||||
}
|
||||
|
||||
function getBanks() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
let banks = cache.getBanks();
|
||||
if (banks != undefined) {
|
||||
return resolve(banks);
|
||||
async function getBanks() {
|
||||
let banks = cache.getBanks();
|
||||
if (banks !== undefined) {
|
||||
return banks;
|
||||
}
|
||||
banks = await util.httpGET(global.config.modep.host, global.config.modep.port, '/banks');
|
||||
for (let index = 0; index < banks.length; index++) {
|
||||
let bank = banks[index];
|
||||
bank.id = index;
|
||||
}
|
||||
banks = util.sortById(banks);
|
||||
cache.setBanks(banks);
|
||||
return banks;
|
||||
}
|
||||
|
||||
async function getBankById(bankId) {
|
||||
const banks = await getBanks();
|
||||
for (let index = 0; index < banks.length; index++) {
|
||||
if (banks[index].id != bankId) {
|
||||
continue;
|
||||
}
|
||||
util.httpGET(global.config.modep.host, global.config.modep.port, '/banks')
|
||||
.then(function (banks) {
|
||||
for (let index = 0; index < banks.length; index++) {
|
||||
let bank = banks[index];
|
||||
bank.id = index;
|
||||
}
|
||||
banks = util.sortById(banks);
|
||||
cache.setBanks(banks);
|
||||
return resolve(banks);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
return banks[index];
|
||||
}
|
||||
throw new Error('could not find bank by id \'' + bankId + '\'');
|
||||
}
|
||||
|
||||
function getBankById(bankId) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
getBanks()
|
||||
.then(function (banks) {
|
||||
for (let index = 0; index < banks.length; index++) {
|
||||
if (banks[index].id != bankId) {
|
||||
continue;
|
||||
}
|
||||
return resolve(banks[index]);
|
||||
}
|
||||
return reject('could not find bank by id \'' + bankId + '\'');
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
function getPedalboards() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
let pedalboards = cache.getPedalboards();
|
||||
if (pedalboards != undefined) {
|
||||
return resolve(pedalboards);
|
||||
async function getPedalboards() {
|
||||
let pedalboards = cache.getPedalboards();
|
||||
if (pedalboards !== undefined) {
|
||||
return pedalboards;
|
||||
}
|
||||
pedalboards = await util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/list');
|
||||
let id = 1;
|
||||
for (let index = 0; index < pedalboards.length; index++) {
|
||||
let pedalboard = pedalboards[index];
|
||||
if (pedalboard.bundle === constants.PEDALBOARD_DEFAULT) {
|
||||
pedalboard.id = 0;
|
||||
defaultPedalboard = pedalboard;
|
||||
continue;
|
||||
}
|
||||
util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/list')
|
||||
.then(function (pedalboards) {
|
||||
let id = 1;
|
||||
for (let index = 0; index < pedalboards.length; index++) {
|
||||
let pedalboard = pedalboards[index];
|
||||
if (pedalboard.bundle == constants.PEDALBOARD_DEFAULT) {
|
||||
pedalboard.id = 0;
|
||||
defaultPedalboard = pedalboard;
|
||||
continue;
|
||||
}
|
||||
pedalboard.id = id;
|
||||
id++;
|
||||
}
|
||||
pedalboards = util.sortById(pedalboards);
|
||||
cache.setPedalboards(pedalboards);
|
||||
return resolve(pedalboards);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
pedalboard.id = id;
|
||||
id++;
|
||||
}
|
||||
pedalboards = util.sortById(pedalboards);
|
||||
cache.setPedalboards(pedalboards);
|
||||
return pedalboards;
|
||||
}
|
||||
|
||||
function getDefaultPedalboard() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
let defaultPedalboard = cache.getDefaultPedalboard();
|
||||
if (defaultPedalboard != undefined) {
|
||||
return resolve(defaultPedalboard);
|
||||
async function getDefaultPedalboard() {
|
||||
let defaultPedalboard = cache.getDefaultPedalboard();
|
||||
if (defaultPedalboard !== undefined) {
|
||||
return defaultPedalboard;
|
||||
}
|
||||
defaultPedalboard = await getPedalboardByBundle(constants.PEDALBOARD_DEFAULT);
|
||||
cache.setDefaultPedalboard(defaultPedalboard);
|
||||
return defaultPedalboard;
|
||||
}
|
||||
|
||||
async function getCurrentPedalboard() {
|
||||
let currentPedalboard = cache.getCurrentPedalboard();
|
||||
if (currentPedalboard !== undefined) {
|
||||
return currentPedalboard;
|
||||
}
|
||||
currentPedalboard = await getPedalboardByBundle(await util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/current'));
|
||||
cache.setCurrentPedalboard(currentPedalboard);
|
||||
return currentPedalboard;
|
||||
}
|
||||
|
||||
async function getPedalboardById(pedalboardId) {
|
||||
const pedalboards = await getPedalboards();
|
||||
if (pedalboards === undefined) {
|
||||
return;
|
||||
}
|
||||
for (let index = 0; index < pedalboards.length; index++) {
|
||||
if (pedalboards[index].id != pedalboardId) {
|
||||
continue;
|
||||
}
|
||||
getPedalboardByBundle(constants.PEDALBOARD_DEFAULT)
|
||||
.then(function (defaultPedalboard) {
|
||||
cache.setDefaultPedalboard(defaultPedalboard);
|
||||
return resolve(defaultPedalboard);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
return pedalboards[index];
|
||||
}
|
||||
throw new Error('could not find pedalboard by id \'' + pedalboardId + '\'');
|
||||
}
|
||||
|
||||
function getCurrentPedalboard() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
let currentPedalboard = cache.getCurrentPedalboard();
|
||||
if (currentPedalboard != undefined && currentPedalboard.id != undefined) {
|
||||
return resolve(currentPedalboard);
|
||||
async function getPedalboardByBundle(pedalboardBundle) {
|
||||
if (pedalboardBundle === undefined) {
|
||||
return await getDefaultPedalboard();
|
||||
}
|
||||
const pedalboards = await getPedalboards();
|
||||
for (let index = 0; index < pedalboards.length; index++) {
|
||||
if (pedalboards[index].bundle != pedalboardBundle) {
|
||||
continue;
|
||||
}
|
||||
util.httpGET(global.config.modep.host, global.config.modep.port, '/pedalboard/current')
|
||||
.then(getPedalboardByBundle)
|
||||
.then(function (currentPedalboard) {
|
||||
cache.setCurrentPedalboard(currentPedalboard);
|
||||
return resolve(currentPedalboard)
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
return pedalboards[index];
|
||||
}
|
||||
throw new Error('could not find pedalboard by bundle \'' + pedalboardBundle + '\'');
|
||||
}
|
||||
|
||||
function getPedalboardById(pedalboardId) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
getPedalboards()
|
||||
.then(function (pedalboards) {
|
||||
for (let index = 0; index < pedalboards.length; index++) {
|
||||
if (pedalboards[index].id != pedalboardId) {
|
||||
continue;
|
||||
}
|
||||
return resolve(pedalboards[index]);
|
||||
}
|
||||
return reject('could not find pedalboard by id \'' + pedalboardId + '\'');
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
function getPedalboardByBundle(pedalboardBundle) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (pedalboardBundle == undefined) {
|
||||
getDefaultPedalboard()
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
return;
|
||||
async function getPedalControlById(pedalId, controlId) {
|
||||
const pedal = await getCurrentPedalById(pedalId);
|
||||
for (let index = 0; index < pedal.controls.length; index++) {
|
||||
if (pedal.controls[index].id != controlId) {
|
||||
continue;
|
||||
}
|
||||
getPedalboards()
|
||||
.then(function (pedalboards) {
|
||||
for (let index = 0; index < pedalboards.length; index++) {
|
||||
if (pedalboards[index].bundle != pedalboardBundle) {
|
||||
continue;
|
||||
}
|
||||
return resolve(pedalboards[index]);
|
||||
}
|
||||
return reject('could not find pedalboard by bundle \'' + pedalboardBundle + '\'');
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
return pedal.controls[index];
|
||||
}
|
||||
throw new Error('could not find control for pedal \'' + pedalId + '\' by id \'' + controlId + '\'');
|
||||
}
|
||||
|
||||
function getPedalControlById(pedalId, controlId) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
getCurrentPedalById(pedalId)
|
||||
.then(function (pedal) {
|
||||
for (let index = 0; index < pedal.controls.length; index++) {
|
||||
if (pedal.controls[index].id != controlId) {
|
||||
continue;
|
||||
}
|
||||
return resolve(pedal.controls[index]);
|
||||
}
|
||||
return reject('could not find control for pedal \'' + pedalId + '\' by id \'' + controlId + '\'');
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
function getCurrentPedalById(id) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
getCurrentPedals()
|
||||
.then(function (currentPedals) {
|
||||
for (let index = 0; index < currentPedals.length; index++) {
|
||||
if (currentPedals[index].id != id) {
|
||||
continue;
|
||||
}
|
||||
return resolve(currentPedals[index]);
|
||||
}
|
||||
return reject('could not find current pedal by id \'' + id + '\'');
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
function getCurrentPedals() {
|
||||
return new Promise(function (resolve, reject) {
|
||||
let currentPedals = cache.getCurrentPedals();
|
||||
if (currentPedals) {
|
||||
return resolve(currentPedals);
|
||||
async function getCurrentPedalById(id) {
|
||||
const currentPedals = await getCurrentPedals();
|
||||
for (let index = 0; index < currentPedals.length; index++) {
|
||||
if (currentPedals[index].id != id) {
|
||||
continue;
|
||||
}
|
||||
getCurrentPedalboard()
|
||||
.then(parseCurrentPedalboard)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
return currentPedals[index];
|
||||
}
|
||||
throw new Error('could not find current pedal by id \'' + id + '\'');
|
||||
}
|
||||
|
||||
async function getCurrentPedals() {
|
||||
let currentPedals = cache.getCurrentPedals();
|
||||
if (currentPedals !== undefined) {
|
||||
return currentPedals;
|
||||
}
|
||||
return await parseCurrentPedalboard(await getCurrentPedalboard());
|
||||
}
|
||||
|
||||
async function parseCurrentPedalboard(currentPedalboard) {
|
||||
|
@ -204,7 +149,6 @@ async function parseCurrentPedalboard(currentPedalboard) {
|
|||
blinky.setStatus(pedals);
|
||||
return pedals;
|
||||
}
|
||||
|
||||
let startTime = new Date();
|
||||
logger.debug('parsing current pedalboard \'' + currentPedalboard.title + '\'...');
|
||||
if (currentPedalboard.uri === undefined) {
|
||||
|
@ -213,82 +157,82 @@ async function parseCurrentPedalboard(currentPedalboard) {
|
|||
// FAKE DATA
|
||||
let file = path.resolve(path.dirname(__dirname) + '/dev/' + currentPedalboard.uri.substring(currentPedalboard.uri.lastIndexOf('/') + 1));
|
||||
// let file = path.resolve(currentPedalboard.uri.replace('file://', ''));
|
||||
pedals = await new Promise((resolve, reject) => {
|
||||
const data = await new Promise((resolve, reject) => {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (err) {
|
||||
return reject('could not parse current pedalboard file \'' + file + '\' >>> ' + err);
|
||||
}
|
||||
let json = ttl2jsonld(data.toString())['@graph'];
|
||||
let id = 0;
|
||||
let currentPedals = [];
|
||||
for (let index = 0; index < json.length; index++) {
|
||||
let tmp = json[index];
|
||||
if (tmp['lv2:prototype'] === undefined) {
|
||||
continue;
|
||||
}
|
||||
let name = tmp['@id'];
|
||||
currentPedals.push({ id: id, name: name, controls: [] });
|
||||
id++;
|
||||
}
|
||||
for (let index = 0; index < json.length; index++) {
|
||||
let tmp = json[index];
|
||||
let name = tmp['@id'];
|
||||
let value = tmp['ingen:value'];
|
||||
if (value === undefined) {
|
||||
continue;
|
||||
}
|
||||
let pedal = undefined;
|
||||
for (let pedalIndex = 0; pedalIndex < currentPedals.length; pedalIndex++) {
|
||||
if (!name.startsWith(currentPedals[pedalIndex].name)) {
|
||||
continue;
|
||||
}
|
||||
pedal = currentPedals[pedalIndex];
|
||||
break;
|
||||
}
|
||||
if (pedal === undefined) {
|
||||
continue;
|
||||
}
|
||||
id = pedal.controls.length;
|
||||
name = name.replace(pedal.name + '/', '');
|
||||
if (name !== constants.CONTROL_BYPASS) {
|
||||
value = value['@value'];
|
||||
}
|
||||
let control = { id, name, value };
|
||||
pedal.controls.push(control);
|
||||
id++;
|
||||
let midi = tmp['midi:binding'];
|
||||
if (midi === undefined) {
|
||||
continue;
|
||||
}
|
||||
control.midi = { channel: midi['midi:channel'], controller: midi['midi:controllerNumber'] }
|
||||
}
|
||||
logger.debug('parsing current pedalboard file \'' + file + '\' took ' + util.timeDiff(startTime) + 'ms')
|
||||
resolve(currentPedals);
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
let json = ttl2jsonld(data.toString())['@graph'];
|
||||
let id = 0;
|
||||
for (let index = 0; index < json.length; index++) {
|
||||
let tmp = json[index];
|
||||
if (tmp['lv2:prototype'] === undefined) {
|
||||
continue;
|
||||
}
|
||||
let name = tmp['@id'];
|
||||
pedals.push({ id: id, name: name, controls: [] });
|
||||
id++;
|
||||
}
|
||||
for (let index = 0; index < json.length; index++) {
|
||||
let tmp = json[index];
|
||||
let name = tmp['@id'];
|
||||
let value = tmp['ingen:value'];
|
||||
if (value === undefined) {
|
||||
continue;
|
||||
}
|
||||
let pedal = undefined;
|
||||
for (let pedalIndex = 0; pedalIndex < pedals.length; pedalIndex++) {
|
||||
if (!name.startsWith(pedals[pedalIndex].name)) {
|
||||
continue;
|
||||
}
|
||||
pedal = pedals[pedalIndex];
|
||||
break;
|
||||
}
|
||||
if (pedal === undefined) {
|
||||
continue;
|
||||
}
|
||||
id = pedal.controls.length;
|
||||
name = name.replace(pedal.name + '/', '');
|
||||
if (name !== constants.CONTROL_BYPASS) {
|
||||
value = value['@value'];
|
||||
}
|
||||
let control = { id, name, value };
|
||||
pedal.controls.push(control);
|
||||
id++;
|
||||
let midi = tmp['midi:binding'];
|
||||
if (midi === undefined) {
|
||||
continue;
|
||||
}
|
||||
control.midi = { channel: midi['midi:channel'], controller: midi['midi:controllerNumber'] }
|
||||
}
|
||||
logger.debug('parsing current pedalboard file \'' + file + '\' took ' + util.timeDiff(startTime) + 'ms')
|
||||
cache.setCurrentPedals(pedals);
|
||||
blinky.setStatus(pedals);
|
||||
return pedals;
|
||||
}
|
||||
|
||||
function setControlByRequest(pedalId, requestParams) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
let controlId = requestParams.get('id');
|
||||
if (controlId === undefined) {
|
||||
reject('could not handle POST - missing parameter \'id\'', 400);
|
||||
}
|
||||
let value = parseInt(requestParams.get('value'));
|
||||
if (value === undefined) {
|
||||
reject('could not handle POST - missing parameter \'value\'', 400);
|
||||
} else if (isNaN(value)) {
|
||||
reject('parameter \'value\' is not a number', 400);
|
||||
}
|
||||
getPedalControlById(pedalId, controlId)
|
||||
.then(function (control) {
|
||||
resolve(setControl(control, value));
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
async function setControlByRequest(pedalId, requestParams) {
|
||||
if (requestParams === undefined) {
|
||||
throw new Error('could not handle POST missing all parameters', 400);
|
||||
}
|
||||
let controlId = requestParams.get('id');
|
||||
if (controlId === undefined || controlId === null) {
|
||||
throw new Error('could not handle POST - missing parameter \'id\'', 400);
|
||||
}
|
||||
if (isNaN(controlId)) {
|
||||
throw new Error('parameter \'id\' is not a number', 400);
|
||||
}
|
||||
let value = requestParams.get('value');
|
||||
if (value === undefined || value === null) {
|
||||
throw new Error('could not handle POST - missing parameter \'value\'', 400);
|
||||
}
|
||||
if (isNaN(value)) {
|
||||
throw new Error('parameter \'value\' is not a number', 400);
|
||||
}
|
||||
return await setControl(await getPedalControlById(pedalId, controlId), parseInt(value));
|
||||
}
|
||||
|
||||
async function setControl(control, value) {
|
||||
|
@ -299,42 +243,27 @@ async function setControl(control, value) {
|
|||
cache.updateControl(control);
|
||||
}
|
||||
|
||||
function setPedalboardById(pedalboardId) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (pedalboardId == undefined) {
|
||||
return reject('no pedalboard id given');
|
||||
}
|
||||
getPedalboardById(pedalboardId)
|
||||
.then(setPedalboard)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
async function setPedalboardById(pedalboardId) {
|
||||
if (pedalboardId === undefined) {
|
||||
throw new Error('no pedalboard id given');
|
||||
}
|
||||
if (isNaN(pedalboardId)) {
|
||||
throw new Error('given pedalboard id \'' + pedalboardId + '\' is not a number');
|
||||
}
|
||||
return await setPedalboard(await getPedalboardById(pedalboardId));
|
||||
}
|
||||
|
||||
function setPedalboard(pedalboard) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (!pedalboard || !pedalboard.bundle) {
|
||||
return reject('no bundle set for pedalboard');
|
||||
}
|
||||
getCurrentPedalboard()
|
||||
.then(function (currentPedalboard) {
|
||||
if (pedalboard.id === currentPedalboard.id) {
|
||||
return Promise.reject('pedalboard with id \'' + currentPedalboard.id + '\' is already active');
|
||||
}
|
||||
})
|
||||
.then(function () {
|
||||
return reset()
|
||||
})
|
||||
.then(function () {
|
||||
return util.httpPOST(global.config.modep.host, global.config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle)
|
||||
})
|
||||
.then(function () {
|
||||
cache.setCurrentPedalboard(pedalboard);
|
||||
return parseCurrentPedalboard(pedalboard);
|
||||
})
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
async function setPedalboard(pedalboard) {
|
||||
if (pedalboard?.bundle === undefined) {
|
||||
throw new Error('no bundle set for pedalboard \'' + pedalboard.title + '\'');
|
||||
}
|
||||
if (pedalboard.id === await getCurrentPedalboard().id) {
|
||||
throw new Error('pedalboard with id \'' + currentPedalboard.id + '\' is already active');
|
||||
}
|
||||
await reset();
|
||||
await util.httpPOST(global.config.modep.host, global.config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle);
|
||||
cache.setCurrentPedalboard(pedalboard);
|
||||
return await parseCurrentPedalboard(pedalboard);
|
||||
}
|
||||
|
||||
function hasControlMidiBindings(control) {
|
||||
|
|
15
libs/osc.js
15
libs/osc.js
|
@ -5,7 +5,7 @@ const commands = require('./commands.js');
|
|||
const CMD = 'oscsend';
|
||||
|
||||
async function send(controller, channel, value) {
|
||||
if (controller == undefined || channel === undefined) {
|
||||
if (controller === undefined || channel === undefined) {
|
||||
return;
|
||||
}
|
||||
if (value === undefined || isNaN(value) || value < 0) {
|
||||
|
@ -14,14 +14,13 @@ async function send(controller, channel, value) {
|
|||
value = 127;
|
||||
}
|
||||
logger.debug('sending value \'' + value + '\' to controller \'' + controller + '\' on channel \'' + channel + '\'...');
|
||||
let args = [
|
||||
global.config.osc.host, global.config.osc.port,
|
||||
global.config.osc.address,
|
||||
'm',
|
||||
'00' + util.toHex(value) + '0' + util.toHex(controller) + 'b' + util.toHex(channel)
|
||||
];
|
||||
try {
|
||||
await commands.execute(CMD, args);
|
||||
await commands.execute(CMD, [
|
||||
global.config.osc.host, global.config.osc.port,
|
||||
global.config.osc.address,
|
||||
'm',
|
||||
'00' + util.toHex(value) + '0' + util.toHex(controller) + 'b' + util.toHex(channel)
|
||||
]);
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ async function start() {
|
|||
if (!server) {
|
||||
server = http.createServer();
|
||||
}
|
||||
server.listen(global.config.server.port, global.config.server.listen).on('listening', function () {
|
||||
server.listen(global.config.server.port, global.config.server.listen).on('listening', () => {
|
||||
logger.debug('server listening on ' + global.config.server.listen + ':' + global.config.server.port + '...');
|
||||
blinky.setActive(true);
|
||||
handleRequests();
|
||||
|
@ -19,7 +19,7 @@ async function start() {
|
|||
}
|
||||
|
||||
function handleRequests() {
|
||||
server.on('request', function (request, response) {
|
||||
server.on('request', (request, response) => {
|
||||
request.timestamp = new Date().getTime();
|
||||
if (request.url.length > 1 && request.url.endsWith('/')) {
|
||||
request.url = request.url.substring(0, request.url.length - 1);
|
||||
|
@ -54,13 +54,13 @@ function handleRequests() {
|
|||
return;
|
||||
}
|
||||
getRequestParams(request)
|
||||
.then(function (params) {
|
||||
.then((params) => {
|
||||
return endpoint.method(id, params);
|
||||
})
|
||||
.then(function (result) {
|
||||
.then((result) => {
|
||||
endRequest(request, response, result);
|
||||
})
|
||||
.catch(function (err, code) {
|
||||
.catch((err, code) => {
|
||||
if (code == undefined) {
|
||||
code = 500;
|
||||
}
|
||||
|
@ -84,20 +84,20 @@ function endRequest(request, response, msg, code) {
|
|||
object.data = msg;
|
||||
}
|
||||
}
|
||||
response.setHeader("Access-Control-Allow-Origin", "*");
|
||||
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||
response.setHeader('Access-Control-Allow-Origin', '*');
|
||||
response.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
|
||||
response.writeHead(object.code);
|
||||
response.end(object.data);
|
||||
logger.http(object);
|
||||
}
|
||||
|
||||
function getRequestParams(request) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var params = "";
|
||||
request.on("data", function (data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
var params = '';
|
||||
request.on('data', (data) => {
|
||||
params += data;
|
||||
});
|
||||
request.on("end", function () {
|
||||
request.on('end', () => {
|
||||
if (params == undefined || params.length == 0) {
|
||||
return resolve();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
const logger = require('./logger.js');
|
||||
const commands = require('./commands.js');
|
||||
const constants = require('./constants.js');
|
||||
const blinky = require('./blinky.js');
|
||||
|
||||
async function getServiceState(name) {
|
||||
if (name === undefined) {
|
||||
|
|
50
libs/util.js
50
libs/util.js
|
@ -29,8 +29,8 @@ function httpPOST(host, port, path, args) {
|
|||
|
||||
function httpRequest(host, port, path, method, args) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
if (!path.startsWith('/')) {
|
||||
path = '/' + path;
|
||||
}
|
||||
const options = {
|
||||
hostname: host,
|
||||
|
@ -53,7 +53,7 @@ function httpRequest(host, port, path, method, args) {
|
|||
if (!response) {
|
||||
return reject('no response from host for ' + requestName);
|
||||
}
|
||||
let responseData = "";
|
||||
let responseData = '';
|
||||
response.on('data', (data) => {
|
||||
responseData += data;
|
||||
});
|
||||
|
@ -89,8 +89,7 @@ function httpRequest(host, port, path, method, args) {
|
|||
}
|
||||
|
||||
function toHex(value) {
|
||||
let hex = Number(value).toString(16);
|
||||
return hex;
|
||||
return Number(value).toString(16);
|
||||
}
|
||||
|
||||
function hexToRGB(hex) {
|
||||
|
@ -98,14 +97,13 @@ function hexToRGB(hex) {
|
|||
if (!validHEXInput) {
|
||||
return;
|
||||
}
|
||||
let output = [
|
||||
return [
|
||||
{
|
||||
red: parseInt(validHEXInput[1], 16),
|
||||
green: parseInt(validHEXInput[2], 16),
|
||||
blue: parseInt(validHEXInput[3], 16),
|
||||
},
|
||||
}
|
||||
];
|
||||
return output;
|
||||
}
|
||||
|
||||
function sortById(array) {
|
||||
|
@ -114,37 +112,19 @@ function sortById(array) {
|
|||
});
|
||||
}
|
||||
|
||||
function fileExists(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (file == undefined) {
|
||||
reject('can not check the existence of an undefined file');
|
||||
}
|
||||
resolvePath(file)
|
||||
.then((path) => {
|
||||
stat(path)
|
||||
.then((stats) => {
|
||||
resolve({ path, stats });
|
||||
})
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
async function fileExists(file) {
|
||||
const path = await resolvePath(file);
|
||||
const stats = await stat(path);
|
||||
return {path, stats};
|
||||
}
|
||||
|
||||
function resolvePath(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (file == undefined) {
|
||||
reject('can not resolve a path to an undefined file');
|
||||
}
|
||||
realpath(file)
|
||||
.then(resolve)
|
||||
.catch((err) => {
|
||||
reject('resolving path \'' + file + '\' encountered an error >>> ' + err);
|
||||
});
|
||||
});
|
||||
async function resolvePath(file) {
|
||||
if (file === undefined) {
|
||||
throw new Error('can not resolve a path to an undefined file');
|
||||
}
|
||||
return await realpath(file);
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
timeDiff,
|
||||
clone,
|
||||
|
|
Loading…
Reference in a new issue