update jukebox
This commit is contained in:
parent
e4fc3950b6
commit
e07a605427
@ -7,6 +7,8 @@ import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Base64;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@ -33,6 +35,9 @@ import reactor.core.publisher.Mono;
|
||||
*/
|
||||
@Component
|
||||
public class JukeboxManager implements SmartInitializingSingleton {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(JukeboxManager.class);
|
||||
|
||||
public static final String CLIENT_ID = "jukebox.client_id";
|
||||
public static final String CLIENT_SECRET = "jukebox.client_secret";
|
||||
public static final String DEVICE_ID = "jukebox.device_id";
|
||||
@ -51,26 +56,6 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
private JukeboxConfig config;
|
||||
protected static Gson gson = new Gson();
|
||||
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#
|
||||
* afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#
|
||||
* afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#
|
||||
* afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#
|
||||
* afterSingletonsInstantiated()
|
||||
*/
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#
|
||||
* afterSingletonsInstantiated()
|
||||
*/
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
getConfig();
|
||||
@ -144,6 +129,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
*/
|
||||
protected boolean checkToken() {
|
||||
if (webClient == null || !StringUtils.hasText(config.getRefreshToken())) {
|
||||
logger.info("Cannot check token: no webClient or missing refreshToken");
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -192,6 +178,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
if (!checkToken()) {
|
||||
config.setActive(false);
|
||||
systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive()));
|
||||
logger.debug("active = false due to failed token check");
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -216,34 +203,46 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
JsonObject device = statusObject.getAsJsonObject("device");
|
||||
if (!device.get("id").getAsString().equals(config.getDeviceId())
|
||||
&& StringUtils.hasText(config.getDeviceId())) {
|
||||
MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>();
|
||||
queryParameters.add("device_id", config.getDeviceId());
|
||||
request = webClient.method(HttpMethod.PUT)
|
||||
.uri(uriBuilder -> uriBuilder.path("/v1/me/player/play")
|
||||
.queryParams(queryParameters).build())
|
||||
.header(HttpHeaders.AUTHORIZATION, "Bearer "
|
||||
+ config.getAccessToken());
|
||||
|
||||
request.retrieve().bodyToMono(String.class).block();
|
||||
logger.debug("different device, try to start playback!");
|
||||
tryStartPlayback();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (disable) {
|
||||
config.setActive(false);
|
||||
systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive()));
|
||||
logger.debug("no device, try to start playback!");
|
||||
tryStartPlayback();
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
if (config.isActive()) {
|
||||
config.setActive(false);
|
||||
systemPropertyManager.set(ACTIVE, String.valueOf(config.isActive()));
|
||||
logger.debug("no player body, try to start playback!");
|
||||
tryStartPlayback();
|
||||
}
|
||||
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
|
||||
protected void tryStartPlayback() {
|
||||
MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>();
|
||||
queryParameters.add("device_id", config.getDeviceId());
|
||||
WebClient.RequestBodySpec request = webClient.method(HttpMethod.PUT)
|
||||
.uri(uriBuilder -> uriBuilder.path("/v1/me/player/play")
|
||||
.queryParams(queryParameters).build())
|
||||
.header(HttpHeaders.AUTHORIZATION, "Bearer "
|
||||
+ config.getAccessToken());
|
||||
|
||||
try {
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search track.
|
||||
*
|
||||
@ -253,6 +252,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
*/
|
||||
public JsonElement searchTrack(String query, Long offset) {
|
||||
if (!checkToken()) {
|
||||
logger.debug("discard searchTrack due to failed token check");
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -285,6 +285,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
*/
|
||||
public JsonElement getLast(Long limit) {
|
||||
if (!checkToken()) {
|
||||
logger.debug("discard getLast due to failed token check");
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -315,6 +316,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
*/
|
||||
public void addToQueue(String uri) {
|
||||
if (!checkToken()) {
|
||||
logger.debug("discard addToQueue due to failed token check");
|
||||
return;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user