]> git.basschouten.com Git - openhab-addons.git/blob
37be5ec3073ce8260347aae1f2cbaf3e58707ea8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.denonmarantz.internal.connector.http;
14
15 import java.beans.Introspector;
16 import java.io.ByteArrayInputStream;
17 import java.io.IOException;
18 import java.io.StringWriter;
19 import java.net.URLEncoder;
20 import java.nio.charset.Charset;
21 import java.nio.charset.StandardCharsets;
22 import java.util.concurrent.ScheduledExecutorService;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
25
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.JAXBException;
28 import javax.xml.bind.Marshaller;
29 import javax.xml.bind.UnmarshalException;
30 import javax.xml.stream.XMLInputFactory;
31 import javax.xml.stream.XMLStreamException;
32 import javax.xml.stream.XMLStreamReader;
33 import javax.xml.stream.util.StreamReaderDelegate;
34
35 import org.eclipse.jdt.annotation.NonNullByDefault;
36 import org.eclipse.jdt.annotation.Nullable;
37 import org.eclipse.jetty.client.HttpClient;
38 import org.eclipse.jetty.client.api.Response;
39 import org.eclipse.jetty.client.api.Result;
40 import org.openhab.binding.denonmarantz.internal.DenonMarantzState;
41 import org.openhab.binding.denonmarantz.internal.config.DenonMarantzConfiguration;
42 import org.openhab.binding.denonmarantz.internal.connector.DenonMarantzConnector;
43 import org.openhab.binding.denonmarantz.internal.xml.dto.Deviceinfo;
44 import org.openhab.binding.denonmarantz.internal.xml.dto.Main;
45 import org.openhab.binding.denonmarantz.internal.xml.dto.ZoneStatus;
46 import org.openhab.binding.denonmarantz.internal.xml.dto.ZoneStatusLite;
47 import org.openhab.binding.denonmarantz.internal.xml.dto.commands.AppCommandRequest;
48 import org.openhab.binding.denonmarantz.internal.xml.dto.commands.AppCommandResponse;
49 import org.openhab.binding.denonmarantz.internal.xml.dto.commands.CommandRx;
50 import org.openhab.binding.denonmarantz.internal.xml.dto.commands.CommandTx;
51 import org.openhab.core.io.net.http.HttpUtil;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 /**
56  * This class makes the connection to the receiver and manages it.
57  * It is also responsible for sending commands to the receiver.
58  * *
59  *
60  * @author Jeroen Idserda - Initial Contribution (1.x Binding)
61  * @author Jan-Willem Veldhuis - Refactored for 2.x
62  */
63 @NonNullByDefault
64 public class DenonMarantzHttpConnector extends DenonMarantzConnector {
65
66     private Logger logger = LoggerFactory.getLogger(DenonMarantzHttpConnector.class);
67
68     private static final int REQUEST_TIMEOUT_MS = 5000; // 5 seconds
69
70     // Main URL for the receiver
71     private static final String URL_MAIN = "formMainZone_MainZoneXml.xml";
72
73     // Main Zone Status URL
74     private static final String URL_ZONE_MAIN = "formMainZone_MainZoneXmlStatus.xml";
75
76     // Secondary zone lite status URL (contains less info)
77     private static final String URL_ZONE_SECONDARY_LITE = "formZone%d_Zone%dXmlStatusLite.xml";
78
79     // Device info URL
80     private static final String URL_DEVICE_INFO = "Deviceinfo.xml";
81
82     // URL to send app commands to
83     private static final String URL_APP_COMMAND = "AppCommand.xml";
84
85     private static final String CONTENT_TYPE_XML = "application/xml";
86
87     private final String cmdUrl;
88
89     private final String statusUrl;
90
91     private final HttpClient httpClient;
92
93     private @Nullable ScheduledFuture<?> pollingJob;
94
95     public DenonMarantzHttpConnector(DenonMarantzConfiguration config, DenonMarantzState state,
96             ScheduledExecutorService scheduler, HttpClient httpClient) {
97         super(config, scheduler, state);
98         this.cmdUrl = String.format("http://%s:%d/goform/formiPhoneAppDirect.xml?", config.getHost(),
99                 config.getHttpPort());
100         this.statusUrl = String.format("http://%s:%d/goform/", config.getHost(), config.getHttpPort());
101         this.httpClient = httpClient;
102     }
103
104     public DenonMarantzState getState() {
105         return state;
106     }
107
108     /**
109      * Set up the connection to the receiver by starting to poll the HTTP API.
110      */
111     @Override
112     public void connect() {
113         if (!isPolling()) {
114             logger.debug("HTTP polling started.");
115             try {
116                 setConfigProperties();
117             } catch (IOException e) {
118                 logger.debug("IO error while retrieving document:", e);
119                 state.connectionError("IO error while connecting to AVR: " + e.getMessage());
120                 return;
121             }
122
123             pollingJob = scheduler.scheduleWithFixedDelay(() -> {
124                 try {
125                     refreshHttpProperties();
126                 } catch (IOException e) {
127                     logger.debug("IO error while retrieving document", e);
128                     state.connectionError("IO error while connecting to AVR: " + e.getMessage());
129                     stopPolling();
130                 } catch (RuntimeException e) {
131                     /**
132                      * We need to catch this RuntimeException, as otherwise the polling stops.
133                      * Log as error as it could be a user configuration error.
134                      */
135                     StringBuilder sb = new StringBuilder();
136                     for (StackTraceElement s : e.getStackTrace()) {
137                         sb.append(s.toString()).append("\n");
138                     }
139                     logger.error("Error while polling Http: \"{}\". Stacktrace: \n{}", e.getMessage(), sb.toString());
140                 }
141             }, 0, config.httpPollingInterval, TimeUnit.SECONDS);
142         }
143     }
144
145     private boolean isPolling() {
146         ScheduledFuture<?> pollingJob = this.pollingJob;
147         return pollingJob != null && !pollingJob.isCancelled();
148     }
149
150     private void stopPolling() {
151         ScheduledFuture<?> pollingJob = this.pollingJob;
152         if (pollingJob != null) {
153             pollingJob.cancel(true);
154             logger.debug("HTTP polling stopped.");
155         }
156     }
157
158     /**
159      * Shutdown the http client
160      */
161     @Override
162     public void dispose() {
163         logger.debug("disposing connector");
164
165         stopPolling();
166     }
167
168     @Override
169     protected void internalSendCommand(String command) {
170         logger.debug("Sending command '{}'", command);
171         if (command.isBlank()) {
172             logger.warn("Trying to send empty command");
173             return;
174         }
175
176         String url = cmdUrl + URLEncoder.encode(command, Charset.defaultCharset());
177         logger.trace("Calling url {}", url);
178
179         httpClient.newRequest(url).timeout(5, TimeUnit.SECONDS).send(new Response.CompleteListener() {
180             @Override
181             public void onComplete(@Nullable Result result) {
182                 if (result != null && result.getResponse().getStatus() != 200) {
183                     logger.warn("Error {} while sending command", result.getResponse().getReason());
184                 }
185             }
186         });
187     }
188
189     private void updateMain() throws IOException {
190         String url = statusUrl + URL_MAIN;
191         logger.trace("Refreshing URL: {}", url);
192
193         Main statusMain = getDocument(url, Main.class);
194         if (statusMain != null) {
195             state.setPower(statusMain.getPower().getValue());
196         }
197     }
198
199     private void updateMainZone() throws IOException {
200         String url = statusUrl + URL_ZONE_MAIN;
201         logger.trace("Refreshing URL: {}", url);
202
203         ZoneStatus mainZone = getDocument(url, ZoneStatus.class);
204         if (mainZone != null) {
205             state.setInput(mainZone.getInputFuncSelect().getValue());
206             state.setMainVolume(mainZone.getMasterVolume().getValue());
207             state.setMainZonePower(mainZone.getPower().getValue());
208             state.setMute(mainZone.getMute().getValue());
209
210             if (config.inputOptions == null) {
211                 config.inputOptions = mainZone.getInputFuncList();
212             }
213
214             if (mainZone.getSurrMode() == null) {
215                 logger.debug("Unable to get the SURROUND_MODE. MainZone update may not be correct.");
216             } else {
217                 state.setSurroundProgram(mainZone.getSurrMode().getValue());
218             }
219         }
220     }
221
222     private void updateSecondaryZones() throws IOException {
223         for (int i = 2; i <= config.getZoneCount(); i++) {
224             String url = String.format("%s" + URL_ZONE_SECONDARY_LITE, statusUrl, i, i);
225             logger.trace("Refreshing URL: {}", url);
226             ZoneStatusLite zoneSecondary = getDocument(url, ZoneStatusLite.class);
227             if (zoneSecondary != null) {
228                 switch (i) {
229                     // maximum 2 secondary zones are supported
230                     case 2:
231                         state.setZone2Power(zoneSecondary.getPower().getValue());
232                         state.setZone2Volume(zoneSecondary.getMasterVolume().getValue());
233                         state.setZone2Mute(zoneSecondary.getMute().getValue());
234                         state.setZone2Input(zoneSecondary.getInputFuncSelect().getValue());
235                         break;
236                     case 3:
237                         state.setZone3Power(zoneSecondary.getPower().getValue());
238                         state.setZone3Volume(zoneSecondary.getMasterVolume().getValue());
239                         state.setZone3Mute(zoneSecondary.getMute().getValue());
240                         state.setZone3Input(zoneSecondary.getInputFuncSelect().getValue());
241                         break;
242                     case 4:
243                         state.setZone4Power(zoneSecondary.getPower().getValue());
244                         state.setZone4Volume(zoneSecondary.getMasterVolume().getValue());
245                         state.setZone4Mute(zoneSecondary.getMute().getValue());
246                         state.setZone4Input(zoneSecondary.getInputFuncSelect().getValue());
247                         break;
248                 }
249             }
250         }
251     }
252
253     private void updateDisplayInfo() throws IOException {
254         String url = statusUrl + URL_APP_COMMAND;
255         logger.trace("Refreshing URL: {}", url);
256
257         AppCommandRequest request = AppCommandRequest.of(CommandTx.CMD_NET_STATUS);
258         AppCommandResponse response = postDocument(url, AppCommandResponse.class, request);
259
260         if (response == null) {
261             return;
262         }
263         CommandRx titleInfo = response.getCommands().get(0);
264         String artist = titleInfo.getText("artist");
265         if (artist != null) {
266             state.setNowPlayingArtist(artist);
267         }
268         String album = titleInfo.getText("album");
269         if (album != null) {
270             state.setNowPlayingAlbum(album);
271         }
272         String track = titleInfo.getText("track");
273         if (track != null) {
274             state.setNowPlayingTrack(track);
275         }
276     }
277
278     private boolean setConfigProperties() throws IOException {
279         String url = statusUrl + URL_DEVICE_INFO;
280         logger.debug("Refreshing URL: {}", url);
281
282         Deviceinfo deviceinfo = getDocument(url, Deviceinfo.class);
283         if (deviceinfo != null) {
284             config.setZoneCount(deviceinfo.getDeviceZones());
285         }
286
287         /**
288          * The maximum volume is received from the telnet connection in the
289          * form of the MVMAX property. It is not always received reliable however,
290          * so we're using a default for now.
291          */
292         config.setMainVolumeMax(DenonMarantzConfiguration.MAX_VOLUME);
293
294         // if deviceinfo is null, something went wrong (and is logged in getDocument catch blocks)
295         return (deviceinfo != null);
296     }
297
298     private void refreshHttpProperties() throws IOException {
299         logger.trace("Refreshing Denon status");
300
301         updateMain();
302         updateMainZone();
303         updateSecondaryZones();
304         updateDisplayInfo();
305     }
306
307     @Nullable
308     private <T> T getDocument(String uri, Class<T> response) throws IOException {
309         try {
310             String result = HttpUtil.executeUrl("GET", uri, REQUEST_TIMEOUT_MS);
311             logger.trace("result of getDocument for uri '{}':\r\n{}", uri, result);
312
313             if (result != null && !result.isBlank()) {
314                 JAXBContext jc = JAXBContext.newInstance(response);
315                 XMLInputFactory xif = XMLInputFactory.newInstance();
316                 xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
317                 xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
318                 XMLStreamReader xsr = xif
319                         .createXMLStreamReader(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
320                 xsr = new PropertyRenamerDelegate(xsr);
321
322                 @SuppressWarnings("unchecked")
323                 T obj = (T) jc.createUnmarshaller().unmarshal(xsr);
324
325                 return obj;
326             }
327         } catch (UnmarshalException e) {
328             logger.debug("Failed to unmarshal xml document: {}", e.getMessage());
329         } catch (JAXBException e) {
330             logger.debug("Unexpected error occurred during unmarshalling of document: {}", e.getMessage());
331         } catch (XMLStreamException e) {
332             logger.debug("Communication error: {}", e.getMessage());
333         }
334
335         return null;
336     }
337
338     @Nullable
339     private <T, S> T postDocument(String uri, Class<T> response, S request) throws IOException {
340         try {
341             JAXBContext jaxbContext = JAXBContext.newInstance(request.getClass());
342             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
343             StringWriter sw = new StringWriter();
344             jaxbMarshaller.marshal(request, sw);
345
346             ByteArrayInputStream inputStream = new ByteArrayInputStream(sw.toString().getBytes(StandardCharsets.UTF_8));
347             String result = HttpUtil.executeUrl("POST", uri, inputStream, CONTENT_TYPE_XML, REQUEST_TIMEOUT_MS);
348
349             if (result != null && !result.isBlank()) {
350                 JAXBContext jcResponse = JAXBContext.newInstance(response);
351
352                 @SuppressWarnings("unchecked")
353                 T obj = (T) jcResponse.createUnmarshaller()
354                         .unmarshal(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
355
356                 return obj;
357             }
358         } catch (JAXBException e) {
359             logger.debug("Encoding error in post", e);
360         }
361
362         return null;
363     }
364
365     private static class PropertyRenamerDelegate extends StreamReaderDelegate {
366
367         public PropertyRenamerDelegate(XMLStreamReader xsr) {
368             super(xsr);
369         }
370
371         @Override
372         public String getAttributeLocalName(int index) {
373             return Introspector.decapitalize(super.getAttributeLocalName(index)).intern();
374         }
375
376         @Override
377         public String getLocalName() {
378             return Introspector.decapitalize(super.getLocalName()).intern();
379         }
380     }
381 }