Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update preset-announcement.js to skip restore of idle players #891

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions lib/helpers/preset-announcement.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
});

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of mutating the array to remove "empty", you can add a .filter(Boolean) after the map() when creating backupPresets.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I don't know how

logger.trace('backup presets', backupPresets);
Expand All @@ -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,
Expand All @@ -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 &&
Expand Down Expand Up @@ -113,4 +122,4 @@ function announcePreset(system, uri, preset, duration) {

}

module.exports = announcePreset;
module.exports = announcePreset;