]> git.basschouten.com Git - openhab-addons.git/blob
5a8698c32877bdfcd89a0f4389ccb345137ff802
[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 package org.openhab.binding.opensprinkler.internal.handler;
14
15 import static org.openhab.binding.opensprinkler.internal.OpenSprinklerBindingConstants.MAX_TIME_SECONDS;
16
17 import java.math.BigDecimal;
18
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;
33
34 /**
35  * @author Chris Graham - Initial contribution
36  * @author Florian Schmidt - Refactoring
37  */
38 @NonNullByDefault
39 public abstract class OpenSprinklerBaseHandler extends BaseThingHandler {
40     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
41     protected BigDecimal nextDurationTime = MAX_TIME_SECONDS;
42
43     @Nullable
44     OpenSprinklerHttpBridgeHandler bridgeHandler;
45
46     public OpenSprinklerBaseHandler(Thing thing) {
47         super(thing);
48     }
49
50     protected @Nullable OpenSprinklerApi getApi() {
51         OpenSprinklerHttpBridgeHandler localBridge = bridgeHandler;
52         if (localBridge == null) {
53             return null;
54         }
55         try {
56             return localBridge.getApi();
57         } catch (IllegalStateException e) {
58             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, e.getMessage());
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 void handleNextDurationCommand(ChannelUID channelUID, Command command) {
73         if (!(command instanceof QuantityType<?>)) {
74             logger.warn("Ignoring implausible non-QuantityType command for NEXT_DURATION");
75             return;
76         }
77         QuantityType<?> quantity = (QuantityType<?>) command;
78         quantity = quantity.toUnit(Units.SECOND);
79         if (quantity != null) {
80             nextDurationTime = quantity.toBigDecimal();
81             updateState(channelUID, quantity);
82         }
83     }
84
85     protected BigDecimal nextDurationValue() {
86         return nextDurationTime;
87     }
88
89     @Override
90     public void initialize() {
91         Bridge bridge = getBridge();
92         if (bridge == null) {
93             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, "No HTTP Bridge thing selected");
94             return;
95         }
96         bridgeHandler = (OpenSprinklerHttpBridgeHandler) bridge.getHandler();
97         updateStatus(ThingStatus.ONLINE);
98     }
99
100     protected abstract void updateChannel(ChannelUID uid);
101 }