]> git.basschouten.com Git - openhab-addons.git/blob
fd07ece33b853ec5476bbbbcb9007eee0646339c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.nuvo.internal.communication;
14
15 import static org.openhab.binding.nuvo.internal.NuvoBindingConstants.*;
16
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;
25
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;
31
32 /**
33  * Abstract class for communicating with the Nuvo device
34  *
35  * @author Laurent Garnier - Initial contribution
36  * @author Michael Lobstein - Adapted for the Nuvo binding
37  */
38 @NonNullByDefault
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";
49
50     private static final byte[] WAKE_STR = "\r".getBytes(StandardCharsets.US_ASCII);
51
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}),(.*)$");
56
57     private final Logger logger = LoggerFactory.getLogger(NuvoConnector.class);
58
59     protected static final String COMMAND_ERROR = "#?";
60
61     /** The output stream */
62     protected @Nullable OutputStream dataOut;
63
64     /** The input stream */
65     protected @Nullable InputStream dataIn;
66
67     /** true if the connection is established, false if not */
68     private boolean connected;
69
70     private @Nullable Thread readerThread;
71
72     private List<NuvoMessageEventListener> listeners = new ArrayList<>();
73
74     private boolean isEssentia = true;
75
76     /**
77      * Get whether the connection is established or not
78      *
79      * @return true if the connection is established
80      */
81     public boolean isConnected() {
82         return connected;
83     }
84
85     /**
86      * Set whether the connection is established or not
87      *
88      * @param connected true if the connection is established
89      */
90     protected void setConnected(boolean connected) {
91         this.connected = connected;
92     }
93
94     /**
95      * Tell the connector if the device is an Essentia G or not
96      *
97      * @param true if the device is an Essentia G
98      */
99     public void setEssentia(boolean isEssentia) {
100         this.isEssentia = isEssentia;
101     }
102
103     /**
104      * Set the thread that handles the feedback messages
105      *
106      * @param readerThread the thread
107      */
108     protected void setReaderThread(Thread readerThread) {
109         this.readerThread = readerThread;
110     }
111
112     /**
113      * Open the connection with the Nuvo device
114      *
115      * @throws NuvoException - In case of any problem
116      */
117     public abstract void open() throws NuvoException;
118
119     /**
120      * Close the connection with the Nuvo device
121      */
122     public abstract void close();
123
124     /**
125      * Stop the thread that handles the feedback messages and close the opened input and output streams
126      */
127     protected void cleanup() {
128         Thread readerThread = this.readerThread;
129         OutputStream dataOut = this.dataOut;
130         if (dataOut != null) {
131             try {
132                 dataOut.close();
133             } catch (IOException e) {
134                 logger.debug("Error closing dataOut: {}", e.getMessage());
135             }
136             this.dataOut = null;
137         }
138         InputStream dataIn = this.dataIn;
139         if (dataIn != null) {
140             try {
141                 dataIn.close();
142             } catch (IOException e) {
143                 logger.debug("Error closing dataIn: {}", e.getMessage());
144             }
145             this.dataIn = null;
146         }
147         if (readerThread != null) {
148             readerThread.interrupt();
149             this.readerThread = null;
150             try {
151                 readerThread.join(3000);
152             } catch (InterruptedException e) {
153                 logger.warn("Error joining readerThread: {}", e.getMessage());
154             }
155         }
156     }
157
158     /**
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.
161      *
162      * @param dataBuffer the buffer into which the data is read.
163      *
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.
166      *
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
169      *             occurs.
170      */
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");
175         }
176         try {
177             return dataIn.read(dataBuffer);
178         } catch (IOException e) {
179             throw new NuvoException("readInput failed: " + e.getMessage(), e);
180         }
181     }
182
183     /**
184      * Request the Nuvo controller to execute an inquiry command
185      *
186      * @param zone the zone for which the command is to be run
187      * @param cmd the command to execute
188      *
189      * @throws NuvoException - In case of any problem
190      */
191     public void sendQuery(NuvoEnum zone, NuvoCommand cmd) throws NuvoException {
192         sendCommand(zone.getId() + cmd.getValue() + QUERY);
193     }
194
195     /**
196      * Request the Nuvo controller to execute a command for a zone that takes no arguments (ie power on, power off,
197      * etc.)
198      *
199      * @param zone the zone for which the command is to be run
200      * @param cmd the command to execute
201      *
202      * @throws NuvoException - In case of any problem
203      */
204     public void sendCommand(NuvoEnum zone, NuvoCommand cmd) throws NuvoException {
205         sendCommand(zone.getId() + cmd.getValue());
206     }
207
208     /**
209      * Request the Nuvo controller to execute a command for a zone and pass in a value
210      *
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.
214      *
215      * @throws NuvoException - In case of any problem
216      */
217     public void sendCommand(NuvoEnum zone, NuvoCommand cmd, @Nullable String value) throws NuvoException {
218         sendCommand(zone.getId() + cmd.getValue() + value);
219     }
220
221     /**
222      * Request the Nuvo controller to execute a configuration command for a zone and pass in a value
223      *
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.
227      *
228      * @throws NuvoException - In case of any problem
229      */
230     public void sendCfgCommand(NuvoEnum zone, NuvoCommand cmd, @Nullable String value) throws NuvoException {
231         sendCommand(zone.getConfigId() + cmd.getValue() + value);
232     }
233
234     /**
235      * Request the Nuvo controller to execute a system command the does not specify a zone or value
236      *
237      * @param cmd the command to execute
238      *
239      * @throws NuvoException - In case of any problem
240      */
241     public void sendCommand(NuvoCommand cmd) throws NuvoException {
242         sendCommand(cmd.getValue());
243     }
244
245     /**
246      * Request the Nuvo controller to execute a raw command string
247      *
248      * @param command the command string to run
249      *
250      * @throws NuvoException - In case of any problem
251      */
252     public void sendCommand(@Nullable String command) throws NuvoException {
253         String messageStr = BEGIN_CMD + command + END_CMD;
254
255         logger.debug("sending command: {}", messageStr);
256
257         OutputStream dataOut = this.dataOut;
258         if (dataOut == null) {
259             throw new NuvoException("Send command \"" + messageStr + "\" failed: output stream is null");
260         }
261         try {
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);
266                 dataOut.flush();
267             }
268             dataOut.write(messageStr.getBytes(StandardCharsets.US_ASCII));
269             dataOut.flush();
270         } catch (IOException e) {
271             throw new NuvoException("Send command \"" + command + "\" failed: " + e.getMessage(), e);
272         }
273     }
274
275     /**
276      * Add a listener to the list of listeners to be notified with events
277      *
278      * @param listener the listener
279      */
280     public void addEventListener(NuvoMessageEventListener listener) {
281         listeners.add(listener);
282     }
283
284     /**
285      * Remove a listener from the list of listeners to be notified with events
286      *
287      * @param listener the listener
288      */
289     public void removeEventListener(NuvoMessageEventListener listener) {
290         listeners.remove(listener);
291     }
292
293     /**
294      * Analyze an incoming message and dispatch corresponding (type, key, value) to the event listeners
295      *
296      * @param incomingMessage the received message
297      */
298     public void handleIncomingMessage(byte[] incomingMessage) {
299         String message = new String(incomingMessage, StandardCharsets.US_ASCII).trim();
300
301         logger.debug("handleIncomingMessage: {}", message);
302
303         if (COMMAND_ERROR.equals(message) || COMMAND_OK.equals(message)) {
304             // ignore
305             return;
306         }
307
308         if (message.contains(PING)) {
309             dispatchKeyValue(TYPE_PING, BLANK, BLANK);
310             return;
311         }
312
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]);
317             return;
318         }
319
320         if (message.equals(ALL_OFF)) {
321             dispatchKeyValue(TYPE_ALLOFF, BLANK, BLANK);
322             return;
323         }
324
325         if (message.contains(MUTE)) {
326             dispatchKeyValue(TYPE_ALLMUTE, BLANK, message.substring(message.length() - 1));
327             return;
328         }
329
330         if (message.contains(PAGE)) {
331             dispatchKeyValue(TYPE_PAGE, BLANK, message.substring(message.length() - 1));
332             return;
333         }
334
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));
341             return;
342         }
343
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));
349             return;
350         }
351
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));
357             return;
358         }
359
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));
365             return;
366         }
367
368         logger.debug("unhandled message: {}", message);
369     }
370
371     /**
372      * Dispatch an event (type, key, value) to the event listeners
373      *
374      * @param type the type
375      * @param key the key
376      * @param value the value
377      */
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));
381     }
382 }