diff --git a/lib/helpers/preset-announcement.js b/lib/helpers/preset-announcement.js index 20fdc696..ff720603 100644 --- a/lib/helpers/preset-announcement.js +++ b/lib/helpers/preset-announcement.js @@ -2,34 +2,44 @@ const logger = require('sonos-discovery/lib/helpers/logger'); const isRadioOrLineIn = require('../helpers/is-radio-or-line-in'); -function saveAll(system) { +function savePresetPlayers(system, pset) { const backupPresets = system.zones.map((zone) => { const coordinator = zone.coordinator; const state = coordinator.state; - const preset = { - players: [ - { roomName: coordinator.roomName, volume: state.volume } - ], - state: state.playbackState, - uri: coordinator.avTransportUri, - metadata: coordinator.avTransportUriMetadata, - playMode: { - repeat: state.playMode.repeat - } - }; + for (var index = 0; index < pset.players.length; ++index) { + if (pset.players[index].roomName == coordinator.roomName) { + const preset = { + players: [ + { roomName: coordinator.roomName, volume: state.volume } + ], + state: state.playbackState, + uri: coordinator.avTransportUri, + metadata: coordinator.avTransportUriMetadata, + playMode: { + repeat: state.playMode.repeat + } + }; + + if (!isRadioOrLineIn(preset.uri)) { + preset.trackNo = state.trackNo; + preset.elapsedTime = state.elapsedTime; + } + + zone.members.forEach(function (player) { + if (coordinator.uuid != player.uuid) { + preset.players.push({ roomName: player.roomName, volume: player.state.volume }); + } + }); - if (!isRadioOrLineIn(preset.uri)) { - preset.trackNo = state.trackNo; - preset.elapsedTime = state.elapsedTime; + return preset; + } } + }); - zone.members.forEach(function (player) { - if (coordinator.uuid != player.uuid) - preset.players.push({ roomName: player.roomName, volume: player.state.volume }); - }); - - return preset; - + Object.keys(backupPresets).forEach(key => { + if (backupPresets[key] === undefined) { + delete backupPresets[key]; + } }); logger.trace('backup presets', backupPresets); @@ -41,9 +51,6 @@ function saveAll(system) { function announcePreset(system, uri, preset, duration) { let abortTimer; - // Save all players - var backupPresets = saveAll(system); - const simplifiedPreset = { uri, players: preset.players, @@ -52,6 +59,8 @@ function announcePreset(system, uri, preset, duration) { state: 'STOPPED' }; + var backupPresets = savePresetPlayers(system,preset); + function hasReachedCorrectTopology(zones) { return zones.some(group => group.members.length === preset.players.length && @@ -113,4 +122,4 @@ function announcePreset(system, uri, preset, duration) { } -module.exports = announcePreset; \ No newline at end of file +module.exports = announcePreset;