diff --git a/.vscode/launch.json b/.vscode/launch.json index 6dd13ce..3451e71 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "skipFiles": [ "/**" ], - "program": "${workspaceFolder}/api.js" + "program": "${workspaceFolder}/pbc.js" } ] } \ No newline at end of file diff --git a/libs/modep.js b/libs/modep.js index ff003c7..aafe064 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -27,15 +27,22 @@ function getBanks() { return resolve(banks); } // FAKE DATA - var fake = [{ "title": "The Button", "pedalboards": [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }] }]; - for (var index = 0; index < fake.length; index++) { - fake.id = index; - } - banks = util.sortById(fake); - return resolve(fake); + // var fake = [{ "title": "The Button", "pedalboards": [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }] }]; + // for (var index = 0; index < fake.length; index++) { + // fake.id = index; + // } + // banks = util.sortById(fake); + // return resolve(fake); util.httpGET(config.modep.host, config.modep.port, '/banks') - .then(resolve) + .then(function (banks) { + for (var index = 0; index < banks.length; index++) { + var bank = banks[index]; + bank.id = index; + } + banks = util.sortById(banks); + return resolve(banks); + }) .catch(reject); }); } @@ -46,24 +53,36 @@ function getPedalboards() { return resolve(pedalboards); } // FAKE DATA - var fake = [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }]; - var id = 1; - for (var index = 0; index < fake.length; index++) { - var pedalboard = fake[index]; - if (pedalboard.bundle == constants.PEDALBOARD_DEFAULT) { - pedalboard.id = 0; - defaultPedalboard = pedalboard; - continue; - } - pedalboard.id = id; - id++; - } - pedalboards = util.sortById(fake); - return resolve(fake); + // var fake = [{ "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/FUZZ.pedalboard/FUZZ.ttl", "bundle": "/var/modep/pedalboards/FUZZ.pedalboard", "title": "FUZZ", "version": 1 }, { "valid": true, "broken": false, "uri": "file:///var/modep/pedalboards/default.pedalboard/default.ttl", "bundle": "/var/modep/pedalboards/default.pedalboard", "title": "Default", "version": 0 }]; + // var id = 1; + // for (var index = 0; index < fake.length; index++) { + // var pedalboard = fake[index]; + // if (pedalboard.bundle == constants.PEDALBOARD_DEFAULT) { + // pedalboard.id = 0; + // defaultPedalboard = pedalboard; + // continue; + // } + // pedalboard.id = id; + // id++; + // } + // pedalboards = util.sortById(fake); + // return resolve(fake); util.httpGET(config.modep.host, config.modep.port, '/pedalboard/list') - .then(function (pedalboards) { - + .then(function (data) { + var id = 1; + for (var index = 0; index < data.length; index++) { + var pedalboard = data[index]; + if (pedalboard.bundle == constants.PEDALBOARD_DEFAULT) { + pedalboard.id = 0; + defaultPedalboard = pedalboard; + continue; + } + pedalboard.id = id; + id++; + } + pedalboards = util.sortById(data); + return resolve(pedalboards); }) .catch(reject); }); @@ -86,23 +105,24 @@ function getCurrentPedalboard() { return resolve(currentPedalboard); } // FAKE DATA - var fake = '/var/modep/pedalboards/FUZZ.pedalboard'; - getPedalboardByBundle(fake) - .then(function (pedalboard) { - currentPedalboard = pedalboard; - return resolve(pedalboard); - }) - .catch(reject); + // var fake = '/var/modep/pedalboards/FUZZ.pedalboard'; + // getPedalboardByBundle(fake) + // .then(function (pedalboard) { + // currentPedalboard = pedalboard; + // return resolve(pedalboard); + // }) + // .catch(reject); // PRODUCTION - // util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current') - // .then(getPedalboardByBundle) - // .then(resolve) - // .catch(reject); + util.httpGET(config.modep.host, config.modep.port, '/pedalboard/current') + .then(getPedalboardByBundle) + .then(resolve) + .catch(reject); }); } -function getPedalboardById(pedalboardId) { +function +getPedalboardById(pedalboardId) { return new Promise(function (resolve, reject) { getPedalboards() .then(function (pedalboards) { @@ -122,6 +142,11 @@ function getPedalboardByBundle(pedalboardBundle) { return new Promise(function (resolve, reject) { getPedalboards() .then(function (pedalboards) { + if (!pedalboardBundle) { + return getDefaultPedalboard() + .then(resolve) + .catch(reject); + } for (var index = 0; index < pedalboards.length; index++) { if (pedalboards[index].bundle != pedalboardBundle) { continue; @@ -153,7 +178,7 @@ function getPedalControlById(pedalId, controlId) { function getCurrentPedalById(id) { return new Promise(function (resolve, reject) { getCurrentPedals() - .then(function () { + .then(function (currentPedals) { for (var index = 0; index < currentPedals.length; index++) { if (currentPedals[index].id != id) { continue; @@ -178,79 +203,75 @@ function getCurrentPedals() { }); } -function parseCurrentPedalboard(pedalboardBundle) { +function parseCurrentPedalboard(currentPedalboard) { return new Promise(function (resolve, reject) { - var startTime = new Date(); - logger.debug('parsing current pedalboard...'); - if (!currentPedalboard || !currentPedalboard.uri) { - for (var index = 0; index < pedalboards.length; index++) { - var pedalboard = pedalboards[index]; - if (!pedalboard.bundle || pedalboardBundle != pedalboard.bundle) { - continue; + getDefaultPedalboard() + .then(function (defaultPedalboard) { + if (defaultPedalboard.id == currentPedalboard.id) { + return resolve([]); } - currentPedalboard = pedalboard; - break; - }; - } - if (!currentPedalboard.uri) { - reject('error: could not determine current pedalboard config file'); - } - // FAKE DATA - var file = path.resolve('./dev/FUZZ.ttl'); - // var file = path.resolve(currentPedalboard.uri.replace('file://', '')); - fs.readFile(file, function (err, data) { - if (err) { - return reject('error: could not parse current pedalboard file \'' + file + '\' >>> ' + err); - } - currentPedals = []; - var json = ttl2jsonld(data.toString())['@graph']; - var id = 0; - for (var index = 0; index < json.length; index++) { - var tmp = json[index]; - if (!tmp['lv2:prototype']) { - continue; + var startTime = new Date(); + logger.debug('parsing current pedalboard...'); + if (!currentPedalboard.uri) { + reject('error: could not determine current pedalboard config file'); } - var name = tmp['@id']; - currentPedals.push({ id: id, name: name, controls: [] }); - id++; - } - for (var index = 0; index < json.length; index++) { - var tmp = json[index]; - var name = tmp['@id']; - var value = tmp['ingen:value']; - if (value == undefined) { - continue; - } - var pedal = undefined; - for (var pedalIndex = 0; pedalIndex < currentPedals.length; pedalIndex++) { - if (!name.startsWith(currentPedals[pedalIndex].name)) { - continue; + // FAKE DATA + var file = path.resolve('./dev/FUZZ.ttl'); + // var file = path.resolve(currentPedalboard.uri.replace('file://', '')); + fs.readFile(file, function (err, data) { + if (err) { + return reject('error: could not parse current pedalboard file \'' + file + '\' >>> ' + err); } - pedal = currentPedals[pedalIndex]; - break; - } - if (!pedal) { - continue; - } - id = pedal.controls.length; - name = name.replace(pedal.name + '/', ''); - if (name == ':bypass') { - value = value; - } else { - value = value['@value']; - } - var control = { id: id, name: name, value: value }; - pedal.controls.push(control); - id++; - var midi = tmp['midi:binding']; - if (!midi) { - 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); - }); + currentPedals = []; + var json = ttl2jsonld(data.toString())['@graph']; + var id = 0; + for (var index = 0; index < json.length; index++) { + var tmp = json[index]; + if (!tmp['lv2:prototype']) { + continue; + } + var name = tmp['@id']; + currentPedals.push({ id: id, name: name, controls: [] }); + id++; + } + for (var index = 0; index < json.length; index++) { + var tmp = json[index]; + var name = tmp['@id']; + var value = tmp['ingen:value']; + if (value == undefined) { + continue; + } + var pedal = undefined; + for (var pedalIndex = 0; pedalIndex < currentPedals.length; pedalIndex++) { + if (!name.startsWith(currentPedals[pedalIndex].name)) { + continue; + } + pedal = currentPedals[pedalIndex]; + break; + } + if (!pedal) { + continue; + } + id = pedal.controls.length; + name = name.replace(pedal.name + '/', ''); + if (name == ':bypass') { + value = value; + } else { + value = value['@value']; + } + var control = { id: id, name: name, value: value }; + pedal.controls.push(control); + id++; + var midi = tmp['midi:binding']; + if (!midi) { + 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); + }); + }).catch(reject); }); } @@ -306,12 +327,19 @@ function setPedalboard(pedalboard) { if (pedalboard.id == currentPedalboard.id) { return resolve('pedalboard \'' + pedalboard.id + '\' is already active'); } + logger.debug("DERP") + reset() - .then(function () { - util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle) - }) - .then(getCurrentPedalboard) - .catch(reject); + .then(logger.debug("RESETTED")) + .catch(logger.error); + + + // reset() + // .then(function () { + // util.httpPOST(config.modep.host, config.modep.port, '/pedalboard/load_bundle/?bundlepath=' + pedalboard.bundle) + // }) + // .then(getCurrentPedalboard) + // .catch(reject); }); } diff --git a/libs/util.js b/libs/util.js index 5882445..8d47414 100644 --- a/libs/util.js +++ b/libs/util.js @@ -47,7 +47,10 @@ function httpRequest(host, port, path, method, args) { }); response.on('end', function () { logger.debug('http \'' + method + '\' request \'' + host + ':' + port + path + '\' returned data \'' + responseData + '\''); - return resolve(responseData); + if (!responseData) { + return resolve(); + } + return resolve(JSON.parse(responseData)); }); }); request.on('error', function (err) {