]> git.basschouten.com Git - openhab-addons.git/blob
0d4d5a866f6ac6d5c88ec27a95ea71e2f1f766c9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.nikohomecontrol.internal.protocol;
14
15 import java.time.LocalDateTime;
16 import java.time.temporal.ChronoUnit;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc1.NhcThermostat1;
21 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc2.NhcThermostat2;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link NhcThermostat} class represents the thermostat Niko Home Control communication object. It contains all
27  * fields representing a Niko Home Control thermostat and has methods to set the thermostat in Niko Home Control and
28  * receive thermostat updates. Specific implementation are {@link NhcThermostat1} and {@link NhcThermostat2}.
29  *
30  * @author Mark Herwege - Initial Contribution
31  */
32 @NonNullByDefault
33 public abstract class NhcThermostat {
34
35     private final Logger logger = LoggerFactory.getLogger(NhcThermostat.class);
36
37     protected NikoHomeControlCommunication nhcComm;
38
39     protected String id;
40     protected String name;
41     protected @Nullable String location;
42     protected volatile int measured;
43     protected volatile int setpoint;
44     protected volatile int mode;
45     protected volatile int overrule;
46     protected volatile int overruletime;
47     protected volatile int ecosave;
48     protected volatile int demand;
49
50     private @Nullable LocalDateTime overruleStart;
51
52     private @Nullable NhcThermostatEvent eventHandler;
53
54     protected NhcThermostat(String id, String name, @Nullable String location, NikoHomeControlCommunication nhcComm) {
55         this.id = id;
56         this.name = name;
57         this.location = location;
58         this.nhcComm = nhcComm;
59     }
60
61     /**
62      * Update all values of the thermostat without touching the thermostat definition (id, name and location) and
63      * without changing the ThingHandler callback.
64      *
65      * @param measured current temperature in 0.1°C multiples
66      * @param setpoint the setpoint temperature in 0.1°C multiples
67      * @param mode thermostat mode 0 = day, 1 = night, 2 = eco, 3 = off, 4 = cool, 5 = prog1, 6 = prog2, 7 =
68      *            prog3
69      * @param overrule the overrule temperature in 0.1°C multiples
70      * @param overruletime in minutes
71      * @param ecosave
72      * @param demand 0 if no demand, > 0 if heating, < 0 if cooling
73      */
74     public void updateState(int measured, int setpoint, int mode, int overrule, int overruletime, int ecosave,
75             int demand) {
76         setMeasured(measured);
77         setSetpoint(setpoint);
78         setMode(mode);
79         setOverrule(overrule);
80         setOverruletime(overruletime);
81         setEcosave(ecosave);
82         setDemand(demand);
83
84         updateChannels();
85     }
86
87     /**
88      * Update overrule values of the thermostat without touching the thermostat definition (id, name and location) and
89      * without changing the ThingHandler callback.
90      *
91      * @param overrule the overrule temperature in 0.1°C multiples
92      * @param overruletime in minutes
93      */
94     public void updateState(int overrule, int overruletime) {
95         setOverrule(overrule);
96         setOverruletime(overruletime);
97
98         updateChannels();
99     }
100
101     /**
102      * Update overrule values of the thermostat without touching the thermostat definition (id, name and location) and
103      * without changing the ThingHandler callback.
104      *
105      * @param overrule the overrule temperature in 0.1°C multiples
106      * @param overruletime in minutes
107      */
108     public void updateState(int mode) {
109         setMode(mode);
110
111         updateChannels();
112     }
113
114     /**
115      * Method called when thermostat is removed from the Niko Home Control Controller.
116      */
117     public void thermostatRemoved() {
118         logger.debug("action removed {}, {}", id, name);
119         NhcThermostatEvent eventHandler = this.eventHandler;
120         if (eventHandler != null) {
121             eventHandler.thermostatRemoved();
122         }
123     }
124
125     private void updateChannels() {
126         NhcThermostatEvent handler = eventHandler;
127         if (handler != null) {
128             logger.debug("update channels for {}", id);
129             handler.thermostatEvent(measured, setpoint, mode, overrule, demand);
130         }
131     }
132
133     /**
134      * This method should be called when an object implementing the {@NhcThermostatEvent} interface is initialized.
135      * It keeps a record of the event handler in that object so it can be updated when the action receives an update
136      * from the Niko Home Control IP-interface.
137      *
138      * @param eventHandler
139      */
140     public void setEventHandler(NhcThermostatEvent eventHandler) {
141         this.eventHandler = eventHandler;
142     }
143
144     /**
145      * Get the id of the thermostat.
146      *
147      * @return the id
148      */
149     public String getId() {
150         return id;
151     }
152
153     /**
154      * Get name of thermostat.
155      *
156      * @return thermostat name
157      */
158     public String getName() {
159         return name;
160     }
161
162     /**
163      * Get location name of action.
164      *
165      * @return location name
166      */
167     public @Nullable String getLocation() {
168         return location;
169     }
170
171     /**
172      * Get measured temperature.
173      *
174      * @return measured temperature in 0.1°C multiples
175      */
176     public int getMeasured() {
177         return measured;
178     }
179
180     private void setMeasured(int measured) {
181         this.measured = measured;
182     }
183
184     /**
185      * @return the setpoint temperature in 0.1°C multiples
186      */
187     public int getSetpoint() {
188         return setpoint;
189     }
190
191     private void setSetpoint(int setpoint) {
192         this.setpoint = setpoint;
193     }
194
195     /**
196      * Get the thermostat mode.
197      *
198      * @return the mode:
199      *         0 = day, 1 = night, 2 = eco, 3 = off, 4 = cool, 5 = prog 1, 6 = prog 2, 7 = prog 3
200      */
201     public int getMode() {
202         return mode;
203     }
204
205     private void setMode(int mode) {
206         this.mode = mode;
207     }
208
209     /**
210      * Get the overrule temperature.
211      *
212      * @return the overrule temperature in 0.1°C multiples
213      */
214     public int getOverrule() {
215         if (overrule > 0) {
216             return overrule;
217         } else {
218             return setpoint;
219         }
220     }
221
222     private void setOverrule(int overrule) {
223         this.overrule = overrule;
224     }
225
226     /**
227      * Get the duration for an overrule temperature
228      *
229      * @return the overruletime in minutes
230      */
231     public int getOverruletime() {
232         return overruletime;
233     }
234
235     /**
236      * Set the duration for an overrule temperature
237      *
238      * @param overruletime the overruletime in minutes
239      */
240     private void setOverruletime(int overruletime) {
241         if (overruletime <= 0) {
242             stopOverrule();
243         } else if (overruletime != this.overruletime) {
244             startOverrule();
245         }
246         this.overruletime = overruletime;
247     }
248
249     /**
250      * @return the ecosave mode
251      */
252     public int getEcosave() {
253         return ecosave;
254     }
255
256     /**
257      * @param ecosave the ecosave mode to set
258      */
259     private void setEcosave(int ecosave) {
260         this.ecosave = ecosave;
261     }
262
263     /**
264      * @return the heating/cooling demand: 0 if no demand, >0 if heating, <0 if cooling
265      */
266     public int getDemand() {
267         return demand;
268     }
269
270     /**
271      * @param demand set the heating/cooling demand
272      */
273     private void setDemand(int demand) {
274         this.demand = demand;
275     }
276
277     /**
278      * Sends thermostat mode to Niko Home Control. This method is implemented in {@link NhcThermostat1} and
279      * {@link NhcThermostat2}.
280      *
281      * @param mode
282      */
283     public abstract void executeMode(int mode);
284
285     /**
286      * Sends thermostat setpoint to Niko Home Control. This method is implemented in {@link NhcThermostat1} and
287      * {@link NhcThermostat2}.
288      *
289      * @param overrule temperature to overrule the setpoint in 0.1°C multiples
290      * @param time time duration in min for overrule
291      */
292     public abstract void executeOverrule(int overrule, int overruletime);
293
294     /**
295      * @return remaining overrule time in minutes
296      */
297     public int getRemainingOverruletime() {
298         if (overruleStart == null) {
299             return 0;
300         } else {
301             // overruletime time max 23h59min, therefore can safely cast to int
302             return overruletime - (int) ChronoUnit.MINUTES.between(overruleStart, LocalDateTime.now());
303         }
304     }
305
306     /**
307      * Start a new overrule, this method is used to be able to calculate the remaining overrule time
308      */
309     private void startOverrule() {
310         overruleStart = LocalDateTime.now();
311     }
312
313     /**
314      * Reset overrule start
315      */
316     private void stopOverrule() {
317         overruleStart = null;
318     }
319 }