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 java.util.stream.Collectors.joining;
17 import java.io.IOException;
18 import java.lang.ref.WeakReference;
19 import java.util.Collection;
20 import java.util.function.Supplier;
22 import org.openhab.binding.yamahareceiver.internal.YamahaReceiverBindingConstants.Zone;
23 import org.openhab.binding.yamahareceiver.internal.protocol.AbstractConnection;
24 import org.openhab.binding.yamahareceiver.internal.protocol.InputConverter;
25 import org.openhab.binding.yamahareceiver.internal.protocol.ReceivedMessageParseException;
26 import org.openhab.binding.yamahareceiver.internal.protocol.ZoneAvailableInputs;
27 import org.openhab.binding.yamahareceiver.internal.state.AvailableInputState;
28 import org.openhab.binding.yamahareceiver.internal.state.AvailableInputStateListener;
29 import org.openhab.binding.yamahareceiver.internal.state.ZoneControlState;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The zone protocol class is used to control one zone of a Yamaha receiver with HTTP/xml.
35 * No state will be saved in here, but in {@link ZoneControlState} instead.
37 * @author David Gräff - Initial contribution
38 * @author Tomasz Maruszak - Refactoring
39 * @author Tomasz Maruszak - Input mapping fix
42 public class ZoneAvailableInputsXML implements ZoneAvailableInputs {
43 protected Logger logger = LoggerFactory.getLogger(ZoneAvailableInputsXML.class);
45 private final WeakReference<AbstractConnection> conReference;
46 private final AvailableInputStateListener observer;
47 private final Supplier<InputConverter> inputConverterSupplier;
48 private final Zone zone;
50 public ZoneAvailableInputsXML(AbstractConnection con, Zone zone, AvailableInputStateListener observer,
51 Supplier<InputConverter> inputConverterSupplier) {
52 this.conReference = new WeakReference<>(con);
54 this.observer = observer;
55 this.inputConverterSupplier = inputConverterSupplier;
61 public Zone getZone() {
66 public void update() throws IOException, ReceivedMessageParseException {
67 if (observer == null) {
71 Collection<XMLProtocolService.InputDto> inputs = XMLProtocolService.getInputs(conReference.get(), zone);
73 AvailableInputState state = new AvailableInputState();
75 inputs.stream().filter(XMLProtocolService.InputDto::isWritable).forEach(x -> {
76 String inputName = inputConverterSupplier.get().fromStateName(x.getParam());
77 state.availableInputs.put(inputName, x.getParam());
80 if (logger.isTraceEnabled()) {
81 logger.trace("Zone {} - available inputs: {}", getZone(),
82 state.availableInputs.keySet().stream().collect(joining(", ")));
85 observer.availableInputsChanged(state);