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.DatagramPacket;
17 import java.net.DatagramSocket;
18 import java.net.InetAddress;
19 import java.nio.charset.StandardCharsets;
21 import java.util.concurrent.CopyOnWriteArraySet;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.mihome.internal.discovery.XiaomiBridgeDiscoveryService;
26 import org.openhab.binding.mihome.internal.handler.XiaomiBridgeHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
34 * Takes care of the communication with MiHome devices.
37 * @author Patrick Boos - Initial contribution
38 * @author Dieter Schmidt - JavaDoc, refactored, reviewed
42 public abstract class XiaomiSocket {
44 static final String MCAST_ADDR = "224.0.0.50";
46 private static final int BUFFER_LENGTH = 1024;
48 private final Logger logger = LoggerFactory.getLogger(XiaomiSocket.class);
50 private final DatagramPacket datagramPacket = new DatagramPacket(new byte[BUFFER_LENGTH], BUFFER_LENGTH);
51 private final Set<XiaomiSocketListener> listeners = new CopyOnWriteArraySet<>();
53 private final int port;
54 private @Nullable DatagramSocket socket;
55 private final Thread socketReceiveThread = new Thread(this::receiveData);
58 * Sets up a {@link XiaomiSocket} with the MiHome multicast address and a random port
60 * @param owner identifies the socket owner
62 public XiaomiSocket(String owner) {
67 * Sets up a {@link XiaomiSocket} with the MiHome multicast address and a specific port
69 * @param port the socket will be bound to this port
70 * @param owner identifies the socket owner
72 public XiaomiSocket(int port, String owner) {
74 socketReceiveThread.setName("XiaomiSocketReceiveThread(" + port + ", " + owner + ")");
77 public void initialize() {
79 if (!socketReceiveThread.isAlive()) {
80 logger.trace("Starting receiver thread {}", socketReceiveThread);
81 socketReceiveThread.start();
85 protected abstract void setupSocket();
88 * Interrupts the {@link ReceiverThread} and closes the {@link XiaomiSocket}.
90 private void closeSocket() {
91 synchronized (XiaomiSocket.class) {
92 logger.debug("Interrupting receiver thread {}", socketReceiveThread);
93 socketReceiveThread.interrupt();
95 DatagramSocket socket = this.socket;
97 logger.debug("Closing socket {}", socket);
105 * Registers a {@link XiaomiSocketListener} to be called back, when data is received.
106 * If no {@link XiaomiSocket} exists, when the method is called, it is being set up.
108 * @param listener {@link XiaomiSocketListener} to be called back
110 public synchronized void registerListener(XiaomiSocketListener listener) {
111 if (listeners.add(listener)) {
112 logger.trace("Added socket listener {}", listener);
115 DatagramSocket socket = this.socket;
116 if (socket == null) {
122 * Unregisters a {@link XiaomiSocketListener}. If there are no listeners left,
123 * the {@link XiaomiSocket} is being closed.
125 * @param listener {@link XiaomiSocketListener} to be unregistered
127 public synchronized void unregisterListener(XiaomiSocketListener listener) {
128 if (listeners.remove(listener)) {
129 logger.trace("Removed socket listener {}", listener);
132 if (listeners.isEmpty()) {
138 * Sends a message through the {@link XiaomiSocket} to a specific address and port
140 * @param message the message to be sent
141 * @param address the message destination address
142 * @param port the message destination port
144 public void sendMessage(String message, InetAddress address, int port) {
145 DatagramSocket socket = this.socket;
146 if (socket == null) {
147 logger.error("Error while sending message (socket is null)");
152 byte[] sendData = message.getBytes(StandardCharsets.UTF_8);
153 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
154 logger.trace("Sending message: {} to {}:{}", message, address, port);
155 socket.send(sendPacket);
156 } catch (IOException e) {
157 logger.error("Error while sending message", e);
162 * @return the port number of this {@link XiaomiSocket}
164 public int getPort() {
168 protected @Nullable DatagramSocket getSocket() {
172 protected void setSocket(DatagramSocket socket) {
173 this.socket = socket;
177 * This method is the main method of the receiver thread for the {@link XiaomiBridgeSocket}.
178 * If the socket has data, it parses the data to a json object and calls all
179 * {@link XiaomiSocketListener} and passes the data to them.
181 private void receiveData() {
182 DatagramSocket socket = this.socket;
183 if (socket == null) {
184 logger.error("Failed to receive data (socket is null)");
188 Thread currentThread = Thread.currentThread();
189 int localPort = socket.getLocalPort();
192 while (!currentThread.isInterrupted()) {
193 logger.trace("Thread {} waiting for data on port {}", currentThread.getName(), localPort);
194 socket.receive(datagramPacket);
195 InetAddress address = datagramPacket.getAddress();
196 logger.debug("Received Datagram from {}:{} on port {}", address.getHostAddress(),
197 datagramPacket.getPort(), localPort);
198 String sentence = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
199 JsonObject message = JsonParser.parseString(sentence).getAsJsonObject();
200 notifyListeners(message, address);
201 logger.trace("Data received and notified {} listeners", listeners.size());
203 } catch (IOException e) {
204 if (!currentThread.isInterrupted()) {
205 logger.error("Error while receiving", e);
207 logger.trace("Receiver thread was interrupted");
210 logger.debug("Receiver thread ended");
214 * Notifies all {@link XiaomiSocketListener} on the parent {@link XiaomiSocket}. First checks for any matching
215 * {@link XiaomiBridgeHandler}, before passing to any {@link XiaomiBridgeDiscoveryService}.
217 * @param message the data message as {@link JsonObject}
218 * @param address the address from which the message was received
220 private void notifyListeners(JsonObject message, InetAddress address) {
221 for (XiaomiSocketListener listener : listeners) {
222 if (listener instanceof XiaomiBridgeHandler) {
223 if (((XiaomiBridgeHandler) listener).getHost().equals(address)) {
224 listener.onDataReceived(message);
226 } else if (listener instanceof XiaomiBridgeDiscoveryService) {
227 listener.onDataReceived(message);