]> git.basschouten.com Git - openhab-addons.git/blob
a914db2e0d6af0d3b4b18f63034f78b9ba5693fc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.ojelectronics.internal;
14
15 import java.time.ZoneId;
16 import java.time.ZonedDateTime;
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.function.Consumer;
20
21 import javax.measure.quantity.Temperature;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.ojelectronics.internal.config.OJElectronicsThermostatConfiguration;
26 import org.openhab.binding.ojelectronics.internal.models.groups.Thermostat;
27 import org.openhab.core.library.types.DateTimeType;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OpenClosedType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.library.unit.SIUnits;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.binding.BaseThingHandler;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.RefreshType;
39
40 /**
41  * The {@link ThermostatHandler} is responsible for handling commands, which are
42  * sent to one of the channels.
43  *
44  * @author Christian Kittel - Initial contribution
45  */
46 @NonNullByDefault
47 public class ThermostatHandler extends BaseThingHandler {
48
49     private final String serialNumber;
50     private @Nullable Thermostat currentThermostat;
51     private static final Map<Integer, String> REGULATION_MODES = createRegulationMap();
52     private final Map<String, Consumer<Thermostat>> channelrefreshActions = createChannelRefreshActionMap();
53
54     /**
55      * Creates a new instance of {@link ThermostatHandler}
56      *
57      * @param thing Thing
58      */
59     public ThermostatHandler(Thing thing) {
60         super(thing);
61         serialNumber = getConfigAs(OJElectronicsThermostatConfiguration.class).serialNumber;
62     }
63
64     /**
65      * Gets the thing's serial number.
66      *
67      * @return serial number
68      */
69     public String getSerialNumber() {
70         return serialNumber;
71     }
72
73     /**
74      * Handles commands to this thing.
75      */
76     @Override
77     public void handleCommand(ChannelUID channelUID, Command command) {
78         if (command instanceof RefreshType) {
79             final Thermostat thermostat = currentThermostat;
80             if (thermostat != null && channelrefreshActions.containsKey(channelUID.getId())) {
81                 channelrefreshActions.get(channelUID.getId()).accept(thermostat);
82             }
83         }
84     }
85
86     /**
87      * Initializes the thing handler.
88      */
89     @Override
90     public void initialize() {
91         updateStatus(ThingStatus.ONLINE);
92     }
93
94     /**
95      * Sets the values after refreshing the thermostats values
96      *
97      * @param thermostat thermostat values
98      */
99     public void handleThermostatRefresh(Thermostat thermostat) {
100         currentThermostat = thermostat;
101         channelrefreshActions.forEach((channelUID, action) -> action.accept(thermostat));
102     }
103
104     private void updateManualSetpoint(Thermostat thermostat) {
105         updateState(BindingConstants.CHANNEL_OWD5_MANUALSETPOINT,
106                 new QuantityType<Temperature>(thermostat.manualModeSetpoint / (double) 100, SIUnits.CELSIUS));
107     }
108
109     private void updateBoostEndTime(Thermostat thermostat) {
110         updateState(BindingConstants.CHANNEL_OWD5_BOOSTENDTIME,
111                 new DateTimeType(ZonedDateTime.ofInstant(thermostat.boostEndTime.toInstant(), ZoneId.systemDefault())));
112     }
113
114     private void updateComfortEndTime(Thermostat thermostat) {
115         updateState(BindingConstants.CHANNEL_OWD5_COMFORTENDTIME, new DateTimeType(
116                 ZonedDateTime.ofInstant(thermostat.comfortEndTime.toInstant(), ZoneId.systemDefault())));
117     }
118
119     private void updateComfortSetpoint(Thermostat thermostat) {
120         updateState(BindingConstants.CHANNEL_OWD5_COMFORTSETPOINT,
121                 new QuantityType<Temperature>(thermostat.comfortSetpoint / (double) 100, SIUnits.CELSIUS));
122     }
123
124     private void updateRegulationMode(Thermostat thermostat) {
125         updateState(BindingConstants.CHANNEL_OWD5_REGULATIONMODE,
126                 StringType.valueOf(getRegulationMode(thermostat.regulationMode)));
127     }
128
129     private void updateThermostatName(Thermostat thermostat) {
130         updateState(BindingConstants.CHANNEL_OWD5_THERMOSTATNAME, StringType.valueOf(thermostat.thermostatName));
131     }
132
133     private void updateFloorTemperature(Thermostat thermostat) {
134         updateState(BindingConstants.CHANNEL_OWD5_FLOORTEMPERATURE,
135                 new QuantityType<Temperature>(thermostat.floorTemperature / (double) 100, SIUnits.CELSIUS));
136     }
137
138     private void updateRoomTemperature(Thermostat thermostat) {
139         updateState(BindingConstants.CHANNEL_OWD5_ROOMTEMPERATURE,
140                 new QuantityType<Temperature>(thermostat.roomTemperature / (double) 100, SIUnits.CELSIUS));
141     }
142
143     private void updateHeating(Thermostat thermostat) {
144         updateState(BindingConstants.CHANNEL_OWD5_HEATING,
145                 thermostat.heating ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
146     }
147
148     private void updateOnline(Thermostat thermostat) {
149         updateState(BindingConstants.CHANNEL_OWD5_ONLINE,
150                 thermostat.online ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
151     }
152
153     private void updateGroupId(Thermostat thermostat) {
154         updateState(BindingConstants.CHANNEL_OWD5_GROUPID, new DecimalType(thermostat.groupId));
155     }
156
157     private void updateGroupName(Thermostat thermostat) {
158         updateState(BindingConstants.CHANNEL_OWD5_GROUPNAME, StringType.valueOf(thermostat.groupName));
159     }
160
161     private String getRegulationMode(int regulationMode) {
162         return REGULATION_MODES.get(regulationMode);
163     }
164
165     private static Map<Integer, String> createRegulationMap() {
166         HashMap<Integer, String> map = new HashMap<>();
167         map.put(1, "auto");
168         map.put(2, "comfort");
169         map.put(3, "manual");
170         map.put(4, "vacation");
171         map.put(6, "frostProtection");
172         map.put(8, "boost");
173         map.put(9, "eco");
174         return map;
175     };
176
177     private Map<String, Consumer<Thermostat>> createChannelRefreshActionMap() {
178         HashMap<String, Consumer<Thermostat>> map = new HashMap<>();
179         map.put(BindingConstants.CHANNEL_OWD5_GROUPNAME, this::updateGroupName);
180         map.put(BindingConstants.CHANNEL_OWD5_GROUPID, this::updateGroupId);
181         map.put(BindingConstants.CHANNEL_OWD5_ONLINE, this::updateOnline);
182         map.put(BindingConstants.CHANNEL_OWD5_HEATING, this::updateHeating);
183         map.put(BindingConstants.CHANNEL_OWD5_ROOMTEMPERATURE, this::updateRoomTemperature);
184         map.put(BindingConstants.CHANNEL_OWD5_FLOORTEMPERATURE, this::updateFloorTemperature);
185         map.put(BindingConstants.CHANNEL_OWD5_THERMOSTATNAME, this::updateThermostatName);
186         map.put(BindingConstants.CHANNEL_OWD5_REGULATIONMODE, this::updateRegulationMode);
187         map.put(BindingConstants.CHANNEL_OWD5_COMFORTSETPOINT, this::updateComfortSetpoint);
188         map.put(BindingConstants.CHANNEL_OWD5_COMFORTENDTIME, this::updateComfortEndTime);
189         map.put(BindingConstants.CHANNEL_OWD5_BOOSTENDTIME, this::updateBoostEndTime);
190         map.put(BindingConstants.CHANNEL_OWD5_MANUALSETPOINT, this::updateManualSetpoint);
191         return map;
192     }
193 }