update jukebox

This commit is contained in:
_Bastler 2022-01-02 13:09:00 +01:00
parent 9022b59408
commit d597700258
4 changed files with 71 additions and 1 deletions

View File

@ -18,6 +18,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.reactive.function.client.WebClient;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
@ -34,6 +35,7 @@ import reactor.core.publisher.Mono;
public class JukeboxManager implements SmartInitializingSingleton {
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";
public static final String REDIRECT_URL = "jukebox.redirect_url";
public static final String ACCOUNT_URL = "jukebox.account_url";
public static final String API_URL = "jukebox.api_url";
@ -47,6 +49,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
protected WebClient webClient;
private JukeboxConfig config;
protected static Gson gson = new Gson();
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#
@ -56,6 +59,10 @@ public class JukeboxManager implements SmartInitializingSingleton {
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()
*/
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#
* afterSingletonsInstantiated()
*/
/*
* @see org.springframework.beans.factory.SmartInitializingSingleton#afterSingletonsInstantiated()
*/
@ -83,6 +90,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
config = new JukeboxConfig();
config.setClientId(systemPropertyManager.get(CLIENT_ID));
config.setClientSecret(systemPropertyManager.get(CLIENT_SECRET));
config.setDeviceId(systemPropertyManager.get(DEVICE_ID));
config.setRedirectUrl(systemPropertyManager.get(REDIRECT_URL));
config.setApiUrl(systemPropertyManager.get(API_URL));
config.setAccountUrl(systemPropertyManager.get(ACCOUNT_URL));
@ -108,6 +116,7 @@ public class JukeboxManager implements SmartInitializingSingleton {
systemPropertyManager.set(CLIENT_ID, config.getClientId());
systemPropertyManager.set(CLIENT_SECRET, config.getClientSecret());
systemPropertyManager.set(DEVICE_ID, config.getDeviceId());
systemPropertyManager.set(REDIRECT_URL, config.getRedirectUrl());
systemPropertyManager.set(API_URL, config.getApiUrl());
systemPropertyManager.set(ACCOUNT_URL, config.getAccountUrl());
@ -202,6 +211,19 @@ public class JukeboxManager implements SmartInitializingSingleton {
JsonObject statusObject = status.getAsJsonObject();
if (!statusObject.has("device")) {
disable = true;
} else {
JsonObject device = statusObject.getAsJsonObject("device");
if (!device.get("id").getAsString().equals(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();
}
}
}
if (disable) {

View File

@ -12,6 +12,7 @@ public class JukeboxConfig {
private String clientId;
private String clientSecret;
private String deviceId;
private String redirectUrl;
private String apiUrl;
private String accountUrl;
@ -56,6 +57,24 @@ public class JukeboxConfig {
this.clientSecret = clientSecret;
}
/**
* Gets the device id.
*
* @return the device id
*/
public String getDeviceId() {
return deviceId;
}
/**
* Sets the device id.
*
* @param deviceId the new device id
*/
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
/**
* Gets the redirect url.
*

View File

@ -23,7 +23,9 @@ import org.springframework.web.bind.annotation.RestController;
import com.beust.jcommander.internal.Maps;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import de.bstly.we.businesslogic.PermissionManager;
import de.bstly.we.controller.BaseController;
@ -140,6 +142,33 @@ public class JukeboxController extends BaseController {
queueList.put(getCurrentUserId(), Instant.now());
}
/**
* Current.
*
* @param response the response
* @throws JsonIOException the json IO exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/current")
public void current(HttpServletResponse response) throws JsonIOException, IOException {
checkSearchPermission();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
JsonElement status = jukeboxManager.getStatus();
if (status != null) {
JsonObject statusObject = status.getAsJsonObject();
if (statusObject.has("item")) {
gson.toJson(statusObject.getAsJsonObject("item"), response.getWriter());
return;
}
}
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
/**
* Last.
*

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.7-SNAPSHOT</revision>
<revision>1.4.8-SNAPSHOT</revision>
</properties>
<parent>