]> git.basschouten.com Git - openhab-addons.git/blob
10a49c0734badd61540b0ca14029fb5b7ff151c0
[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.benqprojector.internal.connector;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.net.Socket;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.benqprojector.internal.BenqProjectorException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Connector for TCP communication.
28  *
29  * @author Michael Lobstein - Initial contribution
30  */
31 @NonNullByDefault
32 public class BenqProjectorTcpConnector implements BenqProjectorConnector {
33
34     private final Logger logger = LoggerFactory.getLogger(BenqProjectorTcpConnector.class);
35     private final String ip;
36     private final int port;
37
38     private @Nullable Socket socket = null;
39     private @Nullable InputStream in = null;
40     private @Nullable OutputStream out = null;
41
42     public BenqProjectorTcpConnector(String ip, int port) {
43         this.ip = ip;
44         this.port = port;
45     }
46
47     @Override
48     public void connect() throws BenqProjectorException {
49         logger.debug("Open connection to address'{}:{}'", ip, port);
50
51         try {
52             Socket socket = new Socket(ip, port);
53             this.socket = socket;
54             in = socket.getInputStream();
55             out = socket.getOutputStream();
56         } catch (IOException e) {
57             throw new BenqProjectorException(e);
58         }
59     }
60
61     @Override
62     public void disconnect() throws BenqProjectorException {
63         OutputStream out = this.out;
64
65         if (out != null) {
66             logger.debug("Close tcp out stream");
67             try {
68                 out.close();
69             } catch (IOException e) {
70                 logger.debug("Error occurred when closing tcp out stream: {}", e.getMessage());
71             }
72         }
73
74         InputStream in = this.in;
75         if (in != null) {
76             logger.debug("Close tcp in stream");
77             try {
78                 in.close();
79             } catch (IOException e) {
80                 logger.debug("Error occurred when closing tcp in stream: {}", e.getMessage());
81             }
82         }
83
84         Socket socket = this.socket;
85         if (socket != null) {
86             logger.debug("Closing socket");
87             try {
88                 socket.close();
89             } catch (IOException e) {
90                 logger.debug("Error occurred when closing tcp socket: {}", e.getMessage());
91             }
92         }
93
94         this.socket = null;
95         this.out = null;
96         this.in = null;
97
98         logger.debug("Closed");
99     }
100
101     @Override
102     public String sendMessage(String data) throws BenqProjectorException {
103         InputStream in = this.in;
104         OutputStream out = this.out;
105
106         if (in == null || out == null) {
107             connect();
108             in = this.in;
109             out = this.out;
110         }
111
112         try {
113             if (in != null) {
114                 // flush input stream
115                 if (in.markSupported()) {
116                     in.reset();
117                 } else {
118                     while (in.available() > 0) {
119                         int availableBytes = in.available();
120
121                         if (availableBytes > 0) {
122                             byte[] tmpData = new byte[availableBytes];
123                             in.read(tmpData, 0, availableBytes);
124                         }
125                     }
126                 }
127                 return sendMsgReadResp(data, in, out);
128             } else {
129                 return BLANK;
130             }
131         } catch (IOException e) {
132             logger.debug("IO error occurred...reconnect and resend once: {}", e.getMessage());
133             disconnect();
134             connect();
135
136             try {
137                 return sendMsgReadResp(data, in, out);
138             } catch (IOException e1) {
139                 throw new BenqProjectorException(e);
140             }
141         }
142     }
143 }