]> git.basschouten.com Git - openhab-addons.git/blob
c5d533073ec313d5145694522477cfd61fd6d774
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.russound.internal.rio;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
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;
29
30 import com.google.gson.Gson;
31
32 /**
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}
37  *
38  * @author Tim Roberts - Initial contribution
39  */
40 public abstract class AbstractBridgeHandler<E extends AbstractRioProtocol> extends BaseBridgeHandler
41         implements RioCallbackHandler {
42     /**
43      * The protocol handler for this base
44      */
45     private E protocolHandler;
46
47     /**
48      * Creates the handler from the given {@link Bridge}
49      *
50      * @param bridge a non-null {@link Bridge}
51      */
52     protected AbstractBridgeHandler(Bridge bridge) {
53         super(bridge);
54     }
55
56     /**
57      * Sets a new {@link AbstractRioProtocol} as the current protocol handler. If one already exists, it will be
58      * disposed of first.
59      *
60      * @param newProtocolHandler a, possibly null, {@link AbstractRioProtocol}
61      */
62     protected void setProtocolHandler(E newProtocolHandler) {
63         if (protocolHandler != null) {
64             protocolHandler.dispose();
65         }
66         protocolHandler = newProtocolHandler;
67     }
68
69     /**
70      * Get's the {@link AbstractRioProtocol} handler. May be null if none currently exists
71      *
72      * @return a {@link AbstractRioProtocol} handler or null if none exists
73      */
74     protected E getProtocolHandler() {
75         return protocolHandler;
76     }
77
78     /**
79      * Overridden to simply get the protocol handler's {@link RioHandlerCallback}
80      *
81      * @return the {@link RioHandlerCallback} or null if not found
82      */
83     @Override
84     public RioHandlerCallback getRioHandlerCallback() {
85         final E protocolHandler = getProtocolHandler();
86         return protocolHandler == null ? null : protocolHandler.getCallback();
87     }
88
89     /**
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}
92      *
93      * @return a {@link SocketSession} or null if none exists
94      */
95     @SuppressWarnings("rawtypes")
96     public SocketSession getSocketSession() {
97         final Bridge bridge = getBridge();
98         if (bridge.getHandler() instanceof AbstractBridgeHandler) {
99             return ((AbstractBridgeHandler) bridge.getHandler()).getSocketSession();
100         }
101         return null;
102     }
103
104     /**
105      * Returns the {@link RioPresetsProtocol} for this {@link Bridge}. The default implementation is to look in the
106      * parent
107      * {@link #getPresetsProtocol()} for the {@link RioPresetsProtocol}
108      *
109      * @return a {@link RioPresetsProtocol} or null if none exists
110      */
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();
116         }
117         return null;
118     }
119
120     /**
121      * Returns the {@link RioSystemFavoritesProtocol} for this {@link Bridge}. The default implementation is to look in
122      * the
123      * parent
124      * {@link #getPresetsProtocol()} for the {@link RioSystemFavoritesProtocol}
125      *
126      * @return a {@link RioSystemFavoritesProtocol} or null if none exists
127      */
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();
133         }
134         return null;
135     }
136
137     /**
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.
141      */
142     @Override
143     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
144         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
145             reconnect();
146         } else {
147             disconnect();
148         }
149         super.bridgeStatusChanged(bridgeStatusInfo);
150     }
151
152     /**
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.
155      *
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
160      */
161     protected <H extends RioNamedHandler> void refreshNamedHandler(Gson gson, Class<H> clazz, String channelId) {
162         if (gson == null) {
163             throw new IllegalArgumentException("gson cannot be null");
164         }
165         if (clazz == null) {
166             throw new IllegalArgumentException("clazz cannot be null");
167         }
168         if (StringUtils.isEmpty(channelId)) {
169             throw new IllegalArgumentException("channelId cannot be null or empty");
170         }
171
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()));
180                     }
181                 }
182             }
183         }
184
185         final String json = gson.toJson(ids);
186         updateState(channelId, new StringType(json));
187     }
188
189     /**
190      * Overrides the base method to remove any state linked to the {@lin ChannelUID} from the
191      * {@link StatefulHandlerCallback}
192      */
193     @Override
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());
199         }
200         super.channelUnlinked(channelUID);
201     }
202
203     /**
204      * Base method to reconnect the handler. The base implementation will simply {@link #disconnect()} then
205      * {@link #initialize()} the handler.
206      */
207     protected void reconnect() {
208         disconnect();
209         initialize();
210     }
211
212     /**
213      * Base method to disconnect the handler. This implementation will simply call
214      * {@link #setProtocolHandler(AbstractRioProtocol)} to null.
215      */
216     protected void disconnect() {
217         setProtocolHandler(null);
218     }
219
220     /**
221      * Overrides the dispose to call the {@link #disconnect()} method to disconnect the handler
222      */
223     @Override
224     public void dispose() {
225         disconnect();
226         super.dispose();
227     }
228
229     /**
230      * Private class that simply stores an ID & Name. This class is solely used to create a json result like "{id:1,
231      * name:'stuff'}"
232      *
233      * @author Tim Roberts
234      */
235     private class IdName {
236         @SuppressWarnings("unused")
237         private final int id;
238         @SuppressWarnings("unused")
239         private final String name;
240
241         public IdName(int id, String name) {
242             this.id = id;
243             this.name = name;
244         }
245     }
246 }