2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.powermax.internal.connector;
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;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * A class for the communication with the Visonic alarm panel through a TCP connection
27 * @author Laurent Garnier - Initial contribution
29 public class PowermaxTcpConnector extends PowermaxConnector {
31 private final Logger logger = LoggerFactory.getLogger(PowermaxTcpConnector.class);
33 private final String ipAddress;
34 private final int tcpPort;
35 private final int connectTimeout;
36 private Socket tcpSocket;
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
46 public PowermaxTcpConnector(String ip, int port, int timeout, String readerThreadName) {
47 super(readerThreadName);
50 connectTimeout = timeout;
54 public void open() throws Exception {
55 logger.debug("open(): Opening TCP Connection");
57 tcpSocket = new Socket();
58 tcpSocket.setSoTimeout(250);
59 SocketAddress socketAddress = new InetSocketAddress(ipAddress, tcpPort);
60 tcpSocket.connect(socketAddress, connectTimeout);
62 setInput(tcpSocket.getInputStream());
63 setOutput(tcpSocket.getOutputStream());
65 setReaderThread(new PowermaxReaderThread(this, readerThreadName));
66 getReaderThread().start();
73 logger.debug("close(): Closing TCP Connection");
77 if (tcpSocket != null) {
80 } catch (IOException e) {
81 logger.debug("Error while closing the socket: {}", e.getMessage());
91 public int read(byte[] buffer) throws IOException {
93 return super.read(buffer);
94 } catch (SocketTimeoutException ignore) {
95 // ignore this exception, instead return 0 to behave like the serial read