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.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;
36 * The {@link MieleMDNSDiscoveryParticipant} is responsible for discovering Miele XGW3000 Gateways. It uses the central
37 * {@link MDNSDiscoveryService}.
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)
44 public class MieleMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
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";
52 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53 return Collections.singleton(MieleBindingConstants.THING_TYPE_XGW3000);
57 public String getServiceType() {
58 return "_mieleathome._tcp.local.";
62 public DiscoveryResult createResult(ServiceInfo service) {
63 if (isMieleGateway(service)) {
64 ThingUID uid = getThingUID(service);
67 Map<String, Object> properties = new HashMap<>(2);
69 InetAddress[] addresses = service.getInetAddresses();
70 if (addresses.length > 0 && addresses[0] != null) {
71 properties.put(MieleBindingConstants.HOST, addresses[0].getHostAddress());
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 : '{}'",
84 return DiscoveryResultBuilder.create(uid).withProperties(properties)
85 .withRepresentationProperty(MieleBindingConstants.HOST).withLabel("Miele XGW3000").build();
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(" ", "_"));
104 * Checks if service is a Miele XGW3000 Gateway
106 * application must be mieleathome
107 * must contain path with value /rest/
109 * @param service the service to check
110 * @return true, if the discovered service is a Miele XGW3000 Gateway
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);