]> git.basschouten.com Git - openhab-addons.git/blob
28aec1acf1ed2402c471ce75e57849a03a85b83b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.powermax.internal.connector;
14
15 import java.io.IOException;
16 import java.io.UnsupportedEncodingException;
17 import java.util.TooManyListenersException;
18
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;
28
29 /**
30  * A class for the communication with the Visonic alarm panel through a serial connection
31  *
32  * @author Laurent Garnier - Initial contribution
33  */
34 public class PowermaxSerialConnector extends PowermaxConnector implements SerialPortEventListener {
35
36     private final Logger logger = LoggerFactory.getLogger(PowermaxSerialConnector.class);
37
38     private final String serialPortName;
39     private final int baudRate;
40     private final SerialPortManager serialPortManager;
41     private SerialPort serialPort;
42
43     /**
44      * Constructor
45      *
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
50      */
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;
58     }
59
60     @Override
61     public void open() {
62         logger.debug("open(): Opening Serial Connection");
63
64         try {
65             SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
66             if (portIdentifier != null) {
67                 SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
68
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);
74
75                 setInput(serialPort.getInputStream());
76                 setOutput(serialPort.getOutputStream());
77
78                 getOutput().flush();
79                 if (getInput().markSupported()) {
80                     getInput().reset();
81                 }
82
83                 // RXTX serial port library causes high CPU load
84                 // Start event listener, which will just sleep and slow down event
85                 // loop
86                 try {
87                     serialPort.addEventListener(this);
88                     serialPort.notifyOnDataAvailable(true);
89                 } catch (TooManyListenersException e) {
90                     logger.debug("Too Many Listeners Exception: {}", e.getMessage(), e);
91                 }
92
93                 setReaderThread(new PowermaxReaderThread(this, readerThreadName));
94                 getReaderThread().start();
95
96                 setConnected(true);
97             } else {
98                 logger.debug("open(): No Such Port: {}", serialPortName);
99                 setConnected(false);
100             }
101         } catch (PortInUseException e) {
102             logger.debug("open(): Port in Use Exception: {}", e.getMessage(), e);
103             setConnected(false);
104         } catch (UnsupportedCommOperationException e) {
105             logger.debug("open(): Unsupported Comm Operation Exception: {}", e.getMessage(), e);
106             setConnected(false);
107         } catch (UnsupportedEncodingException e) {
108             logger.debug("open(): Unsupported Encoding Exception: {}", e.getMessage(), e);
109             setConnected(false);
110         } catch (IOException e) {
111             logger.debug("open(): IO Exception: {}", e.getMessage(), e);
112             setConnected(false);
113         }
114     }
115
116     @Override
117     public void close() {
118         logger.debug("close(): Closing Serial Connection");
119
120         if (serialPort != null) {
121             serialPort.removeEventListener();
122         }
123
124         super.cleanup(true);
125
126         if (serialPort != null) {
127             serialPort.close();
128         }
129
130         serialPort = null;
131
132         setConnected(false);
133
134         logger.debug("close(): Serial Connection closed");
135     }
136
137     @Override
138     public void serialEvent(SerialPortEvent serialPortEvent) {
139         try {
140             logger.trace("RXTX library CPU load workaround, sleep forever");
141             Thread.sleep(Long.MAX_VALUE);
142         } catch (InterruptedException e) {
143         }
144     }
145 }