From 3e9a6f51bfd2ff2e53e9783757a818519ee57d64 Mon Sep 17 00:00:00 2001 From: Phil Tsaryk Date: Mon, 8 Jan 2024 14:14:59 +0100 Subject: [PATCH 1/5] fix(spotify): fix unset volatile callback --- spotify/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spotify/index.js b/spotify/index.js index 5f7ed5862..858bdf045 100644 --- a/spotify/index.js +++ b/spotify/index.js @@ -349,7 +349,7 @@ ControllerSpotify.prototype.initializeSpotifyPlaybackInVolatileMode = function ( self.commandRouter.stateMachine.setConsumeUpdateService(undefined); self.context.coreCommand.stateMachine.setVolatile({ service: 'spop', - callback: self.libRespotGoUnsetVolatile() + callback: self.libRespotGoUnsetVolatile.bind(this) }); setTimeout(()=>{ From 4e5d962a7fd0045388cc0767f0c5564d52575889 Mon Sep 17 00:00:00 2001 From: Phil Tsaryk Date: Wed, 10 Jan 2024 22:20:37 +0100 Subject: [PATCH 2/5] refactor(spotify): remove redundant defer --- spotify/index.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/spotify/index.js b/spotify/index.js index 858bdf045..6b1e49140 100644 --- a/spotify/index.js +++ b/spotify/index.js @@ -393,20 +393,13 @@ ControllerSpotify.prototype.parseArtists = function (spotifyArtists) { ControllerSpotify.prototype.libRespotGoUnsetVolatile = function () { - var self = this; - var defer = libQ.defer(); - - self.debugLog('UNSET VOLATILE'); - self.debugLog(JSON.stringify(currentVolumioState)) - + this.debugLog('UNSET VOLATILE'); + this.debugLog(JSON.stringify(currentVolumioState)) if (currentVolumioState && currentVolumioState.status && currentVolumioState.status !== 'stop') { - self.logger.info('Setting Spotify stop after unset volatile call'); + this.logger.info('Setting Spotify stop after unset volatile call'); setTimeout(()=>{ - self.stop(); - defer.resolve(''); + this.stop(); }, 500); - } else { - defer.resolve(''); } } From 20e4e39801f8a637545b73e8d0fbed1ab7e17e00 Mon Sep 17 00:00:00 2001 From: Phil Tsaryk Date: Thu, 11 Jan 2024 09:05:17 +0100 Subject: [PATCH 3/5] refactor(spotify): remove redundant ignoreStopEvent --- spotify/index.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/spotify/index.js b/spotify/index.js index 6b1e49140..5a1d9073b 100644 --- a/spotify/index.js +++ b/spotify/index.js @@ -35,7 +35,6 @@ var currentVolumioState; var currentSpotifyVolume; var currentVolumioVolume; var isInVolatileMode = false; -var ignoreStopEvent = false; // Volume limiter var deltaVolumeTreshold = 2; @@ -344,17 +343,12 @@ ControllerSpotify.prototype.initializeSpotifyPlaybackInVolatileMode = function ( var self = this; self.logger.info('Spotify is playing in volatile mode'); - ignoreStopEvent = true; self.commandRouter.stateMachine.setConsumeUpdateService(undefined); self.context.coreCommand.stateMachine.setVolatile({ service: 'spop', callback: self.libRespotGoUnsetVolatile.bind(this) }); - - setTimeout(()=>{ - ignoreStopEvent = false; - }, 2000); }; ControllerSpotify.prototype.parseDuration = function (spotifyDuration) { @@ -471,9 +465,7 @@ ControllerSpotify.prototype.stop = function () { this.debugLog('SPOTIFY STOP'); this.debugLog(JSON.stringify(currentVolumioState)) - if (!ignoreStopEvent) { - this.sendSpotifyLocalApiCommand('/player/pause'); - } + this.sendSpotifyLocalApiCommand('/player/pause'); defer.resolve(''); return defer.promise; From 91471f0de01bf4629af07b52f336cb90a4e5f2f5 Mon Sep 17 00:00:00 2001 From: Phil Tsaryk Date: Thu, 11 Jan 2024 09:14:58 +0100 Subject: [PATCH 4/5] fix(spotify): ignore all play/pause events from spotify during unsetting volatile --- spotify/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spotify/index.js b/spotify/index.js index 5a1d9073b..766c38525 100644 --- a/spotify/index.js +++ b/spotify/index.js @@ -35,6 +35,8 @@ var currentVolumioState; var currentSpotifyVolume; var currentVolumioVolume; var isInVolatileMode = false; +let unsettingVolatile = false; +const UNSETTING_VOLATILE_TIMEOUT = 10000; // Volume limiter var deltaVolumeTreshold = 2; @@ -320,6 +322,11 @@ ControllerSpotify.prototype.parseEventState = function (event) { }; ControllerSpotify.prototype.identifyPlaybackMode = function (data) { + if (unsettingVolatile) { + // Ignore all unnecessary events (several "pause" events) from spotify during + // switching from volatile mode to prevent volumio from switching back to volatile mode + return; + } var self = this; // This functions checks if Spotify is playing in volatile mode or in Volumio mode (playback started from Volumio UI) @@ -336,7 +343,6 @@ ControllerSpotify.prototype.identifyPlaybackMode = function (data) { (isInVolatileMode && currentVolumioState.service === 'spop' && currentVolumioState.volatile !== true)) { self.initializeSpotifyPlaybackInVolatileMode(); } - }; ControllerSpotify.prototype.initializeSpotifyPlaybackInVolatileMode = function () { @@ -389,6 +395,10 @@ ControllerSpotify.prototype.parseArtists = function (spotifyArtists) { ControllerSpotify.prototype.libRespotGoUnsetVolatile = function () { this.debugLog('UNSET VOLATILE'); this.debugLog(JSON.stringify(currentVolumioState)) + unsettingVolatile = true; + setTimeout(() => { + unsettingVolatile = false; + }, UNSETTING_VOLATILE_TIMEOUT); if (currentVolumioState && currentVolumioState.status && currentVolumioState.status !== 'stop') { this.logger.info('Setting Spotify stop after unset volatile call'); setTimeout(()=>{ From d50e0a454fc5b0b0131082ad34ee71ad414c38c3 Mon Sep 17 00:00:00 2001 From: Phil Tsaryk Date: Thu, 11 Jan 2024 09:18:35 +0100 Subject: [PATCH 5/5] refactor(spotify): refactor global variable isInVolatileMode --- spotify/index.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/spotify/index.js b/spotify/index.js index 766c38525..ee9abb443 100644 --- a/spotify/index.js +++ b/spotify/index.js @@ -34,7 +34,6 @@ var ws; var currentVolumioState; var currentSpotifyVolume; var currentVolumioVolume; -var isInVolatileMode = false; let unsettingVolatile = false; const UNSETTING_VOLATILE_TIMEOUT = 10000; @@ -327,21 +326,16 @@ ControllerSpotify.prototype.identifyPlaybackMode = function (data) { // switching from volatile mode to prevent volumio from switching back to volatile mode return; } - var self = this; // This functions checks if Spotify is playing in volatile mode or in Volumio mode (playback started from Volumio UI) // play_origin = 'go-librespot' means that Spotify is playing in Volumio mode // play_origin = 'your_library' or 'playlist' means that Spotify is playing in volatile mode - if (data && data.play_origin && data.play_origin === 'go-librespot') { - isInVolatileMode = false; - } else { - isInVolatileMode = true; - } + const isVolumioMode = data && data.play_origin && data.play_origin === 'go-librespot'; // Refactor in order to handle the case where current service is spop but not in volatile mode - if ((isInVolatileMode && currentVolumioState.service !== 'spop') || - (isInVolatileMode && currentVolumioState.service === 'spop' && currentVolumioState.volatile !== true)) { - self.initializeSpotifyPlaybackInVolatileMode(); + if ((!isVolumioMode && currentVolumioState.service !== 'spop') || + (!isVolumioMode && currentVolumioState.service === 'spop' && currentVolumioState.volatile !== true)) { + this.initializeSpotifyPlaybackInVolatileMode(); } };