]> git.basschouten.com Git - openhab-addons.git/blob
c42f2c4e14da595d6f9c47dd3997147a3c6836ce
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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
14 package org.openhab.binding.flicbutton.internal.handler;
15
16 import java.util.Collection;
17 import java.util.Collections;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.openhab.core.thing.ThingStatusInfo;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.openhab.core.thing.binding.BridgeHandler;
27
28 /**
29  * The {@link ChildThingHandler} class is an abstract class for handlers that are dependent from a parent
30  * {@link BridgeHandler}.
31  *
32  * @author Patrick Fink - Initial contribution
33  * @param <BridgeHandlerType> The bridge type this child handler depends on
34  */
35 @NonNullByDefault
36 public abstract class ChildThingHandler<BridgeHandlerType extends BridgeHandler> extends BaseThingHandler {
37     private static final Collection<ThingStatus> DEFAULT_TOLERATED_BRIDGE_STATUSES = Collections
38             .singleton(ThingStatus.ONLINE);
39     protected boolean bridgeValid = false;
40     protected @Nullable BridgeHandlerType bridgeHandler;
41
42     public ChildThingHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public void initialize() {
48         setStatusBasedOnBridge();
49         if (getBridge() != null) {
50             linkBridge();
51         }
52     }
53
54     protected void linkBridge() {
55         try {
56             BridgeHandler bridgeHandlerUncasted = getBridge().getHandler();
57             bridgeHandler = (BridgeHandlerType) bridgeHandlerUncasted;
58         } catch (ClassCastException e) {
59             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge Type is invalid.");
60         }
61     }
62
63     protected void setStatusBasedOnBridge() {
64         setStatusBasedOnBridge(DEFAULT_TOLERATED_BRIDGE_STATUSES);
65     }
66
67     @Override
68     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
69         this.setStatusBasedOnBridge();
70     }
71
72     protected void setStatusBasedOnBridge(Collection<ThingStatus> toleratedBridgeStatuses) {
73         if (getBridge() != null) {
74             if (toleratedBridgeStatuses.contains(getBridge().getStatus())) {
75                 bridgeValid = true;
76             } else {
77                 bridgeValid = false;
78                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
79                         "Bridge in unsupported status: " + getBridge().getStatus());
80             }
81         } else {
82             bridgeValid = false;
83             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, "Bridge missing.");
84         }
85     }
86 }