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.*;
17 import java.lang.ref.WeakReference;
18 import java.util.HashMap;
21 import org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Feature;
22 import org.openhab.binding.yamahareceiver.internal.config.YamahaUtils;
23 import org.openhab.binding.yamahareceiver.internal.protocol.AbstractConnection;
24 import org.openhab.binding.yamahareceiver.internal.state.DeviceInformationState;
25 import org.slf4j.Logger;
28 * Provides basis for all input controls
30 * @author Tomasz Maruszak - Initial contribution
32 public abstract class AbstractInputControlXML {
34 protected final Logger logger;
36 protected final WeakReference<AbstractConnection> comReference;
37 protected final String inputID;
38 protected final Map<String, String> inputToElement;
39 protected final DeviceDescriptorXML deviceDescriptor;
41 protected String inputElement;
42 protected DeviceDescriptorXML.FeatureDescriptor inputFeatureDescriptor;
44 private Map<String, String> loadMapping() {
45 Map<String, String> map = new HashMap<>();
47 // ToDo: For maximum compatibility these should be obtained fro the VNC_Tag of the desc.xml
48 map.put(INPUT_TUNER, "Tuner");
49 map.put(INPUT_NET_RADIO, "NET_RADIO");
50 map.put(INPUT_MUSIC_CAST_LINK, "MusicCast_Link");
55 protected AbstractInputControlXML(Logger logger, String inputID, AbstractConnection con,
56 DeviceInformationState deviceInformationState) {
58 this.comReference = new WeakReference<>(con);
59 this.inputID = inputID;
60 this.deviceDescriptor = DeviceDescriptorXML.getAttached(deviceInformationState);
61 this.inputToElement = loadMapping();
62 this.inputElement = inputToElement.getOrDefault(inputID, inputID);
63 this.inputFeatureDescriptor = getInputFeatureDescriptor();
67 * Wraps the XML message with the inputID tags. Example with inputID=NET_RADIO:
68 * <NET_RADIO>message</NET_RADIO>.
70 * @param message XML message
73 protected String wrInput(String message) {
74 return String.format("<%s>%s</%s>", inputElement, message, inputElement);
77 protected DeviceDescriptorXML.FeatureDescriptor getInputFeatureDescriptor() {
78 if (deviceDescriptor == null) {
79 logger.trace("Descriptor not available");
83 Feature inputFeature = YamahaUtils.tryParseEnum(Feature.class, inputElement);
85 // For RX-V3900 both the inputs 'NET RADIO' and 'USB' need to use the same NET_USB element
86 if ((INPUT_NET_RADIO.equals(inputID) || INPUT_USB.equals(inputID))
87 && deviceDescriptor.features.containsKey(Feature.NET_USB)
88 && !deviceDescriptor.features.containsKey(Feature.NET_RADIO)
89 && !deviceDescriptor.features.containsKey(Feature.USB)) {
90 // have to use the NET_USB xml element in this case
91 inputElement = "NET_USB";
92 inputFeature = Feature.NET_USB;
95 if (inputFeature != null) {
96 return deviceDescriptor.features.getOrDefault(inputFeature, null);