]> git.basschouten.com Git - openhab-addons.git/blob
20a40816c05537b42faca9bb14daff2fa71ff4f7
[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.neohub.internal;
14
15 import static java.nio.charset.StandardCharsets.US_ASCII;
16
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.io.OutputStreamWriter;
20 import java.net.InetSocketAddress;
21 import java.net.Socket;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.google.gson.JsonParser;
28
29 /**
30  * Handles the text based communication via TCP socket between openHAB and NeoHub
31  *
32  * @author Sebastian Prehn - Initial contribution
33  * @author Andrew Fiddian-Green - Refactoring for openHAB v2.x
34  *
35  */
36 @NonNullByDefault
37 public class NeoHubSocket extends NeoHubSocketBase {
38
39     private final Logger logger = LoggerFactory.getLogger(NeoHubSocket.class);
40
41     public NeoHubSocket(NeoHubConfiguration config, String hubId) {
42         super(config, hubId);
43     }
44
45     @Override
46     public synchronized String sendMessage(final String requestJson) throws IOException, NeoHubException {
47         IOException caughtException = null;
48         StringBuilder builder = new StringBuilder();
49
50         try (Socket socket = new Socket()) {
51             int port = config.portNumber > 0 ? config.portNumber : NeoHubBindingConstants.PORT_TCP;
52             socket.connect(new InetSocketAddress(config.hostName, port), config.socketTimeout * 1000);
53             socket.setSoTimeout(config.socketTimeout * 1000);
54
55             try (InputStreamReader reader = new InputStreamReader(socket.getInputStream(), US_ASCII);
56                     OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream(), US_ASCII)) {
57                 //
58                 logger.debug("hub '{}' sending characters:{}", hubId, requestJson.length());
59                 writer.write(requestJson);
60                 writer.write(0); // NULL terminate the command string
61                 writer.flush();
62                 socket.shutdownOutput();
63                 logger.trace("hub '{}' sent:{}", hubId, requestJson);
64
65                 int inChar;
66                 boolean done = false;
67                 // read until end of stream
68                 while ((inChar = reader.read()) != -1) {
69                     // a JSON block is terminated by a newline or NULL
70                     if (!(done |= (inChar == '\n') || (inChar == 0))) {
71                         builder.append((char) inChar);
72                     }
73                 }
74             }
75         } catch (IOException e) {
76             // catch IOExceptions here, and save them to be re-thrown later
77             caughtException = e;
78         }
79
80         String responseJson = builder.toString().strip();
81
82         logger.debug("hub '{}' received characters:{}", hubId, responseJson.length());
83         logger.trace("hub '{}' received:{}", hubId, responseJson);
84
85         // if an IOException was caught above, re-throw it again
86         if (caughtException != null) {
87             throw caughtException;
88         }
89
90         if (JsonParser.parseString(responseJson).isJsonObject()) {
91             return responseJson;
92         }
93         logger.debug("hub '{}' Response is not a JSON object; response:{}", hubId, responseJson);
94         throw new NeoHubException("Invalid response");
95     }
96
97     @Override
98     public void close() {
99         // nothing to do
100     }
101 }