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 static org.openhab.binding.monopriceaudio.internal.MonopriceAudioBindingConstants.*;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.nio.charset.StandardCharsets;
21 import java.util.ArrayList;
22 import java.util.List;
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
35 * @author Michael Lobstein - Add support for additional amplifier types
38 public abstract class MonopriceAudioConnector {
40 public static final String KEY_ZONE_UPDATE = "zone_update";
41 public static final String KEY_PING = "ping";
43 private final Logger logger = LoggerFactory.getLogger(MonopriceAudioConnector.class);
45 /** The output stream */
46 protected @Nullable OutputStream dataOut;
48 /** The input stream */
49 protected @Nullable InputStream dataIn;
51 /** true if the connection is established, false if not */
52 private boolean connected;
53 private boolean pingResponseOnly;
55 private @Nullable AmplifierModel amp;
57 private @Nullable Thread readerThread;
59 private final List<MonopriceAudioMessageEventListener> listeners = new ArrayList<>();
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;
77 this.pingResponseOnly = false;
81 * Set the AmplifierModel
83 * @param amp the AmplifierModel being used
85 protected void setAmplifierModel(AmplifierModel amp) {
90 * Set the thread that handles the feedback messages
92 * @param readerThread the thread
94 protected void setReaderThread(Thread readerThread) {
95 this.readerThread = readerThread;
99 * Open the connection with the MonopriceAudio device
101 * @throws MonopriceAudioException - In case of any problem
103 public abstract void open() throws MonopriceAudioException;
106 * Close the connection with the MonopriceAudio device
108 public abstract void close();
111 * Stop the thread that handles the feedback messages and close the opened input and output streams
113 protected void cleanup() {
114 this.pingResponseOnly = false;
115 Thread readerThread = this.readerThread;
116 OutputStream dataOut = this.dataOut;
117 if (dataOut != null) {
120 } catch (IOException e) {
121 logger.debug("Error closing dataOut: {}", e.getMessage());
125 InputStream dataIn = this.dataIn;
126 if (dataIn != null) {
129 } catch (IOException e) {
130 logger.debug("Error closing dataIn: {}", e.getMessage());
134 if (readerThread != null) {
135 readerThread.interrupt();
137 readerThread.join(3000);
138 } catch (InterruptedException e) {
139 logger.debug("Error joining readerThread: {}", e.getMessage());
141 this.readerThread = null;
146 * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
147 * actually read is returned as an integer.
149 * @param dataBuffer the buffer into which the data is read.
151 * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
152 * stream has been reached.
154 * @throws MonopriceAudioException - If the input stream is null, if the first byte cannot be read for any reason
155 * other than the end of the file, if the input stream has been closed, or if some other I/O error
158 protected int readInput(byte[] dataBuffer) throws MonopriceAudioException {
159 InputStream dataIn = this.dataIn;
160 if (dataIn == null) {
161 throw new MonopriceAudioException("readInput failed: input stream is null");
164 return dataIn.read(dataBuffer);
165 } catch (IOException e) {
166 throw new MonopriceAudioException("readInput failed: " + e.getMessage(), e);
171 * Get only ping success events from the connector. If amplifier does not have keypads or supports
172 * unsolicited updates, the use of this method will cause the connector to only send ping success events until the
173 * next time the connection is reset.
175 * @throws MonopriceAudioException - In case of any problem
177 public void sendPing() throws MonopriceAudioException {
178 pingResponseOnly = true;
179 // poll zone 1 status only to see if the amp responds
180 queryZone(amp.getZoneIds().get(0));
184 * Get the status of a zone
186 * @param zoneId the zone to query for current status
188 * @throws MonopriceAudioException - In case of any problem
190 public void queryZone(String zoneId) throws MonopriceAudioException {
191 sendCommand(amp.getQueryPrefix() + zoneId + amp.getQuerySuffix());
195 * Monoprice 31028 and OSD Audio PAM1270 amps do not report treble, bass and balance with the main status inquiry,
196 * so we must send three extra commands to retrieve those values
198 * @param zoneId the zone to query for current treble, bass and balance status
200 * @throws MonopriceAudioException - In case of any problem
202 public void queryTrebBassBalance(String zoneId) throws MonopriceAudioException {
203 sendCommand(amp.getQueryPrefix() + zoneId + amp.getTrebleCmd());
204 sendCommand(amp.getQueryPrefix() + zoneId + amp.getBassCmd());
205 sendCommand(amp.getQueryPrefix() + zoneId + amp.getBalanceCmd());
209 * Request the MonopriceAudio amplifier to execute a raw command
211 * @param cmd the command to execute
213 * @throws MonopriceAudioException - In case of any problem
215 public void sendCommand(String cmd) throws MonopriceAudioException {
216 sendCommand(null, cmd, null);
220 * Request the MonopriceAudio amplifier to execute a command
222 * @param zoneId the zone for which the command is to be run
223 * @param cmd the command to execute
224 * @param value the integer value to consider for power, volume, bass, treble, etc. adjustment
226 * @throws MonopriceAudioException - In case of any problem
228 public void sendCommand(@Nullable String zoneId, String cmd, @Nullable Integer value)
229 throws MonopriceAudioException {
232 if (zoneId != null && value != null) {
233 // if the command passed a value, build messageStr with prefix, zoneId, command, value and suffix
234 messageStr = amp.getCmdPrefix() + zoneId + cmd + amp.getFormattedValue(value) + amp.getCmdSuffix();
236 // otherwise send the raw cmd from the query() methods
237 messageStr = cmd + amp.getCmdSuffix();
239 logger.debug("Send command {}", messageStr);
241 OutputStream dataOut = this.dataOut;
242 if (dataOut == null) {
243 throw new MonopriceAudioException("Send command \"" + messageStr + "\" failed: output stream is null");
246 dataOut.write(messageStr.getBytes(StandardCharsets.US_ASCII));
248 } catch (IOException e) {
249 throw new MonopriceAudioException("Send command \"" + messageStr + "\" failed: " + e.getMessage(), e);
254 * Add a listener to the list of listeners to be notified with events
256 * @param listener the listener
258 public void addEventListener(MonopriceAudioMessageEventListener listener) {
259 listeners.add(listener);
263 * Remove a listener from the list of listeners to be notified with events
265 * @param listener the listener
267 public void removeEventListener(MonopriceAudioMessageEventListener listener) {
268 listeners.remove(listener);
272 * Analyze an incoming message and dispatch corresponding (key, value) to the event listeners
274 * @param incomingMessage the received message
276 public void handleIncomingMessage(byte[] incomingMessage) {
277 if (pingResponseOnly) {
278 dispatchKeyValue(KEY_PING, EMPTY);
282 String message = new String(incomingMessage, StandardCharsets.US_ASCII).trim();
284 if (EMPTY.equals(message)) {
288 if (message.startsWith(amp.getRespPrefix())) {
289 logger.debug("handleIncomingMessage: {}", message);
290 dispatchKeyValue(KEY_ZONE_UPDATE, message);
292 logger.debug("no match on message: {}", message);
297 * Dispatch an event (key, value) to the event listeners
300 * @param value the value
302 private void dispatchKeyValue(String key, String value) {
303 MonopriceAudioMessageEvent event = new MonopriceAudioMessageEvent(this, key, value);
304 listeners.forEach(l -> l.onNewMessageEvent(event));