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