2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.miele.internal.discovery;
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;
23 import javax.jmdns.ServiceInfo;
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;
37 * The {@link MieleMDNSDiscoveryParticipant} is responsible for discovering Miele XGW3000 Gateways. It uses the central
38 * {@link MDNSDiscoveryService}.
40 * @author Karel Goderis - Initial contribution
41 * @author Martin Lepsy - Added check for Miele gateway for cleaner discovery
45 public class MieleMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
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";
53 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
54 return Collections.singleton(MieleBindingConstants.THING_TYPE_XGW3000);
58 public String getServiceType() {
59 return "_mieleathome._tcp.local.";
63 public DiscoveryResult createResult(ServiceInfo service) {
64 if (isMieleGateway(service)) {
65 ThingUID uid = getThingUID(service);
68 Map<String, Object> properties = new HashMap<>(2);
70 InetAddress[] addresses = service.getInetAddresses();
71 if (addresses.length > 0 && addresses[0] != null) {
72 properties.put(MieleBindingConstants.HOST, addresses[0].getHostAddress());
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 : '{}'",
85 return DiscoveryResultBuilder.create(uid).withProperties(properties)
86 .withRepresentationProperty(MieleBindingConstants.HOST).withLabel("Miele XGW3000 Gateway")
94 public ThingUID getThingUID(ServiceInfo service) {
95 if (service.getType() != null) {
96 if (service.getType().equals(getServiceType())) {
97 logger.trace("Discovered a Miele@Home gateway thing with name '{}'", service.getName());
98 return new ThingUID(MieleBindingConstants.THING_TYPE_XGW3000, service.getName().replace(" ", "_"));
106 * Checks if service is a Miele XGW3000 Gateway
108 * application must be mieleathome
109 * must contain path with value /rest/
111 * @param service the service to check
112 * @return true, if the discovered service is a Miele XGW3000 Gateway
114 private boolean isMieleGateway(ServiceInfo service) {
115 return service.getApplication().contains(SERVICE_NAME) && service.getPropertyString(PATH_PROPERTY_NAME) != null
116 && service.getPropertyString(PATH_PROPERTY_NAME).equalsIgnoreCase(PATH_TO_CHECK_FOR_XGW3000);