2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.squeezebox.internal.handler;
15 import static org.openhab.binding.squeezebox.internal.SqueezeBoxBindingConstants.*;
17 import java.io.BufferedReader;
18 import java.io.BufferedWriter;
19 import java.io.IOException;
20 import java.io.InputStreamReader;
21 import java.io.OutputStreamWriter;
22 import java.io.UnsupportedEncodingException;
23 import java.net.Socket;
24 import java.net.URLDecoder;
25 import java.net.URLEncoder;
26 import java.nio.charset.StandardCharsets;
27 import java.time.Duration;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.Base64;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.HashSet;
34 import java.util.List;
37 import java.util.concurrent.Future;
38 import java.util.concurrent.ScheduledFuture;
39 import java.util.concurrent.TimeUnit;
40 import java.util.stream.Collectors;
42 import org.eclipse.jdt.annotation.NonNullByDefault;
43 import org.openhab.binding.squeezebox.internal.config.SqueezeBoxServerConfig;
44 import org.openhab.binding.squeezebox.internal.dto.ButtonDTO;
45 import org.openhab.binding.squeezebox.internal.dto.ButtonDTODeserializer;
46 import org.openhab.binding.squeezebox.internal.dto.ButtonsDTO;
47 import org.openhab.binding.squeezebox.internal.dto.StatusResponseDTO;
48 import org.openhab.binding.squeezebox.internal.model.Favorite;
49 import org.openhab.core.io.net.http.HttpRequestBuilder;
50 import org.openhab.core.library.types.StringType;
51 import org.openhab.core.thing.Bridge;
52 import org.openhab.core.thing.Channel;
53 import org.openhab.core.thing.ChannelUID;
54 import org.openhab.core.thing.Thing;
55 import org.openhab.core.thing.ThingStatus;
56 import org.openhab.core.thing.ThingStatusDetail;
57 import org.openhab.core.thing.ThingTypeUID;
58 import org.openhab.core.thing.binding.BaseBridgeHandler;
59 import org.openhab.core.thing.binding.ThingHandler;
60 import org.openhab.core.types.Command;
61 import org.openhab.core.types.UnDefType;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import com.google.gson.Gson;
66 import com.google.gson.GsonBuilder;
67 import com.google.gson.JsonSyntaxException;
70 * Handles connection and event handling to a SqueezeBox Server.
72 * @author Markus Wolters - Initial contribution
73 * @author Ben Jones - ?
74 * @author Dan Cunningham - OH2 port
75 * @author Daniel Walters - Fix player discovery when player name contains spaces
76 * @author Mark Hilbush - Improve reconnect logic. Improve player status updates.
77 * @author Mark Hilbush - Implement AudioSink and notifications
78 * @author Mark Hilbush - Added duration channel
79 * @author Mark Hilbush - Added login/password authentication for LMS
80 * @author Philippe Siem - Improve refresh of cover art url,remote title, artist, album, genre, year.
81 * @author Patrik Gfeller - Support for mixer volume message added
82 * @author Mark Hilbush - Get favorites from LMS; update channel and send to players
83 * @author Mark Hilbush - Add like/unlike functionality
85 public class SqueezeBoxServerHandler extends BaseBridgeHandler {
86 private final Logger logger = LoggerFactory.getLogger(SqueezeBoxServerHandler.class);
88 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections
89 .singleton(SQUEEZEBOXSERVER_THING_TYPE);
91 // time in seconds to try to reconnect
92 private static final int RECONNECT_TIME = 60;
95 private static final String UTF8_NAME = StandardCharsets.UTF_8.name();
97 // the value by which the volume is changed by each INCREASE or
99 private static final int VOLUME_CHANGE_SIZE = 5;
100 private static final String NEW_LINE = System.getProperty("line.separator");
102 private static final String CHANNEL_CONFIG_QUOTE_LIST = "quoteList";
104 private static final String JSONRPC_STATUS_REQUEST = "{\"id\":1,\"method\":\"slim.request\",\"params\":[\"@@MAC@@\",[\"status\",\"-\",\"tags:yagJlNKjcB\"]]}";
106 private List<SqueezeBoxPlayerEventListener> squeezeBoxPlayerListeners = Collections
107 .synchronizedList(new ArrayList<>());
109 private Map<String, SqueezeBoxPlayer> players = Collections.synchronizedMap(new HashMap<>());
111 // client socket and listener thread
112 private Socket clientSocket;
113 private SqueezeServerListener listener;
114 private Future<?> reconnectFuture;
122 private String userId;
124 private String password;
126 private final Gson gson = new GsonBuilder().registerTypeAdapter(ButtonDTO.class, new ButtonDTODeserializer())
128 private String jsonRpcUrl;
129 private String basicAuthorization;
131 public SqueezeBoxServerHandler(Bridge bridge) {
136 public void initialize() {
137 logger.debug("initializing server handler for thing {}", getThing().getUID());
138 scheduler.submit(this::connect);
142 public void dispose() {
143 logger.debug("disposing server handler for thing {}", getThing().getUID());
149 public void handleCommand(ChannelUID channelUID, Command command) {
153 * Checks if we have a connection to the Server
157 public synchronized boolean isConnected() {
158 if (clientSocket == null) {
162 // NOTE: isConnected() returns true once a connection is made and will
163 // always return true even after the socket is closed
164 // http://stackoverflow.com/questions/10163358/
165 return clientSocket.isConnected() && !clientSocket.isClosed();
168 public void mute(String mac) {
169 sendCommand(mac + " mixer muting 1");
172 public void unMute(String mac) {
173 sendCommand(mac + " mixer muting 0");
176 public void powerOn(String mac) {
177 sendCommand(mac + " power 1");
180 public void powerOff(String mac) {
181 sendCommand(mac + " power 0");
184 public void syncPlayer(String mac, String player2mac) {
185 sendCommand(mac + " sync " + player2mac);
188 public void unSyncPlayer(String mac) {
189 sendCommand(mac + " sync -");
192 public void play(String mac) {
193 sendCommand(mac + " play");
196 public void playUrl(String mac, String url) {
197 sendCommand(mac + " playlist play " + url);
200 public void pause(String mac) {
201 sendCommand(mac + " pause 1");
204 public void unPause(String mac) {
205 sendCommand(mac + " pause 0");
208 public void stop(String mac) {
209 sendCommand(mac + " stop");
212 public void prev(String mac) {
213 sendCommand(mac + " playlist index -1");
216 public void next(String mac) {
217 sendCommand(mac + " playlist index +1");
220 public void clearPlaylist(String mac) {
221 sendCommand(mac + " playlist clear");
224 public void deletePlaylistItem(String mac, int playlistIndex) {
225 sendCommand(mac + " playlist delete " + playlistIndex);
228 public void playPlaylistItem(String mac, int playlistIndex) {
229 sendCommand(mac + " playlist index " + playlistIndex);
232 public void addPlaylistItem(String mac, String url) {
233 addPlaylistItem(mac, url, null);
236 public void addPlaylistItem(String mac, String url, String title) {
237 StringBuilder playlistCommand = new StringBuilder();
238 playlistCommand.append(mac).append(" playlist add ").append(url);
240 playlistCommand.append(" ").append(title);
242 sendCommand(playlistCommand.toString());
245 public void setPlayingTime(String mac, int time) {
246 sendCommand(mac + " time " + time);
249 public void setRepeatMode(String mac, int repeatMode) {
250 sendCommand(mac + " playlist repeat " + repeatMode);
253 public void setShuffleMode(String mac, int shuffleMode) {
254 sendCommand(mac + " playlist shuffle " + shuffleMode);
257 public void volumeUp(String mac, int currentVolume) {
258 setVolume(mac, currentVolume + VOLUME_CHANGE_SIZE);
261 public void volumeDown(String mac, int currentVolume) {
262 setVolume(mac, currentVolume - VOLUME_CHANGE_SIZE);
265 public void setVolume(String mac, int volume) {
266 int newVolume = volume;
267 newVolume = Math.min(100, newVolume);
268 newVolume = Math.max(0, newVolume);
269 sendCommand(mac + " mixer volume " + String.valueOf(newVolume));
272 public void showString(String mac, String line) {
273 showString(mac, line, 5);
276 public void showString(String mac, String line, int duration) {
277 sendCommand(mac + " show line1:" + line + " duration:" + String.valueOf(duration));
280 public void showStringHuge(String mac, String line) {
281 showStringHuge(mac, line, 5);
284 public void showStringHuge(String mac, String line, int duration) {
285 sendCommand(mac + " show line1:" + line + " font:huge duration:" + String.valueOf(duration));
288 public void showStrings(String mac, String line1, String line2) {
289 showStrings(mac, line1, line2, 5);
292 public void showStrings(String mac, String line1, String line2, int duration) {
293 sendCommand(mac + " show line1:" + line1 + " line2:" + line2 + " duration:" + String.valueOf(duration));
296 public void playFavorite(String mac, String favorite) {
297 sendCommand(mac + " favorites playlist play item_id:" + favorite);
300 public void rate(String mac, String rateCommand) {
301 if (rateCommand != null) {
302 sendCommand(mac + " " + rateCommand);
306 public void sleep(String mac, Duration sleepDuration) {
307 sendCommand(mac + " sleep " + String.valueOf(sleepDuration.toSeconds()));
311 * Send a generic command to a given player
316 public void playerCommand(String mac, String command) {
317 sendCommand(mac + " " + command);
321 * Ask for player list
323 public void requestPlayers() {
324 sendCommand("players 0");
328 * Ask for favorites list
330 public void requestFavorites() {
331 sendCommand("favorites items 0 100");
337 public void login() {
338 if (userId.isEmpty()) {
341 // Create basic auth string for jsonrpc interface
342 basicAuthorization = new String(
343 Base64.getEncoder().encode((userId + ":" + password).getBytes(StandardCharsets.UTF_8)));
344 logger.debug("Logging into Squeeze Server using userId={}", userId);
345 sendCommand("login " + userId + " " + password);
349 * Send a command to the Squeeze Server.
351 private synchronized void sendCommand(String command) {
352 if (getThing().getStatus() != ThingStatus.ONLINE) {
356 if (!isConnected()) {
357 logger.debug("no connection to squeeze server when trying to send command, returning...");
361 logger.debug("Sending command: {}", sanitizeCommand(command));
363 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
364 writer.write(command + NEW_LINE);
366 } catch (IOException e) {
367 logger.error("Error while sending command to Squeeze Server ({}) ", sanitizeCommand(command), e);
372 * Remove password from login command to prevent it from being logged
374 String sanitizeCommand(String command) {
375 String sanitizedCommand = command;
376 if (command.startsWith("login")) {
377 sanitizedCommand = command.replace(password, "**********");
379 return sanitizedCommand;
383 * Connects to a SqueezeBox Server
385 private void connect() {
386 logger.trace("attempting to get a connection to the server");
388 SqueezeBoxServerConfig config = getConfigAs(SqueezeBoxServerConfig.class);
389 this.host = config.ipAddress;
390 this.cliport = config.cliport;
391 this.webport = config.webport;
392 this.userId = config.userId;
393 this.password = config.password;
395 if (host.isEmpty()) {
396 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "host is not set");
399 // Create URL for jsonrpc interface
400 jsonRpcUrl = String.format("http://%s:%d/jsonrpc.js", host, webport);
403 clientSocket = new Socket(host, cliport);
404 } catch (IOException e) {
405 logger.debug("unable to open socket to server: {}", e.getMessage());
406 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, e.getMessage());
412 listener = new SqueezeServerListener();
414 logger.debug("listener connection started to server {}:{}", host, cliport);
415 } catch (IllegalThreadStateException e) {
416 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
418 // Mark the server ONLINE. bridgeStatusChanged will cause the players to come ONLINE
419 updateStatus(ThingStatus.ONLINE);
423 * Disconnects from a SqueezeBox Server
425 private void disconnect() {
427 if (listener != null) {
428 listener.terminate();
430 if (clientSocket != null) {
431 clientSocket.close();
433 } catch (Exception e) {
434 logger.trace("Error attempting to disconnect from Squeeze Server", e);
441 logger.trace("Squeeze Server connection stopped.");
444 private class SqueezeServerListener extends Thread {
445 private boolean terminate = false;
447 public SqueezeServerListener() {
448 super("Squeeze Server Listener");
451 public void terminate() {
452 logger.debug("setting squeeze server listener terminate flag");
453 this.terminate = true;
458 BufferedReader reader = null;
459 boolean endOfStream = false;
460 ScheduledFuture<?> requestFavoritesJob = null;
463 reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
465 updateStatus(ThingStatus.ONLINE);
467 requestFavoritesJob = scheduleRequestFavorites();
468 sendCommand("listen 1");
470 String message = null;
471 while (!terminate && (message = reader.readLine()) != null) {
472 // Message is very long and frequent; only show when running at trace level logging
473 logger.trace("Message received: {}", message);
475 // Fix for some third-party apps that are sending "subscribe playlist"
476 if (message.startsWith("listen 1") || message.startsWith("subscribe playlist")) {
480 if (message.startsWith("players 0")) {
481 handlePlayersList(message);
482 } else if (message.startsWith("favorites")) {
483 handleFavorites(message);
485 handlePlayerUpdate(message);
488 if (message == null) {
491 } catch (IOException e) {
493 logger.warn("failed to read line from squeeze server socket: {}", e.getMessage());
494 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
498 if (reader != null) {
501 } catch (IOException e) {
508 // check for end of stream from readLine
509 if (endOfStream && !terminate) {
510 logger.info("end of stream received from socket during readLine");
511 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
512 "end of stream on socket read");
515 if (requestFavoritesJob != null && !requestFavoritesJob.isDone()) {
516 requestFavoritesJob.cancel(true);
517 logger.debug("Canceled request favorites job");
519 logger.debug("Squeeze Server listener exiting.");
522 private String decode(String raw) {
524 return URLDecoder.decode(raw, UTF8_NAME);
525 } catch (UnsupportedEncodingException e) {
526 logger.debug("Failed to decode '{}' ", raw, e);
531 private String encode(String raw) {
533 return URLEncoder.encode(raw, UTF8_NAME);
534 } catch (UnsupportedEncodingException e) {
535 logger.debug("Failed to encode '{}' ", raw, e);
541 private class KeyValue {
545 public KeyValue(String key, String value) {
551 private List<KeyValue> decodeKeyValueResponse(String[] response) {
552 final List<KeyValue> keysAndValues = new ArrayList<>();
553 if (response != null) {
554 for (String line : response) {
555 final String decoded = decode(line);
556 int colonPos = decoded.indexOf(":");
560 keysAndValues.add(new KeyValue(decoded.substring(0, colonPos), decoded.substring(colonPos + 1)));
563 return keysAndValues;
566 private void handlePlayersList(String message) {
567 final Set<String> connectedPlayers = new HashSet<>();
570 String[] playersList = message.split("playerindex\\S*\\s");
571 for (String playerParams : playersList) {
572 // For each player, split out parameters and decode parameter
573 final Map<String, String> keysAndValues = decodeKeyValueResponse(playerParams.split("\\s")).stream()
574 .collect(Collectors.toMap(kv -> kv.key, kv -> kv.value));
575 final String macAddress = keysAndValues.get("playerid");
577 // if none found then ignore this set of params
578 if (macAddress == null) {
582 final SqueezeBoxPlayer player = new SqueezeBoxPlayer(macAddress, keysAndValues.get("name"),
583 keysAndValues.get("ip"), keysAndValues.get("model"), keysAndValues.get("uuid"));
584 if ("1".equals(keysAndValues.get("connected"))) {
585 connectedPlayers.add(macAddress);
588 // Save player if we haven't seen it yet
589 if (!players.containsKey(macAddress)) {
590 players.put(macAddress, player);
591 updatePlayer(listener -> listener.playerAdded(player));
592 // tell the server we want to subscribe to player updates
593 sendCommand(player.macAddress + " status - 1 subscribe:10 tags:yagJlNKjc");
596 for (final SqueezeBoxPlayer player : players.values()) {
597 final boolean connected = connectedPlayers.contains(player.macAddress);
598 updatePlayer(listener -> listener.connectedStateChangeEvent(player.macAddress, connected));
602 private void handlePlayerUpdate(String message) {
603 String[] messageParts = message.split("\\s");
604 if (messageParts.length < 2) {
605 logger.warn("Invalid message - expecting at least 2 parts. Ignoring.");
609 final String mac = decode(messageParts[0]);
611 // get the message type
612 String messageType = messageParts[1];
613 switch (messageType) {
615 handleClientMessage(mac, messageParts);
618 handleStatusMessage(mac, messageParts);
621 handlePlaylistMessage(mac, messageParts);
624 handlePrefsetMessage(mac, messageParts);
627 handleMixerMessage(mac, messageParts);
630 final String ircode = messageParts[2];
631 updatePlayer(listener -> listener.irCodeChangeEvent(mac, ircode));
634 logger.trace("Unhandled player update message type '{}'.", messageType);
638 private void handleMixerMessage(String mac, String[] messageParts) {
639 if (messageParts.length < 4) {
642 String action = messageParts[2];
646 String volumeStringValue = decode(messageParts[3]);
647 updatePlayer(listener -> {
649 int volume = Math.round(Float.parseFloat(volumeStringValue));
651 // Check if we received a relative volume change, or an absolute
653 if (volumeStringValue.contains("+") || (volumeStringValue.contains("-"))) {
654 listener.relativeVolumeChangeEvent(mac, volume);
656 listener.absoluteVolumeChangeEvent(mac, volume);
658 } catch (NumberFormatException e) {
659 logger.warn("Unable to parse volume [{}] received from mixer message.", volumeStringValue,
665 logger.trace("Unhandled mixer message type '{}'", Arrays.toString(messageParts));
670 private void handleClientMessage(final String mac, String[] messageParts) {
671 if (messageParts.length < 3) {
675 String action = messageParts[2];
676 final boolean connected;
678 if ("new".equals(action) || "reconnect".equals(action)) {
680 } else if ("disconnect".equals(action) || "forget".equals(action)) {
683 logger.trace("Unhandled client message type '{}'", Arrays.toString(messageParts));
687 updatePlayer(listener -> listener.connectedStateChangeEvent(mac, connected));
690 private void handleStatusMessage(final String mac, String[] messageParts) {
691 String remoteTitle = "", artist = "", album = "", genre = "", year = "";
692 boolean coverart = false;
693 String coverid = null;
694 String artworkUrl = null;
696 for (KeyValue entry : decodeKeyValueResponse(messageParts)) {
698 if ("power".equals(entry.key)) {
699 final boolean power = "1".equals(entry.value);
700 updatePlayer(listener -> listener.powerChangeEvent(mac, power));
703 else if ("mixer volume".equals(entry.key)) {
704 final int volume = (int) Double.parseDouble(entry.value);
705 updatePlayer(listener -> listener.absoluteVolumeChangeEvent(mac, volume));
708 else if ("mode".equals(entry.key)) {
709 updatePlayer(listener -> listener.modeChangeEvent(mac, entry.value));
711 // Parameter Playing Time
712 else if ("time".equals(entry.key)) {
713 final int time = (int) Double.parseDouble(entry.value);
714 updatePlayer(listener -> listener.currentPlayingTimeEvent(mac, time));
716 // Parameter duration
717 else if ("duration".equals(entry.key)) {
718 final int duration = (int) Double.parseDouble(entry.value);
719 updatePlayer(listener -> listener.durationEvent(mac, duration));
721 // Parameter Playing Playlist Index
722 else if ("playlist_cur_index".equals(entry.key)) {
723 final int index = (int) Double.parseDouble(entry.value);
724 updatePlayer(listener -> listener.currentPlaylistIndexEvent(mac, index));
726 // Parameter Playlist Number Tracks
727 else if ("playlist_tracks".equals(entry.key)) {
728 final int track = (int) Double.parseDouble(entry.value);
729 updatePlayer(listener -> listener.numberPlaylistTracksEvent(mac, track));
731 // Parameter Playlist Repeat Mode
732 else if ("playlist repeat".equals(entry.key)) {
733 final int repeat = (int) Double.parseDouble(entry.value);
734 updatePlayer(listener -> listener.currentPlaylistRepeatEvent(mac, repeat));
736 // Parameter Playlist Shuffle Mode
737 else if ("playlist shuffle".equals(entry.key)) {
738 final int shuffle = (int) Double.parseDouble(entry.value);
739 updatePlayer(listener -> listener.currentPlaylistShuffleEvent(mac, shuffle));
742 else if ("title".equals(entry.key)) {
743 updatePlayer(listener -> listener.titleChangeEvent(mac, entry.value));
745 // Parameter Remote Title (radio)
746 else if ("remote_title".equals(entry.key)) {
747 remoteTitle = entry.value;
750 else if ("artist".equals(entry.key)) {
751 artist = entry.value;
754 else if ("album".equals(entry.key)) {
758 else if ("genre".equals(entry.key)) {
762 else if ("year".equals(entry.key)) {
765 // Parameter artwork_url contains url to cover art
766 else if ("artwork_url".equals(entry.key)) {
767 artworkUrl = entry.value;
769 // When coverart is "1" coverid will contain a unique coverart id
770 else if ("coverart".equals(entry.key)) {
771 coverart = "1".equals(entry.value);
773 // Id for covert art (only valid when coverart is "1")
774 else if ("coverid".equals(entry.key)) {
775 coverid = entry.value;
777 // Added to be able to see additional status message types
778 logger.trace("Unhandled status message type '{}' (value '{}')", entry.key, entry.value);
782 final String finalUrl = constructCoverArtUrl(mac, coverart, coverid, artworkUrl);
783 final String finalRemoteTitle = remoteTitle;
784 final String finalArtist = artist;
785 final String finalAlbum = album;
786 final String finalGenre = genre;
787 final String finalYear = year;
789 updatePlayer(listener -> {
790 listener.coverArtChangeEvent(mac, finalUrl);
791 listener.remoteTitleChangeEvent(mac, finalRemoteTitle);
792 listener.artistChangeEvent(mac, finalArtist);
793 listener.albumChangeEvent(mac, finalAlbum);
794 listener.genreChangeEvent(mac, finalGenre);
795 listener.yearChangeEvent(mac, finalYear);
799 private String constructCoverArtUrl(String mac, boolean coverart, String coverid, String artwork_url) {
801 if (!userId.isEmpty()) {
802 hostAndPort = "http://" + encode(userId) + ":" + encode(password) + "@" + host + ":" + webport;
804 hostAndPort = "http://" + host + ":" + webport;
807 // Default to using the convenience artwork URL (should be rare)
808 String url = hostAndPort + "/music/current/cover.jpg?player=" + encode(mac);
810 // If additional artwork info provided, use that instead
812 if (coverid != null) {
813 // Typically is used to access cover art of local music files
814 url = hostAndPort + "/music/" + coverid + "/cover.jpg";
816 } else if (artwork_url != null) {
817 if (artwork_url.startsWith("http")) {
818 // Typically indicates that cover art is not local to LMS
820 } else if (artwork_url.startsWith("/")) {
821 // Typically used for default coverart for plugins (e.g. Pandora, etc.)
822 url = hostAndPort + artwork_url;
824 // Another variation of default coverart for plugins (e.g. Pandora, etc.)
825 url = hostAndPort + "/" + artwork_url;
831 private void handlePlaylistMessage(final String mac, String[] messageParts) {
832 if (messageParts.length < 3) {
835 String action = messageParts[2];
837 if (action.equals("newsong")) {
839 // Execute in separate thread to avoid delaying listener
840 scheduler.execute(() -> updateCustomButtons(mac));
841 // Set the track duration to 0
842 updatePlayer(listener -> listener.durationEvent(mac, 0));
843 } else if (action.equals("pause")) {
844 if (messageParts.length < 4) {
847 mode = messageParts[3].equals("0") ? "play" : "pause";
848 } else if (action.equals("stop")) {
850 } else if ("play".equals(action) && "playlist".equals(messageParts[1])) {
851 if (messageParts.length >= 4) {
852 handleSourceChangeMessage(mac, messageParts[3]);
856 // Added so that actions (such as delete, index, jump, open) are not treated as "play"
857 logger.trace("Unhandled playlist message type '{}'", Arrays.toString(messageParts));
860 final String value = mode;
861 updatePlayer(listener -> listener.modeChangeEvent(mac, value));
864 private void handleSourceChangeMessage(String mac, String rawSource) {
865 String source = URLDecoder.decode(rawSource);
866 updatePlayer(listener -> listener.sourceChangeEvent(mac, source));
869 private void handlePrefsetMessage(final String mac, String[] messageParts) {
870 if (messageParts.length < 5) {
874 if (messageParts[2].equals("server")) {
875 String function = messageParts[3];
876 String value = messageParts[4];
877 if (function.equals("power")) {
878 final boolean power = value.equals("1");
879 updatePlayer(listener -> listener.powerChangeEvent(mac, power));
880 } else if (function.equals("volume")) {
881 final int volume = (int) Double.parseDouble(value);
882 updatePlayer(listener -> listener.absoluteVolumeChangeEvent(mac, volume));
887 private void handleFavorites(String message) {
888 String[] messageParts = message.split("\\s");
889 if (messageParts.length == 2 && "changed".equals(messageParts[1])) {
890 // LMS informing us that favorites have changed; request an update to the favorites list
894 if (messageParts.length < 7) {
895 logger.trace("No favorites in message.");
899 List<Favorite> favorites = new ArrayList<>();
901 boolean isTypePlaylist = false;
902 for (KeyValue entry : decodeKeyValueResponse(messageParts)) {
903 // Favorite ID (in form xxxxxxxxx.n)
904 if ("id".equals(entry.key)) {
905 f = new Favorite(entry.value);
907 isTypePlaylist = false;
910 else if ("name".equals(entry.key)) {
911 f.name = entry.value;
912 } else if ("type".equals(entry.key) && "playlist".equals(entry.value)) {
913 isTypePlaylist = true;
915 // When "1", favorite is a submenu with additional favorites
916 else if ("hasitems".equals(entry.key)) {
917 boolean hasitems = "1".equals(entry.value);
919 // Except for some favorites (e.g. Spotify) use hasitems:1 and type:playlist
920 if (hasitems && isTypePlaylist == false) {
928 updatePlayer(listener -> listener.updateFavoritesListEvent(favorites));
929 updateChannelFavoritesList(favorites);
932 private void updateChannelFavoritesList(List<Favorite> favorites) {
933 final Channel channel = getThing().getChannel(CHANNEL_FAVORITES_LIST);
934 if (channel == null) {
935 logger.debug("Channel {} doesn't exist. Delete & add thing to get channel.", CHANNEL_FAVORITES_LIST);
939 // Get channel config parameter indicating whether name should be wrapped with double quotes
940 Boolean includeQuotes = Boolean.FALSE;
941 if (channel.getConfiguration().containsKey(CHANNEL_CONFIG_QUOTE_LIST)) {
942 includeQuotes = (Boolean) channel.getConfiguration().get(CHANNEL_CONFIG_QUOTE_LIST);
945 String quote = includeQuotes.booleanValue() ? "\"" : "";
946 StringBuilder sb = new StringBuilder();
947 for (Favorite favorite : favorites) {
948 sb.append(favorite.shortId).append("=").append(quote).append(favorite.name.replaceAll(",", ""))
949 .append(quote).append(",");
952 if (sb.length() == 0) {
953 updateState(CHANNEL_FAVORITES_LIST, UnDefType.NULL);
955 // Drop the last comma
956 sb.setLength(sb.length() - 1);
957 String favoritesList = sb.toString();
958 logger.trace("Updating favorites channel for {} to state {}", getThing().getUID(), favoritesList);
959 updateState(CHANNEL_FAVORITES_LIST, new StringType(favoritesList));
963 private ScheduledFuture<?> scheduleRequestFavorites() {
964 // Delay the execution to give the player thing handlers a chance to initialize
965 return scheduler.schedule(SqueezeBoxServerHandler.this::requestFavorites, 3L, TimeUnit.SECONDS);
968 private void updateCustomButtons(final String mac) {
969 String response = executePost(jsonRpcUrl, JSONRPC_STATUS_REQUEST.replace("@@MAC@@", mac));
970 if (response != null) {
971 logger.trace("Status response: {}", response);
972 String likeCommand = null;
973 String unlikeCommand = null;
975 StatusResponseDTO status = gson.fromJson(response, StatusResponseDTO.class);
976 if (status != null && status.result != null && status.result.remoteMeta != null
977 && status.result.remoteMeta.buttons != null) {
978 ButtonsDTO buttons = status.result.remoteMeta.buttons;
979 if (buttons.repeat != null && buttons.repeat.isCustom()) {
980 likeCommand = buttons.repeat.command;
982 if (buttons.shuffle != null && buttons.shuffle.isCustom()) {
983 unlikeCommand = buttons.shuffle.command;
986 } catch (JsonSyntaxException e) {
987 logger.debug("JsonSyntaxException parsing status response: {}", response, e);
989 final String like = likeCommand;
990 final String unlike = unlikeCommand;
991 updatePlayer(listener -> listener.buttonsChangeEvent(mac, like, unlike));
995 private String executePost(String url, String content) {
997 HttpRequestBuilder builder = HttpRequestBuilder.postTo(url)
998 .withTimeout(Duration.ofSeconds(5))
999 .withContent(content)
1000 .withHeader("charset", "utf-8")
1001 .withHeader("Content-Type", "application/json");
1003 if (basicAuthorization != null) {
1004 builder = builder.withHeader("Authorization", "Basic " + basicAuthorization);
1007 return builder.getContentAsString();
1008 } catch (IOException e) {
1009 logger.debug("Bridge: IOException on jsonrpc call: {}", e.getMessage(), e);
1016 * Interface to allow us to pass function call-backs to SqueezeBox Player
1019 * @author Dan Cunningham
1022 interface PlayerUpdateEvent {
1023 void updateListener(SqueezeBoxPlayerEventListener listener);
1027 * Update Listeners and child Squeeze Player Things
1031 private void updatePlayer(PlayerUpdateEvent event) {
1032 // update listeners like disco services
1033 synchronized (squeezeBoxPlayerListeners) {
1034 for (SqueezeBoxPlayerEventListener listener : squeezeBoxPlayerListeners) {
1035 event.updateListener(listener);
1038 // update our children
1039 Bridge bridge = getThing();
1041 List<Thing> things = bridge.getThings();
1042 for (Thing thing : things) {
1043 ThingHandler handler = thing.getHandler();
1044 if (handler instanceof SqueezeBoxPlayerEventListener && !squeezeBoxPlayerListeners.contains(handler)) {
1045 event.updateListener((SqueezeBoxPlayerEventListener) handler);
1051 * Adds a listener for player events
1053 * @param squeezeBoxPlayerListener
1056 public boolean registerSqueezeBoxPlayerListener(SqueezeBoxPlayerEventListener squeezeBoxPlayerListener) {
1057 logger.trace("Registering player listener");
1058 return squeezeBoxPlayerListeners.add(squeezeBoxPlayerListener);
1062 * Removes a listener from player events
1064 * @param squeezeBoxPlayerListener
1067 public boolean unregisterSqueezeBoxPlayerListener(SqueezeBoxPlayerEventListener squeezeBoxPlayerListener) {
1068 logger.trace("Unregistering player listener");
1069 return squeezeBoxPlayerListeners.remove(squeezeBoxPlayerListener);
1073 * Removed a player from our known list of players, will populate again if
1078 public void removePlayerCache(String mac) {
1079 players.remove(mac);
1083 * Schedule the server to try and reconnect
1085 private void scheduleReconnect() {
1086 logger.debug("scheduling squeeze server reconnect in {} seconds", RECONNECT_TIME);
1088 reconnectFuture = scheduler.schedule(this::connect, RECONNECT_TIME, TimeUnit.SECONDS);
1092 * Clears our reconnect job if exists
1094 private void cancelReconnect() {
1095 if (reconnectFuture != null) {
1096 reconnectFuture.cancel(true);