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 boolean simu;
38 protected final Thread readerThread;
40 /** The output stream */
41 protected @Nullable OutputStream dataOut;
43 /** The input stream */
44 protected @Nullable InputStream dataIn;
46 /** true if the connection is established, false if not */
47 private boolean connected;
52 * @param protocolHandler the protocol handler
53 * @param simu whether the communication is simulated or real
54 * @param readerThreadName the name of thread to be created
56 public RotelConnector(RotelAbstractProtocolHandler protocolHandler, boolean simu, String readerThreadName) {
58 this.readerThread = new RotelReaderThread(this, protocolHandler, readerThreadName);
62 * Get whether the connection is established or not
64 * @return true if the connection is established
66 public boolean isConnected() {
71 * Set whether the connection is established or not
73 * @param connected true if the connection is established
75 protected void setConnected(boolean connected) {
76 this.connected = connected;
80 * Open the connection with the Rotel device
82 * @throws RotelException - In case of any problem
84 public abstract void open() throws RotelException;
87 * Close the connection with the Rotel device
89 public abstract void close();
92 * Stop the thread that handles the feedback messages and close the opened input and output streams
94 protected void cleanup() {
95 readerThread.interrupt();
98 } catch (InterruptedException e) {
100 OutputStream dataOut = this.dataOut;
101 if (dataOut != null) {
104 } catch (IOException e) {
108 InputStream dataIn = this.dataIn;
109 if (dataIn != null) {
112 } catch (IOException e) {
119 * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
120 * actually read is returned as an integer.
122 * @param dataBuffer the buffer into which the data is read.
124 * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
125 * stream has been reached.
127 * @throws RotelException - If the input stream is null, if the first byte cannot be read for any reason
128 * other than the end of the file, if the input stream has been closed, or if some other I/O error
130 * @throws InterruptedIOException - if the thread was interrupted during the reading of the input stream
132 protected int readInput(byte[] dataBuffer) throws RotelException, InterruptedIOException {
134 throw new RotelException("readInput failed: should not be called in simu mode");
136 InputStream dataIn = this.dataIn;
137 if (dataIn == null) {
138 throw new RotelException("readInput failed: input stream is null");
141 return dataIn.read(dataBuffer);
142 } catch (IOException e) {
143 logger.debug("readInput failed: {}", e.getMessage());
144 throw new RotelException("readInput failed", e);
149 * Request the Rotel device to execute a command
151 * @param cmd the command to execute
152 * @param dataBuffer the data buffer containing the encoded command
154 * @throws RotelException - In case of any problem
156 public void writeOutput(RotelCommand cmd, byte[] dataBuffer) throws RotelException {
160 OutputStream dataOut = this.dataOut;
161 if (dataOut == null) {
162 throw new RotelException("Send command \"" + cmd.getLabel() + "\" failed: output stream is null");
165 dataOut.write(dataBuffer);
167 } catch (IOException e) {
168 logger.debug("Send command \"{}\" failed: {}", cmd, e.getMessage());
169 throw new RotelException("Send command \"" + cmd.getLabel() + "\" failed", e);
171 logger.debug("Send command \"{}\" succeeded", cmd);