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