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.monopriceaudio.internal.communication;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.OutputStream;
18 import java.nio.charset.StandardCharsets;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.monopriceaudio.internal.MonopriceAudioException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Abstract class for communicating with the MonopriceAudio device
33 * @author Laurent Garnier - Initial contribution
34 * @author Michael Lobstein - Adapted for the MonopriceAudio binding
37 public abstract class MonopriceAudioConnector {
38 public static final String READ_ERROR = "Command Error.";
41 public static final String KEY_ZONE_UPDATE = "zone_update";
42 // Special keys used by the binding
43 public static final String KEY_ERROR = "error";
44 public static final String MSG_VALUE_ON = "on";
46 private static final Pattern PATTERN = Pattern.compile("^.*#>(\\d{22})$", Pattern.DOTALL);
47 private static final String BEGIN_CMD = "<";
48 private static final String END_CMD = "\r";
50 private final Logger logger = LoggerFactory.getLogger(MonopriceAudioConnector.class);
52 /** The output stream */
53 protected @Nullable OutputStream dataOut;
55 /** The input stream */
56 protected @Nullable InputStream dataIn;
58 /** true if the connection is established, false if not */
59 private boolean connected;
61 private @Nullable Thread readerThread;
63 private final List<MonopriceAudioMessageEventListener> listeners = new ArrayList<>();
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 * Set the thread that handles the feedback messages
86 * @param readerThread the thread
88 protected void setReaderThread(Thread readerThread) {
89 this.readerThread = readerThread;
93 * Open the connection with the MonopriceAudio device
95 * @throws MonopriceAudioException - In case of any problem
97 public abstract void open() throws MonopriceAudioException;
100 * Close the connection with the MonopriceAudio device
102 public abstract void close();
105 * Stop the thread that handles the feedback messages and close the opened input and output streams
107 protected void cleanup() {
108 Thread readerThread = this.readerThread;
109 OutputStream dataOut = this.dataOut;
110 if (dataOut != null) {
113 } catch (IOException e) {
114 logger.debug("Error closing dataOut: {}", e.getMessage());
118 InputStream dataIn = this.dataIn;
119 if (dataIn != null) {
122 } catch (IOException e) {
123 logger.debug("Error closing dataIn: {}", e.getMessage());
127 if (readerThread != null) {
128 readerThread.interrupt();
130 readerThread.join(3000);
131 } catch (InterruptedException e) {
132 logger.warn("Error joining readerThread: {}", e.getMessage());
134 this.readerThread = null;
139 * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
140 * actually read is returned as an integer.
142 * @param dataBuffer the buffer into which the data is read.
144 * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
145 * stream has been reached.
147 * @throws MonopriceAudioException - If the input stream is null, if the first byte cannot be read for any reason
148 * other than the end of the file, if the input stream has been closed, or if some other I/O error
151 protected int readInput(byte[] dataBuffer) throws MonopriceAudioException {
152 InputStream dataIn = this.dataIn;
153 if (dataIn == null) {
154 throw new MonopriceAudioException("readInput failed: input stream is null");
157 return dataIn.read(dataBuffer);
158 } catch (IOException e) {
159 throw new MonopriceAudioException("readInput failed: " + e.getMessage(), e);
164 * Get the status of a zone
166 * @param zone the zone to query for current status
168 * @throws MonopriceAudioException - In case of any problem
170 public void queryZone(MonopriceAudioZone zone) throws MonopriceAudioException {
171 sendCommand(zone, MonopriceAudioCommand.QUERY, null);
175 * Request the MonopriceAudio controller to execute a command
177 * @param zone the zone for which the command is to be run
178 * @param cmd the command to execute
179 * @param value the integer value to consider for volume, bass, treble, etc. adjustment
181 * @throws MonopriceAudioException - In case of any problem
183 public void sendCommand(MonopriceAudioZone zone, MonopriceAudioCommand cmd, @Nullable Integer value)
184 throws MonopriceAudioException {
185 String messageStr = "";
187 if (cmd == MonopriceAudioCommand.QUERY) {
188 // query special case (ie: ? + zoneId)
189 messageStr = cmd.getValue() + zone.getZoneId();
190 } else if (value != null) {
191 // if the command passed a value, append it to the messageStr
192 messageStr = BEGIN_CMD + zone.getZoneId() + cmd.getValue() + String.format("%02d", value);
194 throw new MonopriceAudioException("Send command \"" + messageStr + "\" failed: passed in value is null");
196 messageStr += END_CMD;
197 logger.debug("Send command {}", messageStr);
199 OutputStream dataOut = this.dataOut;
200 if (dataOut == null) {
201 throw new MonopriceAudioException("Send command \"" + messageStr + "\" failed: output stream is null");
204 dataOut.write(messageStr.getBytes(StandardCharsets.US_ASCII));
206 } catch (IOException e) {
207 throw new MonopriceAudioException("Send command \"" + cmd.getValue() + "\" failed: " + e.getMessage(), e);
212 * Add a listener to the list of listeners to be notified with events
214 * @param listener the listener
216 public void addEventListener(MonopriceAudioMessageEventListener listener) {
217 listeners.add(listener);
221 * Remove a listener from the list of listeners to be notified with events
223 * @param listener the listener
225 public void removeEventListener(MonopriceAudioMessageEventListener listener) {
226 listeners.remove(listener);
230 * Analyze an incoming message and dispatch corresponding (key, value) to the event listeners
232 * @param incomingMessage the received message
234 public void handleIncomingMessage(byte[] incomingMessage) {
235 String message = new String(incomingMessage, StandardCharsets.US_ASCII).trim();
237 logger.debug("handleIncomingMessage: {}", message);
239 if (READ_ERROR.equals(message)) {
240 dispatchKeyValue(KEY_ERROR, MSG_VALUE_ON);
244 // Amp controller sends status string: #>1200010000130809100601
245 Matcher matcher = PATTERN.matcher(message);
246 if (matcher.find()) {
247 // pull out just the digits and send them as an event
248 dispatchKeyValue(KEY_ZONE_UPDATE, matcher.group(1));
250 logger.debug("no match on message: {}", message);
255 * Dispatch an event (key, value) to the event listeners
258 * @param value the value
260 private void dispatchKeyValue(String key, String value) {
261 MonopriceAudioMessageEvent event = new MonopriceAudioMessageEvent(this, key, value);
262 listeners.forEach(l -> l.onNewMessageEvent(event));