2 * Copyright (c) 2010-2020 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.UnsupportedEncodingException;
17 import java.util.TooManyListenersException;
19 import org.openhab.core.io.transport.serial.PortInUseException;
20 import org.openhab.core.io.transport.serial.SerialPort;
21 import org.openhab.core.io.transport.serial.SerialPortEvent;
22 import org.openhab.core.io.transport.serial.SerialPortEventListener;
23 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
24 import org.openhab.core.io.transport.serial.SerialPortManager;
25 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
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
34 public class PowermaxSerialConnector extends PowermaxConnector implements SerialPortEventListener {
36 private final Logger logger = LoggerFactory.getLogger(PowermaxSerialConnector.class);
38 private final String serialPortName;
39 private final int baudRate;
40 private SerialPort serialPort;
41 private SerialPortManager serialPortManager;
46 * @param serialPortManager the serial port manager
47 * @param serialPortName the serial port name
48 * @param baudRate the baud rate to be used
49 * @param readerThreadName the name of thread to be created
51 public PowermaxSerialConnector(SerialPortManager serialPortManager, String serialPortName, int baudRate,
52 String readerThreadName) {
53 super(readerThreadName);
54 this.serialPortManager = serialPortManager;
55 this.serialPortName = serialPortName;
56 this.baudRate = baudRate;
57 this.serialPort = null;
62 logger.debug("open(): Opening Serial Connection");
65 SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
66 if (portIdentifier != null) {
67 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
69 serialPort = commPort;
70 serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
71 SerialPort.PARITY_NONE);
72 serialPort.enableReceiveThreshold(1);
73 serialPort.enableReceiveTimeout(250);
75 setInput(serialPort.getInputStream());
76 setOutput(serialPort.getOutputStream());
79 if (getInput().markSupported()) {
83 // RXTX serial port library causes high CPU load
84 // Start event listener, which will just sleep and slow down event
87 serialPort.addEventListener(this);
88 serialPort.notifyOnDataAvailable(true);
89 } catch (TooManyListenersException e) {
90 logger.debug("Too Many Listeners Exception: {}", e.getMessage(), e);
93 setReaderThread(new PowermaxReaderThread(this, readerThreadName));
94 getReaderThread().start();
98 logger.debug("open(): No Such Port: {}", serialPortName);
101 } catch (PortInUseException e) {
102 logger.debug("open(): Port in Use Exception: {}", e.getMessage(), e);
104 } catch (UnsupportedCommOperationException e) {
105 logger.debug("open(): Unsupported Comm Operation Exception: {}", e.getMessage(), e);
107 } catch (UnsupportedEncodingException e) {
108 logger.debug("open(): Unsupported Encoding Exception: {}", e.getMessage(), e);
110 } catch (IOException e) {
111 logger.debug("open(): IO Exception: {}", e.getMessage(), e);
117 public void close() {
118 logger.debug("close(): Closing Serial Connection");
120 if (serialPort != null) {
121 serialPort.removeEventListener();
126 if (serialPort != null) {
134 logger.debug("close(): Serial Connection closed");
138 public void serialEvent(SerialPortEvent serialPortEvent) {
140 logger.trace("RXTX library CPU load workaround, sleep forever");
141 Thread.sleep(Long.MAX_VALUE);
142 } catch (InterruptedException e) {