From e562c3b459e79e1e05d7436382ff03e0a0c342b9 Mon Sep 17 00:00:00 2001 From: _Bastler Date: Fri, 21 Jan 2022 13:22:56 +0100 Subject: [PATCH] try fix jukebox --- .../jukebox/businesslogic/JukeboxManager.java | 25 ++++++++++++++++--- .../businesslogic/model/JukeboxConfig.java | 15 +++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/jukebox/src/main/java/de/bstly/we/jukebox/businesslogic/JukeboxManager.java b/jukebox/src/main/java/de/bstly/we/jukebox/businesslogic/JukeboxManager.java index 2a9deea..8cb2917 100644 --- a/jukebox/src/main/java/de/bstly/we/jukebox/businesslogic/JukeboxManager.java +++ b/jukebox/src/main/java/de/bstly/we/jukebox/businesslogic/JukeboxManager.java @@ -49,6 +49,7 @@ public class JukeboxManager implements SmartInitializingSingleton { public static final String ACCESS_TOKEN = "jukebox.access_token"; public static final String REFRESH_TOKEN = "jukebox.refresh_token"; public static final String EXPIRES = "jukebox.expires"; + public static final String FALLBACK_CONTEXT_ID = "jukebox.fallbackContextId"; @Autowired private SystemPropertyManager systemPropertyManager; @@ -87,6 +88,7 @@ public class JukeboxManager implements SmartInitializingSingleton { if (StringUtils.hasText(systemPropertyManager.get(EXPIRES))) { config.setExpires(Instant.parse(systemPropertyManager.get(EXPIRES))); } + config.setFallbackContextId(systemPropertyManager.get(FALLBACK_CONTEXT_ID)); } return config; @@ -110,6 +112,7 @@ public class JukeboxManager implements SmartInitializingSingleton { systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive())); systemPropertyManager.set(ACCESS_TOKEN, config.getAccessToken()); systemPropertyManager.set(REFRESH_TOKEN, config.getRefreshToken()); + systemPropertyManager.set(FALLBACK_CONTEXT_ID, config.getFallbackContextId()); if (config.getExpires() != null) { systemPropertyManager.set(EXPIRES, config.getExpires().toString()); @@ -236,6 +239,10 @@ public class JukeboxManager implements SmartInitializingSingleton { } protected void tryStartPlayback() { + tryStartPlayback(null); + } + + protected void tryStartPlayback(String context_uri) { if (!checkToken()) { config.setActive(false); systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive())); @@ -251,6 +258,9 @@ public class JukeboxManager implements SmartInitializingSingleton { MultiValueMap queryParameters = new LinkedMultiValueMap(); queryParameters.add("device_id", config.getDeviceId()); + if (StringUtils.hasText(context_uri)) { + queryParameters.add("context_uri", context_uri); + } WebClient.RequestBodySpec request = webClient.method(HttpMethod.PUT) .uri(uriBuilder -> uriBuilder.path("/v1/me/player/play") .queryParams(queryParameters).build()) @@ -261,9 +271,16 @@ public class JukeboxManager implements SmartInitializingSingleton { request.retrieve().bodyToMono(String.class).block(); logger.debug("started playback"); } catch (Exception e) { - config.setActive(false); - systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive())); - logger.debug("active = false due to failed start playback"); + if (!StringUtils.hasText(context_uri) + && StringUtils.hasText(config.getFallbackContextId())) { + logger.debug("retry to start playback with: " + + config.getFallbackContextId()); + tryStartPlayback(config.getFallbackContextId()); + } else { + config.setActive(false); + systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive())); + logger.debug("active = false due to failed start playback"); + } } } @@ -350,7 +367,7 @@ public class JukeboxManager implements SmartInitializingSingleton { logger.debug("discard addToQueue due to missing device id"); return; } - + MultiValueMap queryParameters = new LinkedMultiValueMap(); queryParameters.add("uri", uri); queryParameters.add("device_id", config.getDeviceId()); diff --git a/jukebox/src/main/java/de/bstly/we/jukebox/businesslogic/model/JukeboxConfig.java b/jukebox/src/main/java/de/bstly/we/jukebox/businesslogic/model/JukeboxConfig.java index 1aa9b61..d8c3b13 100644 --- a/jukebox/src/main/java/de/bstly/we/jukebox/businesslogic/model/JukeboxConfig.java +++ b/jukebox/src/main/java/de/bstly/we/jukebox/businesslogic/model/JukeboxConfig.java @@ -20,6 +20,7 @@ public class JukeboxConfig { private String accessToken; private String refreshToken; private Instant expires; + private String fallbackContextId; /** * Gets the client id. @@ -201,4 +202,18 @@ public class JukeboxConfig { this.expires = expires; } + /** + * @return the fallbackContextId + */ + public String getFallbackContextId() { + return fallbackContextId; + } + + /** + * @param fallbackContextId the fallbackContextId to set + */ + public void setFallbackContextId(String fallbackContextId) { + this.fallbackContextId = fallbackContextId; + } + }