2 * Copyright (c) 2010-2023 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, player.macAddress.replace(":", ""));
90 if (!playerThingExists(thingUID)) {
91 logger.debug("player added {} : {} ", player.macAddress, player.name);
93 Map<String, Object> properties = new HashMap<>(1);
94 String representationPropertyName = "mac";
95 properties.put(representationPropertyName, player.macAddress);
97 // Added other properties
98 properties.put("modelId", player.model);
99 properties.put("name", player.name);
100 properties.put("uid", player.uuid);
101 properties.put("ip", player.ipAddr);
103 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
104 .withRepresentationProperty(representationPropertyName).withBridge(bridgeUID).withLabel(player.name)
107 thingDiscovered(discoveryResult);
111 private boolean playerThingExists(ThingUID newThingUID) {
112 return squeezeBoxServerHandler.getThing().getThing(newThingUID) != null;
116 * Tells the bridge to request a list of players
118 private void setupRequestPlayerJob() {
119 logger.debug("Request player job scheduled to run every {} seconds", TTL);
120 requestPlayerJob = scheduler.scheduleWithFixedDelay(() -> {
121 squeezeBoxServerHandler.requestPlayers();
122 }, 10, TTL, TimeUnit.SECONDS);
125 // we can ignore the other events
127 public void powerChangeEvent(String mac, boolean power) {
131 public void modeChangeEvent(String mac, String mode) {
135 public void absoluteVolumeChangeEvent(String mac, int volume) {
139 public void relativeVolumeChangeEvent(String mac, int volumeChange) {
143 public void muteChangeEvent(String mac, boolean mute) {
147 public void currentPlaylistIndexEvent(String mac, int index) {
151 public void currentPlayingTimeEvent(String mac, int time) {
155 public void durationEvent(String mac, int duration) {
159 public void numberPlaylistTracksEvent(String mac, int track) {
163 public void currentPlaylistShuffleEvent(String mac, int shuffle) {
167 public void currentPlaylistRepeatEvent(String mac, int repeat) {
171 public void titleChangeEvent(String mac, String title) {
175 public void albumChangeEvent(String mac, String album) {
179 public void artistChangeEvent(String mac, String artist) {
183 public void coverArtChangeEvent(String mac, String coverArtUrl) {
187 public void yearChangeEvent(String mac, String year) {
191 public void genreChangeEvent(String mac, String genre) {
195 public void remoteTitleChangeEvent(String mac, String title) {
199 public void irCodeChangeEvent(String mac, String ircode) {
203 public void updateFavoritesListEvent(List<Favorite> favorites) {
207 public void sourceChangeEvent(String mac, String source) {
211 public void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand) {
215 public void connectedStateChangeEvent(String mac, boolean connected) {