]> git.basschouten.com Git - openhab-addons.git/blob
25b1fe4410935487a107478d9991e0ea7ca2b0ed
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.yamahareceiver.internal.protocol.xml;
14
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.*;
19
20 import java.io.IOException;
21
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;
32
33 /**
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.
36  * <p>
37  * The XML nodes <Play_Info> and <Play_Control> are used.
38  * <p>
39  * Example:
40  * <p>
41  * InputWithPlayControl menu = new InputWithPlayControl("NET_RADIO", comObject);
42  * menu.goToPath(menuDir);
43  * menu.selectItem(stationName);
44  * <p>
45  * No state will be saved in here, but in {@link PlayInfoState} and
46  * {@link PresetInfoState} instead.
47  *
48  * @author David Graeff - Initial contribution
49  * @author Tomasz Maruszak - Spotify support, refactoring
50  */
51 public class InputWithPlayControlXML extends AbstractInputControlXML implements InputWithPlayControl {
52
53     private final PlayInfoStateListener observer;
54     private final YamahaBridgeConfig bridgeConfig;
55
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";
61
62     /**
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.
65      *
66      * @param inputID The input ID like USB or NET_RADIO.
67      * @param com The Yamaha communication object to send http requests.
68      */
69     public InputWithPlayControlXML(String inputID, AbstractConnection com, PlayInfoStateListener observer,
70             YamahaBridgeConfig bridgeConfig, DeviceInformationState deviceInformationState) {
71         super(LoggerFactory.getLogger(InputWithPlayControlXML.class), inputID, com, deviceInformationState);
72
73         this.observer = observer;
74         this.bridgeConfig = bridgeConfig;
75
76         this.applyModelVariations();
77     }
78
79     /**
80      * Apply command changes to ensure compatibility with all supported models
81      */
82     protected void applyModelVariations() {
83         if (inputFeatureDescriptor != null) {
84             // For RX-V3900
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);
88             }
89             // For RX-V3900
90             if (inputFeatureDescriptor.hasCommandEnding("Play_Control,Skip")) {
91                 // For RX-V3900 the command value is also different
92                 skipForwardValue = "Fwd";
93                 skipBackwardValue = "Rev";
94
95                 skipCmd = new CommandTemplate("<Play_Control><Skip>%s</Skip></Play_Control>");
96                 logger.debug("Input {} - adjusting command to: {}", inputElement, skipCmd);
97             }
98         }
99     }
100
101     /**
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().
104      *
105      * @throws Exception
106      */
107     @Override
108     public void play() throws IOException, ReceivedMessageParseException {
109         sendCommand(playCmd.apply("Play"));
110     }
111
112     /**
113      * Stop the currently playing content. Use start() to start again.
114      *
115      * @throws Exception
116      */
117     @Override
118     public void stop() throws IOException, ReceivedMessageParseException {
119         sendCommand(playCmd.apply("Stop"));
120     }
121
122     /**
123      * Pause the currently playing content. This is not available for streaming content like on NET_RADIO.
124      *
125      * @throws Exception
126      */
127     @Override
128     public void pause() throws IOException, ReceivedMessageParseException {
129         sendCommand(playCmd.apply("Pause"));
130     }
131
132     /**
133      * Skip forward. This is not available for streaming content like on NET_RADIO.
134      *
135      * @throws Exception
136      */
137     @Override
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);
141             return;
142         }
143         sendCommand(skipCmd.apply(">>|"));
144     }
145
146     /**
147      * Skip reverse. This is not available for streaming content like on NET_RADIO.
148      *
149      * @throws Exception
150      */
151     @Override
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);
155             return;
156         }
157         sendCommand(skipCmd.apply("|<<"));
158     }
159
160     /**
161      * Next track. This is not available for streaming content like on NET_RADIO.
162      *
163      * @throws Exception
164      */
165     @Override
166     public void nextTrack() throws IOException, ReceivedMessageParseException {
167         sendCommand(skipCmd.apply(skipForwardValue));
168     }
169
170     /**
171      * Previous track. This is not available for streaming content like on NET_RADIO.
172      *
173      * @throws Exception
174      */
175     @Override
176     public void previousTrack() throws IOException, ReceivedMessageParseException {
177         sendCommand(skipCmd.apply(skipBackwardValue));
178     }
179
180     /**
181      * Sends a playback command to the AVR. After command is invoked, the state is also being refreshed.
182      *
183      * @param command - the protocol level command name
184      * @throws IOException
185      * @throws ReceivedMessageParseException
186      */
187     private void sendCommand(String command) throws IOException, ReceivedMessageParseException {
188         comReference.get().send(wrInput(command));
189         update();
190     }
191
192     /**
193      * Updates the playback information
194      *
195      * @throws Exception
196      */
197     @Override
198     public void update() throws IOException, ReceivedMessageParseException {
199         if (observer == null) {
200             return;
201         }
202
203         // <YAMAHA_AV rsp="GET" RC="0">
204         // <Spotify>
205         // <Play_Info>
206         // <Feature_Availability>Ready</Feature_Availability>
207         // <Playback_Info>Play</Playback_Info>
208         // <Meta_Info>
209         // <Artist>Way Out West</Artist>
210         // <Album>Tuesday Maybe</Album>
211         // <Track>Tuesday Maybe</Track>
212         // </Meta_Info>
213         // <Album_ART>
214         // <URL>/YamahaRemoteControl/AlbumART/AlbumART3929.jpg</URL>
215         // <ID>39290</ID>
216         // <Format>JPEG</Format>
217         // </Album_ART>
218         // <Input_Logo>
219         // <URL_S>/YamahaRemoteControl/Logos/logo005.png</URL_S>
220         // <URL_M></URL_M>
221         // <URL_L></URL_L>
222         // </Input_Logo>
223         // </Play_Info>
224         // </Spotify>
225         // </YAMAHA_AV>
226
227         AbstractConnection con = comReference.get();
228         Node node = getResponse(con, wrInput(PLAYBACK_STATUS_CMD), inputElement);
229
230         PlayInfoState msg = new PlayInfoState();
231
232         msg.playbackMode = getNodeContentOrDefault(node, playCmd.getPath(), msg.playbackMode);
233
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");
243
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();
248
249         logger.trace("Playback: {}, Station: {}, Artist: {}, Album: {}, Song: {}, SongImageUrl: {}", msg.playbackMode,
250                 msg.station, msg.artist, msg.album, msg.song, msg.songImageUrl);
251
252         observer.playInfoUpdated(msg);
253     }
254 }