2 * Copyright (c) 2010-2023 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.mihome.internal.socket;
15 import java.io.IOException;
16 import java.net.DatagramSocket;
17 import java.net.InetAddress;
18 import java.net.UnknownHostException;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * Takes care of the discovery communication with the MiHome gateway
27 * @author Dieter Schmidt - Initial contribution
31 public class XiaomiDiscoverySocket extends XiaomiSocket {
33 private static final int MCAST_PORT = 4321;
35 private final Logger logger = LoggerFactory.getLogger(XiaomiDiscoverySocket.class);
37 public XiaomiDiscoverySocket(String owner) {
42 * Sets up the {@link XiaomiDiscoverySocket}.
44 * Connects the socket to the specific multicast address and port.
47 protected void setupSocket() {
48 synchronized (XiaomiDiscoverySocket.class) {
50 logger.debug("Setup discovery socket");
51 DatagramSocket socket = new DatagramSocket(0);
53 logger.debug("Initialized socket to {}:{} on {}:{}", socket.getInetAddress(), socket.getPort(),
54 socket.getLocalAddress(), socket.getLocalPort());
55 } catch (IOException e) {
56 logger.error("Setup socket error", e);
62 * Sends a message through the {@link XiaomiDiscoverySocket}
63 * to the MiHome multicast address 224.0.0.50 and port 4321
65 * @param message - Message to be sent
67 public void sendMessage(String message) {
69 sendMessage(message, InetAddress.getByName(MCAST_ADDR), MCAST_PORT);
70 } catch (UnknownHostException e) {
71 logger.error("Sending error", e);