diff --git a/README.md b/README.md index e991a6e..7a4bba8 100644 --- a/README.md +++ b/README.md @@ -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 | **note:** -POST arguments **must** contain the parameters `controller`, `channel` and `value`. \ No newline at end of file +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 | \ No newline at end of file diff --git a/libs/api.js b/libs/api.js index 3b63186..8838523 100644 --- a/libs/api.js +++ b/libs/api.js @@ -5,6 +5,7 @@ const modep = require('./modep.js'); const osc = require('./osc.js'); const info = require('./info.js'); const systemd = require('./systemd.js'); +const cache = require('./cache.js'); const endpoints = new Map(); @@ -71,7 +72,7 @@ async function setupEndpoints() { constants.API_MIDI, undefined, { method: osc.sendByRequest } - ) + ); if (global.config.systemd !== undefined) { for (let index = 0; index < global.config.systemd.length; index++) { 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'); } diff --git a/libs/cache.js b/libs/cache.js index 908f921..8361290 100644 --- a/libs/cache.js +++ b/libs/cache.js @@ -8,8 +8,8 @@ const cache = new Map(); async function fill() { const timestamp = new Date(); - logger.debug('filling cache...'); clear(); + logger.debug('filling cache...'); try { if (modep === undefined) { modep = require('./modep.js'); @@ -27,6 +27,7 @@ async function fill() { } function clear() { + logger.debug('clearing cache...'); cache.clear(); } diff --git a/libs/constants.js b/libs/constants.js index d736701..e2a76c5 100644 --- a/libs/constants.js +++ b/libs/constants.js @@ -9,6 +9,8 @@ const PEDALS = '/pedals'; const BYPASS = '/bypass'; const MIDI = '/midi'; const SYSTEMD = '/systemd'; +const SAVE = '/save'; +const CACHE = '/cache'; exports.SYSTEMD_STATE_ACTIVE = 'active'; exports.SYSTEMD_STATE_INACTIVE = 'inactive'; @@ -33,6 +35,9 @@ exports.API_BYPASS = BYPASS; exports.API_BYPASS_BY_ID = BYPASS + '/' + VARIABLE_ID; exports.API_MIDI = MIDI; 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_PEDALBOARDS = 'pedalboards'; diff --git a/libs/modep.js b/libs/modep.js index 987ea2f..420d7f9 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -303,8 +303,8 @@ function getBypassControlFromPedal(pedal) { } async function toggleBypass(pedalId) { - let currentPedalboard = await getCurrentPedalboard(); - let defaultPedalboard = await getDefaultPedalboard(); + const currentPedalboard = await getCurrentPedalboard(); + const defaultPedalboard = await getDefaultPedalboard(); if (currentPedalboard.id === defaultPedalboard.id && pedalboardIdBypassOrigin === undefined) { throw new Error('could not activate bypass, default pedalboard is currently already active'); } @@ -319,10 +319,11 @@ async function toggleBypass(pedalId) { pedalboardIdBypassOrigin = undefined; } await setPedalboardById(pedalboardId); + getPedalboardById + return { status: 'ok', pedalboard: await getPedalboardById(pedalboardId) }; } catch (err) { throw new Error(err); } - return; } const pedal = await getCurrentPedalById(pedalId); 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); } 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 = { @@ -346,6 +386,7 @@ module.exports = { getPedalboardById, getDefaultPedalboard, getCurrentPedalboard, + saveCurrentPedalboard, getCurrentPedals, getCurrentPedalById, getPedalControlById,