-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat.cpp
140 lines (127 loc) · 6.69 KB
/
thermostat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright 2013 - 2022, nymea GmbH
* Contact: [email protected]
*
* This file is part of nymea.
* This project including source code and documentation is protected by
* copyright law, and remains the property of nymea GmbH. All rights, including
* reproduction, publication, editing and translation, are reserved. The use of
* this project is subject to the terms of a license agreement to be concluded
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
*
* GNU General Public License Usage
* Alternatively, this project may be redistributed and/or modified under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, GNU version 3. This project is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this project. If not, see <https://www.gnu.org/licenses/>.
*
* For any further details and any questions please contact us under
* [email protected] or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "thermostat.h"
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(dcAirConditioning)
Thermostat::Thermostat(ThingManager *thingManager, Thing *thing, QObject *parent):
QObject(parent),
m_thingManager(thingManager),
m_thing(thing)
{
m_cachedTargetTemperature = m_thing->stateValue("targetTemperature").toDouble();
}
Thing *Thermostat::thing() const
{
return m_thing;
}
void Thermostat::setTargetTemperature(double targetTemperature, bool force)
{
qCDebug(dcAirConditioning()) << "setTargetTemp called. Window open:" << m_windowOpen << "force:" << force;
m_cachedTargetTemperature = targetTemperature;
if (m_windowOpen && !force) {
qCDebug(dcAirConditioning()) << "Not setting target temperature on" << m_thing->name() << "because a window is open";
return;
}
if (m_thing->stateValue("targetTemperature").toDouble() != targetTemperature) {
ActionType actionType = m_thing->thingClass().actionTypes().findByName("targetTemperature");
Action action(actionType.id(), m_thing->id(), Action::TriggeredByRule);
action.setParams({Param(actionType.id(), targetTemperature)});
qCDebug(dcAirConditioning()) << "Setting target temperature" << targetTemperature << "to" << m_thing->name() << "from" << m_thing->stateValue("targetTemperature").toDouble();
ThingActionInfo *info = m_thingManager->executeAction(action);
connect(info, &ThingActionInfo::finished, this, [info, this](){
if (info->status() != Thing::ThingErrorNoError) {
qCWarning(dcAirConditioning()) << "Unable to execute targetTemperature action on" << m_thing << info->status() << info->displayMessage();
return;
}
qCDebug(dcAirConditioning()) << "Target temperature set successfully";
});
}
}
void Thermostat::setWindowOpen(bool windowOpen)
{
m_windowOpen = windowOpen;
// First check if the device is capable of handling a window open locks
if (!m_thing->thingClass().actionTypes().findByName("windowOpen").id().isNull()) {
if (m_thing->stateValue("windowOpen").toBool() != windowOpen) {
ActionType actionType = m_thing->thingClass().actionTypes().findByName("windowOpen");
Action action(actionType.id(), m_thing->id(), Action::TriggeredByRule);
action.setParams({Param(actionType.id(), windowOpen)});
qCDebug(dcAirConditioning()) << "Setting window open" << windowOpen << "to" << m_thing->name();
ThingActionInfo *info = m_thingManager->executeAction(action);
connect(info, &ThingActionInfo::finished, this, [info, this](){
if (info->status() != Thing::ThingErrorNoError) {
qCWarning(dcAirConditioning()) << "Unable to execute window Open action on" << m_thing << info->status() << info->displayMessage();
return;
}
});
}
return;
}
// Otherwise see if it can be turned off while the window is open
if (m_thing->hasState("power")) {
if (m_thing->stateValue("power").toBool() == windowOpen) {
ActionType actionType = m_thing->thingClass().actionTypes().findByName("power");
Action action(actionType.id(), m_thing->id(), Action::TriggeredByRule);
action.setParams({Param(actionType.id(), !windowOpen)});
qCDebug(dcAirConditioning()) << "Setting power" << !windowOpen << "to" << m_thing->name();
ThingActionInfo *info = m_thingManager->executeAction(action);
connect(info, &ThingActionInfo::finished, this, [info, this](){
if (info->status() != Thing::ThingErrorNoError) {
qCWarning(dcAirConditioning()) << "Unable to execute power action on" << m_thing << info->status() << info->displayMessage();
return;
}
});
}
}
// If nothing works, let's assume it is a very dump radiator thermostat and set the temperature to minimum
double temp = windowOpen ? m_thing->state("targetTemperature").minValue().toDouble() : m_cachedTargetTemperature;
if (m_thing->stateValue("targetTemperature").toDouble() != temp) {
ActionType actionType = m_thing->thingClass().actionTypes().findByName("targetTemperature");
Action action(actionType.id(), m_thing->id(), Action::TriggeredByRule);
action.setParams({Param(actionType.id(), temp)});
qCDebug(dcAirConditioning()) << "Setting target temperature (window open control)" << temp << "to" << m_thing->name() << "from" << m_thing->stateValue("targetTemperature").toDouble();
ThingActionInfo *info = m_thingManager->executeAction(action);
connect(info, &ThingActionInfo::finished, this, [info, this](){
if (info->status() != Thing::ThingErrorNoError) {
qCWarning(dcAirConditioning()) << "Unable to execute targetTemperature action on" << m_thing << info->status() << info->displayMessage();
return;
}
qCDebug(dcAirConditioning()) << "Target temperature set successfully";
});
}
}
bool Thermostat::hasTemperatureSensor() const
{
return m_thing->thingClass().interfaces().contains("temperaturesensor");
}
double Thermostat::temperature() const
{
return m_thing->stateValue("temperature").toDouble();
}