2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.neohub.internal;
15 import static java.nio.charset.StandardCharsets.US_ASCII;
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;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 import com.google.gson.JsonParser;
30 * Handles the text based communication via TCP socket between openHAB and NeoHub
32 * @author Sebastian Prehn - Initial contribution
33 * @author Andrew Fiddian-Green - Refactoring for openHAB v2.x
37 public class NeoHubSocket extends NeoHubSocketBase {
39 private final Logger logger = LoggerFactory.getLogger(NeoHubSocket.class);
41 public NeoHubSocket(NeoHubConfiguration config, String hubId) {
46 public synchronized String sendMessage(final String requestJson) throws IOException, NeoHubException {
47 IOException caughtException = null;
48 StringBuilder builder = new StringBuilder();
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);
55 try (InputStreamReader reader = new InputStreamReader(socket.getInputStream(), US_ASCII);
56 OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream(), US_ASCII)) {
58 logger.debug("hub '{}' sending characters:{}", hubId, requestJson.length());
59 writer.write(requestJson);
60 writer.write(0); // NULL terminate the command string
62 socket.shutdownOutput();
63 logger.trace("hub '{}' sent:{}", hubId, requestJson);
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);
75 } catch (IOException e) {
76 // catch IOExceptions here, and save them to be re-thrown later
80 String responseJson = builder.toString().strip();
82 logger.debug("hub '{}' received characters:{}", hubId, responseJson.length());
83 logger.trace("hub '{}' received:{}", hubId, responseJson);
85 // if an IOException was caught above, re-throw it again
86 if (caughtException != null) {
87 throw caughtException;
90 if (JsonParser.parseString(responseJson).isJsonObject()) {
93 logger.debug("hub '{}' Response is not a JSON object; response:{}", hubId, responseJson);
94 throw new NeoHubException("Invalid response");