]> git.basschouten.com Git - openhab-addons.git/blob
4fcd9c2348101ff2b952875684b29c21aad71ed4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.harmonyhub.internal.discovery;
14
15 import static org.openhab.binding.harmonyhub.internal.HarmonyHubBindingConstants.*;
16
17 import java.io.BufferedReader;
18 import java.io.IOException;
19 import java.io.InputStreamReader;
20 import java.io.Reader;
21 import java.net.DatagramPacket;
22 import java.net.DatagramSocket;
23 import java.net.InetAddress;
24 import java.net.InterfaceAddress;
25 import java.net.NetworkInterface;
26 import java.net.ServerSocket;
27 import java.net.Socket;
28 import java.util.ArrayList;
29 import java.util.Enumeration;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.concurrent.ScheduledFuture;
34 import java.util.concurrent.TimeUnit;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
37
38 import org.eclipse.jdt.annotation.NonNullByDefault;
39 import org.eclipse.jdt.annotation.Nullable;
40 import org.openhab.binding.harmonyhub.internal.HarmonyHubBindingConstants;
41 import org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler;
42 import org.openhab.core.config.discovery.AbstractDiscoveryService;
43 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
44 import org.openhab.core.config.discovery.DiscoveryService;
45 import org.openhab.core.thing.ThingTypeUID;
46 import org.openhab.core.thing.ThingUID;
47 import org.osgi.service.component.annotations.Component;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * The {@link HarmonyHubDiscoveryService} class discovers Harmony hubs and adds the results to the inbox.
53  *
54  * @author Dan Cunningham - Initial contribution
55  * @author Wouter Born - Add null annotations
56  */
57 @NonNullByDefault
58 @Component(service = DiscoveryService.class, configurationPid = "discovery.harmonyhub")
59 public class HarmonyHubDiscoveryService extends AbstractDiscoveryService {
60
61     private final Logger logger = LoggerFactory.getLogger(HarmonyHubDiscoveryService.class);
62
63     // notice the port appended to the end of the string
64     private static final String DISCOVERY_STRING = "_logitech-reverse-bonjour._tcp.local.\n%d";
65     private static final int DISCOVERY_PORT = 5224;
66     private static final int TIMEOUT = 15;
67     private static final long REFRESH = 600;
68
69     private boolean running;
70
71     private @Nullable HarmonyServer server;
72
73     private @Nullable ScheduledFuture<?> broadcastFuture;
74     private @Nullable ScheduledFuture<?> discoveryFuture;
75     private @Nullable ScheduledFuture<?> timeoutFuture;
76
77     public HarmonyHubDiscoveryService() {
78         super(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
79     }
80
81     @Override
82     public Set<ThingTypeUID> getSupportedThingTypes() {
83         return HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS;
84     }
85
86     @Override
87     public void startScan() {
88         logger.debug("StartScan called");
89         startDiscovery();
90     }
91
92     @Override
93     protected void startBackgroundDiscovery() {
94         logger.debug("Start Harmony Hub background discovery");
95         ScheduledFuture<?> localDiscoveryFuture = discoveryFuture;
96         if (localDiscoveryFuture == null || localDiscoveryFuture.isCancelled()) {
97             logger.debug("Start Scan");
98             discoveryFuture = scheduler.scheduleWithFixedDelay(this::startScan, 0, REFRESH, TimeUnit.SECONDS);
99         }
100     }
101
102     @Override
103     protected void stopBackgroundDiscovery() {
104         logger.debug("Stop HarmonyHub background discovery");
105         ScheduledFuture<?> localDiscoveryFuture = discoveryFuture;
106         if (localDiscoveryFuture != null && !localDiscoveryFuture.isCancelled()) {
107             localDiscoveryFuture.cancel(true);
108             discoveryFuture = null;
109         }
110         stopDiscovery();
111     }
112
113     /**
114      * Starts discovery for Harmony Hubs
115      */
116     private synchronized void startDiscovery() {
117         if (running) {
118             return;
119         }
120
121         try {
122             final HarmonyServer localServer = new HarmonyServer();
123             localServer.start();
124             server = localServer;
125
126             broadcastFuture = scheduler.scheduleWithFixedDelay(() -> {
127                 sendDiscoveryMessage(String.format(DISCOVERY_STRING, localServer.getPort()));
128             }, 0, 2, TimeUnit.SECONDS);
129
130             timeoutFuture = scheduler.schedule(this::stopDiscovery, TIMEOUT, TimeUnit.SECONDS);
131
132             running = true;
133         } catch (IOException e) {
134             logger.error("Could not start Harmony discovery server ", e);
135         }
136     }
137
138     /**
139      * Stops discovery of Harmony Hubs
140      */
141     private synchronized void stopDiscovery() {
142         ScheduledFuture<?> localBroadcastFuture = broadcastFuture;
143         if (localBroadcastFuture != null) {
144             localBroadcastFuture.cancel(true);
145         }
146
147         ScheduledFuture<?> localTimeoutFuture = timeoutFuture;
148         if (localTimeoutFuture != null) {
149             localTimeoutFuture.cancel(true);
150         }
151
152         HarmonyServer localServer = server;
153         if (localServer != null) {
154             localServer.stop();
155         }
156
157         running = false;
158     }
159
160     /**
161      * Send broadcast message over all active interfaces
162      *
163      * @param discoverString
164      *            String to be used for the discovery
165      */
166     private void sendDiscoveryMessage(String discoverString) {
167         try (DatagramSocket bcSend = new DatagramSocket()) {
168             bcSend.setBroadcast(true);
169             byte[] sendData = discoverString.getBytes();
170
171             // Broadcast the message over all the network interfaces
172             Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
173             while (interfaces.hasMoreElements()) {
174                 @Nullable
175                 NetworkInterface networkInterface = interfaces.nextElement();
176                 if (networkInterface.isLoopback() || !networkInterface.isUp()) {
177                     continue;
178                 }
179                 for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
180                     InetAddress[] broadcast = new InetAddress[] { InetAddress.getByName("224.0.0.1"),
181                             InetAddress.getByName("255.255.255.255"), interfaceAddress.getBroadcast() };
182                     for (InetAddress bc : broadcast) {
183                         // Send the broadcast package!
184                         if (bc != null) {
185                             try {
186                                 DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, bc,
187                                         DISCOVERY_PORT);
188                                 bcSend.send(sendPacket);
189                             } catch (IOException e) {
190                                 logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage());
191                             } catch (Exception e) {
192                                 logger.debug("{}", e.getMessage(), e);
193                             }
194                             logger.trace("Request packet sent to: {} Interface: {}", bc.getHostAddress(),
195                                     networkInterface.getDisplayName());
196                         }
197                     }
198                 }
199             }
200         } catch (IOException e) {
201             logger.debug("IO error during HarmonyHub discovery: {}", e.getMessage());
202         }
203     }
204
205     /**
206      * Server which accepts TCP connections from Harmony Hubs during the discovery process
207      *
208      * @author Dan Cunningham - Initial contribution
209      *
210      */
211     private class HarmonyServer {
212         private final ServerSocket serverSocket;
213         private final List<String> responses = new ArrayList<>();
214         private boolean running;
215
216         public HarmonyServer() throws IOException {
217             serverSocket = new ServerSocket(0);
218             logger.debug("Creating Harmony server on port {}", getPort());
219         }
220
221         public int getPort() {
222             return serverSocket.getLocalPort();
223         }
224
225         public void start() {
226             running = true;
227             Thread localThread = new Thread(this::run,
228                     "OH-binding-" + HarmonyHubBindingConstants.BINDING_ID + "discoveryServer");
229             localThread.setDaemon(true);
230             localThread.start();
231         }
232
233         public void stop() {
234             running = false;
235             try {
236                 serverSocket.close();
237             } catch (IOException e) {
238                 logger.error("Could not stop harmony discovery socket", e);
239             }
240         }
241
242         private void run() {
243             while (running) {
244                 try (Socket socket = serverSocket.accept();
245                         Reader isr = new InputStreamReader(socket.getInputStream());
246                         BufferedReader in = new BufferedReader(isr)) {
247                     String input;
248                     while ((input = in.readLine()) != null) {
249                         if (!running) {
250                             break;
251                         }
252                         logger.trace("READ {}", input);
253                         // response format is key1:value1;key2:value2;key3:value3;
254                         Map<String, String> properties = Stream.of(input.split(";")).map(line -> line.split(":", 2))
255                                 .collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
256                         String friendlyName = properties.get("friendlyName");
257                         String hostName = properties.get("host_name");
258                         String ip = properties.get("ip");
259                         String uuid = properties.get("uuid");
260                         if (friendlyName != null && !friendlyName.isBlank() && hostName != null && !hostName.isBlank()
261                                 && ip != null && !ip.isBlank() && uuid != null && !uuid.isBlank()
262                                 && !responses.contains(hostName)) {
263                             responses.add(hostName);
264                             hubDiscovered(ip, friendlyName, hostName, uuid);
265                         }
266                     }
267                 } catch (IOException | IndexOutOfBoundsException e) {
268                     if (running) {
269                         logger.debug("Error connecting with found hub", e);
270                     }
271                 }
272             }
273         }
274     }
275
276     private void hubDiscovered(String ip, String friendlyName, String hostName, String uuid) {
277         String thingId = hostName.replaceAll("[^A-Za-z0-9\\-_]", "");
278         logger.trace("Adding HarmonyHub {} ({}) at host {}", friendlyName, thingId, ip);
279         ThingUID uid = new ThingUID(HARMONY_HUB_THING_TYPE, thingId);
280         // @formatter:off
281         thingDiscovered(DiscoveryResultBuilder.create(uid)
282                 .withLabel("HarmonyHub " + friendlyName)
283                 .withProperty(HUB_PROPERTY_HOST, ip)
284                 .withProperty(HUB_PROPERTY_NAME, friendlyName)
285                 .withProperty(HUB_PROPERTY_ID, uuid)
286                 .withRepresentationProperty(HUB_PROPERTY_ID)
287                 .build());
288         // @formatter:on
289     }
290 }