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.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.openhab.binding.yamahareceiver.internal.config.YamahaBridgeConfig;
23 import org.openhab.binding.yamahareceiver.internal.protocol.AbstractConnection;
24 import org.openhab.binding.yamahareceiver.internal.protocol.InputWithPlayControl;
25 import org.openhab.binding.yamahareceiver.internal.protocol.ReceivedMessageParseException;
26 import org.openhab.binding.yamahareceiver.internal.state.DeviceInformationState;
27 import org.openhab.binding.yamahareceiver.internal.state.PlayInfoState;
28 import org.openhab.binding.yamahareceiver.internal.state.PlayInfoStateListener;
29 import org.openhab.binding.yamahareceiver.internal.state.PresetInfoState;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Node;
34 * This class implements the Yamaha Receiver protocol related to navigation functionally. USB, NET_RADIO, IPOD and
35 * other inputs are using the same way of playback control.
37 * The XML nodes <Play_Info> and <Play_Control> are used.
41 * InputWithPlayControl menu = new InputWithPlayControl("NET_RADIO", comObject);
42 * menu.goToPath(menuDir);
43 * menu.selectItem(stationName);
45 * No state will be saved in here, but in {@link PlayInfoState} and
46 * {@link PresetInfoState} instead.
48 * @author David Graeff
49 * @author Tomasz Maruszak - Spotify support, refactoring
51 public class InputWithPlayControlXML extends AbstractInputControlXML implements InputWithPlayControl {
53 private final PlayInfoStateListener observer;
54 private final YamahaBridgeConfig bridgeConfig;
56 protected CommandTemplate playCmd = new CommandTemplate("<Play_Control><Playback>%s</Playback></Play_Control>",
57 "Play_Info/Playback_Info");
58 protected CommandTemplate skipCmd = new CommandTemplate("<Play_Control><Playback>%s</Playback></Play_Control>");
59 protected String skipForwardValue = "Skip Fwd";
60 protected String skipBackwardValue = "Skip Rev";
63 * Create an InputWithPlayControl object for altering menu positions and requesting current menu information as well
64 * as controlling the playback and choosing a preset item.
66 * @param inputID The input ID like USB or NET_RADIO.
67 * @param com The Yamaha communication object to send http requests.
69 public InputWithPlayControlXML(String inputID, AbstractConnection com, PlayInfoStateListener observer,
70 YamahaBridgeConfig bridgeConfig, DeviceInformationState deviceInformationState) {
71 super(LoggerFactory.getLogger(InputWithPlayControlXML.class), inputID, com, deviceInformationState);
73 this.observer = observer;
74 this.bridgeConfig = bridgeConfig;
76 this.applyModelVariations();
80 * Apply command changes to ensure compatibility with all supported models
82 protected void applyModelVariations() {
83 if (inputFeatureDescriptor != null) {
85 if (inputFeatureDescriptor.hasCommandEnding("Play_Control,Play")) {
86 playCmd = new CommandTemplate("<Play_Control><Play>%s</Play></Play_Control>", "Play_Info/Status");
87 logger.debug("Input {} - adjusting command to: {}", inputElement, playCmd);
90 if (inputFeatureDescriptor.hasCommandEnding("Play_Control,Skip")) {
91 // For RX-V3900 the command value is also different
92 skipForwardValue = "Fwd";
93 skipBackwardValue = "Rev";
95 skipCmd = new CommandTemplate("<Play_Control><Skip>%s</Skip></Play_Control>");
96 logger.debug("Input {} - adjusting command to: {}", inputElement, skipCmd);
102 * Start the playback of the content which is usually selected by the means of the Navigation control class or
103 * which has been stopped by stop().
108 public void play() throws IOException, ReceivedMessageParseException {
109 sendCommand(playCmd.apply("Play"));
113 * Stop the currently playing content. Use start() to start again.
118 public void stop() throws IOException, ReceivedMessageParseException {
119 sendCommand(playCmd.apply("Stop"));
123 * Pause the currently playing content. This is not available for streaming content like on NET_RADIO.
128 public void pause() throws IOException, ReceivedMessageParseException {
129 sendCommand(playCmd.apply("Pause"));
133 * Skip forward. This is not available for streaming content like on NET_RADIO.
138 public void skipFF() throws IOException, ReceivedMessageParseException {
139 if (INPUT_SPOTIFY.equals(inputID)) {
140 logger.warn("Command skip forward is not supported for input {}", inputID);
143 sendCommand(skipCmd.apply(">>|"));
147 * Skip reverse. This is not available for streaming content like on NET_RADIO.
152 public void skipREV() throws IOException, ReceivedMessageParseException {
153 if (INPUT_SPOTIFY.equals(inputID)) {
154 logger.warn("Command skip reverse is not supported for input {}", inputID);
157 sendCommand(skipCmd.apply("|<<"));
161 * Next track. This is not available for streaming content like on NET_RADIO.
166 public void nextTrack() throws IOException, ReceivedMessageParseException {
167 sendCommand(skipCmd.apply(skipForwardValue));
171 * Previous track. This is not available for streaming content like on NET_RADIO.
176 public void previousTrack() throws IOException, ReceivedMessageParseException {
177 sendCommand(skipCmd.apply(skipBackwardValue));
181 * Sends a playback command to the AVR. After command is invoked, the state is also being refreshed.
183 * @param command - the protocol level command name
184 * @throws IOException
185 * @throws ReceivedMessageParseException
187 private void sendCommand(String command) throws IOException, ReceivedMessageParseException {
188 comReference.get().send(wrInput(command));
193 * Updates the playback information
198 public void update() throws IOException, ReceivedMessageParseException {
199 if (observer == null) {
203 // <YAMAHA_AV rsp="GET" RC="0">
206 // <Feature_Availability>Ready</Feature_Availability>
207 // <Playback_Info>Play</Playback_Info>
209 // <Artist>Way Out West</Artist>
210 // <Album>Tuesday Maybe</Album>
211 // <Track>Tuesday Maybe</Track>
214 // <URL>/YamahaRemoteControl/AlbumART/AlbumART3929.jpg</URL>
216 // <Format>JPEG</Format>
219 // <URL_S>/YamahaRemoteControl/Logos/logo005.png</URL_S>
227 AbstractConnection con = comReference.get();
228 Node node = getResponse(con, wrInput(PLAYBACK_STATUS_CMD), inputElement);
230 PlayInfoState msg = new PlayInfoState();
232 msg.playbackMode = getNodeContentOrDefault(node, playCmd.getPath(), msg.playbackMode);
234 // elements for these are named differently per model and per input, so we try to match any known element
235 msg.station = getAnyNodeContentOrDefault(node, msg.station, "Play_Info/Meta_Info/Radio_Text_A",
236 "Play_Info/Meta_Info/Station", "Play_Info/RDS/Program_Service");
237 msg.artist = getAnyNodeContentOrDefault(node, msg.artist, "Play_Info/Meta_Info/Artist",
238 "Play_Info/Title/Artist", "Play_Info/RDS/Radio_Text_A");
239 msg.album = getAnyNodeContentOrDefault(node, msg.album, "Play_Info/Meta_Info/Album", "Play_Info/Title/Album",
240 "Play_Info/RDS/Program_Type");
241 msg.song = getAnyNodeContentOrDefault(node, msg.song, "Play_Info/Meta_Info/Track", "Play_Info/Meta_Info/Song",
242 "Play_Info/Title/Song", "Play_Info/RDS/Radio_Text_B");
244 // Spotify and NET RADIO input supports song cover image (at least on RX-S601D)
245 String songImageUrl = getNodeContentOrEmpty(node, "Play_Info/Album_ART/URL");
246 msg.songImageUrl = !songImageUrl.isEmpty() ? String.format("http://%s%s", con.getHost(), songImageUrl)
247 : bridgeConfig.getAlbumUrl();
249 logger.trace("Playback: {}, Station: {}, Artist: {}, Album: {}, Song: {}, SongImageUrl: {}", msg.playbackMode,
250 msg.station, msg.artist, msg.album, msg.song, msg.songImageUrl);
252 observer.playInfoUpdated(msg);