jukebox fix

This commit is contained in:
_Bastler 2022-01-21 12:54:36 +01:00
parent c4a0174c25
commit 2eec3627d2
2 changed files with 29 additions and 8 deletions

View File

@ -21,6 +21,7 @@ import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.client.WebClient;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
@ -203,8 +204,20 @@ public class JukeboxManager implements SmartInitializingSingleton {
JsonObject device = statusObject.getAsJsonObject("device");
if (!device.get("id").getAsString().equals(config.getDeviceId())
&& StringUtils.hasText(config.getDeviceId())) {
logger.debug("different device, try to start playback!");
tryStartPlayback();
logger.debug("different device, try to transfer playback!");
MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>();
JsonArray device_ids = new JsonArray();
device_ids.add(config.getDeviceId());
queryParameters.add("device_ids", device_ids.toString());
queryParameters.add("play", "true");
request = webClient.method(HttpMethod.PUT)
.uri(uriBuilder -> uriBuilder.path("/v1/me/player")
.queryParams(queryParameters).build())
.header(HttpHeaders.AUTHORIZATION, "Bearer "
+ config.getAccessToken());
request.retrieve().bodyToMono(String.class).block();
return getStatus();
}
}
}
@ -216,15 +229,23 @@ public class JukeboxManager implements SmartInitializingSingleton {
return status;
}
if (config.isActive()) {
logger.debug("no player body, try to start playback!");
tryStartPlayback();
}
return JsonNull.INSTANCE;
}
protected void tryStartPlayback() {
if (!checkToken()) {
config.setActive(false);
systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive()));
logger.debug("discard tryStartPlayback due to failed token check");
return;
}
if (!StringUtils.hasText(config.getDeviceId())) {
config.setActive(false);
systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive()));
logger.debug("discard tryStartPlayback due to missing device id");
}
MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>();
queryParameters.add("device_id", config.getDeviceId());
WebClient.RequestBodySpec request = webClient.method(HttpMethod.PUT)

View File

@ -13,7 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version>
<log4j2.version>2.17.1</log4j2.version>
<revision>1.4.9-SNAPSHOT</revision>
<revision>1.5.0-SNAPSHOT</revision>
</properties>
<parent>