added endpoints '/save' and '/cache'

This commit is contained in:
Daniel Sommer 2022-03-30 12:49:00 +02:00
parent 585e381417
commit 7901cc8aff
5 changed files with 83 additions and 6 deletions

View file

@ -58,4 +58,18 @@ in this case the pedal's bypass is turned on and has the id "8"; now let's turn
| /midi | | send a midi message via `oscsend` defined by the POST arguments to the osc host | | /midi | | send a midi message via `oscsend` defined by the POST arguments to the osc host |
**note:** **note:**
POST arguments **must** contain the parameters `controller`, `channel` and `value`. POST arguments **must** contain the parameters `controller`, `channel` and `value`.
### save
| url | GET | POST |
| - | - | - |
| /save | | save current pedalboard (optionally with new `title`) |
**note:**
POST arguments **can** contain the parameter `title`.
### cache
| url | GET | POST |
| - | - | - |
| /cache/clear | | clear all cached items |
| /cache/refresh | | clear and refresh all cache items |

View file

@ -5,6 +5,7 @@ const modep = require('./modep.js');
const osc = require('./osc.js'); const osc = require('./osc.js');
const info = require('./info.js'); const info = require('./info.js');
const systemd = require('./systemd.js'); const systemd = require('./systemd.js');
const cache = require('./cache.js');
const endpoints = new Map(); const endpoints = new Map();
@ -71,7 +72,7 @@ async function setupEndpoints() {
constants.API_MIDI, constants.API_MIDI,
undefined, undefined,
{ method: osc.sendByRequest } { method: osc.sendByRequest }
) );
if (global.config.systemd !== undefined) { if (global.config.systemd !== undefined) {
for (let index = 0; index < global.config.systemd.length; index++) { for (let index = 0; index < global.config.systemd.length; index++) {
setEndpoint( setEndpoint(
@ -81,6 +82,21 @@ async function setupEndpoints() {
) )
} }
} }
setEndpoint(
constants.API_SAVE,
undefined,
{ method: modep.saveCurrentPedalboard }
);
setEndpoint(
constants.API_CACHE_CLEAR,
undefined,
{ method: cache.clear }
);
setEndpoint(
constants.API_CACHE_REFRESH,
undefined,
{ method: cache.fill }
);
logger.debug('setting up ' + endpoints.size + ' endpoints took ' + util.timeDiff(timestamp) + 'ms'); logger.debug('setting up ' + endpoints.size + ' endpoints took ' + util.timeDiff(timestamp) + 'ms');
} }

View file

@ -8,8 +8,8 @@ const cache = new Map();
async function fill() { async function fill() {
const timestamp = new Date(); const timestamp = new Date();
logger.debug('filling cache...');
clear(); clear();
logger.debug('filling cache...');
try { try {
if (modep === undefined) { if (modep === undefined) {
modep = require('./modep.js'); modep = require('./modep.js');
@ -27,6 +27,7 @@ async function fill() {
} }
function clear() { function clear() {
logger.debug('clearing cache...');
cache.clear(); cache.clear();
} }

View file

@ -9,6 +9,8 @@ const PEDALS = '/pedals';
const BYPASS = '/bypass'; const BYPASS = '/bypass';
const MIDI = '/midi'; const MIDI = '/midi';
const SYSTEMD = '/systemd'; const SYSTEMD = '/systemd';
const SAVE = '/save';
const CACHE = '/cache';
exports.SYSTEMD_STATE_ACTIVE = 'active'; exports.SYSTEMD_STATE_ACTIVE = 'active';
exports.SYSTEMD_STATE_INACTIVE = 'inactive'; exports.SYSTEMD_STATE_INACTIVE = 'inactive';
@ -33,6 +35,9 @@ exports.API_BYPASS = BYPASS;
exports.API_BYPASS_BY_ID = BYPASS + '/' + VARIABLE_ID; exports.API_BYPASS_BY_ID = BYPASS + '/' + VARIABLE_ID;
exports.API_MIDI = MIDI; exports.API_MIDI = MIDI;
exports.API_SYSTEMD = SYSTEMD; exports.API_SYSTEMD = SYSTEMD;
exports.API_SAVE = SAVE;
exports.API_CACHE_CLEAR = CACHE + '/clear';
exports.API_CACHE_REFRESH = CACHE + '/refresh';
exports.CACHE_BANKS = 'banks'; exports.CACHE_BANKS = 'banks';
exports.CACHE_PEDALBOARDS = 'pedalboards'; exports.CACHE_PEDALBOARDS = 'pedalboards';

View file

@ -303,8 +303,8 @@ function getBypassControlFromPedal(pedal) {
} }
async function toggleBypass(pedalId) { async function toggleBypass(pedalId) {
let currentPedalboard = await getCurrentPedalboard(); const currentPedalboard = await getCurrentPedalboard();
let defaultPedalboard = await getDefaultPedalboard(); const defaultPedalboard = await getDefaultPedalboard();
if (currentPedalboard.id === defaultPedalboard.id && pedalboardIdBypassOrigin === undefined) { if (currentPedalboard.id === defaultPedalboard.id && pedalboardIdBypassOrigin === undefined) {
throw new Error('could not activate bypass, default pedalboard is currently already active'); throw new Error('could not activate bypass, default pedalboard is currently already active');
} }
@ -319,10 +319,11 @@ async function toggleBypass(pedalId) {
pedalboardIdBypassOrigin = undefined; pedalboardIdBypassOrigin = undefined;
} }
await setPedalboardById(pedalboardId); await setPedalboardById(pedalboardId);
getPedalboardById
return { status: 'ok', pedalboard: await getPedalboardById(pedalboardId) };
} catch (err) { } catch (err) {
throw new Error(err); throw new Error(err);
} }
return;
} }
const pedal = await getCurrentPedalById(pedalId); const pedal = await getCurrentPedalById(pedalId);
const bypass = getBypassControlFromPedal(pedal); const bypass = getBypassControlFromPedal(pedal);
@ -337,6 +338,45 @@ async function toggleBypass(pedalId) {
throw new Error('could not toggle bypass for pedal ' + pedal.name + ' with id \'' + pedal.id + '\' > ' + err); throw new Error('could not toggle bypass for pedal ' + pedal.name + ' with id \'' + pedal.id + '\' > ' + err);
} }
logger.info('toggled bypass for pedal \'' + pedal.name + '\''); logger.info('toggled bypass for pedal \'' + pedal.name + '\'');
return { status: 'ok', bypass: isBypassActive(bypass), control: bypass };
}
async function saveCurrentPedalboard(id, args) {
const currentPedalboard = await getCurrentPedalboard();
const defaultPedalboard = await getDefaultPedalboard();
if (currentPedalboard.id === defaultPedalboard.id) {
throw new Error('not saving default pedalboard \'' + defaultPedalboard.title + '\'');
}
let title = args?.get('title') || currentPedalboard.title;
let newBoard = 1;
if (title === currentPedalboard.title) {
newBoard = 0;
} else {
const pedalboards = await getPedalboards();
for (let index = 0; index < pedalboards.length; index++) {
if (pedalboards[index].title === title) {
throw new Error('not saving current state of pedalboard \'' + currentPedalboard.title + '\' as new pedalboard \'' + title + '\', pedalboard already exists')
}
}
}
await util.httpPOST(global.config.modep.host, global.config.modep.port, '/pedalboard/save', { title: title, asNew: newBoard });
result = {
status: 'ok',
title: title,
pedalboard: currentPedalboard
};
if (newBoard === 0) {
logger.info('saved current state of pedalboard \'' + currentPedalboard.title + '\'');
} else {
logger.info('saved current state of pedalboard \'' + currentPedalboard.title + '\' as new pedalboard \'' + title + '\'');
cache.clearPedalboards();
await getPedalboards();
}
return {
status: 'ok',
title: title,
pedalboard: currentPedalboard
};
} }
module.exports = { module.exports = {
@ -346,6 +386,7 @@ module.exports = {
getPedalboardById, getPedalboardById,
getDefaultPedalboard, getDefaultPedalboard,
getCurrentPedalboard, getCurrentPedalboard,
saveCurrentPedalboard,
getCurrentPedals, getCurrentPedals,
getCurrentPedalById, getCurrentPedalById,
getPedalControlById, getPedalControlById,