]> git.basschouten.com Git - openhab-addons.git/blob
85b4f916a327a4a58d8784d7146970829827e61f
[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.draytonwiser.internal.handler;
14
15 import static org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.*;
16
17 import java.util.List;
18
19 import javax.measure.quantity.Time;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.draytonwiser.internal.api.DraytonWiserApiException;
24 import org.openhab.binding.draytonwiser.internal.handler.HotWaterHandler.HotWaterData;
25 import org.openhab.binding.draytonwiser.internal.model.DraytonWiserDTO;
26 import org.openhab.binding.draytonwiser.internal.model.HotWaterDTO;
27 import org.openhab.binding.draytonwiser.internal.model.SystemDTO;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.State;
35
36 /**
37  * The {@link HotWaterHandler} is responsible for handling commands, which are
38  * sent to one of the channels.
39  *
40  * @author Andrew Schofield - Initial contribution
41  * @author Hilbrand Bouwkamp - Simplified handler to handle null data
42  */
43 @NonNullByDefault
44 public class HotWaterHandler extends DraytonWiserThingHandler<HotWaterData> {
45
46     public HotWaterHandler(final Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     protected void handleCommand(final String channelId, final Command command) throws DraytonWiserApiException {
52         if (command instanceof OnOffType && CHANNEL_MANUAL_MODE_STATE.equals(channelId)) {
53             setManualMode(OnOffType.ON.equals(command));
54         } else if (command instanceof OnOffType && CHANNEL_HOT_WATER_SETPOINT.equals(channelId)) {
55             setSetPoint(OnOffType.ON.equals(command));
56         } else if (command instanceof DecimalType && CHANNEL_HOT_WATER_BOOST_DURATION.equals(channelId)) {
57             setBoostDuration(Math.round((Float.parseFloat(command.toString()) * 60)));
58         }
59     }
60
61     @Override
62     protected void refresh() {
63         updateState(CHANNEL_HOT_WATER_OVERRIDE, this::getHotWaterOverride);
64         updateState(CHANNEL_HOTWATER_DEMAND_STATE, this::getHotWaterDemandState);
65         updateState(CHANNEL_MANUAL_MODE_STATE, this::getManualModeState);
66         updateState(CHANNEL_HOT_WATER_SETPOINT, this::getSetPointState);
67         updateState(CHANNEL_HOT_WATER_BOOSTED, this::getBoostedState);
68         updateState(CHANNEL_HOT_WATER_BOOST_REMAINING, this::getBoostRemainingState);
69     }
70
71     @Override
72     protected @Nullable HotWaterData collectData(final DraytonWiserDTO domainDTOProxy) {
73         final SystemDTO system = domainDTOProxy.getSystem();
74         final List<HotWaterDTO> hotWater = domainDTOProxy.getHotWater();
75
76         return system == null ? null : new HotWaterData(system, hotWater);
77     }
78
79     private State getHotWaterOverride() {
80         return OnOffType.from("ON".equalsIgnoreCase(getData().system.getHotWaterButtonOverrideState()));
81     }
82
83     private State getHotWaterDemandState() {
84         final List<HotWaterDTO> hotWater = getData().hotWater;
85         return OnOffType.from(!hotWater.isEmpty() && "ON".equalsIgnoreCase(hotWater.get(0).getHotWaterRelayState()));
86     }
87
88     private State getManualModeState() {
89         final List<HotWaterDTO> hotWater = getData().hotWater;
90         return OnOffType.from(!hotWater.isEmpty() && "MANUAL".equalsIgnoreCase(hotWater.get(0).getMode()));
91     }
92
93     private State getSetPointState() {
94         final List<HotWaterDTO> hotWater = getData().hotWater;
95         return OnOffType.from(!hotWater.isEmpty() && "ON".equalsIgnoreCase(hotWater.get(0).getWaterHeatingState()));
96     }
97
98     private void setManualMode(final boolean manualMode) throws DraytonWiserApiException {
99         getApi().setHotWaterManualMode(manualMode);
100     }
101
102     private void setSetPoint(final boolean setPointMode) throws DraytonWiserApiException {
103         getApi().setHotWaterSetPoint(setPointMode ? 1100 : -200);
104     }
105
106     private void setBoostDuration(final int durationMinutes) throws DraytonWiserApiException {
107         if (durationMinutes > 0) {
108             getApi().setHotWaterBoostActive(durationMinutes);
109         } else {
110             getApi().setHotWaterBoostInactive();
111         }
112     }
113
114     private State getBoostedState() {
115         if (getData().hotWater.size() >= 1) {
116             final HotWaterDTO firstChannel = getData().hotWater.get(0);
117
118             if (firstChannel.getOverrideTimeoutUnixTime() != null
119                     && !"NONE".equalsIgnoreCase(firstChannel.getOverrideType())) {
120                 return OnOffType.ON;
121             }
122         }
123
124         updateState(CHANNEL_HOT_WATER_BOOST_DURATION, DecimalType.ZERO);
125
126         return OnOffType.OFF;
127     }
128
129     private State getBoostRemainingState() {
130         if (getData().hotWater.size() >= 1) {
131             final HotWaterDTO firstChannel = getData().hotWater.get(0);
132             final Integer overrideTimeout = firstChannel.getOverrideTimeoutUnixTime();
133
134             if (overrideTimeout != null && !"NONE".equalsIgnoreCase(firstChannel.getOverrideType())) {
135                 return new QuantityType<Time>(overrideTimeout - (System.currentTimeMillis() / 1000L), Units.SECOND);
136             }
137         }
138         return new QuantityType<Time>(0, Units.SECOND);
139     }
140
141     static class HotWaterData {
142         public final SystemDTO system;
143         public final List<HotWaterDTO> hotWater;
144
145         public HotWaterData(final SystemDTO system, final List<HotWaterDTO> hotWater) {
146             this.system = system;
147             this.hotWater = hotWater;
148         }
149     }
150 }