]> git.basschouten.com Git - openhab-addons.git/blob
729065323c8ca1efdbe8717c3d061d3134b80187
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.miele.internal.discovery;
14
15 import java.io.IOException;
16 import java.net.InetAddress;
17 import java.net.Socket;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22
23 import javax.jmdns.ServiceInfo;
24
25 import org.openhab.binding.miele.internal.MieleBindingConstants;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * The {@link MieleMDNSDiscoveryParticipant} is responsible for discovering Miele XGW3000 Gateways. It uses the central
37  * {@link MDNSDiscoveryService}.
38  *
39  * @author Karel Goderis - Initial contribution
40  * @author Martin Lepsy - Added check for Miele gateway for cleaner discovery
41  * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN)
42  */
43 @Component
44 public class MieleMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
45
46     private final Logger logger = LoggerFactory.getLogger(MieleMDNSDiscoveryParticipant.class);
47     private static final String PATH_TO_CHECK_FOR_XGW3000 = "/rest/";
48     private static final String SERVICE_NAME = "mieleathome";
49     private static final String PATH_PROPERTY_NAME = "path";
50
51     @Override
52     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53         return Collections.singleton(MieleBindingConstants.THING_TYPE_XGW3000);
54     }
55
56     @Override
57     public String getServiceType() {
58         return "_mieleathome._tcp.local.";
59     }
60
61     @Override
62     public DiscoveryResult createResult(ServiceInfo service) {
63         if (isMieleGateway(service)) {
64             ThingUID uid = getThingUID(service);
65
66             if (uid != null) {
67                 Map<String, Object> properties = new HashMap<>(2);
68
69                 InetAddress[] addresses = service.getInetAddresses();
70                 if (addresses.length > 0 && addresses[0] != null) {
71                     properties.put(MieleBindingConstants.HOST, addresses[0].getHostAddress());
72
73                     Socket socket = null;
74                     try {
75                         socket = new Socket(addresses[0], 80);
76                         InetAddress ourAddress = socket.getLocalAddress();
77                         properties.put(MieleBindingConstants.INTERFACE, ourAddress.getHostAddress());
78                     } catch (IOException e) {
79                         logger.error("An exception occurred while connecting to the Miele Gateway : '{}'",
80                                 e.getMessage());
81                     }
82                 }
83
84                 return DiscoveryResultBuilder.create(uid).withProperties(properties)
85                         .withRepresentationProperty(MieleBindingConstants.HOST).withLabel("Miele XGW3000").build();
86             }
87         }
88         return null;
89     }
90
91     @Override
92     public ThingUID getThingUID(ServiceInfo service) {
93         if (service.getType() != null) {
94             if (service.getType().equals(getServiceType())) {
95                 logger.trace("Discovered a Miele@Home gateway thing with name '{}'", service.getName());
96                 return new ThingUID(MieleBindingConstants.THING_TYPE_XGW3000, service.getName().replace(" ", "_"));
97             }
98         }
99
100         return null;
101     }
102
103     /**
104      * Checks if service is a Miele XGW3000 Gateway
105      *
106      * application must be mieleathome
107      * must contain path with value /rest/
108      *
109      * @param service the service to check
110      * @return true, if the discovered service is a Miele XGW3000 Gateway
111      */
112     private boolean isMieleGateway(ServiceInfo service) {
113         return service.getApplication().contains(SERVICE_NAME) && service.getPropertyString(PATH_PROPERTY_NAME) != null
114                 && service.getPropertyString(PATH_PROPERTY_NAME).equalsIgnoreCase(PATH_TO_CHECK_FOR_XGW3000);
115     }
116 }