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.discovery;
15 import static org.openhab.binding.squeezebox.internal.SqueezeBoxBindingConstants.SQUEEZEBOXPLAYER_THING_TYPE;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
23 import org.openhab.binding.squeezebox.internal.handler.SqueezeBoxPlayer;
24 import org.openhab.binding.squeezebox.internal.handler.SqueezeBoxPlayerEventListener;
25 import org.openhab.binding.squeezebox.internal.handler.SqueezeBoxPlayerHandler;
26 import org.openhab.binding.squeezebox.internal.handler.SqueezeBoxServerHandler;
27 import org.openhab.binding.squeezebox.internal.model.Favorite;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingUID;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * When a {@link SqueezeBoxServerHandler} finds a new SqueezeBox Player we will
37 * add it to the system.
39 * @author Dan Cunningham - Initial contribution
40 * @author Mark Hilbush - added method to cancel request player job, and to set thing properties
41 * @author Mark Hilbush - Added duration channel
42 * @author Mark Hilbush - Added event to update favorites list
45 public class SqueezeBoxPlayerDiscoveryParticipant extends AbstractDiscoveryService
46 implements SqueezeBoxPlayerEventListener {
47 private final Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerDiscoveryParticipant.class);
49 private static final int TIMEOUT = 60;
50 private static final int TTL = 60;
52 private SqueezeBoxServerHandler squeezeBoxServerHandler;
53 private ScheduledFuture<?> requestPlayerJob;
56 * Discovers SqueezeBox Players attached to a SqueezeBox Server
58 * @param squeezeBoxServerHandler
60 public SqueezeBoxPlayerDiscoveryParticipant(SqueezeBoxServerHandler squeezeBoxServerHandler) {
61 super(SqueezeBoxPlayerHandler.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
62 this.squeezeBoxServerHandler = squeezeBoxServerHandler;
63 setupRequestPlayerJob();
67 protected void startScan() {
68 logger.debug("startScan invoked in SqueezeBoxPlayerDiscoveryParticipant");
69 this.squeezeBoxServerHandler.requestPlayers();
70 this.squeezeBoxServerHandler.requestFavorites();
74 * Allows request player job to be canceled when server handler is removed
76 public void cancelRequestPlayerJob() {
77 logger.debug("canceling RequestPlayerJob");
78 if (requestPlayerJob != null) {
79 requestPlayerJob.cancel(true);
80 requestPlayerJob = null;
85 public void playerAdded(SqueezeBoxPlayer player) {
86 ThingUID bridgeUID = squeezeBoxServerHandler.getThing().getUID();
88 ThingUID thingUID = new ThingUID(SQUEEZEBOXPLAYER_THING_TYPE, bridgeUID,
89 player.getMacAddress().replace(":", ""));
91 if (!playerThingExists(thingUID)) {
92 logger.debug("player added {} : {} ", player.getMacAddress(), player.getName());
94 Map<String, Object> properties = new HashMap<>(1);
95 String representationPropertyName = "mac";
96 properties.put(representationPropertyName, player.getMacAddress());
98 // Added other properties
99 properties.put("modelId", player.getModel());
100 properties.put("name", player.getName());
101 properties.put("uid", player.getUuid());
102 properties.put("ip", player.getIpAddr());
104 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
105 .withRepresentationProperty(representationPropertyName).withBridge(bridgeUID)
106 .withLabel(player.getName()).build();
108 thingDiscovered(discoveryResult);
112 private boolean playerThingExists(ThingUID newThingUID) {
113 return squeezeBoxServerHandler.getThing().getThing(newThingUID) != null ? true : false;
117 * Tells the bridge to request a list of players
119 private void setupRequestPlayerJob() {
120 logger.debug("Request player job scheduled to run every {} seconds", TTL);
121 requestPlayerJob = scheduler.scheduleWithFixedDelay(() -> {
122 squeezeBoxServerHandler.requestPlayers();
123 }, 10, TTL, TimeUnit.SECONDS);
126 // we can ignore the other events
128 public void powerChangeEvent(String mac, boolean power) {
132 public void modeChangeEvent(String mac, String mode) {
136 public void absoluteVolumeChangeEvent(String mac, int volume) {
140 public void relativeVolumeChangeEvent(String mac, int volumeChange) {
144 public void muteChangeEvent(String mac, boolean mute) {
148 public void currentPlaylistIndexEvent(String mac, int index) {
152 public void currentPlayingTimeEvent(String mac, int time) {
156 public void durationEvent(String mac, int duration) {
160 public void numberPlaylistTracksEvent(String mac, int track) {
164 public void currentPlaylistShuffleEvent(String mac, int shuffle) {
168 public void currentPlaylistRepeatEvent(String mac, int repeat) {
172 public void titleChangeEvent(String mac, String title) {
176 public void albumChangeEvent(String mac, String album) {
180 public void artistChangeEvent(String mac, String artist) {
184 public void coverArtChangeEvent(String mac, String coverArtUrl) {
188 public void yearChangeEvent(String mac, String year) {
192 public void genreChangeEvent(String mac, String genre) {
196 public void remoteTitleChangeEvent(String mac, String title) {
200 public void irCodeChangeEvent(String mac, String ircode) {
204 public void updateFavoritesListEvent(List<Favorite> favorites) {
208 public void sourceChangeEvent(String mac, String source) {
212 public void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand) {
216 public void connectedStateChangeEvent(String mac, boolean connected) {