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.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;
42 * The {@link FreeboxDiscoveryService} is responsible for discovering all things
43 * except the Freebox Server thing itself
45 * @author Laurent Garnier - Initial contribution
46 * @author Laurent Garnier - add discovery settings
47 * @author Laurent Garnier - use new internal API manager
49 public class FreeboxDiscoveryService extends AbstractDiscoveryService implements FreeboxDataListener {
51 private final Logger logger = LoggerFactory.getLogger(FreeboxDiscoveryService.class);
53 private static final int SEARCH_TIME = 10;
55 private static final String PHONE_ID = "wired";
57 private FreeboxHandler bridgeHandler;
58 private boolean discoverPhone;
59 private boolean discoverNetDevice;
60 private boolean discoverNetInterface;
61 private boolean discoverAirPlayReceiver;
64 * Creates a FreeboxDiscoveryService with background discovery disabled.
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;
76 public void activate(@Nullable Map<@NonNull String, @Nullable Object> configProperties) {
77 super.activate(configProperties);
78 applyConfig(configProperties);
79 bridgeHandler.registerDataListener(this);
83 public void deactivate() {
84 bridgeHandler.unregisterDataListener(this);
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();
95 property = configProperties.get(FreeboxServerConfiguration.DISCOVER_NET_DEVICE);
96 if (property != null) {
97 discoverNetDevice = ((Boolean) property).booleanValue();
99 property = configProperties.get(FreeboxServerConfiguration.DISCOVER_NET_INTERFACE);
100 if (property != null) {
101 discoverNetInterface = ((Boolean) property).booleanValue();
103 property = configProperties.get(FreeboxServerConfiguration.DISCOVER_AIRPLAY_RECEIVER);
104 if (property != null) {
105 discoverAirPlayReceiver = ((Boolean) property).booleanValue();
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);
115 protected void startScan() {
116 logger.debug("Starting Freebox discovery scan");
117 if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
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);
129 public void onDataFetched(ThingUID bridge, List<FreeboxLanHost> lanHosts,
130 List<FreeboxAirMediaReceiver> airPlayDevices) {
131 if (bridge == null) {
136 DiscoveryResult discoveryResult;
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")
144 thingDiscovered(discoveryResult);
147 if (lanHosts != null && (discoverNetDevice || discoverNetInterface)) {
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)
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);
164 properties.put(FreeboxNetDeviceConfiguration.MAC_ADDRESS, mac);
165 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
166 .withBridge(bridge).withLabel(name).build();
167 thingDiscovered(discoveryResult);
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,
179 if (primaryName != null && !primaryName.isEmpty()) {
180 name += " (" + (primaryName + ")");
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);
187 properties.put(FreeboxNetInterfaceConfiguration.IP_ADDRESS, addr);
188 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
189 .withBridge(bridge).withLabel(name).build();
190 thingDiscovered(discoveryResult);
198 if (airPlayDevices != null && discoverAirPlayReceiver) {
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);