]> git.basschouten.com Git - openhab-addons.git/blob
f0df8273942685082aa0032352c36a8a7138da52
[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.satel.internal.protocol;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.util.TooManyListenersException;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.core.io.transport.serial.PortInUseException;
22 import org.openhab.core.io.transport.serial.SerialPort;
23 import org.openhab.core.io.transport.serial.SerialPortEvent;
24 import org.openhab.core.io.transport.serial.SerialPortEventListener;
25 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
26 import org.openhab.core.io.transport.serial.SerialPortManager;
27 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Represents Satel INT-RS module. Implements methods required to connect and
33  * communicate with that module over serial protocol.
34  *
35  * @author Krzysztof Goworek - Initial contribution
36  */
37 @NonNullByDefault
38 public class IntRSModule extends SatelModule {
39
40     private final Logger logger = LoggerFactory.getLogger(IntRSModule.class);
41
42     private final String port;
43
44     private final SerialPortManager serialPortManager;
45
46     /**
47      * Creates new instance with port and timeout set to specified values.
48      *
49      * @param port serial port the module is connected to
50      * @param serialPortManager serial port manager object
51      * @param timeout timeout value in milliseconds for connect/read/write operations
52      * @param extPayloadSupport if <code>true</code>, the module supports extended command payload for reading
53      *            INTEGRA 256 state
54      */
55     public IntRSModule(String port, SerialPortManager serialPortManager, int timeout, boolean extPayloadSupport) {
56         super(timeout, extPayloadSupport);
57
58         this.port = port;
59         this.serialPortManager = serialPortManager;
60     }
61
62     @Override
63     protected CommunicationChannel connect() throws ConnectionFailureException {
64         logger.info("Connecting to INT-RS module at {}", this.port);
65
66         try {
67             SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(this.port);
68             if (portIdentifier == null) {
69                 throw new ConnectionFailureException(String.format("Port %s does not exist", this.port));
70             }
71             SerialPort serialPort = portIdentifier.open("org.openhab.binding.satel", 2000);
72             boolean supportsReceiveTimeout = false;
73             serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
74             try {
75                 serialPort.enableReceiveTimeout(this.getTimeout());
76                 supportsReceiveTimeout = true;
77             } catch (UnsupportedCommOperationException e) {
78                 logger.debug("Receive timeout is unsupported for port {}", this.port);
79             }
80             try {
81                 serialPort.enableReceiveThreshold(1);
82             } catch (UnsupportedCommOperationException e) {
83                 logger.debug("Receive threshold is unsupported for port {}", this.port);
84             }
85             // RXTX serial port library causes high CPU load
86             // Start event listener, which will just sleep and slow down event
87             // loop
88             serialPort.addEventListener(new SerialPortEventListener() {
89                 @Override
90                 public void serialEvent(SerialPortEvent ev) {
91                     try {
92                         logger.trace("RXTX library CPU load workaround, sleep forever");
93                         Thread.sleep(Long.MAX_VALUE);
94                     } catch (InterruptedException e) {
95                     }
96                 }
97             });
98             serialPort.notifyOnDataAvailable(false);
99
100             logger.info("INT-RS module connected successfuly");
101             return new SerialCommunicationChannel(serialPort, supportsReceiveTimeout);
102         } catch (PortInUseException e) {
103             throw new ConnectionFailureException(String.format("Port %s in use", this.port), e);
104         } catch (UnsupportedCommOperationException e) {
105             throw new ConnectionFailureException(String.format("Unsupported comm operation on port %s", this.port), e);
106         } catch (TooManyListenersException e) {
107             throw new ConnectionFailureException(String.format("Too many listeners on port %s", this.port), e);
108         }
109     }
110
111     private class SerialCommunicationChannel implements CommunicationChannel {
112
113         private final SerialPort serialPort;
114
115         private final boolean supportsReceiveTimeout;
116
117         public SerialCommunicationChannel(SerialPort serialPort, boolean supportsReceiveTimeout) {
118             this.serialPort = serialPort;
119             this.supportsReceiveTimeout = supportsReceiveTimeout;
120         }
121
122         @Override
123         public InputStream getInputStream() throws IOException {
124             final InputStream stream = this.serialPort.getInputStream();
125             if (stream != null) {
126                 return stream;
127             }
128             throw new IOException("Selected port doesn't support receiving data: " + this.serialPort.getName());
129         }
130
131         @Override
132         public OutputStream getOutputStream() throws IOException {
133             final OutputStream stream = this.serialPort.getOutputStream();
134             if (stream != null) {
135                 return stream;
136             }
137             throw new IOException("Selected port doesn't support sending data: " + this.serialPort.getName());
138         }
139
140         @Override
141         public void disconnect() {
142             logger.debug("Closing connection to INT-RS module");
143             try {
144                 this.serialPort.close();
145                 logger.info("Connection to INT-RS module has been closed");
146             } catch (Exception e) {
147                 logger.error("An error occurred during closing serial port", e);
148             }
149         }
150
151         @Override
152         public boolean supportsReceiveTimeout() {
153             return supportsReceiveTimeout;
154         }
155     }
156 }