Skip to content

Commit

Permalink
Merge pull request #234 from ioBroker/tts
Browse files Browse the repository at this point in the history
stores the tts files in files instead of binary states
  • Loading branch information
Apollon77 authored Apr 22, 2024
2 parents 3e616ef + 2ab1047 commit e7b5910
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 51 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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
Expand Down
84 changes: 34 additions & 50 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit e7b5910

Please sign in to comment.