]> git.basschouten.com Git - openhab-addons.git/blob
2eac9354f4729c4271d5b81cb8de0d1998f4a86b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.opensprinkler.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApi;
18 import org.openhab.core.thing.Bridge;
19 import org.openhab.core.thing.ChannelUID;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.openhab.core.thing.ThingStatusInfo;
24 import org.openhab.core.thing.binding.BaseThingHandler;
25 import org.openhab.core.thing.binding.BridgeHandler;
26
27 /**
28  * @author Florian Schmidt - Refactoring
29  */
30 @NonNullByDefault
31 public abstract class OpenSprinklerBaseHandler extends BaseThingHandler {
32     public OpenSprinklerBaseHandler(Thing thing) {
33         super(thing);
34     }
35
36     @Override
37     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
38         super.bridgeStatusChanged(bridgeStatusInfo);
39
40         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
41             updateStatus(ThingStatus.UNKNOWN);
42         }
43     }
44
45     @Nullable
46     protected OpenSprinklerApi getApi() {
47         Bridge bridge = getBridge();
48         if (bridge == null) {
49             return null;
50         }
51         BridgeHandler handler = bridge.getHandler();
52         if (!(handler instanceof OpenSprinklerBaseBridgeHandler)) {
53             return null;
54         }
55         try {
56             return ((OpenSprinklerBaseBridgeHandler) handler).getApi();
57         } catch (IllegalStateException e) {
58             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
59             return null;
60         }
61     }
62
63     public void updateChannels() {
64         this.getThing().getChannels().forEach(channel -> {
65             updateChannel(channel.getUID());
66         });
67         if (getApi() != null) {
68             updateStatus(ThingStatus.ONLINE);
69         }
70     }
71
72     protected abstract void updateChannel(ChannelUID uid);
73 }