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.heos.internal.handler;
15 import java.io.IOException;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.heos.internal.exception.HeosNotFoundException;
21 import org.openhab.binding.heos.internal.json.payload.Media;
22 import org.openhab.binding.heos.internal.resources.HeosEventListener;
23 import org.openhab.binding.heos.internal.resources.Telnet.ReadException;
24 import org.openhab.core.thing.ThingUID;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.RefreshType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link HeosChannelHandlerInputs} handles the Input channel command
32 * from the implementing thing.
34 * @author Johannes Einig - Initial contribution
37 public class HeosChannelHandlerInputs extends BaseHeosChannelHandler {
38 protected final Logger logger = LoggerFactory.getLogger(HeosChannelHandlerInputs.class);
40 private final HeosEventListener eventListener;
42 public HeosChannelHandlerInputs(HeosEventListener eventListener, HeosBridgeHandler bridge) {
44 this.eventListener = eventListener;
48 public void handlePlayerCommand(Command command, String id, ThingUID uid) throws IOException, ReadException {
49 handleCommand(command, id);
53 public void handleGroupCommand(Command command, @Nullable String id, ThingUID uid,
54 HeosGroupHandler heosGroupHandler) throws IOException, ReadException {
56 throw new HeosNotFoundException();
58 handleCommand(command, id);
62 public void handleBridgeCommand(Command command, ThingUID uid) {
66 private void handleCommand(Command command, String id) throws IOException, ReadException {
67 if (command instanceof RefreshType) {
69 Media payload = getApi().getNowPlayingMedia(id).payload;
70 if (payload != null) {
71 eventListener.playerMediaChangeEvent(id, payload);
76 Map<String, String> selectedPlayers = bridge.getSelectedPlayer();
77 if (selectedPlayers.isEmpty()) {
78 // no selected player, just play it from the player itself
79 getApi().playInputSource(id, command.toString());
80 } else if (selectedPlayers.size() > 1) {
81 logger.debug("Only one source can be selected for HEOS Input. Selected amount of sources: {} ",
82 selectedPlayers.size());
84 for (String sourcePid : selectedPlayers.keySet()) {
85 getApi().playInputSource(id, sourcePid, command.toString());