]> git.basschouten.com Git - openhab-addons.git/blob
25145471d7f2199e4f24f59b2b627103346a01bc
[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.hyperion.internal.connection;
14
15 import java.io.BufferedReader;
16 import java.io.DataOutputStream;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.io.Reader;
20 import java.net.InetAddress;
21 import java.net.Socket;
22 import java.net.UnknownHostException;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link JsonTcpConnection} class is responsible for handling
29  * the communication with the Hyperion json server.
30  *
31  * @author Daniel Walters - Initial contribution
32  */
33 public class JsonTcpConnection {
34
35     private final Logger logger = LoggerFactory.getLogger(JsonTcpConnection.class);
36     private InetAddress address;
37     private int port;
38     private Socket hyperionServerSocket;
39
40     public JsonTcpConnection(InetAddress address, int port) {
41         this.setAddress(address);
42         this.port = port;
43     }
44
45     public JsonTcpConnection(String sAddress, int port) throws UnknownHostException {
46         this(InetAddress.getByName(sAddress), port);
47     }
48
49     public int getPort() {
50         return port;
51     }
52
53     public InetAddress getAddress() {
54         return address;
55     }
56
57     public void setAddress(InetAddress address) {
58         this.address = address;
59     }
60
61     public String send(String json) throws IOException {
62         String response = null;
63         try (Socket hyperionServer = new Socket(address, port);
64                 DataOutputStream outToServer = new DataOutputStream(hyperionServer.getOutputStream());
65                 Reader isr = new InputStreamReader(hyperionServer.getInputStream());
66                 BufferedReader inFromServer = new BufferedReader(isr)) {
67             logger.debug("Sending: {}", json);
68             outToServer.writeBytes(json + System.lineSeparator());
69             outToServer.flush();
70             response = inFromServer.readLine();
71         }
72         logger.debug("Received: {}", response);
73         return response;
74     }
75
76     public void connect() throws IOException {
77         if (hyperionServerSocket == null || !hyperionServerSocket.isConnected()) {
78             hyperionServerSocket = new Socket(address, port);
79         }
80     }
81
82     public void close() throws IOException {
83         if (hyperionServerSocket != null && hyperionServerSocket.isConnected()) {
84             hyperionServerSocket.close();
85         }
86     }
87
88     public boolean isConnected() {
89         return hyperionServerSocket != null && hyperionServerSocket.isConnected();
90     }
91 }