From d8ee674776841ac91bda611555bc516deb140607 Mon Sep 17 00:00:00 2001 From: Marcus Crane Date: Thu, 12 Sep 2024 22:01:04 +1200 Subject: [PATCH] Add basic support for receiving dealer requests Signed-off-by: Marcus Crane --- spotify/spotify.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spotify/spotify.go b/spotify/spotify.go index 88b20ce..f912961 100644 --- a/spotify/spotify.go +++ b/spotify/spotify.go @@ -367,6 +367,7 @@ 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 { @@ -374,10 +375,35 @@ func (c *Client) Run(ps *playback.PlaybackSystem) { 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: @@ -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") @@ -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: "", }, },