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