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.io.InputStream;
17 import java.io.OutputStream;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.io.transport.serial.SerialPort;
22 import org.openhab.core.io.transport.serial.SerialPortEvent;
23 import org.openhab.core.io.transport.serial.SerialPortEventListener;
24 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
25 import org.openhab.core.io.transport.serial.SerialPortManager;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * A class for the communication with the Visonic alarm panel through a serial connection
32 * @author Laurent Garnier - Initial contribution
35 public class PowermaxSerialConnector extends PowermaxConnector implements SerialPortEventListener {
37 private final Logger logger = LoggerFactory.getLogger(PowermaxSerialConnector.class);
39 private final String serialPortName;
40 private final int baudRate;
41 private final SerialPortManager serialPortManager;
42 private @Nullable SerialPort serialPort;
47 * @param serialPortManager the serial port manager
48 * @param serialPortName the serial port name
49 * @param baudRate the baud rate to be used
50 * @param readerThreadName the name of thread to be created
52 public PowermaxSerialConnector(SerialPortManager serialPortManager, String serialPortName, int baudRate,
53 String readerThreadName) {
54 super(readerThreadName);
55 this.serialPortManager = serialPortManager;
56 this.serialPortName = serialPortName;
57 this.baudRate = baudRate;
61 public void open() throws Exception {
62 logger.debug("open(): Opening Serial Connection");
64 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
65 if (portIdentifier == null) {
66 throw new IOException("No Such Port: " + serialPortName);
69 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
71 serialPort = commPort;
72 commPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
73 commPort.enableReceiveThreshold(1);
74 commPort.enableReceiveTimeout(250);
76 InputStream inputStream = commPort.getInputStream();
77 setInput(inputStream);
78 OutputStream outputStream = commPort.getOutputStream();
79 setOutput(outputStream);
81 if (outputStream != null) {
84 if (inputStream != null && inputStream.markSupported()) {
88 // RXTX serial port library causes high CPU load
89 // Start event listener, which will just sleep and slow down event
91 commPort.addEventListener(this);
92 commPort.notifyOnDataAvailable(true);
94 PowermaxReaderThread readerThread = new PowermaxReaderThread(this, readerThreadName);
95 setReaderThread(readerThread);
102 public void close() {
103 logger.debug("close(): Closing Serial Connection");
105 SerialPort commPort = serialPort;
106 if (commPort != null) {
107 commPort.removeEventListener();
112 if (commPort != null) {
120 logger.debug("close(): Serial Connection closed");
124 public void serialEvent(SerialPortEvent serialPortEvent) {
126 logger.trace("RXTX library CPU load workaround, sleep forever");
127 Thread.sleep(Long.MAX_VALUE);
128 } catch (InterruptedException e) {
129 Thread.currentThread().interrupt();