]> git.basschouten.com Git - openhab-addons.git/blob
1cb0aa1bbdf8176584a927e26a93bab571895ef4
[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.*;
16
17 import java.lang.ref.WeakReference;
18 import java.util.HashMap;
19 import java.util.Map;
20
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;
26
27 /**
28  * Provides basis for all input controls
29  *
30  * @author Tomasz Maruszak - Initial contribution
31  */
32 public abstract class AbstractInputControlXML {
33
34     protected final Logger logger;
35
36     protected final WeakReference<AbstractConnection> comReference;
37     protected final String inputID;
38     protected final Map<String, String> inputToElement;
39     protected final DeviceDescriptorXML deviceDescriptor;
40
41     protected String inputElement;
42     protected DeviceDescriptorXML.FeatureDescriptor inputFeatureDescriptor;
43
44     private Map<String, String> loadMapping() {
45         Map<String, String> map = new HashMap<>();
46
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");
51
52         return map;
53     }
54
55     protected AbstractInputControlXML(Logger logger, String inputID, AbstractConnection con,
56             DeviceInformationState deviceInformationState) {
57         this.logger = logger;
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();
64     }
65
66     /**
67      * Wraps the XML message with the inputID tags. Example with inputID=NET_RADIO:
68      * <NET_RADIO>message</NET_RADIO>.
69      *
70      * @param message XML message
71      * @return
72      */
73     protected String wrInput(String message) {
74         return String.format("<%s>%s</%s>", inputElement, message, inputElement);
75     }
76
77     protected DeviceDescriptorXML.FeatureDescriptor getInputFeatureDescriptor() {
78         if (deviceDescriptor == null) {
79             logger.trace("Descriptor not available");
80             return null;
81         }
82
83         Feature inputFeature = YamahaUtils.tryParseEnum(Feature.class, inputElement);
84
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;
93         }
94
95         if (inputFeature != null) {
96             return deviceDescriptor.features.getOrDefault(inputFeature, null);
97         }
98         return null;
99     }
100 }