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.SocketException;
20 import java.net.SocketTimeoutException;
21 import java.net.UnknownHostException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * A class for the communication with the Visonic alarm panel through a TCP connection
29 * @author Laurent Garnier - Initial contribution
31 public class PowermaxTcpConnector extends PowermaxConnector {
33 private final Logger logger = LoggerFactory.getLogger(PowermaxTcpConnector.class);
35 private final String ipAddress;
36 private final int tcpPort;
37 private final int connectTimeout;
38 private Socket tcpSocket;
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
48 public PowermaxTcpConnector(String ip, int port, int timeout, String readerThreadName) {
49 super(readerThreadName);
52 connectTimeout = timeout;
57 logger.debug("open(): Opening TCP Connection");
60 tcpSocket = new Socket();
61 tcpSocket.setSoTimeout(250);
62 SocketAddress socketAddress = new InetSocketAddress(ipAddress, tcpPort);
63 tcpSocket.connect(socketAddress, connectTimeout);
65 setInput(tcpSocket.getInputStream());
66 setOutput(tcpSocket.getOutputStream());
68 setReaderThread(new PowermaxReaderThread(this, readerThreadName));
69 getReaderThread().start();
72 } catch (UnknownHostException e) {
73 logger.debug("open(): Unknown Host Exception: {}", e.getMessage(), e);
75 } catch (SocketException e) {
76 logger.debug("open(): Socket Exception: {}", e.getMessage(), e);
78 } catch (IOException e) {
79 logger.debug("open(): IO Exception: {}", e.getMessage(), e);
81 } catch (Exception e) {
82 logger.debug("open(): Exception: {}", e.getMessage(), e);
89 logger.debug("close(): Closing TCP Connection");
93 if (tcpSocket != null) {
96 } catch (IOException e) {
97 logger.debug("Error while closing the socket: {}", e.getMessage());
107 public int read(byte[] buffer) throws IOException {
109 return super.read(buffer);
110 } catch (SocketTimeoutException ignore) {
111 // ignore this exception, instead return 0 to behave like the serial read