]> git.basschouten.com Git - openhab-addons.git/blob
53085b219ceac4d3d72dfd54c1702e0d61f390a5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.Map;
17 import java.util.Set;
18
19 import javax.jmdns.ServiceInfo;
20
21 import org.openhab.binding.freebox.internal.FreeboxBindingConstants;
22 import org.openhab.binding.freebox.internal.config.FreeboxServerConfiguration;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link FreeboxServerDiscoveryParticipant} is responsible for discovering
35  * the Freebox Server (bridge) thing using mDNS discovery service
36  *
37  * @author Laurent Garnier - Initial contribution
38  */
39 @Component
40 public class FreeboxServerDiscoveryParticipant implements MDNSDiscoveryParticipant {
41
42     private final Logger logger = LoggerFactory.getLogger(FreeboxServerDiscoveryParticipant.class);
43
44     private static final String SERVICE_TYPE = "_fbx-api._tcp.local.";
45
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return FreeboxBindingConstants.SUPPORTED_BRIDGE_TYPES_UIDS;
49     }
50
51     @Override
52     public String getServiceType() {
53         return SERVICE_TYPE;
54     }
55
56     @Override
57     public ThingUID getThingUID(ServiceInfo service) {
58         if ((service.getType() != null) && service.getType().equals(getServiceType())
59                 && (service.getPropertyString("uid") != null)) {
60             return new ThingUID(FreeboxBindingConstants.FREEBOX_BRIDGE_TYPE_SERVER,
61                     service.getPropertyString("uid").replaceAll("[^A-Za-z0-9_]", "_"));
62         }
63         return null;
64     }
65
66     @Override
67     public DiscoveryResult createResult(ServiceInfo service) {
68         logger.debug("createResult ServiceInfo: {}", service);
69         DiscoveryResult result = null;
70         String ip = null;
71         if (service.getHostAddresses() != null && service.getHostAddresses().length > 0
72                 && !service.getHostAddresses()[0].isEmpty()) {
73             ip = service.getHostAddresses()[0];
74         }
75         ThingUID thingUID = getThingUID(service);
76         if (thingUID != null && ip != null) {
77             logger.info("Created a DiscoveryResult for Freebox Server {} on IP {}", thingUID, ip);
78             Map<String, Object> properties = new HashMap<>(1);
79             properties.put(FreeboxServerConfiguration.FQDN, ip + ":" + service.getPort());
80             properties.put(FreeboxServerConfiguration.USE_ONLY_HTTP, "true");
81             if (service.getPropertyString("device_type") != null) {
82                 properties.put(Thing.PROPERTY_HARDWARE_VERSION, service.getPropertyString("device_type"));
83             }
84             if (service.getPropertyString("api_base_url") != null) {
85                 properties.put(FreeboxBindingConstants.API_BASE_URL, service.getPropertyString("api_base_url"));
86             }
87             if (service.getPropertyString("api_version") != null) {
88                 properties.put(FreeboxBindingConstants.API_VERSION, service.getPropertyString("api_version"));
89             }
90             result = DiscoveryResultBuilder.create(thingUID).withProperties(properties).withLabel(service.getName())
91                     .build();
92         }
93         return result;
94     }
95 }