]> git.basschouten.com Git - openhab-addons.git/blob
66ddd15d51c13d0b7521f7fc3d2bc9d076b6cb17
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.miio.internal.discovery;
14
15 import static org.openhab.binding.miio.internal.MiIoBindingConstants.*;
16
17 import java.io.IOException;
18 import java.net.DatagramPacket;
19 import java.net.DatagramSocket;
20 import java.net.InetAddress;
21 import java.net.SocketException;
22 import java.util.Arrays;
23 import java.util.Dictionary;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ScheduledFuture;
30 import java.util.concurrent.TimeUnit;
31
32 import org.eclipse.jdt.annotation.NonNullByDefault;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.openhab.binding.miio.internal.Message;
35 import org.openhab.binding.miio.internal.MiIoDevices;
36 import org.openhab.binding.miio.internal.Utils;
37 import org.openhab.binding.miio.internal.cloud.CloudConnector;
38 import org.openhab.binding.miio.internal.cloud.CloudDeviceDTO;
39 import org.openhab.core.config.discovery.AbstractDiscoveryService;
40 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
41 import org.openhab.core.config.discovery.DiscoveryService;
42 import org.openhab.core.net.NetUtil;
43 import org.openhab.core.thing.ThingTypeUID;
44 import org.openhab.core.thing.ThingUID;
45 import org.osgi.service.cm.Configuration;
46 import org.osgi.service.cm.ConfigurationAdmin;
47 import org.osgi.service.component.annotations.Activate;
48 import org.osgi.service.component.annotations.Component;
49 import org.osgi.service.component.annotations.Reference;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * The {@link MiIoDiscovery} is responsible for discovering new Xiaomi Mi IO devices
55  * and their token
56  *
57  * @author Marcel Verpaalen - Initial contribution
58  *
59  */
60 @NonNullByDefault
61 @Component(service = DiscoveryService.class, configurationPid = "discovery.miio")
62 public class MiIoDiscovery extends AbstractDiscoveryService {
63
64     /** The refresh interval for background discovery */
65     private static final long SEARCH_INTERVAL = 600;
66     private static final int BUFFER_LENGTH = 1024;
67     private static final int DISCOVERY_TIME = 10;
68     private static final String DISABLED = "disabled";
69     private static final String SUPPORTED = "supportedonly";
70     private static final String ALL = "all";
71
72     private @Nullable ScheduledFuture<?> miIoDiscoveryJob;
73     protected @Nullable DatagramSocket clientSocket;
74     private @Nullable Thread socketReceiveThread;
75     private Set<String> responseIps = new HashSet<>();
76
77     private final Logger logger = LoggerFactory.getLogger(MiIoDiscovery.class);
78     private final CloudConnector cloudConnector;
79     private Map<String, String> cloudDevices = new ConcurrentHashMap<>();
80     private @Nullable Configuration miioConfig;
81
82     @Activate
83     public MiIoDiscovery(@Reference CloudConnector cloudConnector, @Reference ConfigurationAdmin configAdmin)
84             throws IllegalArgumentException {
85         super(DISCOVERY_TIME);
86         this.cloudConnector = cloudConnector;
87         try {
88             miioConfig = configAdmin.getConfiguration("binding.miio");
89         } catch (IOException | SecurityException e) {
90             logger.debug("Error getting configuration: {}", e.getMessage());
91         }
92     }
93
94     private String getCloudDiscoveryMode() {
95         if (miioConfig != null) {
96             try {
97                 Dictionary<String, @Nullable Object> properties = miioConfig.getProperties();
98                 String cloudDiscoveryModeConfig;
99                 if (properties == null) {
100                     cloudDiscoveryModeConfig = DISABLED;
101                 } else {
102                     cloudDiscoveryModeConfig = (String) properties.get("cloudDiscoveryMode");
103                     if (cloudDiscoveryModeConfig == null) {
104                         cloudDiscoveryModeConfig = DISABLED;
105                     } else {
106                         cloudDiscoveryModeConfig = cloudDiscoveryModeConfig.toLowerCase();
107                     }
108                 }
109                 return Set.of(SUPPORTED, ALL).contains(cloudDiscoveryModeConfig) ? cloudDiscoveryModeConfig : DISABLED;
110             } catch (ClassCastException | SecurityException e) {
111                 logger.debug("Error getting cloud discovery configuration: {}", e.getMessage());
112             }
113         }
114         return DISABLED;
115     }
116
117     @Override
118     public Set<ThingTypeUID> getSupportedThingTypes() {
119         return SUPPORTED_THING_TYPES_UIDS;
120     }
121
122     @Override
123     protected void startBackgroundDiscovery() {
124         logger.debug("Start Xiaomi Mi IO background discovery with cloudDiscoveryMode: {}", getCloudDiscoveryMode());
125         final @Nullable ScheduledFuture<?> miIoDiscoveryJob = this.miIoDiscoveryJob;
126         if (miIoDiscoveryJob == null || miIoDiscoveryJob.isCancelled()) {
127             this.miIoDiscoveryJob = scheduler.scheduleWithFixedDelay(this::discover, 0, SEARCH_INTERVAL,
128                     TimeUnit.SECONDS);
129         }
130     }
131
132     @Override
133     protected void stopBackgroundDiscovery() {
134         logger.debug("Stop Xiaomi  Mi IO background discovery");
135         final @Nullable ScheduledFuture<?> miIoDiscoveryJob = this.miIoDiscoveryJob;
136         if (miIoDiscoveryJob != null) {
137             miIoDiscoveryJob.cancel(true);
138             this.miIoDiscoveryJob = null;
139         }
140     }
141
142     @Override
143     protected void deactivate() {
144         stopReceiverThreat();
145         final DatagramSocket clientSocket = this.clientSocket;
146         if (clientSocket != null) {
147             clientSocket.close();
148         }
149         this.clientSocket = null;
150         super.deactivate();
151     }
152
153     @Override
154     protected void startScan() {
155         String cloudDiscoveryMode = getCloudDiscoveryMode();
156         logger.debug("Start Xiaomi Mi IO discovery with cloudDiscoveryMode: {}", cloudDiscoveryMode);
157         if (!cloudDiscoveryMode.contentEquals(DISABLED)) {
158             cloudDiscovery();
159         }
160         final DatagramSocket clientSocket = getSocket();
161         if (clientSocket != null) {
162             logger.debug("Discovery using socket on port {}", clientSocket.getLocalPort());
163             discover();
164         } else {
165             logger.debug("Discovery not started. Client DatagramSocket null");
166         }
167     }
168
169     private void discover() {
170         startReceiverThreat();
171         responseIps = new HashSet<>();
172         HashSet<String> broadcastAddresses = new HashSet<>();
173         broadcastAddresses.add("224.0.0.1");
174         broadcastAddresses.add("224.0.0.50");
175         broadcastAddresses.addAll(NetUtil.getAllBroadcastAddresses());
176         for (String broadcastAdress : broadcastAddresses) {
177             sendDiscoveryRequest(broadcastAdress);
178         }
179     }
180
181     private void cloudDiscovery() {
182         String cloudDiscoveryMode = getCloudDiscoveryMode();
183         cloudDevices.clear();
184         if (cloudConnector.isConnected()) {
185             List<CloudDeviceDTO> dv = cloudConnector.getDevicesList();
186             for (CloudDeviceDTO device : dv) {
187                 String id = device.getDid();
188                 if (cloudDiscoveryMode.contentEquals(SUPPORTED)) {
189                     if (MiIoDevices.getType(device.getModel()).getThingType().equals(THING_TYPE_UNSUPPORTED)) {
190                         logger.warn("Discovered from cloud, but ignored because not supported: {} {}", id, device);
191                     }
192                 }
193                 if (device.getIsOnline()) {
194                     logger.debug("Discovered from cloud: {} {}", id, device);
195                     cloudDevices.put(id, device.getLocalip());
196                     String token = device.getToken();
197                     String label = device.getName() + " " + id + " (" + Utils.getHexId(id) + ")";
198                     String country = device.getServer();
199                     boolean isOnline = device.getIsOnline();
200                     String ip = device.getLocalip();
201                     submitDiscovery(ip, token, id, label, country, isOnline);
202                 } else {
203                     logger.debug("Discovered from cloud, but ignored because not online: {} {}", id, device);
204                 }
205             }
206         }
207     }
208
209     private void discovered(String ip, byte[] response) {
210         logger.trace("Discovery responses from : {}:{}", ip, Utils.getSpacedHex(response));
211         Message msg = new Message(response);
212         String token = Utils.getHex(msg.getChecksum());
213         String hexId = Utils.getHex(msg.getDeviceId());
214         String id = Utils.fromHEX(hexId);
215         String label = "Xiaomi Mi Device " + id + " (" + Utils.getHexId(id) + ")";
216         String country = "";
217         boolean isOnline = false;
218         if (ip.equals(cloudDevices.get(id))) {
219             logger.debug("Skipped adding local found {}. Already discovered by cloud.", label);
220             return;
221         }
222         if (cloudConnector.isConnected()) {
223             cloudConnector.getDevicesList();
224             CloudDeviceDTO cloudInfo = cloudConnector.getDeviceInfo(id);
225             if (cloudInfo != null) {
226                 logger.debug("Cloud Info: {}", cloudInfo);
227                 token = cloudInfo.getToken();
228                 label = cloudInfo.getName() + " " + id + " (" + Utils.getHexId(id) + ")";
229                 country = cloudInfo.getServer();
230                 isOnline = cloudInfo.getIsOnline();
231             }
232         }
233         submitDiscovery(ip, token, id, label, country, isOnline);
234     }
235
236     private void submitDiscovery(String ip, String token, String id, String label, String country, boolean isOnline) {
237         ThingUID uid = new ThingUID(THING_TYPE_MIIO, Utils.getHexId(id).replace(".", "_"));
238         DiscoveryResultBuilder dr = DiscoveryResultBuilder.create(uid).withProperty(PROPERTY_HOST_IP, ip)
239                 .withProperty(PROPERTY_DID, id);
240         if (IGNORED_TOKENS.contains(token)) {
241             logger.debug("Discovered Mi Device {} ({}) at {} as {}", id, Utils.getHexId(id), ip, uid);
242             logger.debug(
243                     "No token discovered for device {}. For options how to get the token, check the binding readme.",
244                     id);
245             dr = dr.withRepresentationProperty(PROPERTY_DID).withLabel(label);
246         } else {
247             logger.debug("Discovered Mi Device {} ({}) at {} as {} with token {}", id, Utils.getHexId(id), ip, uid,
248                     token);
249             dr = dr.withProperty(PROPERTY_TOKEN, token).withRepresentationProperty(PROPERTY_DID)
250                     .withLabel(label + " with token");
251         }
252         if (!country.isEmpty() && isOnline) {
253             dr = dr.withProperty(PROPERTY_CLOUDSERVER, country);
254         }
255         thingDiscovered(dr.build());
256     }
257
258     synchronized @Nullable DatagramSocket getSocket() {
259         DatagramSocket clientSocket = this.clientSocket;
260         if (clientSocket != null && clientSocket.isBound()) {
261             return clientSocket;
262         }
263         try {
264             logger.debug("Getting new socket for discovery");
265             clientSocket = new DatagramSocket();
266             clientSocket.setReuseAddress(true);
267             clientSocket.setBroadcast(true);
268             this.clientSocket = clientSocket;
269             return clientSocket;
270         } catch (SocketException | SecurityException e) {
271             logger.debug("Error getting socket for discovery: {}", e.getMessage());
272         }
273         return null;
274     }
275
276     private void closeSocket() {
277         final @Nullable DatagramSocket clientSocket = this.clientSocket;
278         if (clientSocket != null) {
279             clientSocket.close();
280         } else {
281             return;
282         }
283         this.clientSocket = null;
284     }
285
286     private void sendDiscoveryRequest(String ipAddress) {
287         final @Nullable DatagramSocket socket = getSocket();
288         if (socket != null) {
289             try {
290                 byte[] sendData = DISCOVER_STRING;
291                 logger.trace("Discovery sending ping to {} from {}:{}", ipAddress, socket.getLocalAddress(),
292                         socket.getLocalPort());
293                 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
294                         InetAddress.getByName(ipAddress), PORT);
295                 for (int i = 1; i <= 1; i++) {
296                     socket.send(sendPacket);
297                 }
298             } catch (IOException e) {
299                 logger.trace("Discovery on {} error: {}", ipAddress, e.getMessage());
300             }
301         }
302     }
303
304     /**
305      * starts the {@link ReceiverThread} thread
306      */
307     private synchronized void startReceiverThreat() {
308         final Thread srt = socketReceiveThread;
309         if (srt != null) {
310             if (srt.isAlive() && !srt.isInterrupted()) {
311                 return;
312             }
313         }
314         stopReceiverThreat();
315         Thread socketReceiveThread = new ReceiverThread();
316         socketReceiveThread.start();
317         this.socketReceiveThread = socketReceiveThread;
318     }
319
320     /**
321      * Stops the {@link ReceiverThread} thread
322      */
323     private synchronized void stopReceiverThreat() {
324         if (socketReceiveThread != null) {
325             socketReceiveThread.interrupt();
326             socketReceiveThread = null;
327         }
328         closeSocket();
329     }
330
331     /**
332      * The thread, which waits for data and submits the unique results addresses to the discovery results
333      *
334      */
335     private class ReceiverThread extends Thread {
336         @Override
337         public void run() {
338             DatagramSocket socket = getSocket();
339             if (socket != null) {
340                 logger.debug("Starting discovery receiver thread for socket on port {}", socket.getLocalPort());
341                 receiveData(socket);
342             }
343         }
344
345         /**
346          * This method waits for data and submits the unique results addresses to the discovery results
347          *
348          * @param socket - The multicast socket to (re)use
349          */
350         private void receiveData(DatagramSocket socket) {
351             DatagramPacket receivePacket = new DatagramPacket(new byte[BUFFER_LENGTH], BUFFER_LENGTH);
352             try {
353                 while (!interrupted()) {
354                     logger.trace("Thread {} waiting for data on port {}", this, socket.getLocalPort());
355                     socket.receive(receivePacket);
356                     String hostAddress = receivePacket.getAddress().getHostAddress();
357                     logger.trace("Received {} bytes response from {}:{} on Port {}", receivePacket.getLength(),
358                             hostAddress, receivePacket.getPort(), socket.getLocalPort());
359
360                     byte[] messageBuf = Arrays.copyOfRange(receivePacket.getData(), receivePacket.getOffset(),
361                             receivePacket.getOffset() + receivePacket.getLength());
362                     if (logger.isTraceEnabled()) {
363                         Message miIoResponse = new Message(messageBuf);
364                         logger.trace("Discovery response received from {} DeviceID: {}\r\n{}", hostAddress,
365                                 Utils.getHex(miIoResponse.getDeviceId()), miIoResponse.toSting());
366                     }
367                     if (!responseIps.contains(hostAddress)) {
368                         scheduler.schedule(() -> {
369                             try {
370                                 discovered(hostAddress, messageBuf);
371                             } catch (Exception e) {
372                                 logger.debug("Error submitting discovered Mi IO device at {}", hostAddress, e);
373                             }
374                         }, 0, TimeUnit.SECONDS);
375                     }
376                     responseIps.add(hostAddress);
377                 }
378             } catch (SocketException e) {
379                 logger.debug("Receiver thread received SocketException: {}", e.getMessage());
380             } catch (IOException e) {
381                 logger.trace("Receiver thread was interrupted");
382             }
383             logger.debug("Receiver thread ended");
384         }
385     }
386 }