Skip to content

Commit

Permalink
Add basic support for receiving dealer requests
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Crane <[email protected]>
  • Loading branch information
marcus-crane committed Sep 12, 2024
1 parent ab3cc8e commit d8ee674
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions spotify/spotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,17 +367,43 @@ func (c *Client) Run(ps *playback.PlaybackSystem) {
}
apRecv := c.sess.Accesspoint().Receive(ap.PacketTypeProductInfo, ap.PacketTypeCountryCode)
msgChan := c.dealer.ReceiveMessage("hm://pusher/v1/connections/", "hm://connect-state/v1/")
reqRecv := c.sess.Dealer().ReceiveRequest("hm://connect-state/v1/player/command")

for {
select {
case pkt := <-apRecv:
c.handleAccessPointPacket(pkt.Type, pkt.Payload)
case msg := <-msgChan:
c.handleMessage(msg, ps)
case req := <-reqRecv:
c.handleDealerRequest(req, ps)
}
}
}

func (c *Client) handleDealerRequest(req dealer.Request, ps *playback.PlaybackSystem) {
slog.With("uri", req.MessageIdent).Info("received request from spotify")
switch req.MessageIdent {
case "hm://connect-state/v1/player/command":
c.handlePlayerCommand(req.Payload, ps)
default:
slog.With(slog.String("ident", req.MessageIdent)).Warn("unknown dealer request: %s")
}
}

func (c *Client) handlePlayerCommand(req dealer.RequestPayload, _ *playback.PlaybackSystem) {
switch req.Command.Endpoint {
case "transfer":
var transferState connectpb.TransferState
if err := proto.Unmarshal(req.Command.Data, &transferState); err != nil {
slog.With(slog.String("error", err.Error())).Error("failed unmarshalling TransferState")
}
slog.With(slog.String("command", "transfer")).Info(transferState.GetPlayback().GetCurrentTrack().String())
default:
slog.With(slog.String("endpoint", req.Command.Endpoint)).Error("unsupported player command")
}
}

func (c *Client) handleAccessPointPacket(pktType ap.PacketType, payload []byte) error {
switch pktType {
case ap.PacketTypeProductInfo:
Expand All @@ -401,6 +427,7 @@ func (c *Client) handleAccessPointPacket(pktType ap.PacketType, payload []byte)
}

func (c *Client) handleMessage(msg dealer.Message, ps *playback.PlaybackSystem) {
slog.With("uri", msg.Uri).Info("received message from spotify")
if strings.HasPrefix(msg.Uri, "hm://pusher/v1/connections/") {
spotConnId := msg.Headers["Spotify-Connection-Id"]
slog.With(slog.String("connection_id", spotConnId)).Debug("Established connection to Spotify")
Expand Down Expand Up @@ -429,9 +456,12 @@ func (c *Client) handleMessage(msg dealer.Message, ps *playback.PlaybackSystem)
CommandAcks: true,
DisableVolume: true,
ConnectDisabled: false,
SupportsPlaylistV2: true,
Hidden: false,
NeedsFullPlayerState: false,
SupportsTransferCommand: false,
SupportsCommandRequest: true,
SupportsGzipPushes: true,
ConnectCapabilities: "",
},
},
Expand Down

0 comments on commit d8ee674

Please sign in to comment.