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