]> git.basschouten.com Git - openhab-addons.git/blob
f55df95f9f8ce19a463295ecab031306a0f81bdd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 import java.io.UnsupportedEncodingException;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.rotel.internal.RotelException;
24 import org.openhab.binding.rotel.internal.RotelModel;
25 import org.openhab.core.io.transport.serial.PortInUseException;
26 import org.openhab.core.io.transport.serial.SerialPort;
27 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
28 import org.openhab.core.io.transport.serial.SerialPortManager;
29 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Class for communicating with the Rotel device through a serial connection
35  *
36  * @author Laurent Garnier - Initial contribution
37  */
38 @NonNullByDefault
39 public class RotelSerialConnector extends RotelConnector {
40
41     private final Logger logger = LoggerFactory.getLogger(RotelSerialConnector.class);
42
43     private String serialPortName;
44     private SerialPortManager serialPortManager;
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 model the projector model in use
54      * @param protocol the protocol to be used
55      * @param readerThreadName the name of thread to be created
56      */
57     public RotelSerialConnector(SerialPortManager serialPortManager, String serialPortName, RotelModel model,
58             RotelProtocol protocol, Map<RotelSource, String> sourcesLabels, String readerThreadName) {
59         super(model, protocol, sourcesLabels, false, readerThreadName);
60
61         this.serialPortManager = serialPortManager;
62         this.serialPortName = serialPortName;
63     }
64
65     @Override
66     public synchronized void open() throws RotelException {
67         logger.debug("Opening serial connection on port {}", serialPortName);
68         try {
69             SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
70             if (portIdentifier == null) {
71                 setConnected(false);
72                 logger.debug("Opening serial connection failed: No Such Port: {}", serialPortName);
73                 throw new RotelException("Opening serial connection failed: No Such Port");
74             }
75
76             SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
77
78             commPort.setSerialPortParams(getModel().getBaudRate(), 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             Thread thread = new RotelReaderThread(this, readerThreadName);
98             setReaderThread(thread);
99             thread.start();
100
101             this.serialPort = commPort;
102             this.dataIn = dataIn;
103             this.dataOut = dataOut;
104
105             setConnected(true);
106
107             logger.debug("Serial connection opened");
108         } catch (PortInUseException e) {
109             setConnected(false);
110             logger.debug("Opening serial connection failed: Port in Use Exception: {}", e.getMessage(), e);
111             throw new RotelException("Opening serial connection failed: Port in Use Exception");
112         } catch (UnsupportedCommOperationException e) {
113             setConnected(false);
114             logger.debug("Opening serial connection failed: Unsupported Comm Operation Exception: {}", e.getMessage(),
115                     e);
116             throw new RotelException("Opening serial connection failed: Unsupported Comm Operation Exception");
117         } catch (UnsupportedEncodingException e) {
118             setConnected(false);
119             logger.debug("Opening serial connection failed: Unsupported Encoding Exception: {}", e.getMessage(), e);
120             throw new RotelException("Opening serial connection failed: Unsupported Encoding Exception");
121         } catch (IOException e) {
122             setConnected(false);
123             logger.debug("Opening serial connection failed: IO Exception: {}", e.getMessage(), e);
124             throw new RotelException("Opening serial connection failed: IO Exception");
125         }
126     }
127
128     @Override
129     public synchronized void close() {
130         logger.debug("Closing serial connection");
131         SerialPort serialPort = this.serialPort;
132         if (serialPort != null) {
133             serialPort.removeEventListener();
134         }
135         super.cleanup();
136         if (serialPort != null) {
137             serialPort.close();
138             this.serialPort = null;
139         }
140         setConnected(false);
141         logger.debug("Serial connection closed");
142     }
143 }