StateTransaction.cpp
6.87 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
\file StateTransaction.cpp
Copyright Notice\n
Copyright (C) 2020 Jan Rogall - developer\n
Copyright (C) 2020 Moritz Wirger - developer\n
This file is part of hueplusplus.
hueplusplus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
hueplusplus 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
**/
#include "hueplusplus/StateTransaction.h"
#include <set>
#include "hueplusplus/HueExceptionMacro.h"
#include "hueplusplus/StateTransaction.h"
#include "hueplusplus/Utils.h"
namespace hueplusplus
{
StateTransaction::StateTransaction(
const HueCommandAPI& commands, const std::string& path, const nlohmann::json& currentState)
: commands(commands), path(path), state(currentState), request(nlohmann::json::object())
{
assert(currentState.is_object());
}
bool StateTransaction::commit(bool trimRequest) &&
{
if (trimRequest)
{
this->trimRequest();
}
// Empty request or request with only transition makes no sense
if (!request.empty() && !(request.size() == 1 && request.count("transitiontime")))
{
if (!request.count("on"))
{
if (!state.value("on", false)
&& (request.value("bri", 0) != 0 || request.count("effect") || request.count("hue")
|| request.count("sat") || request.count("xy") || request.count("ct")))
{
// Turn on if it was turned off
request["on"] = true;
}
else if (request.value("bri", 254) == 0 && state.value("on", true))
{
// Turn off if brightness is 0
request["on"] = false;
}
}
nlohmann::json reply = commands.PUTRequest(path, request, CURRENT_FILE_INFO);
return utils::validatePUTReply(path, request, reply);
}
return true;
}
ScheduleCommand StateTransaction::toScheduleCommand() &&
{
nlohmann::json command {{"method", "PUT"}, {"address", commands.combinedPath(path)}, {"body", request}};
return ScheduleCommand(command);
}
StateTransaction&& StateTransaction::setOn(bool on) &&
{
request["on"] = on;
return std::move(*this);
}
StateTransaction&& StateTransaction::setBrightness(uint8_t brightness) &&
{
uint8_t clamped = std::min<uint8_t>(brightness, 254);
request["bri"] = clamped;
return std::move(*this);
}
StateTransaction&& StateTransaction::setColorSaturation(uint8_t saturation) &&
{
uint8_t clamped = std::min<uint8_t>(saturation, 254);
request["sat"] = clamped;
return std::move(*this);
}
StateTransaction&& StateTransaction::setColorHue(uint16_t hue) &&
{
request["hue"] = hue;
return std::move(*this);
}
StateTransaction&& StateTransaction::setColorXY(float x, float y) &&
{
float clampedX = std::max(0.f, std::min(x, 1.f));
float clampedY = std::max(0.f, std::min(y, 1.f));
request["xy"] = {clampedX, clampedY};
return std::move(*this);
}
StateTransaction&& StateTransaction::setColorXY(const XYBrightness& xy)&&
{
request["xy"] = { xy.xy.x, xy.xy.y };
request["bri"] = static_cast<int>(std::round(xy.brightness * 255.f));
return std::move(*this);
}
StateTransaction&& StateTransaction::setColorTemperature(unsigned int mired) &&
{
unsigned int clamped = std::max(153u, std::min(mired, 500u));
request["ct"] = clamped;
return std::move(*this);
}
StateTransaction&& StateTransaction::setColorLoop(bool on) &&
{
request["effect"] = on ? "colorloop" : "none";
return std::move(*this);
}
StateTransaction&& StateTransaction::incrementBrightness(int increment) &&
{
request["bri_inc"] = std::max(-254, std::min(increment, 254));
return std::move(*this);
}
StateTransaction&& StateTransaction::incrementSaturation(int increment) &&
{
request["sat_inc"] = std::max(-254, std::min(increment, 254));
return std::move(*this);
}
StateTransaction&& StateTransaction::incrementHue(int increment) &&
{
request["hue_inc"] = std::max(-65534, std::min(increment, 65534));
return std::move(*this);
}
StateTransaction&& StateTransaction::incrementColorTemperature(int increment) &&
{
request["ct_inc"] = std::max(-65534, std::min(increment, 65534));
return std::move(*this);
}
StateTransaction&& StateTransaction::incrementColorXY(float xInc, float yInc) &&
{
request["xy_inc"] = {std::max(-0.5f, std::min(xInc, 0.5f)), std::max(-0.5f, std::min(yInc, 0.5f))};
return std::move(*this);
}
StateTransaction&& StateTransaction::setTransition(uint16_t transition) &&
{
if (transition != 4)
{
request["transitiontime"] = transition;
}
return std::move(*this);
}
StateTransaction&& StateTransaction::alert() &&
{
request["alert"] = "select";
return std::move(*this);
}
StateTransaction&& StateTransaction::longAlert() &&
{
request["alert"] = "lselect";
return std::move(*this);
}
StateTransaction&& StateTransaction::stopAlert() &&
{
request["alert"] = "none";
return std::move(*this);
}
void StateTransaction::trimRequest()
{
static const std::map<std::string, std::string> colormodes
= {{"sat", "hs"}, {"hue", "hs"}, {"xy", "xy"}, {"ct", "ct"}};
static const std::set<std::string> otherRemove = {"on", "bri", "effect"};
// Skip when there is no state provided (e.g. for groups)
if (state.empty())
{
return;
}
for (auto it = request.begin(); it != request.end();)
{
auto colormodeIt = colormodes.find(it.key());
if (colormodeIt != colormodes.end())
{
// Only erase color commands if colormode and value matches
auto stateIt = state.find(it.key());
if (stateIt != state.end() && state.value("colormode", "") == colormodeIt->second)
{
// Compare xy using float comparison
if ((!it->is_array() && *stateIt == *it)
|| (stateIt->is_array() && utils::floatEquals((*stateIt)[0].get<float>(), (*it)[0].get<float>())
&& utils::floatEquals((*stateIt)[1].get<float>(), (*it)[1].get<float>())))
{
it = request.erase(it);
continue;
}
}
}
else if (otherRemove.count(it.key()))
{
if (state.count(it.key()) && state[it.key()] == *it)
{
it = request.erase(it);
continue;
}
}
++it;
}
}
} // namespace hueplusplus