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 static org.openhab.binding.heos.internal.resources.HeosConstants.SONG;
17 import java.io.IOException;
18 import java.util.HashMap;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.heos.internal.exception.HeosNotFoundException;
24 import org.openhab.binding.heos.internal.json.payload.Media;
25 import org.openhab.binding.heos.internal.resources.HeosEventListener;
26 import org.openhab.binding.heos.internal.resources.HeosMediaEventListener;
27 import org.openhab.binding.heos.internal.resources.Telnet.ReadException;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
33 * The {@link HeosChannelHandlerControl} handles the control commands
34 * coming from the implementing thing
36 * @author Johannes Einig - Initial contribution
37 * @author Martin van Wingerden - change handling of stop/pause depending on playing item type
40 public class HeosChannelHandlerControl extends BaseHeosChannelHandler implements HeosMediaEventListener {
41 private final HeosEventListener eventListener;
42 private final Map<String, Media> playingMediaCache = new HashMap<>();
44 public HeosChannelHandlerControl(HeosEventListener eventListener, HeosBridgeHandler bridge) {
46 bridge.registerMediaEventListener(this);
47 this.eventListener = eventListener;
51 public void handlePlayerCommand(Command command, String id, ThingUID uid) throws IOException, ReadException {
52 handleCommand(command, id);
56 public void handleGroupCommand(Command command, @Nullable String id, ThingUID uid,
57 HeosGroupHandler heosGroupHandler) throws IOException, ReadException {
59 throw new HeosNotFoundException();
61 handleCommand(command, id);
65 public void handleBridgeCommand(Command command, ThingUID uid) {
66 // No such channel within bridge
69 private void handleCommand(Command command, String id) throws IOException, ReadException {
70 if (command instanceof RefreshType) {
71 eventListener.playerStateChangeEvent(getApi().getPlayState(id));
74 switch (command.toString()) {
81 if (shouldPause(id)) {
91 getApi().previous(id);
96 private boolean shouldPause(String id) {
97 Media applicableMedia = playingMediaCache.get(id);
98 if (applicableMedia == null || SONG.equals(applicableMedia.type)) {
101 // we have a station here, just have to check which one
102 switch (applicableMedia.sourceId) {
103 case Media.SOURCE_TUNE_IN:
104 case Media.SOURCE_I_HEART_RADIO:
105 case Media.SOURCE_SIRIUS_XM:
106 case Media.SOURCE_AUX:
116 public void playerMediaChangeEvent(String pid, Media media) {
117 playingMediaCache.put(pid, media);