2 * Copyright (c) 2010-2020 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.opensprinkler.internal.handler;
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;
28 * @author Florian Schmidt - Refactoring
31 public abstract class OpenSprinklerBaseHandler extends BaseThingHandler {
32 public OpenSprinklerBaseHandler(Thing thing) {
37 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
38 super.bridgeStatusChanged(bridgeStatusInfo);
40 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
41 updateStatus(ThingStatus.UNKNOWN);
46 protected OpenSprinklerApi getApi() {
47 Bridge bridge = getBridge();
51 BridgeHandler handler = bridge.getHandler();
52 if (!(handler instanceof OpenSprinklerBaseBridgeHandler)) {
56 return ((OpenSprinklerBaseBridgeHandler) handler).getApi();
57 } catch (IllegalStateException e) {
58 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
63 public void updateChannels() {
64 this.getThing().getChannels().forEach(channel -> {
65 updateChannel(channel.getUID());
67 if (getApi() != null) {
68 updateStatus(ThingStatus.ONLINE);
72 protected abstract void updateChannel(ChannelUID uid);