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;
15 import static org.openhab.binding.heos.internal.HeosBindingConstants.*;
17 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.heos.internal.handler.HeosBridgeHandler;
23 import org.openhab.binding.heos.internal.handler.HeosChannelHandler;
24 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerBuildGroup;
25 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerClearQueue;
26 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerControl;
27 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerFavorite;
28 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerGrouping;
29 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerInputs;
30 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerMute;
31 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerNowPlaying;
32 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerPlayURL;
33 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerPlayerSelect;
34 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerPlaylist;
35 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerQueue;
36 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerRawCommand;
37 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerReboot;
38 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerRepeatMode;
39 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerShuffleMode;
40 import org.openhab.binding.heos.internal.handler.HeosChannelHandlerVolume;
41 import org.openhab.binding.heos.internal.handler.HeosDynamicStateDescriptionProvider;
42 import org.openhab.binding.heos.internal.resources.HeosEventListener;
43 import org.openhab.core.thing.ChannelUID;
44 import org.openhab.core.thing.type.ChannelTypeUID;
47 * The {@link HeosChannelHandlerFactory} is responsible for creating and returning
48 * of the single handler for each channel of the single things.
49 * It also stores already created handler for further use.
51 * @author Johannes Einig - Initial contribution
54 public class HeosChannelHandlerFactory {
55 private final HeosBridgeHandler bridge;
56 private final HeosDynamicStateDescriptionProvider heosDynamicStateDescriptionProvider;
57 private final Map<ChannelUID, HeosChannelHandler> handlerStorageMap = new HashMap<>();
59 public HeosChannelHandlerFactory(HeosBridgeHandler bridge,
60 HeosDynamicStateDescriptionProvider heosDynamicStateDescriptionProvider) {
62 this.heosDynamicStateDescriptionProvider = heosDynamicStateDescriptionProvider;
65 public @Nullable HeosChannelHandler getChannelHandler(ChannelUID channelUID, HeosEventListener eventListener,
66 @Nullable ChannelTypeUID channelTypeUID) {
67 if (handlerStorageMap.containsKey(channelUID)) {
68 return handlerStorageMap.get(channelUID);
70 HeosChannelHandler handler = createNewChannelHandler(channelUID, eventListener, channelTypeUID);
71 if (handler != null) {
72 handlerStorageMap.put(channelUID, handler);
78 private @Nullable HeosChannelHandler createNewChannelHandler(ChannelUID channelUID, HeosEventListener eventListener,
79 @Nullable ChannelTypeUID channelTypeUID) {
80 switch (channelUID.getId()) {
82 return new HeosChannelHandlerControl(eventListener, bridge);
84 return new HeosChannelHandlerVolume(eventListener, bridge);
86 return new HeosChannelHandlerMute(eventListener, bridge);
88 return new HeosChannelHandlerInputs(eventListener, bridge);
89 case CH_ID_REPEAT_MODE:
90 return new HeosChannelHandlerRepeatMode(eventListener, bridge);
91 case CH_ID_SHUFFLE_MODE:
92 return new HeosChannelHandlerShuffleMode(eventListener, bridge);
99 return new HeosChannelHandlerNowPlaying(eventListener, bridge);
101 return new HeosChannelHandlerQueue(heosDynamicStateDescriptionProvider, bridge);
102 case CH_ID_CLEAR_QUEUE:
103 return new HeosChannelHandlerClearQueue(bridge);
106 return new HeosChannelHandlerPlayURL(bridge);
108 return new HeosChannelHandlerGrouping(bridge);
109 case CH_ID_RAW_COMMAND:
110 return new HeosChannelHandlerRawCommand(eventListener, bridge);
112 return new HeosChannelHandlerReboot(bridge);
113 case CH_ID_BUILDGROUP:
114 return new HeosChannelHandlerBuildGroup(channelUID, bridge);
115 case CH_ID_PLAYLISTS:
116 return new HeosChannelHandlerPlaylist(heosDynamicStateDescriptionProvider, bridge);
117 case CH_ID_FAVORITES:
118 return new HeosChannelHandlerFavorite(heosDynamicStateDescriptionProvider, bridge);
121 // nothing to handle, we receive updates automatically
125 if (channelTypeUID != null) {
126 if (CH_TYPE_PLAYER.equals(channelTypeUID)) {
127 return new HeosChannelHandlerPlayerSelect(channelUID, bridge);