2 * Copyright (c) 2010-2024 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.*;
16 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLConstants.*;
17 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLConstants.Commands.*;
18 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLProtocolService.getZoneResponse;
19 import static org.openhab.binding.yamahareceiver.internal.protocol.xml.XMLUtils.*;
21 import java.io.IOException;
22 import java.lang.ref.WeakReference;
23 import java.util.function.Supplier;
25 import org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Zone;
26 import org.openhab.binding.yamahareceiver.internal.config.YamahaZoneConfig;
27 import org.openhab.binding.yamahareceiver.internal.protocol.AbstractConnection;
28 import org.openhab.binding.yamahareceiver.internal.protocol.InputConverter;
29 import org.openhab.binding.yamahareceiver.internal.protocol.ReceivedMessageParseException;
30 import org.openhab.binding.yamahareceiver.internal.protocol.ZoneControl;
31 import org.openhab.binding.yamahareceiver.internal.state.DeviceInformationState;
32 import org.openhab.binding.yamahareceiver.internal.state.ZoneControlState;
33 import org.openhab.binding.yamahareceiver.internal.state.ZoneControlStateListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.w3c.dom.Node;
39 * The zone protocol class is used to control one zone of a Yamaha receiver with HTTP/xml.
40 * No state will be saved in here, but in {@link ZoneControlState} instead.
42 * @author David Gräff - Refactored
45 * @author Tomasz Maruszak - Refactoring, input mapping fix, added Straight surround, volume DB fix and config
48 public class ZoneControlXML implements ZoneControl {
50 protected Logger logger = LoggerFactory.getLogger(ZoneControlXML.class);
52 private static final String SURROUND_PROGRAM_STRAIGHT = "Straight";
54 private final ZoneControlStateListener observer;
55 private final Supplier<InputConverter> inputConverterSupplier;
56 private final WeakReference<AbstractConnection> comReference;
57 private final Zone zone;
58 private final YamahaZoneConfig zoneConfig;
59 private final DeviceDescriptorXML.ZoneDescriptor zoneDescriptor;
61 protected CommandTemplate power = new CommandTemplate("<Power_Control><Power>%s</Power></Power_Control>",
62 "Power_Control/Power");
63 protected CommandTemplate mute = new CommandTemplate("<Volume><Mute>%s</Mute></Volume>", "Volume/Mute");
64 protected CommandTemplate volume = new CommandTemplate(
65 "<Volume><Lvl><Val>%d</Val><Exp>1</Exp><Unit>dB</Unit></Lvl></Volume>", "Volume/Lvl/Val");
66 protected CommandTemplate inputSel = new CommandTemplate("<Input><Input_Sel>%s</Input_Sel></Input>",
68 protected String inputSelNamePath = "Input/Input_Sel_Item_Info/Title";
69 protected CommandTemplate surroundSelProgram = new CommandTemplate(
70 "<Surround><Program_Sel><Current><Sound_Program>%s</Sound_Program></Current></Program_Sel></Surround>",
71 "Surround/Program_Sel/Current/Sound_Program");
72 protected CommandTemplate surroundSelStraight = new CommandTemplate(
73 "<Surround><Program_Sel><Current><Straight>On</Straight></Current></Program_Sel></Surround>",
74 "Surround/Program_Sel/Current/Straight");
75 protected CommandTemplate sceneSel = new CommandTemplate("<Scene><Scene_Sel>%s</Scene_Sel></Scene>");
76 protected boolean sceneSelSupported = false;
77 protected CommandTemplate dialogueLevel = new CommandTemplate(
78 "<Sound_Video><Dialogue_Adjust><Dialogue_Lvl>%d</Dialogue_Lvl></Dialogue_Adjust></Sound_Video>",
79 "Sound_Video/Dialogue_Adjust/Dialogue_Lvl");
80 protected CommandTemplate hdmi1Out = new CommandTemplate(
81 "<System><Sound_Video><HDMI><Output><OUT_1>%s</OUT_1></Output></HDMI></Sound_Video></System>",
82 "Sound_Video/HDMI/Output/OUT_1");
83 protected CommandTemplate hdmi2Out = new CommandTemplate(
84 "<System><Sound_Video><HDMI><Output><OUT_2>%s</OUT_2></Output></HDMI></Sound_Video></System>",
85 "Sound_Video/HDMI/Output/OUT_2");
86 protected boolean dialogueLevelSupported = false;
87 protected boolean hdmi1OutSupported = false;
88 protected boolean hdmi2OutSupported = false;
90 public ZoneControlXML(AbstractConnection con, Zone zone, YamahaZoneConfig zoneSettings,
91 ZoneControlStateListener observer, DeviceInformationState deviceInformationState,
92 Supplier<InputConverter> inputConverterSupplier) {
93 this.comReference = new WeakReference<>(con);
95 this.zoneConfig = zoneSettings;
96 this.zoneDescriptor = DeviceDescriptorXML.getAttached(deviceInformationState).zones.getOrDefault(zone, null);
97 this.observer = observer;
98 this.inputConverterSupplier = inputConverterSupplier;
100 this.applyModelVariations();
104 * Apply command changes to ensure compatibility with all supported models
106 protected void applyModelVariations() {
107 if (zoneDescriptor == null) {
108 logger.trace("Zone {} - descriptor not available", getZone());
112 logger.trace("Zone {} - compatibility detection", getZone());
114 // Note: Detection if scene is supported
115 sceneSelSupported = zoneDescriptor.hasCommandEnding("Scene,Scene_Sel", () -> logger
116 .debug("Zone {} - the {} channel is not supported on your model", getZone(), CHANNEL_SCENE));
118 // Note: Detection if dialogue level is supported
119 dialogueLevelSupported = zoneDescriptor.hasAnyCommandEnding("Sound_Video,Dialogue_Adjust,Dialogue_Lvl",
120 "Sound_Video,Dialogue_Adjust,Dialogue_Lift");
121 if (zoneDescriptor.hasCommandEnding("Sound_Video,Dialogue_Adjust,Dialogue_Lift")) {
122 dialogueLevel = dialogueLevel.replace("Dialogue_Lvl", "Dialogue_Lift");
123 logger.debug("Zone {} - adjusting command to: {}", getZone(), dialogueLevel);
125 if (!dialogueLevelSupported) {
126 logger.debug("Zone {} - the {} channel is not supported on your model", getZone(), CHANNEL_DIALOGUE_LEVEL);
129 hdmi1OutSupported = zoneDescriptor.hasCommandEnding("Sound_Video,HDMI,Output,OUT_1", () -> logger
130 .debug("Zone {} - the {} channel is not supported on your model", getZone(), CHANNEL_HDMI1OUT));
132 hdmi2OutSupported = zoneDescriptor.hasCommandEnding("Sound_Video,HDMI,Output,OUT_2", () -> logger
133 .debug("Zone {} - the {} channel is not supported on your model", getZone(), CHANNEL_HDMI2OUT));
135 // Note: Detection for RX-V3900, which uses <Vol> instead of <Volume>
136 if (zoneDescriptor.hasCommandEnding("Vol,Lvl")) {
137 volume = volume.replace("Volume", "Vol");
138 logger.debug("Zone {} - adjusting command to: {}", getZone(), volume);
140 if (zoneDescriptor.hasCommandEnding("Vol,Mute")) {
141 mute = mute.replace("Volume", "Vol");
142 logger.debug("Zone {} - adjusting command to: {}", getZone(), mute);
146 // Note: Detection for RX-V3900, which has a different XML node for surround program
147 Node basicStatusNode = getZoneResponse(comReference.get(), getZone(), ZONE_BASIC_STATUS_CMD,
148 ZONE_BASIC_STATUS_PATH);
149 String surroundProgram = getNodeContentOrEmpty(basicStatusNode, "Surr/Pgm_Sel/Pgm");
151 if (!surroundProgram.isEmpty()) {
152 surroundSelProgram = new CommandTemplate(
153 "<Surr><Pgm_Sel><Straight>Off</Straight><Pgm>%s</Pgm></Pgm_Sel></Surr>", "Surr/Pgm_Sel/Pgm");
154 logger.debug("Zone {} - adjusting command to: {}", getZone(), surroundSelProgram);
156 surroundSelStraight = new CommandTemplate("<Surr><Pgm_Sel><Straight>On</Straight></Pgm_Sel></Surr>",
157 "Surr/Pgm_Sel/Straight");
158 logger.debug("Zone {} - adjusting command to: {}", getZone(), surroundSelStraight);
161 } catch (ReceivedMessageParseException | IOException e) {
162 logger.debug("Could not perform feature detection for RX-V3900");
166 protected void sendCommand(String message) throws IOException {
167 comReference.get().send(XMLUtils.wrZone(zone, message));
170 protected void sendCommandWithoutZone(String message) throws IOException {
171 comReference.get().send(message);
177 public Zone getZone() {
182 public void setPower(boolean on) throws IOException, ReceivedMessageParseException {
183 String cmd = power.apply(on ? ON : POWER_STANDBY);
189 public void setMute(boolean on) throws IOException, ReceivedMessageParseException {
190 String cmd = this.mute.apply(on ? ON : OFF);
196 * Sets the absolute volume in decibel.
198 * @param volume Absolute value in decibel ([-80,+12]).
199 * @throws IOException
202 public void setVolumeDB(float volume) throws IOException, ReceivedMessageParseException {
203 if (volume < zoneConfig.getVolumeDbMin()) {
204 volume = zoneConfig.getVolumeDbMin();
206 if (volume > zoneConfig.getVolumeDbMax()) {
207 volume = zoneConfig.getVolumeDbMax();
210 // Yamaha accepts only integer values with .0 or .5 at the end only (-20.5dB, -20.0dB) - at least on RX-S601D.
211 // The order matters here. We want to cast to integer first and then scale by 10.
212 // Effectively we're only allowing dB values with .0 at the end.
213 int vol = (int) volume * 10;
214 sendCommand(this.volume.apply(vol));
219 * Sets the volume in percent
222 * @throws IOException
225 public void setVolume(float volume) throws IOException, ReceivedMessageParseException {
232 // Compute value in db
233 setVolumeDB(zoneConfig.getVolumeDb(volume));
237 * Increase or decrease the volume by the given percentage.
240 * @throws IOException
243 public void setVolumeRelative(ZoneControlState state, float percent)
244 throws IOException, ReceivedMessageParseException {
245 setVolume(zoneConfig.getVolumePercentage(state.volumeDB) + percent);
249 public void setInput(String name) throws IOException, ReceivedMessageParseException {
250 name = inputConverterSupplier.get().toCommandName(name);
251 String cmd = inputSel.apply(name);
257 public void setSurroundProgram(String name) throws IOException, ReceivedMessageParseException {
258 String cmd = name.equalsIgnoreCase(SURROUND_PROGRAM_STRAIGHT) ? surroundSelStraight.apply()
259 : surroundSelProgram.apply(name);
266 public void setDialogueLevel(int level) throws IOException, ReceivedMessageParseException {
267 if (!dialogueLevelSupported) {
270 sendCommand(dialogueLevel.apply(level));
275 public void setHDMI1Out(boolean on) throws IOException, ReceivedMessageParseException {
276 if (!hdmi1OutSupported) {
279 sendCommandWithoutZone(hdmi1Out.apply(on ? ON : OFF));
284 public void setHDMI2Out(boolean on) throws IOException, ReceivedMessageParseException {
285 if (!hdmi2OutSupported) {
288 sendCommandWithoutZone(hdmi2Out.apply(on ? ON : OFF));
293 public void setScene(String scene) throws IOException, ReceivedMessageParseException {
294 if (!sceneSelSupported) {
297 sendCommand(sceneSel.apply(scene));
302 public void update() throws IOException, ReceivedMessageParseException {
303 if (observer == null) {
307 Node statusNode = getZoneResponse(comReference.get(), zone, ZONE_BASIC_STATUS_CMD, ZONE_BASIC_STATUS_PATH);
311 ZoneControlState state = new ZoneControlState();
313 value = getNodeContentOrEmpty(statusNode, power.getPath());
314 state.power = ON.equalsIgnoreCase(value);
316 value = getNodeContentOrEmpty(statusNode, mute.getPath());
317 state.mute = ON.equalsIgnoreCase(value);
319 // The value comes in dB x 10, on AVR it says -30.5dB, the values comes as -305
320 value = getNodeContentOrDefault(statusNode, volume.getPath(), String.valueOf(zoneConfig.getVolumeDbMin()));
321 state.volumeDB = Float.parseFloat(value) * .1f; // in dB
323 value = getNodeContentOrEmpty(statusNode, inputSel.getPath());
324 state.inputID = inputConverterSupplier.get().fromStateName(value);
325 if (state.inputID == null || state.inputID.isBlank()) {
326 throw new ReceivedMessageParseException("Expected inputID. Failed to read Input/Input_Sel");
329 value = getNodeContentOrEmpty(statusNode, hdmi1Out.getPath());
330 state.hdmi1Out = ON.equalsIgnoreCase(value);
332 value = getNodeContentOrEmpty(statusNode, hdmi1Out.getPath());
333 state.hdmi2Out = ON.equalsIgnoreCase(value);
335 // Some receivers may use Src_Name instead?
336 value = getNodeContentOrEmpty(statusNode, inputSelNamePath);
337 state.inputName = value;
339 value = getNodeContentOrEmpty(statusNode, surroundSelStraight.getPath());
340 boolean straightOn = ON.equalsIgnoreCase(value);
342 value = getNodeContentOrEmpty(statusNode, surroundSelProgram.getPath());
343 // Surround is either in straight mode or sound program
344 state.surroundProgram = straightOn ? SURROUND_PROGRAM_STRAIGHT : value;
346 value = getNodeContentOrDefault(statusNode, dialogueLevel.getPath(), "0");
347 state.dialogueLevel = Integer.parseInt(value);
349 logger.debug("Zone {} state - power: {}, mute: {}, volumeDB: {}, input: {}, surroundProgram: {}", getZone(),
350 state.power, state.mute, state.volumeDB, state.inputID, state.surroundProgram);
352 observer.zoneStateChanged(state);