]> git.basschouten.com Git - openhab-addons.git/blob
54b6e1ae93b251234f2d0e1ec21954181cb5893e
[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.io.openhabcloud.internal;
14
15 import java.io.IOException;
16 import java.net.MalformedURLException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.net.URL;
20 import java.net.URLEncoder;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.eclipse.jetty.client.HttpClient;
30 import org.eclipse.jetty.client.api.Request;
31 import org.eclipse.jetty.client.util.BytesContentProvider;
32 import org.eclipse.jetty.http.HttpField;
33 import org.eclipse.jetty.http.HttpFields;
34 import org.eclipse.jetty.http.HttpMethod;
35 import org.eclipse.jetty.http.HttpStatus;
36 import org.eclipse.jetty.util.BufferUtil;
37 import org.eclipse.jetty.util.URIUtil;
38 import org.json.JSONException;
39 import org.json.JSONObject;
40 import org.openhab.core.OpenHAB;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import io.socket.client.IO;
45 import io.socket.client.Manager;
46 import io.socket.client.Socket;
47 import io.socket.emitter.Emitter;
48 import io.socket.engineio.client.Transport;
49
50 /**
51  * This class provides communication between openHAB and the openHAB Cloud service.
52  * It also implements async http proxy for serving requests from user to
53  * openHAB through the openHAB Cloud. It uses Socket.IO connection to connect to
54  * the openHAB Cloud service and Jetty Http client to send local http requests to
55  * openHAB.
56  *
57  * @author Victor Belov - Initial contribution
58  * @author Kai Kreuzer - migrated code to new Jetty client and ESH APIs
59  */
60 public class CloudClient {
61     /*
62      * Logger for this class
63      */
64     private final Logger logger = LoggerFactory.getLogger(CloudClient.class);
65
66     /*
67      * This variable holds base URL for the openHAB Cloud connections
68      */
69     private final String baseURL;
70
71     /*
72      * This variable holds openHAB's UUID for authenticating and connecting to the openHAB Cloud
73      */
74     private final String uuid;
75
76     /*
77      * This variable holds openHAB's secret for authenticating and connecting to the openHAB Cloud
78      */
79     private final String secret;
80
81     /*
82      * This variable holds local openHAB's base URL for connecting to the local openHAB instance
83      */
84     private final String localBaseUrl;
85
86     /*
87      * This variable holds instance of Jetty HTTP client to make requests to local openHAB
88      */
89     private final HttpClient jettyClient;
90
91     /*
92      * This map holds HTTP requests to local openHAB which are currently running
93      */
94     private final Map<Integer, Request> runningRequests = new ConcurrentHashMap<>();
95
96     /*
97      * This variable indicates if connection to the openHAB Cloud is currently in an established state
98      */
99     private boolean isConnected;
100
101     /*
102      * This variable holds version of local openHAB
103      */
104     private String openHABVersion;
105
106     /*
107      * This variable holds instance of Socket.IO client class which provides communication
108      * with the openHAB Cloud
109      */
110     private Socket socket;
111
112     /*
113      * The protocol of the openHAB-cloud URL.
114      */
115     private String protocol = "https";
116
117     /*
118      * This variable holds instance of CloudClientListener which provides callbacks to communicate
119      * certain events from the openHAB Cloud back to openHAB
120      */
121     private CloudClientListener listener;
122     private boolean remoteAccessEnabled;
123     private Set<String> exposedItems;
124
125     /**
126      * Constructor of CloudClient
127      *
128      * @param uuid openHAB's UUID to connect to the openHAB Cloud
129      * @param secret openHAB's Secret to connect to the openHAB Cloud
130      * @param remoteAccessEnabled Allow the openHAB Cloud to be used as a remote proxy
131      * @param exposedItems Items that are made available to apps connected to the openHAB Cloud
132      */
133     public CloudClient(HttpClient httpClient, String uuid, String secret, String baseURL, String localBaseUrl,
134             boolean remoteAccessEnabled, Set<String> exposedItems) {
135         this.uuid = uuid;
136         this.secret = secret;
137         this.baseURL = baseURL;
138         this.localBaseUrl = localBaseUrl;
139         this.remoteAccessEnabled = remoteAccessEnabled;
140         this.exposedItems = exposedItems;
141         this.jettyClient = httpClient;
142     }
143
144     /**
145      * Connect to the openHAB Cloud
146      */
147
148     public void connect() {
149         try {
150             socket = IO.socket(baseURL);
151             URL parsed = new URL(baseURL);
152             protocol = parsed.getProtocol();
153         } catch (URISyntaxException e) {
154             logger.error("Error creating Socket.IO: {}", e.getMessage());
155         } catch (MalformedURLException e) {
156             logger.error("Error parsing baseURL to get protocol, assuming https. Error: {}", e.getMessage());
157         }
158         socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
159             @Override
160             public void call(Object... args) {
161                 logger.trace("Manager.EVENT_TRANSPORT");
162                 Transport transport = (Transport) args[0];
163                 transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
164                     @Override
165                     public void call(Object... args) {
166                         logger.trace("Transport.EVENT_REQUEST_HEADERS");
167                         @SuppressWarnings("unchecked")
168                         Map<String, List<String>> headers = (Map<String, List<String>>) args[0];
169                         headers.put("uuid", List.of(uuid));
170                         headers.put("secret", List.of(secret));
171                         headers.put("openhabversion", List.of(OpenHAB.getVersion()));
172                         headers.put("clientversion", List.of(CloudService.clientVersion));
173                         headers.put("remoteaccess", List.of(((Boolean) remoteAccessEnabled).toString()));
174                     }
175                 });
176             }
177         });
178         socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
179             @Override
180             public void call(Object... args) {
181                 logger.debug("Socket.IO connected");
182                 isConnected = true;
183                 onConnect();
184             }
185         }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
186             @Override
187             public void call(Object... args) {
188                 logger.debug("Socket.IO disconnected");
189                 isConnected = false;
190                 onDisconnect();
191             }
192         }).on(Socket.EVENT_ERROR, new Emitter.Listener() {
193             @Override
194             public void call(Object... args) {
195                 if (logger.isDebugEnabled()) {
196                     logger.error("Error connecting to the openHAB Cloud instance: {}", args[0]);
197                 } else {
198                     logger.error("Error connecting to the openHAB Cloud instance");
199                 }
200             }
201         }).on("request", new Emitter.Listener() {
202             @Override
203             public void call(Object... args) {
204                 onEvent("request", (JSONObject) args[0]);
205             }
206         }).on("cancel", new Emitter.Listener() {
207             @Override
208             public void call(Object... args) {
209                 onEvent("cancel", (JSONObject) args[0]);
210             }
211         }).on("command", new Emitter.Listener() {
212
213             @Override
214             public void call(Object... args) {
215                 onEvent("command", (JSONObject) args[0]);
216             }
217         });
218         socket.connect();
219     }
220
221     /**
222      * Callback method for socket.io client which is called when connection is established
223      */
224
225     public void onConnect() {
226         logger.info("Connected to the openHAB Cloud service (UUID = {}, base URL = {})", this.uuid, this.localBaseUrl);
227         isConnected = true;
228     }
229
230     /**
231      * Callback method for socket.io client which is called when disconnect occurs
232      */
233
234     public void onDisconnect() {
235         logger.info("Disconnected from the openHAB Cloud service (UUID = {}, base URL = {})", this.uuid,
236                 this.localBaseUrl);
237         isConnected = false;
238         // And clean up the list of running requests
239         runningRequests.clear();
240     }
241
242     /**
243      * Callback method for socket.io client which is called when an error occurs
244      */
245
246     public void onError(IOException error) {
247         logger.debug("{}", error.getMessage());
248     }
249
250     /**
251      * Callback method for socket.io client which is called when a message is received
252      */
253
254     public void onEvent(String event, JSONObject data) {
255         logger.debug("on(): {}", event);
256         if ("command".equals(event)) {
257             handleCommandEvent(data);
258             return;
259         }
260         if (remoteAccessEnabled) {
261             if ("request".equals(event)) {
262                 handleRequestEvent(data);
263             } else if ("cancel".equals(event)) {
264                 handleCancelEvent(data);
265             } else {
266                 logger.warn("Unsupported event from openHAB Cloud: {}", event);
267             }
268         }
269     }
270
271     private void handleRequestEvent(JSONObject data) {
272         try {
273             // Get unique request Id
274             int requestId = data.getInt("id");
275             logger.debug("Got request {}", requestId);
276             // Get request path
277             String requestPath = data.getString("path");
278             logger.debug("Path {}", requestPath);
279             // Get request method
280             String requestMethod = data.getString("method");
281             logger.debug("Method {}", requestMethod);
282             // Get JSONObject for request headers
283             JSONObject requestHeadersJson = data.getJSONObject("headers");
284             logger.debug("Headers: {}", requestHeadersJson.toString());
285             // Get request body
286             String requestBody = data.getString("body");
287             logger.trace("Body {}", requestBody);
288             // Get JSONObject for request query parameters
289             JSONObject requestQueryJson = data.getJSONObject("query");
290             logger.debug("Query {}", requestQueryJson.toString());
291             // Create URI builder with base request URI of openHAB and path from request
292             String newPath = URIUtil.addPaths(localBaseUrl, requestPath);
293             Iterator<String> queryIterator = requestQueryJson.keys();
294             // Add query parameters to URI builder, if any
295             newPath += "?";
296             while (queryIterator.hasNext()) {
297                 String queryName = queryIterator.next();
298                 newPath += queryName;
299                 newPath += "=";
300                 newPath += URLEncoder.encode(requestQueryJson.getString(queryName), "UTF-8");
301                 if (queryIterator.hasNext()) {
302                     newPath += "&";
303                 }
304             }
305             // Finally get the future request URI
306             URI requestUri = new URI(newPath);
307             // All preparations which are common for different methods are done
308             // Now perform the request to openHAB
309             // If method is GET
310             logger.debug("Request method is {}", requestMethod);
311             Request request = jettyClient.newRequest(requestUri);
312             setRequestHeaders(request, requestHeadersJson);
313             String proto = protocol;
314             if (data.has("protocol")) {
315                 proto = data.getString("protocol");
316             }
317             request.header("X-Forwarded-Proto", proto);
318             HttpMethod method = HttpMethod.fromString(requestMethod);
319             if (method == null) {
320                 logger.debug("Unsupported request method {}", requestMethod);
321                 return;
322             }
323             request.method(method);
324             if (!requestBody.isEmpty()) {
325                 request.content(new BytesContentProvider(requestBody.getBytes()));
326             }
327
328             request.onResponseHeaders(response -> {
329                 logger.debug("onHeaders {}", requestId);
330                 JSONObject responseJson = new JSONObject();
331                 try {
332                     responseJson.put("id", requestId);
333                     responseJson.put("headers", getJSONHeaders(response.getHeaders()));
334                     responseJson.put("responseStatusCode", response.getStatus());
335                     responseJson.put("responseStatusText", "OK");
336                     socket.emit("responseHeader", responseJson);
337                     logger.trace("Sent headers to request {}", requestId);
338                     logger.trace("{}", responseJson.toString());
339                 } catch (JSONException e) {
340                     logger.debug("{}", e.getMessage());
341                 }
342             }).onResponseContent((theResponse, content) -> {
343                 logger.debug("onResponseContent: {}, content size {}", requestId, String.valueOf(content.remaining()));
344                 JSONObject responseJson = new JSONObject();
345                 try {
346                     responseJson.put("id", requestId);
347                     responseJson.put("body", BufferUtil.toArray(content));
348                     if (logger.isTraceEnabled()) {
349                         logger.trace("{}", StandardCharsets.UTF_8.decode(content).toString());
350                     }
351                     socket.emit("responseContentBinary", responseJson);
352                     logger.trace("Sent content to request {}", requestId);
353                 } catch (JSONException e) {
354                     logger.debug("{}", e.getMessage());
355                 }
356             }).onRequestFailure((origRequest, failure) -> {
357                 logger.debug("onRequestFailure: {},  {}", requestId, failure.getMessage());
358                 JSONObject responseJson = new JSONObject();
359                 try {
360                     responseJson.put("id", requestId);
361                     responseJson.put("responseStatusText", "openHAB connection error: " + failure.getMessage());
362                     socket.emit("responseError", responseJson);
363                 } catch (JSONException e) {
364                     logger.debug("{}", e.getMessage());
365                 }
366             }).send(result -> {
367                 logger.debug("onComplete: {}", requestId);
368                 // Remove this request from list of running requests
369                 runningRequests.remove(requestId);
370                 if ((result != null && result.isFailed())
371                         && (result.getResponse() != null && result.getResponse().getStatus() != HttpStatus.OK_200)) {
372                     if (result.getFailure() != null) {
373                         logger.debug("Jetty request {} failed: {}", requestId, result.getFailure().getMessage());
374                     }
375                     if (result.getRequestFailure() != null) {
376                         logger.debug("Request Failure: {}", result.getRequestFailure().getMessage());
377                     }
378                     if (result.getResponseFailure() != null) {
379                         logger.debug("Response Failure: {}", result.getResponseFailure().getMessage());
380                     }
381                 }
382                 JSONObject responseJson = new JSONObject();
383                 try {
384                     responseJson.put("id", requestId);
385                     socket.emit("responseFinished", responseJson);
386                     logger.debug("Finished responding to request {}", requestId);
387                 } catch (JSONException e) {
388                     logger.debug("{}", e.getMessage());
389                 }
390             });
391
392             // If successfully submitted request to http client, add it to the list of currently
393             // running requests to be able to cancel it if needed
394             runningRequests.put(requestId, request);
395         } catch (JSONException | IOException | URISyntaxException e) {
396             logger.debug("{}", e.getMessage());
397         }
398     }
399
400     private void setRequestHeaders(Request request, JSONObject requestHeadersJson) {
401         Iterator<String> headersIterator = requestHeadersJson.keys();
402         // Convert JSONObject of headers into Header ArrayList
403         while (headersIterator.hasNext()) {
404             String headerName = headersIterator.next();
405             String headerValue;
406             try {
407                 headerValue = requestHeadersJson.getString(headerName);
408                 logger.debug("Jetty set header {} = {}", headerName, headerValue);
409                 if (!headerName.equalsIgnoreCase("Content-Length")) {
410                     request.header(headerName, headerValue);
411                 }
412             } catch (JSONException e) {
413                 logger.warn("Error processing request headers: {}", e.getMessage());
414             }
415         }
416     }
417
418     private void handleCancelEvent(JSONObject data) {
419         try {
420             int requestId = data.getInt("id");
421             logger.debug("Received cancel for request {}", requestId);
422             // Find and abort running request
423             Request request = runningRequests.get(requestId);
424             if (request != null) {
425                 request.abort(new InterruptedException());
426                 runningRequests.remove(requestId);
427             }
428         } catch (JSONException e) {
429             logger.debug("{}", e.getMessage());
430         }
431     }
432
433     private void handleCommandEvent(JSONObject data) {
434         String itemName = data.getString("item");
435         if (exposedItems.contains(itemName)) {
436             try {
437                 logger.debug("Received command {} for item {}.", data.getString("command"), itemName);
438                 if (this.listener != null) {
439                     this.listener.sendCommand(itemName, data.getString("command"));
440                 }
441             } catch (JSONException e) {
442                 logger.debug("{}", e.getMessage());
443             }
444         } else {
445             logger.warn("Received command from openHAB Cloud for item '{}', which is not exposed.", itemName);
446         }
447     }
448
449     /**
450      * This method sends notification to the openHAB Cloud
451      *
452      * @param userId openHAB Cloud user id
453      * @param message notification message text
454      * @param icon name of the icon for this notification
455      * @param severity severity name for this notification
456      */
457     public void sendNotification(String userId, String message, @Nullable String icon, @Nullable String severity) {
458         if (isConnected()) {
459             JSONObject notificationMessage = new JSONObject();
460             try {
461                 notificationMessage.put("userId", userId);
462                 notificationMessage.put("message", message);
463                 notificationMessage.put("icon", icon);
464                 notificationMessage.put("severity", severity);
465                 socket.emit("notification", notificationMessage);
466             } catch (JSONException e) {
467                 logger.debug("{}", e.getMessage());
468             }
469         } else {
470             logger.debug("No connection, notification is not sent");
471         }
472     }
473
474     /**
475      * This method sends log notification to the openHAB Cloud
476      *
477      * @param message notification message text
478      * @param icon name of the icon for this notification
479      * @param severity severity name for this notification
480      */
481     public void sendLogNotification(String message, @Nullable String icon, @Nullable String severity) {
482         if (isConnected()) {
483             JSONObject notificationMessage = new JSONObject();
484             try {
485                 notificationMessage.put("message", message);
486                 notificationMessage.put("icon", icon);
487                 notificationMessage.put("severity", severity);
488                 socket.emit("lognotification", notificationMessage);
489             } catch (JSONException e) {
490                 logger.debug("{}", e.getMessage());
491             }
492         } else {
493             logger.debug("No connection, notification is not sent");
494         }
495     }
496
497     /**
498      * This method sends broadcast notification to the openHAB Cloud
499      *
500      * @param message notification message text
501      * @param icon name of the icon for this notification
502      * @param severity severity name for this notification
503      */
504     public void sendBroadcastNotification(String message, @Nullable String icon, @Nullable String severity) {
505         if (isConnected()) {
506             JSONObject notificationMessage = new JSONObject();
507             try {
508                 notificationMessage.put("message", message);
509                 notificationMessage.put("icon", icon);
510                 notificationMessage.put("severity", severity);
511                 socket.emit("broadcastnotification", notificationMessage);
512             } catch (JSONException e) {
513                 logger.debug("{}", e.getMessage());
514             }
515         } else {
516             logger.debug("No connection, notification is not sent");
517         }
518     }
519
520     /**
521      * Send item update to openHAB Cloud
522      *
523      * @param itemName the name of the item
524      * @param itemState updated item state
525      *
526      */
527     public void sendItemUpdate(String itemName, String itemState) {
528         if (isConnected()) {
529             logger.debug("Sending update '{}' for item '{}'", itemState, itemName);
530             JSONObject itemUpdateMessage = new JSONObject();
531             try {
532                 itemUpdateMessage.put("itemName", itemName);
533                 itemUpdateMessage.put("itemStatus", itemState);
534                 socket.emit("itemupdate", itemUpdateMessage);
535             } catch (JSONException e) {
536                 logger.debug("{}", e.getMessage());
537             }
538         } else {
539             logger.debug("No connection, Item update is not sent");
540         }
541     }
542
543     /**
544      * Returns true if openHAB Cloud connection is active
545      */
546     public boolean isConnected() {
547         return isConnected;
548     }
549
550     /**
551      * Disconnect from openHAB Cloud
552      */
553     public void shutdown() {
554         logger.info("Shutting down openHAB Cloud service connection");
555         socket.disconnect();
556     }
557
558     public String getOpenHABVersion() {
559         return openHABVersion;
560     }
561
562     public void setOpenHABVersion(String openHABVersion) {
563         this.openHABVersion = openHABVersion;
564     }
565
566     public void setListener(CloudClientListener listener) {
567         this.listener = listener;
568     }
569
570     private JSONObject getJSONHeaders(HttpFields httpFields) {
571         JSONObject headersJSON = new JSONObject();
572         try {
573             for (HttpField field : httpFields) {
574                 headersJSON.put(field.getName(), field.getValue());
575             }
576         } catch (JSONException e) {
577             logger.warn("Error forming response headers: {}", e.getMessage());
578         }
579         return headersJSON;
580     }
581 }