2 * Copyright (c) 2010-2021 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.yamahareceiver.internal.protocol.xml;
15 import static org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Inputs.INPUT_SPOTIFY;
16 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLConstants.Commands.PLAYBACK_STATUS_CMD;
17 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLProtocolService.getResponse;
18 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLUtils.*;
20 import java.io.IOException;
22 import org.apache.commons.lang.StringUtils;
23 import org.openhab.binding.yamahareceiver.internal.config.YamahaBridgeConfig;
24 import org.openhab.binding.yamahareceiver.internal.protocol.AbstractConnection;
25 import org.openhab.binding.yamahareceiver.internal.protocol.InputWithPlayControl;
26 import org.openhab.binding.yamahareceiver.internal.protocol.ReceivedMessageParseException;
27 import org.openhab.binding.yamahareceiver.internal.state.DeviceInformationState;
28 import org.openhab.binding.yamahareceiver.internal.state.PlayInfoState;
29 import org.openhab.binding.yamahareceiver.internal.state.PlayInfoStateListener;
30 import org.openhab.binding.yamahareceiver.internal.state.PresetInfoState;
31 import org.slf4j.LoggerFactory;
32 import org.w3c.dom.Node;
35 * This class implements the Yamaha Receiver protocol related to navigation functionally. USB, NET_RADIO, IPOD and
36 * other inputs are using the same way of playback control.
38 * The XML nodes <Play_Info> and <Play_Control> are used.
42 * InputWithPlayControl menu = new InputWithPlayControl("NET_RADIO", comObject);
43 * menu.goToPath(menuDir);
44 * menu.selectItem(stationName);
46 * No state will be saved in here, but in {@link PlayInfoState} and
47 * {@link PresetInfoState} instead.
49 * @author David Graeff
50 * @author Tomasz Maruszak - Spotify support, refactoring
52 public class InputWithPlayControlXML extends AbstractInputControlXML implements InputWithPlayControl {
54 private final PlayInfoStateListener observer;
55 private final YamahaBridgeConfig bridgeConfig;
57 protected CommandTemplate playCmd = new CommandTemplate("<Play_Control><Playback>%s</Playback></Play_Control>",
58 "Play_Info/Playback_Info");
59 protected CommandTemplate skipCmd = new CommandTemplate("<Play_Control><Playback>%s</Playback></Play_Control>");
60 protected String skipForwardValue = "Skip Fwd";
61 protected String skipBackwardValue = "Skip Rev";
64 * Create a InputWithPlayControl object for altering menu positions and requesting current menu information as well
65 * as controlling the playback and choosing a preset item.
67 * @param inputID The input ID like USB or NET_RADIO.
68 * @param com The Yamaha communication object to send http requests.
70 public InputWithPlayControlXML(String inputID, AbstractConnection com, PlayInfoStateListener observer,
71 YamahaBridgeConfig bridgeConfig, DeviceInformationState deviceInformationState) {
72 super(LoggerFactory.getLogger(InputWithPlayControlXML.class), inputID, com, deviceInformationState);
74 this.observer = observer;
75 this.bridgeConfig = bridgeConfig;
77 this.applyModelVariations();
81 * Apply command changes to ensure compatibility with all supported models
83 protected void applyModelVariations() {
84 if (inputFeatureDescriptor != null) {
86 if (inputFeatureDescriptor.hasCommandEnding("Play_Control,Play")) {
87 playCmd = new CommandTemplate("<Play_Control><Play>%s</Play></Play_Control>", "Play_Info/Status");
88 logger.debug("Input {} - adjusting command to: {}", inputElement, playCmd);
91 if (inputFeatureDescriptor.hasCommandEnding("Play_Control,Skip")) {
92 // For RX-V3900 the command value is also different
93 skipForwardValue = "Fwd";
94 skipBackwardValue = "Rev";
96 skipCmd = new CommandTemplate("<Play_Control><Skip>%s</Skip></Play_Control>");
97 logger.debug("Input {} - adjusting command to: {}", inputElement, skipCmd);
103 * Start the playback of the content which is usually selected by the means of the Navigation control class or
104 * which has been stopped by stop().
109 public void play() throws IOException, ReceivedMessageParseException {
110 sendCommand(playCmd.apply("Play"));
114 * Stop the currently playing content. Use start() to start again.
119 public void stop() throws IOException, ReceivedMessageParseException {
120 sendCommand(playCmd.apply("Stop"));
124 * Pause the currently playing content. This is not available for streaming content like on NET_RADIO.
129 public void pause() throws IOException, ReceivedMessageParseException {
130 sendCommand(playCmd.apply("Pause"));
134 * Skip forward. This is not available for streaming content like on NET_RADIO.
139 public void skipFF() throws IOException, ReceivedMessageParseException {
140 if (INPUT_SPOTIFY.equals(inputID)) {
141 logger.warn("Command skip forward is not supported for input {}", inputID);
144 sendCommand(skipCmd.apply(">>|"));
148 * Skip reverse. This is not available for streaming content like on NET_RADIO.
153 public void skipREV() throws IOException, ReceivedMessageParseException {
154 if (INPUT_SPOTIFY.equals(inputID)) {
155 logger.warn("Command skip reverse is not supported for input {}", inputID);
158 sendCommand(skipCmd.apply("|<<"));
162 * Next track. This is not available for streaming content like on NET_RADIO.
167 public void nextTrack() throws IOException, ReceivedMessageParseException {
168 sendCommand(skipCmd.apply(skipForwardValue));
172 * Previous track. This is not available for streaming content like on NET_RADIO.
177 public void previousTrack() throws IOException, ReceivedMessageParseException {
178 sendCommand(skipCmd.apply(skipBackwardValue));
182 * Sends a playback command to the AVR. After command is invoked, the state is also being refreshed.
184 * @param command - the protocol level command name
185 * @throws IOException
186 * @throws ReceivedMessageParseException
188 private void sendCommand(String command) throws IOException, ReceivedMessageParseException {
189 comReference.get().send(wrInput(command));
194 * Updates the playback information
199 public void update() throws IOException, ReceivedMessageParseException {
200 if (observer == null) {
204 // <YAMAHA_AV rsp="GET" RC="0">
207 // <Feature_Availability>Ready</Feature_Availability>
208 // <Playback_Info>Play</Playback_Info>
210 // <Artist>Way Out West</Artist>
211 // <Album>Tuesday Maybe</Album>
212 // <Track>Tuesday Maybe</Track>
215 // <URL>/YamahaRemoteControl/AlbumART/AlbumART3929.jpg</URL>
217 // <Format>JPEG</Format>
220 // <URL_S>/YamahaRemoteControl/Logos/logo005.png</URL_S>
228 AbstractConnection con = comReference.get();
229 Node node = getResponse(con, wrInput(PLAYBACK_STATUS_CMD), inputElement);
231 PlayInfoState msg = new PlayInfoState();
233 msg.playbackMode = getNodeContentOrDefault(node, playCmd.getPath(), msg.playbackMode);
235 // elements for these are named differently per model and per input, so we try to match any known element
236 msg.station = getAnyNodeContentOrDefault(node, msg.station, "Play_Info/Meta_Info/Radio_Text_A",
237 "Play_Info/Meta_Info/Station", "Play_Info/RDS/Program_Service");
238 msg.artist = getAnyNodeContentOrDefault(node, msg.artist, "Play_Info/Meta_Info/Artist",
239 "Play_Info/Title/Artist", "Play_Info/RDS/Radio_Text_A");
240 msg.album = getAnyNodeContentOrDefault(node, msg.album, "Play_Info/Meta_Info/Album", "Play_Info/Title/Album",
241 "Play_Info/RDS/Program_Type");
242 msg.song = getAnyNodeContentOrDefault(node, msg.song, "Play_Info/Meta_Info/Track", "Play_Info/Meta_Info/Song",
243 "Play_Info/Title/Song", "Play_Info/RDS/Radio_Text_B");
245 // Spotify and NET RADIO input supports song cover image (at least on RX-S601D)
246 String songImageUrl = getNodeContentOrEmpty(node, "Play_Info/Album_ART/URL");
247 msg.songImageUrl = StringUtils.isNotEmpty(songImageUrl)
248 ? String.format("http://%s%s", con.getHost(), songImageUrl)
249 : bridgeConfig.getAlbumUrl();
251 logger.trace("Playback: {}, Station: {}, Artist: {}, Album: {}, Song: {}, SongImageUrl: {}", msg.playbackMode,
252 msg.station, msg.artist, msg.album, msg.song, msg.songImageUrl);
254 observer.playInfoUpdated(msg);