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