]> git.basschouten.com Git - openhab-addons.git/blob
892e05f9ef1719cf48db483ac255d0944a107943
[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
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;
24
25 /**
26  * A class for the communication with the Visonic alarm panel through a serial connection
27  *
28  * @author Laurent Garnier - Initial contribution
29  */
30 public class PowermaxSerialConnector extends PowermaxConnector implements SerialPortEventListener {
31
32     private final Logger logger = LoggerFactory.getLogger(PowermaxSerialConnector.class);
33
34     private final String serialPortName;
35     private final int baudRate;
36     private final SerialPortManager serialPortManager;
37     private SerialPort serialPort;
38
39     /**
40      * Constructor
41      *
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
46      */
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;
54     }
55
56     @Override
57     public void open() throws Exception {
58         logger.debug("open(): Opening Serial Connection");
59
60         SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
61         if (portIdentifier == null) {
62             throw new IOException("No Such Port: " + serialPortName);
63         }
64
65         SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
66
67         serialPort = commPort;
68         serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
69         serialPort.enableReceiveThreshold(1);
70         serialPort.enableReceiveTimeout(250);
71
72         setInput(serialPort.getInputStream());
73         setOutput(serialPort.getOutputStream());
74
75         getOutput().flush();
76         if (getInput().markSupported()) {
77             getInput().reset();
78         }
79
80         // RXTX serial port library causes high CPU load
81         // Start event listener, which will just sleep and slow down event
82         // loop
83         serialPort.addEventListener(this);
84         serialPort.notifyOnDataAvailable(true);
85
86         setReaderThread(new PowermaxReaderThread(this, readerThreadName));
87         getReaderThread().start();
88
89         setConnected(true);
90     }
91
92     @Override
93     public void close() {
94         logger.debug("close(): Closing Serial Connection");
95
96         if (serialPort != null) {
97             serialPort.removeEventListener();
98         }
99
100         super.cleanup(true);
101
102         if (serialPort != null) {
103             serialPort.close();
104         }
105
106         serialPort = null;
107
108         setConnected(false);
109
110         logger.debug("close(): Serial Connection closed");
111     }
112
113     @Override
114     public void serialEvent(SerialPortEvent serialPortEvent) {
115         try {
116             logger.trace("RXTX library CPU load workaround, sleep forever");
117             Thread.sleep(Long.MAX_VALUE);
118         } catch (InterruptedException e) {
119         }
120     }
121 }