2 * Copyright (c) 2010-2022 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.nuvo.internal.communication;
15 import static org.openhab.binding.nuvo.internal.NuvoBindingConstants.*;
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;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.nuvo.internal.NuvoException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Abstract class for communicating with the Nuvo device
35 * @author Laurent Garnier - Initial contribution
36 * @author Michael Lobstein - Adapted for the Nuvo binding
39 public abstract class NuvoConnector {
40 private static final String COMMAND_OK = "#OK";
41 private static final String BEGIN_CMD = "*";
42 private static final String END_CMD = "\r";
43 private static final String QUERY = "?";
44 private static final String VER_STR = "#VER\"NV-";
45 private static final String ALL_OFF = "#ALLOFF";
46 private static final String MUTE = "#MUTE";
47 private static final String PAGE = "#PAGE";
48 private static final String PING = "#PING";
50 private static final byte[] WAKE_STR = "\r".getBytes(StandardCharsets.US_ASCII);
52 private static final Pattern SRC_PATTERN = Pattern.compile("^#S(\\d{1})(.*)$");
53 private static final Pattern ZONE_PATTERN = Pattern.compile("^#Z(\\d{1,2}),(.*)$");
54 private static final Pattern ZONE_BUTTON_PATTERN = Pattern.compile("^#Z(\\d{1,2})S(\\d{1})(.*)$");
55 private static final Pattern ZONE_CFG_PATTERN = Pattern.compile("^#ZCFG(\\d{1,2}),(.*)$");
57 private final Logger logger = LoggerFactory.getLogger(NuvoConnector.class);
59 protected static final String COMMAND_ERROR = "#?";
61 /** The output stream */
62 protected @Nullable OutputStream dataOut;
64 /** The input stream */
65 protected @Nullable InputStream dataIn;
67 /** true if the connection is established, false if not */
68 private boolean connected;
70 private @Nullable Thread readerThread;
72 private List<NuvoMessageEventListener> listeners = new ArrayList<>();
74 private boolean isEssentia = true;
77 * Get whether the connection is established or not
79 * @return true if the connection is established
81 public boolean isConnected() {
86 * Set whether the connection is established or not
88 * @param connected true if the connection is established
90 protected void setConnected(boolean connected) {
91 this.connected = connected;
95 * Tell the connector if the device is an Essentia G or not
97 * @param true if the device is an Essentia G
99 public void setEssentia(boolean isEssentia) {
100 this.isEssentia = isEssentia;
104 * Set the thread that handles the feedback messages
106 * @param readerThread the thread
108 protected void setReaderThread(Thread readerThread) {
109 this.readerThread = readerThread;
113 * Open the connection with the Nuvo device
115 * @throws NuvoException - In case of any problem
117 public abstract void open() throws NuvoException;
120 * Close the connection with the Nuvo device
122 public abstract void close();
125 * Stop the thread that handles the feedback messages and close the opened input and output streams
127 protected void cleanup() {
128 Thread readerThread = this.readerThread;
129 OutputStream dataOut = this.dataOut;
130 if (dataOut != null) {
133 } catch (IOException e) {
134 logger.debug("Error closing dataOut: {}", e.getMessage());
138 InputStream dataIn = this.dataIn;
139 if (dataIn != null) {
142 } catch (IOException e) {
143 logger.debug("Error closing dataIn: {}", e.getMessage());
147 if (readerThread != null) {
148 readerThread.interrupt();
149 this.readerThread = null;
151 readerThread.join(3000);
152 } catch (InterruptedException e) {
153 logger.warn("Error joining readerThread: {}", e.getMessage());
159 * Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes
160 * actually read is returned as an integer.
162 * @param dataBuffer the buffer into which the data is read.
164 * @return the total number of bytes read into the buffer, or -1 if there is no more data because the end of the
165 * stream has been reached.
167 * @throws NuvoException - If the input stream is null, if the first byte cannot be read for any reason
168 * other than the end of the file, if the input stream has been closed, or if some other I/O error
171 protected int readInput(byte[] dataBuffer) throws NuvoException {
172 InputStream dataIn = this.dataIn;
173 if (dataIn == null) {
174 throw new NuvoException("readInput failed: input stream is null");
177 return dataIn.read(dataBuffer);
178 } catch (IOException e) {
179 throw new NuvoException("readInput failed: " + e.getMessage(), e);
184 * Request the Nuvo controller to execute an inquiry command
186 * @param zone the zone for which the command is to be run
187 * @param cmd the command to execute
189 * @throws NuvoException - In case of any problem
191 public void sendQuery(NuvoEnum zone, NuvoCommand cmd) throws NuvoException {
192 sendCommand(zone.getId() + cmd.getValue() + QUERY);
196 * Request the Nuvo controller to execute a command for a zone that takes no arguments (ie power on, power off,
199 * @param zone the zone for which the command is to be run
200 * @param cmd the command to execute
202 * @throws NuvoException - In case of any problem
204 public void sendCommand(NuvoEnum zone, NuvoCommand cmd) throws NuvoException {
205 sendCommand(zone.getId() + cmd.getValue());
209 * Request the Nuvo controller to execute a command for a zone and pass in a value
211 * @param zone the zone for which the command is to be run
212 * @param cmd the command to execute
213 * @param value the string value to consider for volume, source, etc.
215 * @throws NuvoException - In case of any problem
217 public void sendCommand(NuvoEnum zone, NuvoCommand cmd, @Nullable String value) throws NuvoException {
218 sendCommand(zone.getId() + cmd.getValue() + value);
222 * Request the Nuvo controller to execute a configuration command for a zone and pass in a value
224 * @param zone the zone for which the command is to be run
225 * @param cmd the command to execute
226 * @param value the string value to consider for bass, treble, balance, etc.
228 * @throws NuvoException - In case of any problem
230 public void sendCfgCommand(NuvoEnum zone, NuvoCommand cmd, @Nullable String value) throws NuvoException {
231 sendCommand(zone.getConfigId() + cmd.getValue() + value);
235 * Request the Nuvo controller to execute a system command the does not specify a zone or value
237 * @param cmd the command to execute
239 * @throws NuvoException - In case of any problem
241 public void sendCommand(NuvoCommand cmd) throws NuvoException {
242 sendCommand(cmd.getValue());
246 * Request the Nuvo controller to execute a raw command string
248 * @param command the command string to run
250 * @throws NuvoException - In case of any problem
252 public void sendCommand(@Nullable String command) throws NuvoException {
253 String messageStr = BEGIN_CMD + command + END_CMD;
255 logger.debug("sending command: {}", messageStr);
257 OutputStream dataOut = this.dataOut;
258 if (dataOut == null) {
259 throw new NuvoException("Send command \"" + messageStr + "\" failed: output stream is null");
262 // Essentia G needs time to wake up when in standby mode
263 // I don't want to track that in the binding, so just do this always
264 if (this.isEssentia) {
265 dataOut.write(WAKE_STR);
268 dataOut.write(messageStr.getBytes(StandardCharsets.US_ASCII));
270 } catch (IOException e) {
271 throw new NuvoException("Send command \"" + command + "\" failed: " + e.getMessage(), e);
276 * Add a listener to the list of listeners to be notified with events
278 * @param listener the listener
280 public void addEventListener(NuvoMessageEventListener listener) {
281 listeners.add(listener);
285 * Remove a listener from the list of listeners to be notified with events
287 * @param listener the listener
289 public void removeEventListener(NuvoMessageEventListener listener) {
290 listeners.remove(listener);
294 * Analyze an incoming message and dispatch corresponding (type, key, value) to the event listeners
296 * @param incomingMessage the received message
298 public void handleIncomingMessage(byte[] incomingMessage) {
299 String message = new String(incomingMessage, StandardCharsets.US_ASCII).trim();
301 logger.debug("handleIncomingMessage: {}", message);
303 if (COMMAND_ERROR.equals(message) || COMMAND_OK.equals(message)) {
308 if (message.contains(PING)) {
309 dispatchKeyValue(TYPE_PING, BLANK, BLANK);
313 if (message.contains(VER_STR)) {
314 // example: #VER"NV-E6G FWv2.66 HWv0"
315 // split on " and return the version number
316 dispatchKeyValue(TYPE_VERSION, "", message.split("\"")[1]);
320 if (message.equals(ALL_OFF)) {
321 dispatchKeyValue(TYPE_ALLOFF, BLANK, BLANK);
325 if (message.contains(MUTE)) {
326 dispatchKeyValue(TYPE_ALLMUTE, BLANK, message.substring(message.length() - 1));
330 if (message.contains(PAGE)) {
331 dispatchKeyValue(TYPE_PAGE, BLANK, message.substring(message.length() - 1));
335 // Amp controller send a source update ie: #S2DISPINFO,DUR3380,POS3090,STATUS2
336 // or #S2DISPLINE1,"1 of 17"
337 Matcher matcher = SRC_PATTERN.matcher(message);
338 if (matcher.find()) {
339 // pull out the source id and the remainder of the message
340 dispatchKeyValue(TYPE_SOURCE_UPDATE, matcher.group(1), matcher.group(2));
344 // Amp controller send a zone update ie: #Z11,ON,SRC3,VOL63,DND0,LOCK0
345 matcher = ZONE_PATTERN.matcher(message);
346 if (matcher.find()) {
347 // pull out the zone id and the remainder of the message
348 dispatchKeyValue(TYPE_ZONE_UPDATE, matcher.group(1), matcher.group(2));
352 // Amp controller send a zone button press event ie: #Z11S3PLAYPAUSE
353 matcher = ZONE_BUTTON_PATTERN.matcher(message);
354 if (matcher.find()) {
355 // pull out the source id and the remainder of the message, ignore the zone id
356 dispatchKeyValue(TYPE_ZONE_BUTTON, matcher.group(2), matcher.group(3));
360 // Amp controller send a zone configuration response ie: #ZCFG1,BASS1,TREB-2,BALR2,LOUDCMP1
361 matcher = ZONE_CFG_PATTERN.matcher(message);
362 if (matcher.find()) {
363 // pull out the zone id and the remainder of the message
364 dispatchKeyValue(TYPE_ZONE_CONFIG, matcher.group(1), matcher.group(2));
368 logger.debug("unhandled message: {}", message);
372 * Dispatch an event (type, key, value) to the event listeners
374 * @param type the type
376 * @param value the value
378 private void dispatchKeyValue(String type, String key, String value) {
379 NuvoMessageEvent event = new NuvoMessageEvent(this, type, key, value);
380 listeners.forEach(l -> l.onNewMessageEvent(event));