diff --git a/README.md b/README.md index bd70fdc..ac155e8 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ Please note: highlighting current playing favorite is not supported. ### __WORK IN PROGRESS__ * (seb2010) Added support for treble and bass information +* (Apollon77) stores the tts files in files instead of binary states ### 3.0.0 (2023-10-09) * (udondan) Added support for the playing Sonos playlists (added new state `playlist_set`) @@ -124,7 +125,7 @@ Please note: highlighting current playing favorite is not supported. * (foxriver76) fixed cover url ### 2.3.2 (2023-09-20) -* (foxriver76) store the cover file in files instead of binary states +* (foxriver76) stores the cover file in files instead of binary states ### 2.3.1 (2023-03-22) * (Apollon77) Prepare for future js-controller versions diff --git a/main.js b/main.js index f387add..38b0eba 100644 --- a/main.js +++ b/main.js @@ -918,7 +918,7 @@ function browse(callback) { let currentFileNum = 0; -function text2speech(fileName, sonosIp, callback) { +async function text2speech(fileName, sonosIp, callback) { // Extract volume let volume = null; @@ -966,57 +966,40 @@ function text2speech(fileName, sonosIp, callback) { // Upload this file to objects DB try { const data = fs.readFileSync(fileName); - const id = `${adapter.namespace}.TTS.${dest}`; + const id = `/TTS/${adapter.namespace}/${dest}`; - adapter.setForeignObject( - id, - { - common: { - type: 'file', - name: fileName, - read: true, - write: false - }, - native: {}, - type: 'state' - }, - () => { - adapter.setBinaryState(id, data, err => { - if (err) { - adapter.log.error(`Cannot upload ${id}: ${err}`); - callback && callback(err); - } else { - adapter.getForeignObject(adapter.config.webServer, (err, obj) => { - if (obj && obj.native && discovery) { - fileName = `http${obj.native.secure ? 's' : ''}://${discovery.localEndpoint}:${ - obj.native.port - }/state/${id}`; - if (sonosIp) { - sonosIp = sonosIp.replace(/[.\s]+/g, '_'); - } - - // Play on all players - for (let i = 0; i < discovery.players.length; i++) { - if (!discovery.players[i]._address) { - discovery.players[i]._address = getIp(discovery.players[i]); - } + try { + await adapter.writeFile(adapter.name, id, data); + const obj = await adapter.getForeignObject(adapter.config.webServer); + if (obj && obj.native && discovery) { + fileName = `http${obj.native.secure ? 's' : ''}://${discovery.localEndpoint}:${ + obj.native.port + }/files/${adapter.name}${id}`; + if (sonosIp) { + sonosIp = sonosIp.replace(/[.\s]+/g, '_'); + } - const ip = discovery.players[i]._address; + // Play on all players + for (let i = 0; i < discovery.players.length; i++) { + if (!discovery.players[i]._address) { + discovery.players[i]._address = getIp(discovery.players[i]); + } - if (sonosIp && ip !== sonosIp) { - continue; - } + const ip = discovery.players[i]._address; - setTimeout(() => playOnSonos(fileName, discovery.players[i].uuid, volume), 100); - } - } - - callback && callback(); - }); + if (sonosIp && ip !== sonosIp) { + continue; } - }); + + setTimeout(() => playOnSonos(fileName, discovery.players[i].uuid, volume), 100); + } } - ); + } catch (err) { + adapter.log.error(`Cannot upload ${id}: ${err}`); + return callback && callback(err); + } + + callback && callback(); } catch (e) { adapter.log.error(e); callback && callback(e); @@ -1808,10 +1791,10 @@ function processSonosEvents(event, data) { } } else if (event === 'treble') { // node-sonos-discovery is not emitting any events on treble changes yet, so it is not - // possible to get the externally set treble value, yet. + // possible to get the externally set treble value, yet. } else if (event === 'bass') { // node-sonos-discovery is not emitting any events on bass changes yet, so it is not - // possible to get the externally set bass value, yet. + // possible to get the externally set bass value, yet. } else if (event === 'mute') { // { // uuid: _this.uuid, @@ -2191,9 +2174,10 @@ function main() { * Clear legacy binary states, as we migrated to files */ async function clearLegacyBinaryStates() { - const states = await adapter.getStatesAsync('*.cover_png'); + const coverStates = await adapter.getStatesAsync('*.cover_png'); + const ttsStates = await adapter.getStatesAsync('TTS.tts*'); - for (const id of Object.keys(states)) { + for (const id of [...Object.keys(coverStates), ...Object.keys(ttsStates)]) { await adapter.delObjectAsync(id); } }