]> git.basschouten.com Git - openhab-addons.git/blob
277ea2908e21c608667d393d0044fd1b8d47a43f
[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.Set;
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 = Set.of(ThingStatus.ONLINE);
38     protected boolean bridgeValid = false;
39     protected @Nullable BridgeHandlerType bridgeHandler;
40
41     public ChildThingHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     public void initialize() {
47         setStatusBasedOnBridge();
48         if (getBridge() != null) {
49             linkBridge();
50         }
51     }
52
53     protected void linkBridge() {
54         try {
55             BridgeHandler bridgeHandlerUncasted = getBridge().getHandler();
56             bridgeHandler = (BridgeHandlerType) bridgeHandlerUncasted;
57         } catch (ClassCastException e) {
58             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge Type is invalid.");
59         }
60     }
61
62     protected void setStatusBasedOnBridge() {
63         setStatusBasedOnBridge(DEFAULT_TOLERATED_BRIDGE_STATUSES);
64     }
65
66     @Override
67     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
68         this.setStatusBasedOnBridge();
69     }
70
71     protected void setStatusBasedOnBridge(Collection<ThingStatus> toleratedBridgeStatuses) {
72         if (getBridge() != null) {
73             if (toleratedBridgeStatuses.contains(getBridge().getStatus())) {
74                 bridgeValid = true;
75             } else {
76                 bridgeValid = false;
77                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
78                         "Bridge in unsupported status: " + getBridge().getStatus());
79             }
80         } else {
81             bridgeValid = false;
82             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, "Bridge missing.");
83         }
84     }
85 }