]> git.basschouten.com Git - openhab-addons.git/blob
833d2bddaef312f7f91602d3ccf6c77019a26f96
[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.SocketException;
20 import java.net.SocketTimeoutException;
21 import java.net.UnknownHostException;
22
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 public class PowermaxTcpConnector extends PowermaxConnector {
32
33     private final Logger logger = LoggerFactory.getLogger(PowermaxTcpConnector.class);
34
35     private final String ipAddress;
36     private final int tcpPort;
37     private final int connectTimeout;
38     private Socket tcpSocket;
39
40     /**
41      * Constructor.
42      *
43      * @param ip the IP address
44      * @param port the TCP port number
45      * @param timeout the timeout for socket communications
46      * @param readerThreadName the name of thread to be created
47      */
48     public PowermaxTcpConnector(String ip, int port, int timeout, String readerThreadName) {
49         super(readerThreadName);
50         ipAddress = ip;
51         tcpPort = port;
52         connectTimeout = timeout;
53     }
54
55     @Override
56     public void open() {
57         logger.debug("open(): Opening TCP Connection");
58
59         try {
60             tcpSocket = new Socket();
61             tcpSocket.setSoTimeout(250);
62             SocketAddress socketAddress = new InetSocketAddress(ipAddress, tcpPort);
63             tcpSocket.connect(socketAddress, connectTimeout);
64
65             setInput(tcpSocket.getInputStream());
66             setOutput(tcpSocket.getOutputStream());
67
68             setReaderThread(new PowermaxReaderThread(this, readerThreadName));
69             getReaderThread().start();
70
71             setConnected(true);
72         } catch (UnknownHostException e) {
73             logger.debug("open(): Unknown Host Exception: {}", e.getMessage(), e);
74             setConnected(false);
75         } catch (SocketException e) {
76             logger.debug("open(): Socket Exception: {}", e.getMessage(), e);
77             setConnected(false);
78         } catch (IOException e) {
79             logger.debug("open(): IO Exception: {}", e.getMessage(), e);
80             setConnected(false);
81         } catch (Exception e) {
82             logger.debug("open(): Exception: {}", e.getMessage(), e);
83             setConnected(false);
84         }
85     }
86
87     @Override
88     public void close() {
89         logger.debug("close(): Closing TCP Connection");
90
91         super.cleanup(false);
92
93         if (tcpSocket != null) {
94             try {
95                 tcpSocket.close();
96             } catch (IOException e) {
97                 logger.debug("Error while closing the socket: {}", e.getMessage());
98             }
99         }
100
101         tcpSocket = null;
102
103         setConnected(false);
104     }
105
106     @Override
107     public int read(byte[] buffer) throws IOException {
108         try {
109             return super.read(buffer);
110         } catch (SocketTimeoutException ignore) {
111             // ignore this exception, instead return 0 to behave like the serial read
112             return 0;
113         }
114     }
115 }