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.russound.internal.rio;
15 import org.openhab.binding.russound.internal.net.SocketSession;
16 import org.openhab.binding.russound.internal.net.SocketSessionListener;
17 import org.openhab.core.thing.Bridge;
18 import org.openhab.core.thing.ChannelUID;
19 import org.openhab.core.thing.Thing;
20 import org.openhab.core.thing.ThingStatus;
21 import org.openhab.core.thing.ThingStatusInfo;
22 import org.openhab.core.thing.binding.BaseThingHandler;
25 * Represents the abstract base to a {@link BaseThingHandler} for common functionality to all Things. This abstract
26 * base provides management of the {@link AbstractRioProtocol}, parent {@link #bridgeStatusChanged(ThingStatusInfo)}
27 * event processing and the ability to get the current {@link SocketSession}.
28 * {@link #sendCommand(String)} and responses will be received on any {@link SocketSessionListener}
30 * @author Tim Roberts - Initial contribution
32 public abstract class AbstractThingHandler<E extends AbstractRioProtocol> extends BaseThingHandler
33 implements RioCallbackHandler {
35 * The protocol handler for this base
37 private E protocolHandler;
40 * Creates the handler from the given {@link Thing}
42 * @param thing a non-null {@link Thing}
44 protected AbstractThingHandler(Thing thing) {
49 * Sets a new {@link AbstractRioProtocol} as the current protocol handler. If one already exists, it will be
52 * @param newProtocolHandler a, possibly null, {@link AbstractRioProtocol}
54 protected void setProtocolHandler(E newProtocolHandler) {
55 if (protocolHandler != null) {
56 protocolHandler.dispose();
58 protocolHandler = newProtocolHandler;
62 * Get's the {@link AbstractRioProtocol} handler. May be null if none currently exists
64 * @return an {@link AbstractRioProtocol} handler or null if none exists
66 protected E getProtocolHandler() {
67 return protocolHandler;
71 * Overridden to simply get the protocol handler's {@link RioHandlerCallback}
73 * @return the {@link RioHandlerCallback} or null if not found
76 public RioHandlerCallback getRioHandlerCallback() {
77 final E protocolHandler = getProtocolHandler();
78 return protocolHandler == null ? null : protocolHandler.getCallback();
82 * Returns the {@link SocketSession} for this {@link Bridge}. The default implementation is to look in the parent
83 * {@link #getBridge()} for the {@link SocketSession}
85 * @return a {@link SocketSession} or null if none exists
87 @SuppressWarnings("rawtypes")
88 protected SocketSession getSocketSession() {
89 final Bridge bridge = getBridge();
90 if (bridge != null && bridge.getHandler() instanceof AbstractBridgeHandler) {
91 return ((AbstractBridgeHandler) bridge.getHandler()).getSocketSession();
97 * Overrides the base to initialize or dispose the handler based on the parent bridge status changing. If offline,
98 * {@link #dispose()} will be called instead. We then try to reinitialize ourselves when the bridge goes back online
99 * via the {@link #retryBridge()} method.
102 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
103 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
108 super.bridgeStatusChanged(bridgeStatusInfo);
112 * Overrides the base method to remove any state linked to the {@link ChannelUID} from the
113 * {@link StatefulHandlerCallback}
116 public void channelUnlinked(ChannelUID channelUID) {
117 // Remove any state when unlinking (that way if it is relinked - we get it)
118 final RioHandlerCallback callback = getProtocolHandler().getCallback();
119 if (callback instanceof StatefulHandlerCallback handlerCallback) {
120 handlerCallback.removeState(channelUID.getId());
122 super.channelUnlinked(channelUID);
126 * Base method to reconnect the handler. The base implementation will simply {@link #disconnect()} then
127 * {@link #initialize()} the handler.
129 protected void reconnect() {
135 * Base method to disconnect the handler. This implementation will simply call
136 * {@link #setProtocolHandler(AbstractRioProtocol)} to null.
138 protected void disconnect() {
139 setProtocolHandler(null);
143 * Overrides the dispose to call the {@link #disconnect()} method to disconnect the handler
146 public void dispose() {