]> git.basschouten.com Git - openhab-addons.git/blob
70caec5fa5c30a79fcc7eab750fd9c2c96d15be4
[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.miele.internal.discovery;
14
15 import java.io.IOException;
16 import java.net.InetAddress;
17 import java.net.Socket;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import javax.jmdns.ServiceInfo;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.miele.internal.MieleBindingConstants;
27 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Modified;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link MieleMDNSDiscoveryParticipant} is responsible for discovering Miele XGW3000 Gateways. It uses the central
41  * {@link org.openhab.core.config.discovery.mdns.internal.MDNSDiscoveryService}.
42  *
43  * @author Karel Goderis - Initial contribution
44  * @author Martin Lepsy - Added check for Miele gateway for cleaner discovery
45  * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN)
46  */
47 @NonNullByDefault
48 @Component(configurationPid = "discovery.miele")
49 public class MieleMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
50
51     private static final String SERVICE_TYPE = "_mieleathome._tcp.local.";
52     private static final String PATH_TO_CHECK_FOR_XGW3000 = "/rest/";
53     private static final String SERVICE_NAME = "mieleathome";
54     private static final String PATH_PROPERTY_NAME = "path";
55
56     private final Logger logger = LoggerFactory.getLogger(MieleMDNSDiscoveryParticipant.class);
57
58     private long removalGracePeriodSeconds = 15;
59
60     @Activate
61     public void activate(@Nullable Map<String, Object> configProperties) {
62         if (configProperties == null) {
63             return;
64         }
65         updateRemovalGracePeriod(configProperties);
66     }
67
68     @Modified
69     public void modified(@Nullable Map<String, Object> configProperties) {
70         if (configProperties == null) {
71             return;
72         }
73         updateRemovalGracePeriod(configProperties);
74     }
75
76     /**
77      * Update the removalGracePeriodSeconds when the component is activates or modified.
78      *
79      * @param configProperties the passed configuration parameters.
80      */
81     private void updateRemovalGracePeriod(Map<String, Object> configProperties) {
82         Object value = configProperties.get(MieleBindingConstants.REMOVAL_GRACE_PERIOD);
83         if (value != null) {
84             try {
85                 removalGracePeriodSeconds = Integer.parseInt(value.toString());
86             } catch (NumberFormatException e) {
87                 logger.warn("Configuration property '{}' has invalid value: {}",
88                         MieleBindingConstants.REMOVAL_GRACE_PERIOD, value);
89             }
90         }
91     }
92
93     @Override
94     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
95         return MieleBridgeHandler.SUPPORTED_THING_TYPES;
96     }
97
98     @Override
99     public String getServiceType() {
100         return SERVICE_TYPE;
101     }
102
103     @Override
104     public @Nullable DiscoveryResult createResult(ServiceInfo service) {
105         if (!isMieleGateway(service)) {
106             return null;
107         }
108
109         ThingUID uid = getThingUID(service);
110         if (uid == null) {
111             return null;
112         }
113
114         InetAddress[] addresses = service.getInetAddresses();
115         if (addresses.length <= 0 || addresses[0] == null) {
116             return null;
117         }
118
119         String ipAddress = addresses[0].getHostAddress();
120         Map<String, Object> properties = new HashMap<>(2);
121         properties.put(MieleBindingConstants.HOST, ipAddress);
122
123         Socket socket = null;
124         try {
125             socket = new Socket(addresses[0], 80);
126             InetAddress ourAddress = socket.getLocalAddress();
127             String interfaceIpAddress = ourAddress.getHostAddress();
128             socket.close();
129
130             properties.put(MieleBindingConstants.INTERFACE, interfaceIpAddress);
131             logger.debug("Discovered Miele@home gateway with IP address {} and interface IP address {}", ipAddress,
132                     interfaceIpAddress);
133         } catch (IOException e) {
134             logger.warn("An exception occurred while connecting to the Miele Gateway: '{}'", e.getMessage());
135             return null;
136         }
137
138         return DiscoveryResultBuilder.create(uid).withProperties(properties)
139                 .withRepresentationProperty(MieleBindingConstants.HOST).withLabel("@text/discovery.xgw3000.label")
140                 .build();
141     }
142
143     @Override
144     public @Nullable ThingUID getThingUID(ServiceInfo service) {
145         if (SERVICE_TYPE.equals(service.getType())) {
146             return new ThingUID(MieleBindingConstants.THING_TYPE_XGW3000, service.getName().replace(" ", "_"));
147         }
148
149         return null;
150     }
151
152     /**
153      * Checks if service is a Miele XGW 3000 Gateway
154      *
155      * application must be mieleathome
156      * must contain path with value /rest/
157      *
158      * @param service the service to check
159      * @return true, if the discovered service is a Miele XGW3000 Gateway
160      */
161     private boolean isMieleGateway(ServiceInfo serviceInfo) {
162         return serviceInfo.getApplication().contains(SERVICE_NAME)
163                 && PATH_TO_CHECK_FOR_XGW3000.equalsIgnoreCase(serviceInfo.getPropertyString(PATH_PROPERTY_NAME));
164     }
165
166     /**
167      * Miele devices are sometimes a few seconds late in updating their mDNS announcements, which means that they are
168      * repeatedly removed from, and (re)added to, the Inbox. To prevent this, we override this method to specify an
169      * additional delay period (grace period) to wait before the device is removed from the Inbox.
170      */
171     @Override
172     public long getRemovalGracePeriodSeconds(ServiceInfo serviceInfo) {
173         return removalGracePeriodSeconds;
174     }
175 }