]> git.basschouten.com Git - openhab-addons.git/blob
92c0dc3fdaed836fcfaf81f002cba657a7e6dc1e
[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.qbus.internal.protocol;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.qbus.internal.handler.QbusThermostatHandler;
20
21 /**
22  * The {@link QbusThermostat} class represents the thermostat Qbus communication object. It contains all
23  * fields representing a Qbus thermostat and has methods to set the thermostat mode and setpoint in Qbus and
24  * receive thermostat updates.
25  *
26  * @author Koen Schockaert - Initial Contribution
27  */
28
29 @NonNullByDefault
30 public final class QbusThermostat {
31
32     private @Nullable QbusCommunication qComm;
33
34     private Integer id;
35     private double measured = 0.0;
36     private double setpoint = 0.0;
37     private @Nullable Integer mode;
38
39     private @Nullable QbusThermostatHandler thingHandler;
40
41     QbusThermostat(Integer id) {
42         this.id = id;
43     }
44
45     /**
46      * This method should be called if the ThingHandler for the thing corresponding to the termostat is initialized.
47      * It keeps a record of the thing handler in this object so the thing can be updated when
48      * the thermostat receives an update from the Qbus client.
49      *
50      * @param handler
51      */
52     public void setThingHandler(QbusThermostatHandler handler) {
53         this.thingHandler = handler;
54     }
55
56     /**
57      * This method sets a pointer to the qComm THERMOSTAT of class {@link QbusCommuncation}.
58      * This is then used to be able to call back the sendCommand method in this class to send a command to the
59      * Qbus client.
60      *
61      * @param qComm
62      */
63     public void setQComm(QbusCommunication qComm) {
64         this.qComm = qComm;
65     }
66
67     /**
68      * Update all values of the Thermostat
69      *
70      * @param measured current temperature in 1°C multiples
71      * @param setpoint the setpoint temperature in 1°C multiples
72      * @param mode 0="Manual", 1="Freeze", 2="Economic", 3="Comfort", 4="Night"
73      */
74     public void updateState(Double measured, Double setpoint, Integer mode) {
75         this.measured = measured;
76         this.setpoint = setpoint;
77         this.mode = mode;
78
79         QbusThermostatHandler handler = this.thingHandler;
80         if (handler != null) {
81             handler.handleStateUpdate(this);
82         }
83     }
84
85     /**
86      * Get measured temperature of the Thermostat.
87      *
88      * @return measured temperature in 0.5°C multiples
89      */
90     public @Nullable Double getMeasured() {
91         return this.measured;
92     }
93
94     /**
95      * Get setpoint temperature of the Thermostat.
96      *
97      * @return the setpoint temperature in 0.5°C multiples
98      */
99     public @Nullable Double getSetpoint() {
100         return this.setpoint;
101     }
102
103     /**
104      * Get the Thermostat mode.
105      *
106      * @return the mode: 0="Manual", 1="Freeze", 2="Economic", 3="Comfort", 4="Night"
107      */
108     public @Nullable Integer getMode() {
109         return this.mode;
110     }
111
112     /**
113      * Sends Thermostat mode to Qbus.
114      *
115      * @param mode
116      * @param sn
117      * @throws InterruptedException
118      * @throws IOException
119      */
120     public void executeMode(int mode, String sn) throws InterruptedException, IOException {
121         QbusMessageCmd qCmd = new QbusMessageCmd(sn, "executeThermostat").withId(this.id).withMode(mode);
122         QbusCommunication comm = this.qComm;
123         if (comm != null) {
124             comm.sendMessage(qCmd);
125         }
126     }
127
128     /**
129      * Sends Thermostat setpoint to Qbus.
130      *
131      * @param setpoint
132      * @throws IOException
133      * @throws InterruptedException
134      */
135     public void executeSetpoint(double setpoint, String sn) throws InterruptedException, IOException {
136         QbusMessageCmd qCmd = new QbusMessageCmd(sn, "executeThermostat").withId(this.id).withSetPoint(setpoint);
137         QbusCommunication comm = this.qComm;
138         if (comm != null) {
139             comm.sendMessage(qCmd);
140         }
141     }
142 }