]> git.basschouten.com Git - openhab-addons.git/blob
0546da7a152a987d3f0be47093199998487bf25f
[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.heos.internal.handler;
14
15 import java.io.IOException;
16 import java.util.Map;
17
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;
29
30 /**
31  * The {@link HeosChannelHandlerInputs} handles the Input channel command
32  * from the implementing thing.
33  *
34  * @author Johannes Einig - Initial contribution
35  */
36 @NonNullByDefault
37 public class HeosChannelHandlerInputs extends BaseHeosChannelHandler {
38     protected final Logger logger = LoggerFactory.getLogger(HeosChannelHandlerInputs.class);
39
40     private final HeosEventListener eventListener;
41
42     public HeosChannelHandlerInputs(HeosEventListener eventListener, HeosBridgeHandler bridge) {
43         super(bridge);
44         this.eventListener = eventListener;
45     }
46
47     @Override
48     public void handlePlayerCommand(Command command, String id, ThingUID uid) throws IOException, ReadException {
49         handleCommand(command, id);
50     }
51
52     @Override
53     public void handleGroupCommand(Command command, @Nullable String id, ThingUID uid,
54             HeosGroupHandler heosGroupHandler) throws IOException, ReadException {
55         if (id == null) {
56             throw new HeosNotFoundException();
57         }
58         handleCommand(command, id);
59     }
60
61     @Override
62     public void handleBridgeCommand(Command command, ThingUID uid) {
63         // not used on bridge
64     }
65
66     private void handleCommand(Command command, String id) throws IOException, ReadException {
67         if (command instanceof RefreshType) {
68             @Nullable
69             Media payload = getApi().getNowPlayingMedia(id).payload;
70             if (payload != null) {
71                 eventListener.playerMediaChangeEvent(id, payload);
72             }
73             return;
74         }
75
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());
83         } else {
84             for (String sourcePid : selectedPlayers.keySet()) {
85                 getApi().playInputSource(id, sourcePid, command.toString());
86             }
87         }
88     }
89 }