]> git.basschouten.com Git - openhab-addons.git/blob
d744c7a87efb24f003e50690129a677903791ca7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.freebox.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.freebox.internal.FreeboxBindingConstants;
22 import org.openhab.binding.freebox.internal.FreeboxDataListener;
23 import org.openhab.binding.freebox.internal.api.FreeboxException;
24 import org.openhab.binding.freebox.internal.api.model.FreeboxAirMediaReceiver;
25 import org.openhab.binding.freebox.internal.api.model.FreeboxLanHost;
26 import org.openhab.binding.freebox.internal.api.model.FreeboxLanHostL3Connectivity;
27 import org.openhab.binding.freebox.internal.config.FreeboxAirPlayDeviceConfiguration;
28 import org.openhab.binding.freebox.internal.config.FreeboxNetDeviceConfiguration;
29 import org.openhab.binding.freebox.internal.config.FreeboxNetInterfaceConfiguration;
30 import org.openhab.binding.freebox.internal.config.FreeboxServerConfiguration;
31 import org.openhab.binding.freebox.internal.handler.FreeboxHandler;
32 import org.openhab.core.config.discovery.AbstractDiscoveryService;
33 import org.openhab.core.config.discovery.DiscoveryResult;
34 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
35 import org.openhab.core.thing.Thing;
36 import org.openhab.core.thing.ThingStatus;
37 import org.openhab.core.thing.ThingUID;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link FreeboxDiscoveryService} is responsible for discovering all things
43  * except the Freebox Server thing itself
44  *
45  * @author Laurent Garnier - Initial contribution
46  * @author Laurent Garnier - add discovery settings
47  * @author Laurent Garnier - use new internal API manager
48  */
49 public class FreeboxDiscoveryService extends AbstractDiscoveryService implements FreeboxDataListener {
50
51     private final Logger logger = LoggerFactory.getLogger(FreeboxDiscoveryService.class);
52
53     private static final int SEARCH_TIME = 10;
54
55     private static final String PHONE_ID = "wired";
56
57     private FreeboxHandler bridgeHandler;
58     private boolean discoverPhone;
59     private boolean discoverNetDevice;
60     private boolean discoverNetInterface;
61     private boolean discoverAirPlayReceiver;
62
63     /**
64      * Creates a FreeboxDiscoveryService with background discovery disabled.
65      */
66     public FreeboxDiscoveryService(FreeboxHandler freeboxBridgeHandler) {
67         super(FreeboxBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
68         this.bridgeHandler = freeboxBridgeHandler;
69         this.discoverPhone = true;
70         this.discoverNetDevice = true;
71         this.discoverNetInterface = true;
72         this.discoverAirPlayReceiver = true;
73     }
74
75     @Override
76     public void activate(@Nullable Map<@NonNull String, @Nullable Object> configProperties) {
77         super.activate(configProperties);
78         applyConfig(configProperties);
79         bridgeHandler.registerDataListener(this);
80     }
81
82     @Override
83     public void deactivate() {
84         bridgeHandler.unregisterDataListener(this);
85         super.deactivate();
86     }
87
88     @Override
89     public void applyConfig(Map<String, Object> configProperties) {
90         if (configProperties != null) {
91             Object property = configProperties.get(FreeboxServerConfiguration.DISCOVER_PHONE);
92             if (property != null) {
93                 discoverPhone = ((Boolean) property).booleanValue();
94             }
95             property = configProperties.get(FreeboxServerConfiguration.DISCOVER_NET_DEVICE);
96             if (property != null) {
97                 discoverNetDevice = ((Boolean) property).booleanValue();
98             }
99             property = configProperties.get(FreeboxServerConfiguration.DISCOVER_NET_INTERFACE);
100             if (property != null) {
101                 discoverNetInterface = ((Boolean) property).booleanValue();
102             }
103             property = configProperties.get(FreeboxServerConfiguration.DISCOVER_AIRPLAY_RECEIVER);
104             if (property != null) {
105                 discoverAirPlayReceiver = ((Boolean) property).booleanValue();
106             }
107         }
108         logger.debug("Freebox discovery - discoverPhone : {}", discoverPhone);
109         logger.debug("Freebox discovery - discoverNetDevice : {}", discoverNetDevice);
110         logger.debug("Freebox discovery - discoverNetInterface : {}", discoverNetInterface);
111         logger.debug("Freebox discovery - discoverAirPlayReceiver : {}", discoverAirPlayReceiver);
112     }
113
114     @Override
115     protected void startScan() {
116         logger.debug("Starting Freebox discovery scan");
117         if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
118             try {
119                 List<FreeboxLanHost> lanHosts = bridgeHandler.getApiManager().getLanHosts();
120                 List<FreeboxAirMediaReceiver> airPlayDevices = bridgeHandler.getApiManager().getAirMediaReceivers();
121                 onDataFetched(bridgeHandler.getThing().getUID(), lanHosts, airPlayDevices);
122             } catch (FreeboxException e) {
123                 logger.warn("Error while requesting data for things discovery", e);
124             }
125         }
126     }
127
128     @Override
129     public void onDataFetched(ThingUID bridge, List<FreeboxLanHost> lanHosts,
130             List<FreeboxAirMediaReceiver> airPlayDevices) {
131         if (bridge == null) {
132             return;
133         }
134
135         ThingUID thingUID;
136         DiscoveryResult discoveryResult;
137
138         if (discoverPhone) {
139             // Phone
140             thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_PHONE, bridge, PHONE_ID);
141             logger.trace("Adding new Freebox Phone {} to inbox", thingUID);
142             discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridge).withLabel("Wired phone")
143                     .build();
144             thingDiscovered(discoveryResult);
145         }
146
147         if (lanHosts != null && (discoverNetDevice || discoverNetInterface)) {
148             // Network devices
149             for (FreeboxLanHost host : lanHosts) {
150                 String mac = host.getMAC();
151                 String primaryName = host.getPrimaryName();
152                 String vendorName = host.getVendorName();
153                 if (mac != null && !mac.isEmpty()) {
154                     if (discoverNetDevice) {
155                         String uid = mac.replaceAll("[^A-Za-z0-9_]", "_");
156                         thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_NET_DEVICE, bridge, uid);
157                         String name = (primaryName == null || primaryName.isEmpty()) ? ("Freebox Network Device " + mac)
158                                 : primaryName;
159                         logger.trace("Adding new Freebox Network Device {} to inbox", thingUID);
160                         Map<String, Object> properties = new HashMap<>(1);
161                         if (vendorName != null && !vendorName.isEmpty()) {
162                             properties.put(Thing.PROPERTY_VENDOR, vendorName);
163                         }
164                         properties.put(FreeboxNetDeviceConfiguration.MAC_ADDRESS, mac);
165                         discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
166                                 .withBridge(bridge).withLabel(name).build();
167                         thingDiscovered(discoveryResult);
168                     }
169
170                     // Network interfaces
171                     if (host.getL3Connectivities() != null && discoverNetInterface) {
172                         for (FreeboxLanHostL3Connectivity l3 : host.getL3Connectivities()) {
173                             String addr = l3.getAddr();
174                             if (addr != null && !addr.isEmpty()) {
175                                 String uid = addr.replaceAll("[^A-Za-z0-9_]", "_");
176                                 thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_NET_INTERFACE,
177                                         bridge, uid);
178                                 String name = addr;
179                                 if (primaryName != null && !primaryName.isEmpty()) {
180                                     name += " (" + (primaryName + ")");
181                                 }
182                                 logger.trace("Adding new Freebox Network Interface {} to inbox", thingUID);
183                                 Map<String, Object> properties = new HashMap<>(1);
184                                 if (vendorName != null && !vendorName.isEmpty()) {
185                                     properties.put(Thing.PROPERTY_VENDOR, vendorName);
186                                 }
187                                 properties.put(FreeboxNetInterfaceConfiguration.IP_ADDRESS, addr);
188                                 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
189                                         .withBridge(bridge).withLabel(name).build();
190                                 thingDiscovered(discoveryResult);
191                             }
192                         }
193                     }
194                 }
195             }
196         }
197
198         if (airPlayDevices != null && discoverAirPlayReceiver) {
199             // AirPlay devices
200             for (FreeboxAirMediaReceiver device : airPlayDevices) {
201                 String name = device.getName();
202                 boolean videoCapable = device.isVideoCapable();
203                 logger.debug("AirPlay Device name {} video capable {}", name, videoCapable);
204                 // The Freebox API allows pushing media only to receivers with photo or video capabilities
205                 // but not to receivers with only audio capability; so receivers without video capability
206                 // are ignored by the discovery
207                 if (name != null && !name.isEmpty() && videoCapable) {
208                     String uid = name.replaceAll("[^A-Za-z0-9_]", "_");
209                     thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_AIRPLAY, bridge, uid);
210                     logger.trace("Adding new Freebox AirPlay Device {} to inbox", thingUID);
211                     Map<String, Object> properties = new HashMap<>(1);
212                     properties.put(FreeboxAirPlayDeviceConfiguration.NAME, name);
213                     discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
214                             .withBridge(bridge).withLabel(name + " (AirPlay)").build();
215                     thingDiscovered(discoveryResult);
216                 }
217             }
218         }
219     }
220 }