Skip to content

Commit 63f901a

Browse files
committed
Added events endpoint to API
1 parent eff9c47 commit 63f901a

3 files changed

Lines changed: 65 additions & 2 deletions

File tree

api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import io.undertow.server.RoutingHandler;
55
import org.apache.log4j.Logger;
66
import org.jetbrains.annotations.NotNull;
7+
import xyz.gianlu.librespot.api.handlers.EventsHandler;
78
import xyz.gianlu.librespot.api.handlers.MetadataHandler;
89
import xyz.gianlu.librespot.api.handlers.PlayerHandler;
910
import xyz.gianlu.librespot.core.Session;
@@ -23,7 +24,8 @@ public ApiServer(int port) {
2324

2425
private static void prepareHandlers(@NotNull RoutingHandler root, @NotNull Session session) {
2526
root.post("/player/{cmd}", new PlayerHandler(session))
26-
.post("/metadata/{type}/{uri}", new MetadataHandler(session));
27+
.post("/metadata/{type}/{uri}", new MetadataHandler(session))
28+
.get("/events", new EventsHandler(session));
2729
}
2830

2931
public void start(@NotNull Session session) {
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package xyz.gianlu.librespot.api.handlers;
2+
3+
import com.google.gson.JsonObject;
4+
import com.spotify.metadata.proto.Metadata;
5+
import io.undertow.websockets.WebSocketConnectionCallback;
6+
import io.undertow.websockets.WebSocketProtocolHandshakeHandler;
7+
import io.undertow.websockets.core.WebSocketChannel;
8+
import io.undertow.websockets.core.WebSockets;
9+
import org.apache.log4j.Logger;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.jetbrains.annotations.Nullable;
12+
import xyz.gianlu.librespot.common.ProtobufToJson;
13+
import xyz.gianlu.librespot.core.Session;
14+
import xyz.gianlu.librespot.mercury.model.PlayableId;
15+
import xyz.gianlu.librespot.player.Player;
16+
17+
public class EventsHandler extends WebSocketProtocolHandshakeHandler implements Player.EventsListener {
18+
private static final Logger LOGGER = Logger.getLogger(EventsHandler.class);
19+
20+
public EventsHandler(@NotNull Session session) {
21+
super((WebSocketConnectionCallback) (exchange, channel) -> LOGGER.info(String.format("Accepted new websocket connection from %s.", channel.getSourceAddress().getAddress())));
22+
session.player().addEventsListener(this);
23+
}
24+
25+
private void dispatch(@NotNull JsonObject obj) {
26+
for (WebSocketChannel channel : getPeerConnections())
27+
WebSockets.sendText(obj.toString(), channel, null);
28+
}
29+
30+
@Override
31+
public void onContextChanged(@NotNull String newUri) {
32+
JsonObject obj = new JsonObject();
33+
obj.addProperty("event", "contextChanged");
34+
obj.addProperty("uri", newUri);
35+
dispatch(obj);
36+
}
37+
38+
@Override
39+
public void onTrackChanged(@NotNull PlayableId id, Metadata.@Nullable Track track, Metadata.@Nullable Episode episode) {
40+
JsonObject obj = new JsonObject();
41+
obj.addProperty("event", "trackChanged");
42+
obj.addProperty("uri", id.toSpotifyUri());
43+
if (track != null) obj.add("track", ProtobufToJson.convert(track));
44+
if (episode != null) obj.add("episode", ProtobufToJson.convert(episode));
45+
dispatch(obj);
46+
}
47+
48+
@Override
49+
public void onPlaybackPaused() {
50+
JsonObject obj = new JsonObject();
51+
obj.addProperty("event", "playbackPaused");
52+
dispatch(obj);
53+
}
54+
55+
@Override
56+
public void onPlaybackResumed() {
57+
JsonObject obj = new JsonObject();
58+
obj.addProperty("event", "playbackResumed");
59+
dispatch(obj);
60+
}
61+
}

core/src/main/java/xyz/gianlu/librespot/player/Player.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ public interface Configuration {
694694
int releaseLineDelay();
695695
}
696696

697-
private interface EventsListener {
697+
public interface EventsListener {
698698
void onContextChanged(@NotNull String newUri);
699699

700700
void onTrackChanged(@NotNull PlayableId id, @Nullable Metadata.Track track, @Nullable Metadata.Episode episode);

0 commit comments

Comments
 (0)