]> git.basschouten.com Git - openhab-addons.git/blob
4dc0cbc317221d2178d89c71e03f00f7a9a80b84
[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.InterruptedIOException;
18 import java.io.OutputStream;
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.protocol.RotelAbstractProtocolHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Abstract class for communicating with the Rotel device
29  *
30  * @author Laurent Garnier - Initial contribution
31  */
32 @NonNullByDefault
33 public abstract class RotelConnector {
34
35     private final Logger logger = LoggerFactory.getLogger(RotelConnector.class);
36
37     private final RotelAbstractProtocolHandler protocolHandler;
38     private final boolean simu;
39     private final String readerThreadName;
40
41     private @Nullable Thread readerThread;
42
43     /** The output stream */
44     protected @Nullable OutputStream dataOut;
45
46     /** The input stream */
47     protected @Nullable InputStream dataIn;
48
49     /** true if the connection is established, false if not */
50     private boolean connected;
51
52     /**
53      * Constructor
54      *
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
58      */
59     public RotelConnector(RotelAbstractProtocolHandler protocolHandler, boolean simu, String readerThreadName) {
60         this.protocolHandler = protocolHandler;
61         this.simu = simu;
62         this.readerThreadName = readerThreadName;
63     }
64
65     /**
66      * Get whether the connection is established or not
67      *
68      * @return true if the connection is established
69      */
70     public boolean isConnected() {
71         return connected;
72     }
73
74     /**
75      * Set whether the connection is established or not
76      *
77      * @param connected true if the connection is established
78      */
79     protected void setConnected(boolean connected) {
80         this.connected = connected;
81     }
82
83     /**
84      * Open the connection with the Rotel device
85      *
86      * @throws RotelException - In case of any problem
87      */
88     public abstract void open() throws RotelException;
89
90     /**
91      * Close the connection with the Rotel device
92      */
93     public abstract void close();
94
95     protected void startReaderThread() {
96         Thread thread = readerThread;
97         if (thread == null || thread.isInterrupted()) {
98             thread = new RotelReaderThread(this, protocolHandler, readerThreadName);
99             readerThread = thread;
100             thread.start();
101         }
102     }
103
104     protected void stopReaderThread() {
105         Thread thread = readerThread;
106         if (thread != null) {
107             thread.interrupt();
108             try {
109                 thread.join();
110             } catch (InterruptedException e) {
111             }
112             readerThread = null;
113         }
114     }
115
116     /**
117      * Stop the thread that handles the feedback messages and close the opened input and output streams
118      */
119     protected void cleanup() {
120         stopReaderThread();
121         OutputStream dataOut = this.dataOut;
122         if (dataOut != null) {
123             try {
124                 dataOut.close();
125             } catch (IOException e) {
126             }
127             this.dataOut = null;
128         }
129         InputStream dataIn = this.dataIn;
130         if (dataIn != null) {
131             try {
132                 dataIn.close();
133             } catch (IOException e) {
134             }
135             this.dataIn = null;
136         }
137     }
138
139     /**
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.
142      *
143      * @param dataBuffer the buffer into which the data is read.
144      *
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.
147      *
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
150      *             occurs.
151      * @throws InterruptedIOException - if the thread was interrupted during the reading of the input stream
152      */
153     protected int readInput(byte[] dataBuffer) throws RotelException, InterruptedIOException {
154         if (simu) {
155             throw new RotelException("readInput failed: should not be called in simu mode");
156         }
157         InputStream dataIn = this.dataIn;
158         if (dataIn == null) {
159             throw new RotelException("readInput failed: input stream is null");
160         }
161         try {
162             return dataIn.read(dataBuffer);
163         } catch (IOException e) {
164             logger.debug("readInput failed: {}", e.getMessage());
165             throw new RotelException("readInput failed", e);
166         }
167     }
168
169     /**
170      * Request the Rotel device to execute a command
171      *
172      * @param cmd the command to execute
173      * @param dataBuffer the data buffer containing the encoded command
174      *
175      * @throws RotelException - In case of any problem
176      */
177     public void writeOutput(RotelCommand cmd, byte[] dataBuffer) throws RotelException {
178         if (simu) {
179             return;
180         }
181         OutputStream dataOut = this.dataOut;
182         if (dataOut == null) {
183             throw new RotelException("Send command \"" + cmd.getLabel() + "\" failed: output stream is null");
184         }
185         try {
186             dataOut.write(dataBuffer);
187             dataOut.flush();
188         } catch (IOException e) {
189             logger.debug("Send command \"{}\" failed: {}", cmd, e.getMessage());
190             throw new RotelException("Send command \"" + cmd.getLabel() + "\" failed", e);
191         }
192         logger.debug("Send command \"{}\" succeeded", cmd);
193     }
194 }