update jukebox to play if not running
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
package de.bstly.we.jukebox.businesslogic;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Base64;
|
||||
@@ -11,6 +12,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
@@ -58,6 +60,13 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
private JukeboxConfig config;
|
||||
protected static Gson gson = new Gson();
|
||||
|
||||
@Value("${we.bstly.jukebox.daemon.retries:5}")
|
||||
private int daemonRetries;
|
||||
|
||||
/*
|
||||
* @see org.springframework.beans.factory.SmartInitializingSingleton#
|
||||
* afterSingletonsInstantiated()
|
||||
*/
|
||||
@Override
|
||||
public void afterSingletonsInstantiated() {
|
||||
getConfig();
|
||||
@@ -239,10 +248,18 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
return JsonNull.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try start playback.
|
||||
*/
|
||||
protected void tryStartPlayback() {
|
||||
tryStartPlayback(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try start playback.
|
||||
*
|
||||
* @param context_uri the context uri
|
||||
*/
|
||||
protected void tryStartPlayback(String context_uri) {
|
||||
if (!checkToken()) {
|
||||
config.setActive(false);
|
||||
@@ -371,6 +388,8 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
|
||||
JsonElement status = getStatus();
|
||||
|
||||
boolean queue = true;
|
||||
|
||||
// start playing if not running
|
||||
if (status == null || !status.isJsonObject()) {
|
||||
MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>();
|
||||
@@ -382,13 +401,19 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
+ config.getAccessToken());
|
||||
|
||||
JsonObject body = new JsonObject();
|
||||
JsonArray uris = new JsonArray();
|
||||
uris.add(uri);
|
||||
body.add("uris", uris);
|
||||
if (StringUtils.hasText(config.getFallbackContextId())) {
|
||||
body.addProperty("context_uri", config.getFallbackContextId());
|
||||
} else {
|
||||
JsonArray uris = new JsonArray();
|
||||
uris.add(uri);
|
||||
body.add("uris", uris);
|
||||
queue = false;
|
||||
}
|
||||
request.bodyValue(body.toString());
|
||||
|
||||
request.retrieve().bodyToMono(String.class).block();
|
||||
} else {
|
||||
}
|
||||
|
||||
if (queue) {
|
||||
MultiValueMap<String, String> queryParameters = new LinkedMultiValueMap<String, String>();
|
||||
queryParameters.add("uri", uri);
|
||||
queryParameters.add("device_id", config.getDeviceId());
|
||||
@@ -412,4 +437,88 @@ public class JukeboxManager implements SmartInitializingSingleton {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check devices.
|
||||
*/
|
||||
public void checkDevices() {
|
||||
checkDevices(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check devices.
|
||||
*
|
||||
* @param tries the tries
|
||||
*/
|
||||
public void checkDevices(int tries) {
|
||||
if (tries > daemonRetries) {
|
||||
logger.warn("Could not setup spotifyd device!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!checkToken()) {
|
||||
logger.warn("Failed token check!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!StringUtils.hasText(config.getDeviceId())) {
|
||||
logger.info("No device ID set.");
|
||||
return;
|
||||
}
|
||||
|
||||
WebClient.RequestBodySpec request = webClient.method(HttpMethod.GET)
|
||||
.uri(uriBuilder -> uriBuilder.path("/v1/me/player/devices").build())
|
||||
.header(HttpHeaders.AUTHORIZATION, "Bearer "
|
||||
+ config.getAccessToken());
|
||||
|
||||
boolean deviceFound = false;
|
||||
|
||||
String jsonString = request.retrieve().bodyToMono(String.class).block();
|
||||
|
||||
if (StringUtils.hasText(jsonString)) {
|
||||
JsonElement response = JsonParser.parseString(jsonString);
|
||||
|
||||
if (response != null && !response.isJsonNull() && response.isJsonObject()) {
|
||||
JsonArray devices = response.getAsJsonObject().getAsJsonArray("devices");
|
||||
for (JsonElement deviceElement : devices) {
|
||||
JsonObject device = deviceElement.getAsJsonObject();
|
||||
if (device.has("id")
|
||||
&& device.get("id").getAsString().equals(config.getDeviceId())) {
|
||||
deviceFound = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!deviceFound) {
|
||||
String command = "sudo /bin/systemctl restart spotifyd.service";
|
||||
try {
|
||||
Runtime.getRuntime().exec(command);
|
||||
} catch (IOException e) {
|
||||
logger.warn("Could execute command: "
|
||||
+ command, e);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
tries++;
|
||||
Thread.sleep(5000 * tries);
|
||||
checkDevices(tries);
|
||||
} catch (InterruptedException e) {
|
||||
logger.warn("Could not sleep!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check spotify D.
|
||||
*/
|
||||
@Scheduled(cron = "${we.bstly.jukebox.daemon.cron:0 */15 * * * * }")
|
||||
public void checkSpotifyD() {
|
||||
if (config.isActive()) {
|
||||
checkDevices();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -203,14 +203,18 @@ public class JukeboxConfig {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the fallbackContextId
|
||||
* Gets the fallback context id.
|
||||
*
|
||||
* @return the fallback context id
|
||||
*/
|
||||
public String getFallbackContextId() {
|
||||
return fallbackContextId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param fallbackContextId the fallbackContextId to set
|
||||
* Sets the fallback context id.
|
||||
*
|
||||
* @param fallbackContextId the new fallback context id
|
||||
*/
|
||||
public void setFallbackContextId(String fallbackContextId) {
|
||||
this.fallbackContextId = fallbackContextId;
|
||||
|
||||
@@ -52,6 +52,8 @@ public class JukeboxController extends BaseController {
|
||||
|
||||
/**
|
||||
* Check queue permission.
|
||||
*
|
||||
* @return the json element
|
||||
*/
|
||||
protected JsonElement checkQueuePermission() {
|
||||
if (!permissionManager.hasPermission(getCurrentUserId(), ParteyPermissions.PARTEY)) {
|
||||
@@ -101,8 +103,9 @@ public class JukeboxController extends BaseController {
|
||||
/**
|
||||
* Check.
|
||||
*
|
||||
* @param response the response
|
||||
* @throws JsonIOException the json IO exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@GetMapping
|
||||
@@ -126,11 +129,11 @@ public class JukeboxController extends BaseController {
|
||||
/**
|
||||
* Search.
|
||||
*
|
||||
* @param query the query
|
||||
* @param offset the offset
|
||||
* @param query the query
|
||||
* @param offset the offset
|
||||
* @param response the response
|
||||
* @throws JsonIOException the json IO exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@GetMapping("/search")
|
||||
@@ -162,7 +165,7 @@ public class JukeboxController extends BaseController {
|
||||
*
|
||||
* @param response the response
|
||||
* @throws JsonIOException the json IO exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@GetMapping("/current")
|
||||
@@ -194,10 +197,10 @@ public class JukeboxController extends BaseController {
|
||||
/**
|
||||
* Last.
|
||||
*
|
||||
* @param limit the limit
|
||||
* @param limit the limit
|
||||
* @param response the response
|
||||
* @throws JsonIOException the json IO exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@GetMapping("/last")
|
||||
|
||||
Reference in New Issue
Block a user