2 * Copyright (c) 2010-2020 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.russound.internal.rio;
15 import java.util.ArrayList;
16 import java.util.List;
18 import org.apache.commons.lang.StringUtils;
19 import org.openhab.binding.russound.internal.net.SocketSession;
20 import org.openhab.binding.russound.internal.net.SocketSessionListener;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusInfo;
27 import org.openhab.core.thing.binding.BaseBridgeHandler;
28 import org.openhab.core.thing.binding.ThingHandler;
30 import com.google.gson.Gson;
33 * Represents the abstract base to a {@link BaseBridgeHandler} for common functionality to all Bridges. This abstract
34 * base provides management of the {@link AbstractRioProtocol}, parent {@link #bridgeStatusChanged(ThingStatusInfo)}
35 * event processing and the ability to get the current {@link SocketSession}.
36 * {@link #sendCommand(String)} and responses will be received on any {@link SocketSessionListener}
38 * @author Tim Roberts - Initial contribution
40 public abstract class AbstractBridgeHandler<E extends AbstractRioProtocol> extends BaseBridgeHandler
41 implements RioCallbackHandler {
43 * The protocol handler for this base
45 private E protocolHandler;
48 * Creates the handler from the given {@link Bridge}
50 * @param bridge a non-null {@link Bridge}
52 protected AbstractBridgeHandler(Bridge bridge) {
57 * Sets a new {@link AbstractRioProtocol} as the current protocol handler. If one already exists, it will be
60 * @param newProtocolHandler a, possibly null, {@link AbstractRioProtocol}
62 protected void setProtocolHandler(E newProtocolHandler) {
63 if (protocolHandler != null) {
64 protocolHandler.dispose();
66 protocolHandler = newProtocolHandler;
70 * Get's the {@link AbstractRioProtocol} handler. May be null if none currently exists
72 * @return a {@link AbstractRioProtocol} handler or null if none exists
74 protected E getProtocolHandler() {
75 return protocolHandler;
79 * Overridden to simply get the protocol handler's {@link RioHandlerCallback}
81 * @return the {@link RioHandlerCallback} or null if not found
84 public RioHandlerCallback getRioHandlerCallback() {
85 final E protocolHandler = getProtocolHandler();
86 return protocolHandler == null ? null : protocolHandler.getCallback();
90 * Returns the {@link SocketSession} for this {@link Bridge}. The default implementation is to look in the parent
91 * {@link #getBridge()} for the {@link SocketSession}
93 * @return a {@link SocketSession} or null if none exists
95 @SuppressWarnings("rawtypes")
96 public SocketSession getSocketSession() {
97 final Bridge bridge = getBridge();
98 if (bridge.getHandler() instanceof AbstractBridgeHandler) {
99 return ((AbstractBridgeHandler) bridge.getHandler()).getSocketSession();
105 * Returns the {@link RioPresetsProtocol} for this {@link Bridge}. The default implementation is to look in the
107 * {@link #getPresetsProtocol()} for the {@link RioPresetsProtocol}
109 * @return a {@link RioPresetsProtocol} or null if none exists
111 @SuppressWarnings("rawtypes")
112 public RioPresetsProtocol getPresetsProtocol() {
113 final Bridge bridge = getBridge();
114 if (bridge != null && bridge.getHandler() instanceof AbstractBridgeHandler) {
115 return ((AbstractBridgeHandler) bridge.getHandler()).getPresetsProtocol();
121 * Returns the {@link RioSystemFavoritesProtocol} for this {@link Bridge}. The default implementation is to look in
124 * {@link #getPresetsProtocol()} for the {@link RioSystemFavoritesProtocol}
126 * @return a {@link RioSystemFavoritesProtocol} or null if none exists
128 @SuppressWarnings("rawtypes")
129 public RioSystemFavoritesProtocol getSystemFavoritesHandler() {
130 final Bridge bridge = getBridge();
131 if (bridge != null && bridge.getHandler() instanceof AbstractBridgeHandler) {
132 return ((AbstractBridgeHandler) bridge.getHandler()).getSystemFavoritesHandler();
138 * Overrides the base to initialize or dispose the handler based on the parent bridge status changing. If offline,
139 * {@link #dispose()} will be called instead. We then try to reinitialize ourselves when the bridge goes back online
140 * via the {@link #retryBridge()} method.
143 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
144 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
149 super.bridgeStatusChanged(bridgeStatusInfo);
153 * Creates an "{id:x, name: 'xx'}" json string from all {@link RioNamedHandler} for a specific class and sends that
154 * result to a channel id.
156 * @param gson a non-null {@link Gson} to use
157 * @param clazz a non-null class that the results will be for
158 * @param channelId a non-null, non-empty channel identifier to send the results to
159 * @throws IllegalArgumentException if any argument is null or empty
161 protected <H extends RioNamedHandler> void refreshNamedHandler(Gson gson, Class<H> clazz, String channelId) {
163 throw new IllegalArgumentException("gson cannot be null");
166 throw new IllegalArgumentException("clazz cannot be null");
168 if (StringUtils.isEmpty(channelId)) {
169 throw new IllegalArgumentException("channelId cannot be null or empty");
172 final List<IdName> ids = new ArrayList<>();
173 for (Thing thn : getThing().getThings()) {
174 if (thn.getStatus() == ThingStatus.ONLINE) {
175 final ThingHandler handler = thn.getHandler();
176 if (handler != null && handler.getClass().isAssignableFrom(clazz)) {
177 final RioNamedHandler namedHandler = (RioNamedHandler) handler;
178 if (namedHandler.getId() > 0) { // 0 returned when handler is initializing
179 ids.add(new IdName(namedHandler.getId(), namedHandler.getName()));
185 final String json = gson.toJson(ids);
186 updateState(channelId, new StringType(json));
190 * Overrides the base method to remove any state linked to the {@lin ChannelUID} from the
191 * {@link StatefulHandlerCallback}
194 public void channelUnlinked(ChannelUID channelUID) {
195 // Remove any state when unlinking (that way if it is relinked - we get it)
196 final RioHandlerCallback callback = getProtocolHandler().getCallback();
197 if (callback instanceof StatefulHandlerCallback) {
198 ((StatefulHandlerCallback) callback).removeState(channelUID.getId());
200 super.channelUnlinked(channelUID);
204 * Base method to reconnect the handler. The base implementation will simply {@link #disconnect()} then
205 * {@link #initialize()} the handler.
207 protected void reconnect() {
213 * Base method to disconnect the handler. This implementation will simply call
214 * {@link #setProtocolHandler(AbstractRioProtocol)} to null.
216 protected void disconnect() {
217 setProtocolHandler(null);
221 * Overrides the dispose to call the {@link #disconnect()} method to disconnect the handler
224 public void dispose() {
230 * Private class that simply stores an ID & Name. This class is solely used to create a json result like "{id:1,
233 * @author Tim Roberts
235 private class IdName {
236 @SuppressWarnings("unused")
237 private final int id;
238 @SuppressWarnings("unused")
239 private final String name;
241 public IdName(int id, String name) {