]> git.basschouten.com Git - openhab-addons.git/blob
0849ac4a781a526312c1360ec542ac901126bc39
[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.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 boolean simu;
38     protected final Thread readerThread;
39
40     /** The output stream */
41     protected @Nullable OutputStream dataOut;
42
43     /** The input stream */
44     protected @Nullable InputStream dataIn;
45
46     /** true if the connection is established, false if not */
47     private boolean connected;
48
49     /**
50      * Constructor
51      *
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
55      */
56     public RotelConnector(RotelAbstractProtocolHandler protocolHandler, boolean simu, String readerThreadName) {
57         this.simu = simu;
58         this.readerThread = new RotelReaderThread(this, protocolHandler, readerThreadName);
59     }
60
61     /**
62      * Get whether the connection is established or not
63      *
64      * @return true if the connection is established
65      */
66     public boolean isConnected() {
67         return connected;
68     }
69
70     /**
71      * Set whether the connection is established or not
72      *
73      * @param connected true if the connection is established
74      */
75     protected void setConnected(boolean connected) {
76         this.connected = connected;
77     }
78
79     /**
80      * Open the connection with the Rotel device
81      *
82      * @throws RotelException - In case of any problem
83      */
84     public abstract void open() throws RotelException;
85
86     /**
87      * Close the connection with the Rotel device
88      */
89     public abstract void close();
90
91     /**
92      * Stop the thread that handles the feedback messages and close the opened input and output streams
93      */
94     protected void cleanup() {
95         readerThread.interrupt();
96         try {
97             readerThread.join();
98         } catch (InterruptedException e) {
99         }
100         OutputStream dataOut = this.dataOut;
101         if (dataOut != null) {
102             try {
103                 dataOut.close();
104             } catch (IOException e) {
105             }
106             this.dataOut = null;
107         }
108         InputStream dataIn = this.dataIn;
109         if (dataIn != null) {
110             try {
111                 dataIn.close();
112             } catch (IOException e) {
113             }
114             this.dataIn = null;
115         }
116     }
117
118     /**
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.
121      *
122      * @param dataBuffer the buffer into which the data is read.
123      *
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.
126      *
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
129      *             occurs.
130      * @throws InterruptedIOException - if the thread was interrupted during the reading of the input stream
131      */
132     protected int readInput(byte[] dataBuffer) throws RotelException, InterruptedIOException {
133         if (simu) {
134             throw new RotelException("readInput failed: should not be called in simu mode");
135         }
136         InputStream dataIn = this.dataIn;
137         if (dataIn == null) {
138             throw new RotelException("readInput failed: input stream is null");
139         }
140         try {
141             return dataIn.read(dataBuffer);
142         } catch (IOException e) {
143             logger.debug("readInput failed: {}", e.getMessage());
144             throw new RotelException("readInput failed", e);
145         }
146     }
147
148     /**
149      * Request the Rotel device to execute a command
150      *
151      * @param cmdName the command name
152      * @param dataBuffer the data buffer containing the encoded command
153      *
154      * @throws RotelException - In case of any problem
155      */
156     public void writeOutput(String cmdName, byte[] dataBuffer) throws RotelException {
157         if (simu) {
158             return;
159         }
160         OutputStream dataOut = this.dataOut;
161         if (dataOut == null) {
162             throw new RotelException("Send command \"" + cmdName + "\" failed: output stream is null");
163         }
164         try {
165             dataOut.write(dataBuffer);
166             dataOut.flush();
167         } catch (IOException e) {
168             logger.debug("Send command \"{}\" failed: {}", cmdName, e.getMessage());
169             throw new RotelException("Send command \"" + cmdName + "\" failed", e);
170         }
171         logger.debug("Send command \"{}\" succeeded", cmdName);
172     }
173 }