]> git.basschouten.com Git - openhab-addons.git/blob
1c91803eae529755758a4763f18b9d55acf3ae5b
[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.rotel.internal.communication;
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.binding.rotel.internal.RotelException;
22 import org.openhab.binding.rotel.internal.protocol.RotelAbstractProtocolHandler;
23 import org.openhab.core.io.transport.serial.PortInUseException;
24 import org.openhab.core.io.transport.serial.SerialPort;
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  * Class for communicating with the Rotel device through a serial connection
33  *
34  * @author Laurent Garnier - Initial contribution
35  */
36 @NonNullByDefault
37 public class RotelSerialConnector extends RotelConnector {
38
39     private final Logger logger = LoggerFactory.getLogger(RotelSerialConnector.class);
40
41     private String serialPortName;
42     private SerialPortManager serialPortManager;
43
44     private int baudRate;
45
46     private @Nullable SerialPort serialPort;
47
48     /**
49      * Constructor
50      *
51      * @param serialPortManager the serial port manager
52      * @param serialPortName the serial port name to be used
53      * @param baudRate the baud rate to be used
54      * @param protocolHandler the protocol handler
55      * @param readerThreadName the name of thread to be created
56      */
57     public RotelSerialConnector(SerialPortManager serialPortManager, String serialPortName, int baudRate,
58             RotelAbstractProtocolHandler protocolHandler, String readerThreadName) {
59         super(protocolHandler, false, readerThreadName);
60
61         this.baudRate = baudRate;
62         this.serialPortManager = serialPortManager;
63         this.serialPortName = serialPortName;
64     }
65
66     @Override
67     public synchronized void open() throws RotelException {
68         logger.debug("Opening serial connection on port {}", serialPortName);
69         try {
70             SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
71             if (portIdentifier == null) {
72                 setConnected(false);
73                 throw new RotelException("Opening serial connection failed: no port " + serialPortName);
74             }
75
76             SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
77
78             commPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
79                     SerialPort.PARITY_NONE);
80             commPort.enableReceiveThreshold(1);
81             commPort.enableReceiveTimeout(100);
82             commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
83
84             InputStream dataIn = commPort.getInputStream();
85             OutputStream dataOut = commPort.getOutputStream();
86
87             if (dataOut != null) {
88                 dataOut.flush();
89             }
90             if (dataIn != null && dataIn.markSupported()) {
91                 try {
92                     dataIn.reset();
93                 } catch (IOException e) {
94                 }
95             }
96
97             startReaderThread();
98
99             this.serialPort = commPort;
100             this.dataIn = dataIn;
101             this.dataOut = dataOut;
102
103             setConnected(true);
104
105             logger.debug("Serial connection opened");
106         } catch (PortInUseException | UnsupportedCommOperationException | IOException e) {
107             setConnected(false);
108             throw new RotelException("Opening serial connection failed", e);
109         }
110     }
111
112     @Override
113     public synchronized void close() {
114         logger.debug("Closing serial connection");
115         SerialPort serialPort = this.serialPort;
116         if (serialPort != null) {
117             serialPort.removeEventListener();
118         }
119         super.cleanup();
120         if (serialPort != null) {
121             serialPort.close();
122             this.serialPort = null;
123         }
124         setConnected(false);
125         logger.debug("Serial connection closed");
126     }
127 }