]> git.basschouten.com Git - openhab-addons.git/blob
1e095cad4078957c62c4c1a04428eeaf551e741f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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, player.macAddress.replace(":", ""));
89
90         if (!playerThingExists(thingUID)) {
91             logger.debug("player added {} : {} ", player.macAddress, player.name);
92
93             Map<String, Object> properties = new HashMap<>(1);
94             String representationPropertyName = "mac";
95             properties.put(representationPropertyName, player.macAddress);
96
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);
102
103             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
104                     .withRepresentationProperty(representationPropertyName).withBridge(bridgeUID).withLabel(player.name)
105                     .build();
106
107             thingDiscovered(discoveryResult);
108         }
109     }
110
111     private boolean playerThingExists(ThingUID newThingUID) {
112         return squeezeBoxServerHandler.getThing().getThing(newThingUID) != null;
113     }
114
115     /**
116      * Tells the bridge to request a list of players
117      */
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);
123     }
124
125     // we can ignore the other events
126     @Override
127     public void powerChangeEvent(String mac, boolean power) {
128     }
129
130     @Override
131     public void modeChangeEvent(String mac, String mode) {
132     }
133
134     @Override
135     public void absoluteVolumeChangeEvent(String mac, int volume) {
136     }
137
138     @Override
139     public void relativeVolumeChangeEvent(String mac, int volumeChange) {
140     }
141
142     @Override
143     public void muteChangeEvent(String mac, boolean mute) {
144     }
145
146     @Override
147     public void currentPlaylistIndexEvent(String mac, int index) {
148     }
149
150     @Override
151     public void currentPlayingTimeEvent(String mac, int time) {
152     }
153
154     @Override
155     public void durationEvent(String mac, int duration) {
156     }
157
158     @Override
159     public void numberPlaylistTracksEvent(String mac, int track) {
160     }
161
162     @Override
163     public void currentPlaylistShuffleEvent(String mac, int shuffle) {
164     }
165
166     @Override
167     public void currentPlaylistRepeatEvent(String mac, int repeat) {
168     }
169
170     @Override
171     public void titleChangeEvent(String mac, String title) {
172     }
173
174     @Override
175     public void albumChangeEvent(String mac, String album) {
176     }
177
178     @Override
179     public void artistChangeEvent(String mac, String artist) {
180     }
181
182     @Override
183     public void coverArtChangeEvent(String mac, String coverArtUrl) {
184     }
185
186     @Override
187     public void yearChangeEvent(String mac, String year) {
188     }
189
190     @Override
191     public void genreChangeEvent(String mac, String genre) {
192     }
193
194     @Override
195     public void remoteTitleChangeEvent(String mac, String title) {
196     }
197
198     @Override
199     public void irCodeChangeEvent(String mac, String ircode) {
200     }
201
202     @Override
203     public void updateFavoritesListEvent(List<Favorite> favorites) {
204     }
205
206     @Override
207     public void sourceChangeEvent(String mac, String source) {
208     }
209
210     @Override
211     public void buttonsChangeEvent(String mac, String likeCommand, String unlikeCommand) {
212     }
213
214     @Override
215     public void connectedStateChangeEvent(String mac, boolean connected) {
216     }
217 }