try fix jukebox

This commit is contained in:
_Bastler 2022-01-21 13:22:56 +01:00
parent 447c137e4c
commit e562c3b459
2 changed files with 36 additions and 4 deletions

View File

@ -49,6 +49,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
public static final String ACCESS_TOKEN = "jukebox.access_token"; public static final String ACCESS_TOKEN = "jukebox.access_token";
public static final String REFRESH_TOKEN = "jukebox.refresh_token"; public static final String REFRESH_TOKEN = "jukebox.refresh_token";
public static final String EXPIRES = "jukebox.expires"; public static final String EXPIRES = "jukebox.expires";
public static final String FALLBACK_CONTEXT_ID = "jukebox.fallbackContextId";
@Autowired @Autowired
private SystemPropertyManager systemPropertyManager; private SystemPropertyManager systemPropertyManager;
@ -87,6 +88,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
if (StringUtils.hasText(systemPropertyManager.get(EXPIRES))) { if (StringUtils.hasText(systemPropertyManager.get(EXPIRES))) {
config.setExpires(Instant.parse(systemPropertyManager.get(EXPIRES))); config.setExpires(Instant.parse(systemPropertyManager.get(EXPIRES)));
} }
config.setFallbackContextId(systemPropertyManager.get(FALLBACK_CONTEXT_ID));
} }
return config; return config;
@ -110,6 +112,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive())); systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive()));
systemPropertyManager.set(ACCESS_TOKEN, config.getAccessToken()); systemPropertyManager.set(ACCESS_TOKEN, config.getAccessToken());
systemPropertyManager.set(REFRESH_TOKEN, config.getRefreshToken()); systemPropertyManager.set(REFRESH_TOKEN, config.getRefreshToken());
systemPropertyManager.set(FALLBACK_CONTEXT_ID, config.getFallbackContextId());
if (config.getExpires() != null) { if (config.getExpires() != null) {
systemPropertyManager.set(EXPIRES, config.getExpires().toString()); systemPropertyManager.set(EXPIRES, config.getExpires().toString());
@ -236,6 +239,10 @@ public class JukeboxManager implements SmartInitializingSingleton {
} }
protected void tryStartPlayback() { protected void tryStartPlayback() {
tryStartPlayback(null);
}
protected void tryStartPlayback(String context_uri) {
if (!checkToken()) { if (!checkToken()) {
config.setActive(false); config.setActive(false);
systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive())); systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive()));
@ -251,6 +258,9 @@ public class JukeboxManager implements SmartInitializingSingleton {
MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>();
queryParameters.add("device_id", config.getDeviceId()); queryParameters.add("device_id", config.getDeviceId());
if (StringUtils.hasText(context_uri)) {
queryParameters.add("context_uri", context_uri);
}
WebClient.RequestBodySpec request = webClient.method(HttpMethod.PUT) WebClient.RequestBodySpec request = webClient.method(HttpMethod.PUT)
.uri(uriBuilder -> uriBuilder.path("/v1/me/player/play") .uri(uriBuilder -> uriBuilder.path("/v1/me/player/play")
.queryParams(queryParameters).build()) .queryParams(queryParameters).build())
@ -261,11 +271,18 @@ public class JukeboxManager implements SmartInitializingSingleton {
request.retrieve().bodyToMono(String.class).block(); request.retrieve().bodyToMono(String.class).block();
logger.debug("started playback"); logger.debug("started playback");
} catch (Exception e) { } catch (Exception e) {
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); config.setActive(false);
systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive())); systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive()));
logger.debug("active = false due to failed start playback"); logger.debug("active = false due to failed start playback");
} }
} }
}
/** /**
* Search track. * Search track.

View File

@ -20,6 +20,7 @@ public class JukeboxConfig {
private String accessToken; private String accessToken;
private String refreshToken; private String refreshToken;
private Instant expires; private Instant expires;
private String fallbackContextId;
/** /**
* Gets the client id. * Gets the client id.
@ -201,4 +202,18 @@ public class JukeboxConfig {
this.expires = expires; 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;
}
} }