]> git.basschouten.com Git - openhab-addons.git/blob
c1400aa067f883e33d2a183b311c1183088166a0
[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.powermax.internal.connector;
14
15 import java.io.IOException;
16 import java.net.InetSocketAddress;
17 import java.net.Socket;
18 import java.net.SocketAddress;
19 import java.net.SocketTimeoutException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * A class for the communication with the Visonic alarm panel through a TCP connection
28  *
29  * @author Laurent Garnier - Initial contribution
30  */
31 @NonNullByDefault
32 public class PowermaxTcpConnector extends PowermaxConnector {
33
34     private final Logger logger = LoggerFactory.getLogger(PowermaxTcpConnector.class);
35
36     private final String ipAddress;
37     private final int tcpPort;
38     private final int connectTimeout;
39     private @Nullable Socket tcpSocket;
40
41     /**
42      * Constructor.
43      *
44      * @param ip the IP address
45      * @param port the TCP port number
46      * @param timeout the timeout for socket communications
47      * @param readerThreadName the name of thread to be created
48      */
49     public PowermaxTcpConnector(String ip, int port, int timeout, String readerThreadName) {
50         super(readerThreadName);
51         ipAddress = ip;
52         tcpPort = port;
53         connectTimeout = timeout;
54     }
55
56     @Override
57     public void open() throws Exception {
58         logger.debug("open(): Opening TCP Connection");
59
60         Socket socket = new Socket();
61         tcpSocket = socket;
62         socket.setSoTimeout(250);
63         SocketAddress socketAddress = new InetSocketAddress(ipAddress, tcpPort);
64         socket.connect(socketAddress, connectTimeout);
65
66         setInput(socket.getInputStream());
67         setOutput(socket.getOutputStream());
68
69         PowermaxReaderThread readerThread = new PowermaxReaderThread(this, readerThreadName);
70         setReaderThread(readerThread);
71         readerThread.start();
72
73         setConnected(true);
74     }
75
76     @Override
77     public void close() {
78         logger.debug("close(): Closing TCP Connection");
79
80         super.cleanup(false);
81
82         Socket socket = tcpSocket;
83         if (socket != null) {
84             try {
85                 socket.close();
86             } catch (IOException e) {
87                 logger.debug("Error while closing the socket: {}", e.getMessage());
88             }
89         }
90
91         tcpSocket = null;
92
93         setConnected(false);
94     }
95
96     @Override
97     public int read(byte[] buffer) throws IOException {
98         try {
99             return super.read(buffer);
100         } catch (SocketTimeoutException ignore) {
101             // ignore this exception, instead return 0 to behave like the serial read
102             return 0;
103         }
104     }
105 }