2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.rotel.internal.communication;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InterruptedIOException;
18 import java.io.OutputStream;
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.protocol.RotelAbstractProtocolHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Abstract class for communicating with the Rotel device
30 * @author Laurent Garnier - Initial contribution
33 public abstract class RotelConnector {
35 private final Logger logger = LoggerFactory.getLogger(RotelConnector.class);
37 private final RotelAbstractProtocolHandler protocolHandler;
38 private final boolean simu;
39 private final String readerThreadName;
41 private @Nullable Thread readerThread;
43 /** The output stream */
44 protected @Nullable OutputStream dataOut;
46 /** The input stream */
47 protected @Nullable InputStream dataIn;
49 /** true if the connection is established, false if not */
50 private boolean connected;
55 * @param protocolHandler the protocol handler
56 * @param simu whether the communication is simulated or real
57 * @param readerThreadName the name of thread to be created
59 public RotelConnector(RotelAbstractProtocolHandler protocolHandler, boolean simu, String readerThreadName) {
60 this.protocolHandler = protocolHandler;
62 this.readerThreadName = readerThreadName;
66 * Get whether the connection is established or not
68 * @return true if the connection is established
70 public boolean isConnected() {
75 * Set whether the connection is established or not
77 * @param connected true if the connection is established
79 protected void setConnected(boolean connected) {
80 this.connected = connected;
84 * Open the connection with the Rotel device
86 * @throws RotelException - In case of any problem
88 public abstract void open() throws RotelException;
91 * Close the connection with the Rotel device
93 public abstract void close();
95 protected void startReaderThread() {
96 Thread thread = readerThread;
97 if (thread == null || thread.isInterrupted()) {
98 thread = new RotelReaderThread(this, protocolHandler, readerThreadName);
99 readerThread = thread;
104 protected void stopReaderThread() {
105 Thread thread = readerThread;
106 if (thread != null) {
110 } catch (InterruptedException e) {
117 * Stop the thread that handles the feedback messages and close the opened input and output streams
119 protected void cleanup() {
121 OutputStream dataOut = this.dataOut;
122 if (dataOut != null) {
125 } catch (IOException e) {
129 InputStream dataIn = this.dataIn;
130 if (dataIn != null) {
133 } catch (IOException e) {
140 * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
141 * actually read is returned as an integer.
143 * @param dataBuffer the buffer into which the data is read.
145 * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
146 * stream has been reached.
148 * @throws RotelException - If the input stream is null, if the first byte cannot be read for any reason
149 * other than the end of the file, if the input stream has been closed, or if some other I/O error
151 * @throws InterruptedIOException - if the thread was interrupted during the reading of the input stream
153 protected int readInput(byte[] dataBuffer) throws RotelException, InterruptedIOException {
155 throw new RotelException("readInput failed: should not be called in simu mode");
157 InputStream dataIn = this.dataIn;
158 if (dataIn == null) {
159 throw new RotelException("readInput failed: input stream is null");
162 return dataIn.read(dataBuffer);
163 } catch (IOException e) {
164 logger.debug("readInput failed: {}", e.getMessage());
165 throw new RotelException("readInput failed", e);
170 * Request the Rotel device to execute a command
172 * @param cmd the command to execute
173 * @param dataBuffer the data buffer containing the encoded command
175 * @throws RotelException - In case of any problem
177 public void writeOutput(RotelCommand cmd, byte[] dataBuffer) throws RotelException {
181 OutputStream dataOut = this.dataOut;
182 if (dataOut == null) {
183 throw new RotelException("Send command \"" + cmd.getLabel() + "\" failed: output stream is null");
186 dataOut.write(dataBuffer);
188 } catch (IOException e) {
189 logger.debug("Send command \"{}\" failed: {}", cmd, e.getMessage());
190 throw new RotelException("Send command \"" + cmd.getLabel() + "\" failed", e);
192 logger.debug("Send command \"{}\" succeeded", cmd);