]> git.basschouten.com Git - openhab-addons.git/blob
5cd1b03bdb7b4f8f042c146dbd192862ef21f229
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.squeezebox.internal.discovery;
14
15 import static org.openhab.binding.squeezebox.internal.SqueezeBoxBindingConstants.SQUEEZEBOXPLAYER_THING_TYPE;
16
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22
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;
34
35 /**
36  * When a {@link SqueezeBoxServerHandler} finds a new SqueezeBox Player we will
37  * add it to the system.
38  *
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
43  *
44  */
45 public class SqueezeBoxPlayerDiscoveryParticipant extends AbstractDiscoveryService
46         implements SqueezeBoxPlayerEventListener {
47     private final Logger logger = LoggerFactory.getLogger(SqueezeBoxPlayerDiscoveryParticipant.class);
48
49     private static final int TIMEOUT = 60;
50     private static final int TTL = 60;
51
52     private SqueezeBoxServerHandler squeezeBoxServerHandler;
53     private ScheduledFuture<?> requestPlayerJob;
54
55     /**
56      * Discovers SqueezeBox Players attached to a SqueezeBox Server
57      *
58      * @param squeezeBoxServerHandler
59      */
60     public SqueezeBoxPlayerDiscoveryParticipant(SqueezeBoxServerHandler squeezeBoxServerHandler) {
61         super(SqueezeBoxPlayerHandler.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
62         this.squeezeBoxServerHandler = squeezeBoxServerHandler;
63         setupRequestPlayerJob();
64     }
65
66     @Override
67     protected void startScan() {
68         logger.debug("startScan invoked in SqueezeBoxPlayerDiscoveryParticipant");
69         this.squeezeBoxServerHandler.requestPlayers();
70         this.squeezeBoxServerHandler.requestFavorites();
71     }
72
73     /*
74      * Allows request player job to be canceled when server handler is removed
75      */
76     public void cancelRequestPlayerJob() {
77         logger.debug("canceling RequestPlayerJob");
78         if (requestPlayerJob != null) {
79             requestPlayerJob.cancel(true);
80             requestPlayerJob = null;
81         }
82     }
83
84     @Override
85     public void playerAdded(SqueezeBoxPlayer player) {
86         ThingUID bridgeUID = squeezeBoxServerHandler.getThing().getUID();
87
88         ThingUID thingUID = new ThingUID(SQUEEZEBOXPLAYER_THING_TYPE, bridgeUID,
89                 player.getMacAddress().replace(":", ""));
90
91         if (!playerThingExists(thingUID)) {
92             logger.debug("player added {} : {} ", player.getMacAddress(), player.getName());
93
94             Map<String, Object> properties = new HashMap<>(1);
95             String representationPropertyName = "mac";
96             properties.put(representationPropertyName, player.getMacAddress());
97
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());
103
104             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
105                     .withRepresentationProperty(representationPropertyName).withBridge(bridgeUID)
106                     .withLabel(player.getName()).build();
107
108             thingDiscovered(discoveryResult);
109         }
110     }
111
112     private boolean playerThingExists(ThingUID newThingUID) {
113         return squeezeBoxServerHandler.getThing().getThing(newThingUID) != null ? true : false;
114     }
115
116     /**
117      * Tells the bridge to request a list of players
118      */
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);
124     }
125
126     // we can ignore the other events
127     @Override
128     public void powerChangeEvent(String mac, boolean power) {
129     }
130
131     @Override
132     public void modeChangeEvent(String mac, String mode) {
133     }
134
135     @Override
136     public void absoluteVolumeChangeEvent(String mac, int volume) {
137     }
138
139     @Override
140     public void relativeVolumeChangeEvent(String mac, int volumeChange) {
141     }
142
143     @Override
144     public void muteChangeEvent(String mac, boolean mute) {
145     }
146
147     @Override
148     public void currentPlaylistIndexEvent(String mac, int index) {
149     }
150
151     @Override
152     public void currentPlayingTimeEvent(String mac, int time) {
153     }
154
155     @Override
156     public void durationEvent(String mac, int duration) {
157     }
158
159     @Override
160     public void numberPlaylistTracksEvent(String mac, int track) {
161     }
162
163     @Override
164     public void currentPlaylistShuffleEvent(String mac, int shuffle) {
165     }
166
167     @Override
168     public void currentPlaylistRepeatEvent(String mac, int repeat) {
169     }
170
171     @Override
172     public void titleChangeEvent(String mac, String title) {
173     }
174
175     @Override
176     public void albumChangeEvent(String mac, String album) {
177     }
178
179     @Override
180     public void artistChangeEvent(String mac, String artist) {
181     }
182
183     @Override
184     public void coverArtChangeEvent(String mac, String coverArtUrl) {
185     }
186
187     @Override
188     public void yearChangeEvent(String mac, String year) {
189     }
190
191     @Override
192     public void genreChangeEvent(String mac, String genre) {
193     }
194
195     @Override
196     public void remoteTitleChangeEvent(String mac, String title) {
197     }
198
199     @Override
200     public void irCodeChangeEvent(String mac, String ircode) {
201     }
202
203     @Override
204     public void updateFavoritesListEvent(List<Favorite> favorites) {
205     }
206
207     @Override
208     public void sourceChangeEvent(String mac, String source) {
209     }
210
211     @Override
212     public void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand) {
213     }
214
215     @Override
216     public void connectedStateChangeEvent(String mac, boolean connected) {
217     }
218 }