]> git.basschouten.com Git - openhab-addons.git/blob
4a61e775227342a2adc812e3be080693629340bb
[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 java.util.function.Supplier;
16
17 import org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants;
18 import org.openhab.binding.yamahareceiver.internal.config.YamahaBridgeConfig;
19 import org.openhab.binding.yamahareceiver.internal.config.YamahaZoneConfig;
20 import org.openhab.binding.yamahareceiver.internal.protocol.AbstractConnection;
21 import org.openhab.binding.yamahareceiver.internal.protocol.ConnectionStateListener;
22 import org.openhab.binding.yamahareceiver.internal.protocol.DeviceInformation;
23 import org.openhab.binding.yamahareceiver.internal.protocol.InputConverter;
24 import org.openhab.binding.yamahareceiver.internal.protocol.InputWithNavigationControl;
25 import org.openhab.binding.yamahareceiver.internal.protocol.InputWithPlayControl;
26 import org.openhab.binding.yamahareceiver.internal.protocol.InputWithPresetControl;
27 import org.openhab.binding.yamahareceiver.internal.protocol.InputWithTunerBandControl;
28 import org.openhab.binding.yamahareceiver.internal.protocol.ProtocolFactory;
29 import org.openhab.binding.yamahareceiver.internal.protocol.SystemControl;
30 import org.openhab.binding.yamahareceiver.internal.protocol.ZoneAvailableInputs;
31 import org.openhab.binding.yamahareceiver.internal.protocol.ZoneControl;
32 import org.openhab.binding.yamahareceiver.internal.state.AvailableInputStateListener;
33 import org.openhab.binding.yamahareceiver.internal.state.DabBandStateListener;
34 import org.openhab.binding.yamahareceiver.internal.state.DeviceInformationState;
35 import org.openhab.binding.yamahareceiver.internal.state.NavigationControlState;
36 import org.openhab.binding.yamahareceiver.internal.state.NavigationControlStateListener;
37 import org.openhab.binding.yamahareceiver.internal.state.PlayInfoStateListener;
38 import org.openhab.binding.yamahareceiver.internal.state.PresetInfoStateListener;
39 import org.openhab.binding.yamahareceiver.internal.state.SystemControlStateListener;
40 import org.openhab.binding.yamahareceiver.internal.state.ZoneControlStateListener;
41
42 /**
43  * Implementation of {@link ProtocolFactory} for XML protocol.
44  *
45  * @author Tomasz Maruszak - Initial contribution.
46  */
47 public class XMLProtocolFactory implements ProtocolFactory {
48     @Override
49     public void createConnection(String host, ConnectionStateListener connectionStateListener) {
50         connectionStateListener.onConnectionCreated(new XMLConnection(host));
51     }
52
53     @Override
54     public SystemControl SystemControl(AbstractConnection connection, SystemControlStateListener listener,
55             DeviceInformationState deviceInformationState) {
56         return new SystemControlXML(connection, listener, deviceInformationState);
57     }
58
59     @Override
60     public InputWithPlayControl InputWithPlayControl(AbstractConnection connection, String currentInputID,
61             PlayInfoStateListener listener, YamahaBridgeConfig bridgeConfig,
62             DeviceInformationState deviceInformationState) {
63         return new InputWithPlayControlXML(currentInputID, connection, listener, bridgeConfig, deviceInformationState);
64     }
65
66     @Override
67     public InputWithPresetControl InputWithPresetControl(AbstractConnection connection, String currentInputID,
68             PresetInfoStateListener listener, DeviceInformationState deviceInformationState) {
69         return new InputWithPresetControlXML(currentInputID, connection, listener, deviceInformationState);
70     }
71
72     @Override
73     public InputWithTunerBandControl InputWithDabBandControl(String currentInputID, AbstractConnection connection,
74             DabBandStateListener observerForBand, PresetInfoStateListener observerForPreset,
75             PlayInfoStateListener observerForPlayInfo, DeviceInformationState deviceInformationState) {
76         return new InputWithTunerDABControlXML(currentInputID, connection, observerForBand, observerForPreset,
77                 observerForPlayInfo, deviceInformationState);
78     }
79
80     @Override
81     public InputWithNavigationControl InputWithNavigationControl(AbstractConnection connection,
82             NavigationControlState state, String inputID, NavigationControlStateListener observer,
83             DeviceInformationState deviceInformationState) {
84         return new InputWithNavigationControlXML(state, inputID, connection, observer, deviceInformationState);
85     }
86
87     @Override
88     public ZoneControl ZoneControl(AbstractConnection connection, YamahaZoneConfig zoneSettings,
89             ZoneControlStateListener listener, Supplier<InputConverter> inputConverterSupplier,
90             DeviceInformationState deviceInformationState) {
91         if (isZoneB(zoneSettings.getZone(), deviceInformationState)) {
92             return new ZoneBControlXML(connection, zoneSettings, listener, deviceInformationState,
93                     inputConverterSupplier);
94         }
95         return new ZoneControlXML(connection, zoneSettings.getZone(), zoneSettings, listener, deviceInformationState,
96                 inputConverterSupplier);
97     }
98
99     @Override
100     public ZoneAvailableInputs ZoneAvailableInputs(AbstractConnection connection, YamahaZoneConfig zoneSettings,
101             AvailableInputStateListener listener, Supplier<InputConverter> inputConverterSupplier,
102             DeviceInformationState deviceInformationState) {
103         if (isZoneB(zoneSettings.getZone(), deviceInformationState)) {
104             return new ZoneBAvailableInputsXML(connection, listener, inputConverterSupplier);
105         }
106         return new ZoneAvailableInputsXML(connection, zoneSettings.getZone(), listener, inputConverterSupplier);
107     }
108
109     /**
110      * Checks if the specified Zone_2 should be emulated using Zone_B feature.
111      *
112      * @param zone
113      * @param deviceInformationState
114      * @return
115      */
116     private boolean isZoneB(YamahaReceiverBindingConstants.Zone zone, DeviceInformationState deviceInformationState) {
117         return YamahaReceiverBindingConstants.Zone.Zone_2.equals(zone)
118                 && deviceInformationState.features.contains(YamahaReceiverBindingConstants.Feature.ZONE_B);
119     }
120
121     @Override
122     public DeviceInformation DeviceInformation(AbstractConnection connection, DeviceInformationState state) {
123         return new DeviceInformationXML(connection, state);
124     }
125
126     @Override
127     public InputConverter InputConverter(AbstractConnection connection, String setting) {
128         return new InputConverterXML(connection, setting);
129     }
130 }