2 * Copyright (c) 2010-2020 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.freebox.internal.discovery;
15 import java.util.HashMap;
16 import java.util.List;
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;
40 * The {@link FreeboxDiscoveryService} is responsible for discovering all things
41 * except the Freebox Server thing itself
43 * @author Laurent Garnier - Initial contribution
44 * @author Laurent Garnier - add discovery settings
45 * @author Laurent Garnier - use new internal API manager
47 public class FreeboxDiscoveryService extends AbstractDiscoveryService implements FreeboxDataListener {
49 private final Logger logger = LoggerFactory.getLogger(FreeboxDiscoveryService.class);
51 private static final int SEARCH_TIME = 10;
53 private static final String PHONE_ID = "wired";
55 private FreeboxHandler bridgeHandler;
56 private boolean discoverPhone;
57 private boolean discoverNetDevice;
58 private boolean discoverNetInterface;
59 private boolean discoverAirPlayReceiver;
62 * Creates a FreeboxDiscoveryService with background discovery disabled.
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;
74 public void activate(Map<String, Object> configProperties) {
75 super.activate(configProperties);
76 applyConfig(configProperties);
77 bridgeHandler.registerDataListener(this);
81 public void deactivate() {
82 bridgeHandler.unregisterDataListener(this);
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();
93 property = configProperties.get(FreeboxServerConfiguration.DISCOVER_NET_DEVICE);
94 if (property != null) {
95 discoverNetDevice = ((Boolean) property).booleanValue();
97 property = configProperties.get(FreeboxServerConfiguration.DISCOVER_NET_INTERFACE);
98 if (property != null) {
99 discoverNetInterface = ((Boolean) property).booleanValue();
101 property = configProperties.get(FreeboxServerConfiguration.DISCOVER_AIRPLAY_RECEIVER);
102 if (property != null) {
103 discoverAirPlayReceiver = ((Boolean) property).booleanValue();
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);
113 protected void startScan() {
114 logger.debug("Starting Freebox discovery scan");
115 if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
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);
127 public void onDataFetched(ThingUID bridge, List<FreeboxLanHost> lanHosts,
128 List<FreeboxAirMediaReceiver> airPlayDevices) {
129 if (bridge == null) {
134 DiscoveryResult discoveryResult;
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")
142 thingDiscovered(discoveryResult);
145 if (lanHosts != null && (discoverNetDevice || discoverNetInterface)) {
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)
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);
162 properties.put(FreeboxNetDeviceConfiguration.MAC_ADDRESS, mac);
163 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
164 .withBridge(bridge).withLabel(name).build();
165 thingDiscovered(discoveryResult);
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,
177 if (primaryName != null && !primaryName.isEmpty()) {
178 name += " (" + (primaryName + ")");
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);
185 properties.put(FreeboxNetInterfaceConfiguration.IP_ADDRESS, addr);
186 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
187 .withBridge(bridge).withLabel(name).build();
188 thingDiscovered(discoveryResult);
196 if (airPlayDevices != null && discoverAirPlayReceiver) {
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);