]> git.basschouten.com Git - openhab-addons.git/blob
b1cefd491fa2c76878d8d60dac6eebf5437e7989
[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.rfxcom.internal.connector;
14
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.net.Socket;
18 import java.net.SocketTimeoutException;
19
20 import org.openhab.binding.rfxcom.internal.config.RFXComBridgeConfiguration;
21 import org.openhab.core.util.HexUtils;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * RFXCOM connector for TCP/IP communication.
27  *
28  * @author Pauli Anttila - Initial contribution
29  * @author Ivan F. Martinez, James Hewitt-Thomas - Implementation
30  */
31 public class RFXComTcpConnector extends RFXComBaseConnector {
32     private final Logger logger = LoggerFactory.getLogger(RFXComTcpConnector.class);
33
34     private OutputStream out;
35     private Socket socket;
36
37     private final String readerThreadName;
38     private Thread readerThread;
39
40     public RFXComTcpConnector(String readerThreadName) {
41         super();
42         this.readerThreadName = readerThreadName;
43     }
44
45     @Override
46     public void connect(RFXComBridgeConfiguration device) throws IOException {
47         logger.info("Connecting to RFXCOM at {}:{} over TCP/IP", device.host, device.port);
48         socket = new Socket(device.host, device.port);
49         socket.setSoTimeout(100); // In ms. Small values mean faster shutdown but more cpu usage.
50         in = socket.getInputStream();
51         out = socket.getOutputStream();
52
53         out.flush();
54         if (in.markSupported()) {
55             in.reset();
56         }
57
58         readerThread = new RFXComStreamReader(this, readerThreadName);
59         readerThread.start();
60     }
61
62     @Override
63     public void disconnect() {
64         logger.debug("Disconnecting");
65
66         if (readerThread != null) {
67             logger.debug("Interrupt stream listener");
68             readerThread.interrupt();
69             try {
70                 readerThread.join();
71             } catch (InterruptedException e) {
72             }
73         }
74
75         if (socket != null) {
76             logger.debug("Close socket");
77             try {
78                 socket.close();
79             } catch (IOException e) {
80                 logger.debug("Error while closing the socket: {}", e.getMessage());
81             }
82         }
83
84         readerThread = null;
85         socket = null;
86         out = null;
87         in = null;
88
89         logger.debug("Closed");
90     }
91
92     @Override
93     public void sendMessage(byte[] data) throws IOException {
94         if (logger.isTraceEnabled()) {
95             logger.trace("Send data (len={}): {}", data.length, HexUtils.bytesToHex(data));
96         }
97         out.write(data);
98         out.flush();
99     }
100
101     @Override
102     int read(byte[] buffer, int offset, int length) throws IOException {
103         try {
104             return super.read(buffer, offset, length);
105         } catch (SocketTimeoutException ignore) {
106             // ignore this exception, instead return 0 to behave like the serial read
107             return 0;
108         }
109     }
110 }