2 * Copyright (c) 2010-2023 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.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
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
32 public class PowermaxTcpConnector extends PowermaxConnector {
34 private final Logger logger = LoggerFactory.getLogger(PowermaxTcpConnector.class);
36 private final String ipAddress;
37 private final int tcpPort;
38 private final int connectTimeout;
39 private @Nullable Socket tcpSocket;
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
49 public PowermaxTcpConnector(String ip, int port, int timeout, String readerThreadName) {
50 super(readerThreadName);
53 connectTimeout = timeout;
57 public void open() throws Exception {
58 logger.debug("open(): Opening TCP Connection");
60 Socket socket = new Socket();
62 socket.setSoTimeout(250);
63 SocketAddress socketAddress = new InetSocketAddress(ipAddress, tcpPort);
64 socket.connect(socketAddress, connectTimeout);
66 setInput(socket.getInputStream());
67 setOutput(socket.getOutputStream());
69 PowermaxReaderThread readerThread = new PowermaxReaderThread(this, readerThreadName);
70 setReaderThread(readerThread);
78 logger.debug("close(): Closing TCP Connection");
82 Socket socket = tcpSocket;
86 } catch (IOException e) {
87 logger.debug("Error while closing the socket: {}", e.getMessage());
97 public int read(byte[] buffer) throws IOException {
99 return super.read(buffer);
100 } catch (SocketTimeoutException ignore) {
101 // ignore this exception, instead return 0 to behave like the serial read