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;
17 import org.openhab.core.io.transport.serial.SerialPort;
18 import org.openhab.core.io.transport.serial.SerialPortEvent;
19 import org.openhab.core.io.transport.serial.SerialPortEventListener;
20 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
21 import org.openhab.core.io.transport.serial.SerialPortManager;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * A class for the communication with the Visonic alarm panel through a serial connection
28 * @author Laurent Garnier - Initial contribution
30 public class PowermaxSerialConnector extends PowermaxConnector implements SerialPortEventListener {
32 private final Logger logger = LoggerFactory.getLogger(PowermaxSerialConnector.class);
34 private final String serialPortName;
35 private final int baudRate;
36 private final SerialPortManager serialPortManager;
37 private SerialPort serialPort;
42 * @param serialPortManager the serial port manager
43 * @param serialPortName the serial port name
44 * @param baudRate the baud rate to be used
45 * @param readerThreadName the name of thread to be created
47 public PowermaxSerialConnector(SerialPortManager serialPortManager, String serialPortName, int baudRate,
48 String readerThreadName) {
49 super(readerThreadName);
50 this.serialPortManager = serialPortManager;
51 this.serialPortName = serialPortName;
52 this.baudRate = baudRate;
53 this.serialPort = null;
57 public void open() throws Exception {
58 logger.debug("open(): Opening Serial Connection");
60 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
61 if (portIdentifier == null) {
62 throw new IOException("No Such Port: " + serialPortName);
65 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
67 serialPort = commPort;
68 serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
69 serialPort.enableReceiveThreshold(1);
70 serialPort.enableReceiveTimeout(250);
72 setInput(serialPort.getInputStream());
73 setOutput(serialPort.getOutputStream());
76 if (getInput().markSupported()) {
80 // RXTX serial port library causes high CPU load
81 // Start event listener, which will just sleep and slow down event
83 serialPort.addEventListener(this);
84 serialPort.notifyOnDataAvailable(true);
86 setReaderThread(new PowermaxReaderThread(this, readerThreadName));
87 getReaderThread().start();
94 logger.debug("close(): Closing Serial Connection");
96 if (serialPort != null) {
97 serialPort.removeEventListener();
102 if (serialPort != null) {
110 logger.debug("close(): Serial Connection closed");
114 public void serialEvent(SerialPortEvent serialPortEvent) {
116 logger.trace("RXTX library CPU load workaround, sleep forever");
117 Thread.sleep(Long.MAX_VALUE);
118 } catch (InterruptedException e) {
119 Thread.currentThread().interrupt();