2 * Copyright (c) 2010-2023 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 static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.MAX_TIME_SECONDS;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.opensprinkler.internal.api.OpenSprinklerApi;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.unit.Units;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.thing.binding.BaseThingHandler;
30 import org.openhab.core.types.Command;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * @author Chris Graham - Initial contribution
36 * @author Florian Schmidt - Refactoring
39 public abstract class OpenSprinklerBaseHandler extends BaseThingHandler {
40 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
41 protected BigDecimal nextDurationTime = MAX_TIME_SECONDS;
44 OpenSprinklerHttpBridgeHandler bridgeHandler;
46 public OpenSprinklerBaseHandler(Thing thing) {
50 protected @Nullable OpenSprinklerApi getApi() {
51 OpenSprinklerHttpBridgeHandler localBridge = bridgeHandler;
52 if (localBridge == null) {
56 return localBridge.getApi();
57 } catch (IllegalStateException e) {
58 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, e.getMessage());
63 public void updateChannels() {
64 this.getThing().getChannels().forEach(channel -> {
65 updateChannel(channel.getUID());
67 if (getApi() != null) {
68 updateStatus(ThingStatus.ONLINE);
72 protected void handleNextDurationCommand(ChannelUID channelUID, Command command) {
73 if (!(command instanceof QuantityType<?>)) {
74 logger.warn("Ignoring implausible non-QuantityType command for NEXT_DURATION");
77 QuantityType<?> quantity = (QuantityType<?>) command;
78 quantity = quantity.toUnit(Units.SECOND);
79 if (quantity != null) {
80 nextDurationTime = quantity.toBigDecimal();
81 updateState(channelUID, quantity);
85 protected BigDecimal nextDurationValue() {
86 return nextDurationTime;
90 public void initialize() {
91 Bridge bridge = getBridge();
93 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "No HTTP Bridge thing selected");
96 bridgeHandler = (OpenSprinklerHttpBridgeHandler) bridge.getHandler();
97 updateStatus(ThingStatus.ONLINE);
100 protected abstract void updateChannel(ChannelUID uid);