]> git.basschouten.com Git - openhab-addons.git/blob
b705fe0d37cd4adc7208e27b8abd5e465b8a7dd7
[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.PID;
16
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.ChannelUID;
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 HeosChannelHandlerPlayerSelect} handles the player selection channel command
32  * from the implementing thing.
33  *
34  * @author Johannes Einig - Initial contribution
35  */
36 @NonNullByDefault
37 public class HeosChannelHandlerPlayerSelect extends BaseHeosChannelHandler {
38     protected final Logger logger = LoggerFactory.getLogger(HeosChannelHandlerPlayerSelect.class);
39
40     private final ChannelUID channelUID;
41
42     public HeosChannelHandlerPlayerSelect(ChannelUID channelUID, HeosBridgeHandler bridge) {
43         super(bridge);
44         this.channelUID = channelUID;
45     }
46
47     @Override
48     public void handlePlayerCommand(Command command, String id, ThingUID uid) {
49         // not used on player
50     }
51
52     @Override
53     public void handleGroupCommand(Command command, @Nullable String id, ThingUID uid,
54             HeosGroupHandler heosGroupHandler) {
55         // not used on group
56     }
57
58     @Override
59     public void handleBridgeCommand(Command command, ThingUID uid) {
60         if (command instanceof RefreshType) {
61             return;
62         }
63         Channel channel = bridge.getThing().getChannel(channelUID.getId());
64         if (channel == null) {
65             logger.debug("Channel {} not found", channelUID);
66             return;
67         }
68
69         List<String[]> selectedPlayerList = bridge.getSelectedPlayerList();
70
71         if (command.equals(OnOffType.ON)) {
72             String[] selectedPlayerInfo = new String[2];
73             selectedPlayerInfo[0] = channel.getProperties().get(PID);
74             selectedPlayerInfo[1] = channelUID.getId();
75             selectedPlayerList.add(selectedPlayerInfo);
76         } else if (!selectedPlayerList.isEmpty()) {
77             int indexPlayerChannel = -1;
78             for (int i = 0; i < selectedPlayerList.size(); i++) {
79                 String localPID = selectedPlayerList.get(i)[0];
80                 if (localPID.equals(channel.getProperties().get(PID))) {
81                     indexPlayerChannel = i;
82                 }
83             }
84             selectedPlayerList.remove(indexPlayerChannel);
85             bridge.setSelectedPlayerList(selectedPlayerList);
86         }
87     }
88 }