]> git.basschouten.com Git - openhab-addons.git/blob
e8bf2424d97019b3c09f8a5316dc9676f814b62b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.InputStream;
17 import java.io.OutputStream;
18
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;
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 @NonNullByDefault
35 public class PowermaxSerialConnector extends PowermaxConnector implements SerialPortEventListener {
36
37     private final Logger logger = LoggerFactory.getLogger(PowermaxSerialConnector.class);
38
39     private final String serialPortName;
40     private final int baudRate;
41     private final SerialPortManager serialPortManager;
42     private @Nullable SerialPort serialPort;
43
44     /**
45      * Constructor
46      *
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
51      */
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;
58     }
59
60     @Override
61     public void open() throws Exception {
62         logger.debug("open(): Opening Serial Connection");
63
64         SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
65         if (portIdentifier == null) {
66             throw new IOException("No Such Port: " + serialPortName);
67         }
68
69         SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
70
71         serialPort = commPort;
72         commPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
73         commPort.enableReceiveThreshold(1);
74         commPort.enableReceiveTimeout(250);
75
76         InputStream inputStream = commPort.getInputStream();
77         setInput(inputStream);
78         OutputStream outputStream = commPort.getOutputStream();
79         setOutput(outputStream);
80
81         if (outputStream != null) {
82             outputStream.flush();
83         }
84         if (inputStream != null && inputStream.markSupported()) {
85             inputStream.reset();
86         }
87
88         // RXTX serial port library causes high CPU load
89         // Start event listener, which will just sleep and slow down event
90         // loop
91         commPort.addEventListener(this);
92         commPort.notifyOnDataAvailable(true);
93
94         PowermaxReaderThread readerThread = new PowermaxReaderThread(this, readerThreadName);
95         setReaderThread(readerThread);
96         readerThread.start();
97
98         setConnected(true);
99     }
100
101     @Override
102     public void close() {
103         logger.debug("close(): Closing Serial Connection");
104
105         SerialPort commPort = serialPort;
106         if (commPort != null) {
107             commPort.removeEventListener();
108         }
109
110         super.cleanup(true);
111
112         if (commPort != null) {
113             commPort.close();
114         }
115
116         serialPort = null;
117
118         setConnected(false);
119
120         logger.debug("close(): Serial Connection closed");
121     }
122
123     @Override
124     public void serialEvent(SerialPortEvent serialPortEvent) {
125         try {
126             logger.trace("RXTX library CPU load workaround, sleep forever");
127             Thread.sleep(Long.MAX_VALUE);
128         } catch (InterruptedException e) {
129             Thread.currentThread().interrupt();
130         }
131     }
132 }