]> git.basschouten.com Git - openhab-addons.git/blob
bf9114bb06c39520605a775a94f47935ed9a9ac9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.rotel.internal.RotelException;
23 import org.openhab.binding.rotel.internal.RotelModel;
24 import org.openhab.core.io.transport.serial.PortInUseException;
25 import org.openhab.core.io.transport.serial.SerialPort;
26 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
27 import org.openhab.core.io.transport.serial.SerialPortManager;
28 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Class for communicating with the Rotel device through a serial connection
34  *
35  * @author Laurent Garnier - Initial contribution
36  */
37 @NonNullByDefault
38 public class RotelSerialConnector extends RotelConnector {
39
40     private final Logger logger = LoggerFactory.getLogger(RotelSerialConnector.class);
41
42     private String serialPortName;
43     private SerialPortManager serialPortManager;
44
45     private @Nullable SerialPort serialPort;
46
47     /**
48      * Constructor
49      *
50      * @param serialPortManager the serial port manager
51      * @param serialPortName the serial port name to be used
52      * @param model the projector model in use
53      * @param protocol the protocol to be used
54      * @param readerThreadName the name of thread to be created
55      */
56     public RotelSerialConnector(SerialPortManager serialPortManager, String serialPortName, RotelModel model,
57             RotelProtocol protocol, Map<RotelSource, String> sourcesLabels, String readerThreadName) {
58         super(model, protocol, sourcesLabels, false, readerThreadName);
59
60         this.serialPortManager = serialPortManager;
61         this.serialPortName = serialPortName;
62     }
63
64     @Override
65     public synchronized void open() throws RotelException {
66         logger.debug("Opening serial connection on port {}", serialPortName);
67         try {
68             SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
69             if (portIdentifier == null) {
70                 setConnected(false);
71                 throw new RotelException("Opening serial connection failed: no port " + serialPortName);
72             }
73
74             SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
75
76             commPort.setSerialPortParams(getModel().getBaudRate(), SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
77                     SerialPort.PARITY_NONE);
78             commPort.enableReceiveThreshold(1);
79             commPort.enableReceiveTimeout(100);
80             commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
81
82             InputStream dataIn = commPort.getInputStream();
83             OutputStream dataOut = commPort.getOutputStream();
84
85             if (dataOut != null) {
86                 dataOut.flush();
87             }
88             if (dataIn != null && dataIn.markSupported()) {
89                 try {
90                     dataIn.reset();
91                 } catch (IOException e) {
92                 }
93             }
94
95             Thread thread = new RotelReaderThread(this, readerThreadName);
96             setReaderThread(thread);
97             thread.start();
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 }