]> git.basschouten.com Git - openhab-addons.git/blob
011319754f7120b9c02e94abf1b68d53dd3cfb74
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.adorne.internal.hub;
14
15 import java.io.IOException;
16 import java.io.InputStreamReader;
17 import java.io.PrintStream;
18 import java.net.Socket;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParseException;
28 import com.google.gson.JsonPrimitive;
29 import com.google.gson.JsonStreamParser;
30
31 /**
32  * The {@link AdorneHubConnection} manages basic connectivity with the Adorne hub.
33  *
34  * @author Mark Theiding - Initial Contribution
35  */
36 @NonNullByDefault
37 public class AdorneHubConnection {
38     private final Logger logger = LoggerFactory.getLogger(AdorneHubConnection.class);
39
40     private final Socket hubSocket;
41     private final PrintStream hubOut;
42     private final InputStreamReader hubInReader;
43     private final JsonStreamParser hubIn;
44
45     public AdorneHubConnection(String hubHost, int hubPort, int timeout) throws IOException {
46         hubSocket = new Socket(hubHost, hubPort);
47         hubSocket.setSoTimeout(timeout);
48         hubOut = new PrintStream(hubSocket.getOutputStream());
49         hubInReader = new InputStreamReader(hubSocket.getInputStream());
50         hubIn = new JsonStreamParser(hubInReader);
51     }
52
53     public void close() {
54         try {
55             hubInReader.close(); // Closes underlying input stream as well
56         } catch (IOException e) {
57             logger.warn("Closing hub input reader failed ({})", e.getMessage());
58         }
59         hubOut.close(); // Closes underlying output stream as well
60         try {
61             hubSocket.close();
62         } catch (IOException e) {
63             logger.warn("Closing hub controller socket failed ({})", e.getMessage());
64         }
65     }
66
67     public void cancel() {
68         try {
69             hubSocket.shutdownInput();
70         } catch (IOException e) {
71             logger.debug("Couldn't shutdown hub socket");
72         }
73     }
74
75     public void putMsg(String cmd) {
76         hubOut.print(cmd);
77     }
78
79     public @Nullable JsonObject getMsg() throws JsonParseException {
80         JsonElement msg = null;
81         JsonObject msgJsonObject = null;
82
83         msg = hubIn.next();
84
85         if (msg == null || (msg instanceof JsonPrimitive && msg.getAsCharacter() == 0)) {
86             return null; // Eat empty messages
87         }
88         logger.debug("Received message {}", msg);
89         if (msg instanceof JsonObject object) {
90             msgJsonObject = object;
91         }
92         return msgJsonObject;
93     }
94 }