]> git.basschouten.com Git - openhab-addons.git/blob
ff38cdbba9af7fab84eb8385a2dff60bf6377b6c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 static org.openhab.binding.heos.internal.resources.HeosConstants.SONG;
16
17 import java.io.IOException;
18 import java.util.HashMap;
19 import java.util.Map;
20
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;
31
32 /**
33  * The {@link HeosChannelHandlerControl} handles the control commands
34  * coming from the implementing thing
35  *
36  * @author Johannes Einig - Initial contribution
37  * @author Martin van Wingerden - change handling of stop/pause depending on playing item type
38  */
39 @NonNullByDefault
40 public class HeosChannelHandlerControl extends BaseHeosChannelHandler implements HeosMediaEventListener {
41     private final HeosEventListener eventListener;
42     private final Map<String, Media> playingMediaCache = new HashMap<>();
43
44     public HeosChannelHandlerControl(HeosEventListener eventListener, HeosBridgeHandler bridge) {
45         super(bridge);
46         bridge.registerMediaEventListener(this);
47         this.eventListener = eventListener;
48     }
49
50     @Override
51     public void handlePlayerCommand(Command command, String id, ThingUID uid) throws IOException, ReadException {
52         handleCommand(command, id);
53     }
54
55     @Override
56     public void handleGroupCommand(Command command, @Nullable String id, ThingUID uid,
57             HeosGroupHandler heosGroupHandler) throws IOException, ReadException {
58         if (id == null) {
59             throw new HeosNotFoundException();
60         }
61         handleCommand(command, id);
62     }
63
64     @Override
65     public void handleBridgeCommand(Command command, ThingUID uid) {
66         // No such channel within bridge
67     }
68
69     private void handleCommand(Command command, String id) throws IOException, ReadException {
70         if (command instanceof RefreshType) {
71             eventListener.playerStateChangeEvent(getApi().getPlayState(id));
72             return;
73         }
74         switch (command.toString()) {
75             case "PLAY":
76             case "ON":
77                 getApi().play(id);
78                 break;
79             case "PAUSE":
80             case "OFF":
81                 if (shouldPause(id)) {
82                     getApi().pause(id);
83                 } else {
84                     getApi().stop(id);
85                 }
86                 break;
87             case "NEXT":
88                 getApi().next(id);
89                 break;
90             case "PREVIOUS":
91                 getApi().previous(id);
92                 break;
93         }
94     }
95
96     private boolean shouldPause(String id) {
97         Media applicableMedia = playingMediaCache.get(id);
98         if (applicableMedia == null || SONG.equals(applicableMedia.type)) {
99             return true;
100         } else {
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:
107                     return false;
108
109                 default:
110                     return true;
111             }
112         }
113     }
114
115     @Override
116     public void playerMediaChangeEvent(String pid, Media media) {
117         playingMediaCache.put(pid, media);
118     }
119 }