Rule.cpp
6.71 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
\file Rule.cpp
Copyright Notice\n
Copyright (C) 2020 Jan Rogall - 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/HueExceptionMacro.h>
#include <hueplusplus/Rule.h>
namespace hueplusplus
{
Condition::Condition(const std::string& address, Operator op, const std::string& value)
: address(address), op(op), value(value)
{ }
std::string Condition::getAddress() const
{
return address;
}
Condition::Operator Condition::getOperator() const
{
return op;
}
std::string Condition::getValue() const
{
return value;
}
nlohmann::json Condition::toJson() const
{
std::string opStr;
switch (op)
{
case Operator::eq:
opStr = "eq";
break;
case Operator::gt:
opStr = "gt";
break;
case Operator::lt:
opStr = "lt";
break;
case Operator::dx:
opStr = "dx";
break;
case Operator::ddx:
opStr = "ddx";
break;
case Operator::stable:
opStr = "stable";
break;
case Operator::notStable:
opStr = "not stable";
break;
case Operator::in:
opStr = "in";
break;
case Operator::notIn:
opStr = "not in";
break;
default:
throw HueException(CURRENT_FILE_INFO, "Invalid operator enum value: " + std::to_string(static_cast<int>(op)));
}
nlohmann::json result = {{"address", address}, {"operator", opStr}};
if (!value.empty())
{
result["value"] = value;
}
return result;
}
Condition Condition::parse(const nlohmann::json& json)
{
std::string address = json.at("address").get<std::string>();
std::string value = json.value("value", "");
std::string opStr = json.at("operator").get<std::string>();
Operator op = Operator::eq;
if (opStr == "gt")
{
op = Operator::gt;
}
else if (opStr == "lt")
{
op = Operator::lt;
}
else if (opStr == "dx")
{
op = Operator::dx;
}
else if (opStr == "ddx")
{
op = Operator::ddx;
}
else if (opStr == "stable")
{
op = Operator::stable;
}
else if (opStr == "not stable")
{
op = Operator::notStable;
}
else if (opStr == "in")
{
op = Operator::in;
}
else if (opStr == "not in")
{
op = Operator::notIn;
}
else if(opStr != "eq")
{
throw HueException(CURRENT_FILE_INFO, "Unknown condition operator: " + opStr);
}
return Condition(address, op, value);
}
Rule::Rule(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration)
: id(id), state("/rules/" + std::to_string(id), commands, refreshDuration)
{
state.refresh();
}
void Rule::refresh(bool force)
{
if (force)
{
state.refresh();
}
else
{
state.getValue();
}
}
int Rule::getId() const
{
return id;
}
std::string Rule::getName() const
{
return state.getValue().at("name").get<std::string>();
}
void Rule::setName(const std::string& name)
{
nlohmann::json request = {{"name", name}};
sendPutRequest(request, CURRENT_FILE_INFO);
refresh(true);
}
time::AbsoluteTime Rule::getCreated() const
{
return time::AbsoluteTime::parseUTC(state.getValue().at("created").get<std::string>());
}
time::AbsoluteTime Rule::getLastTriggered() const
{
const std::string lasttriggered = state.getValue().value("lasttriggered", "none");
if (lasttriggered.empty() || lasttriggered == "none")
{
return time::AbsoluteTime(std::chrono::system_clock::time_point(std::chrono::seconds(0)));
}
return time::AbsoluteTime::parseUTC(lasttriggered);
}
int Rule::getTimesTriggered() const
{
return state.getValue().at("timestriggered").get<int>();
}
bool Rule::isEnabled() const
{
return state.getValue().at("status").get<std::string>() == "enabled";
}
void Rule::setEnabled(bool enabled)
{
sendPutRequest({{"status", enabled ? "enabled" : "disabled"}}, CURRENT_FILE_INFO);
refresh(true);
}
std::string Rule::getOwner() const
{
return state.getValue().at("owner").get<std::string>();
}
std::vector<Condition> Rule::getConditions() const
{
std::vector<Condition> result;
const nlohmann::json& conditions = state.getValue().at("conditions");
for (const nlohmann::json& c : conditions)
{
result.emplace_back(Condition::parse(c));
}
return result;
}
std::vector<Action> Rule::getActions() const
{
std::vector<Action> result;
const nlohmann::json& actions = state.getValue().at("actions");
for (const nlohmann::json& a : actions)
{
result.emplace_back(a);
}
return result;
}
void Rule::setConditions(const std::vector<Condition>& conditions)
{
nlohmann::json json;
for (const Condition& c : conditions)
{
json.push_back(c.toJson());
}
sendPutRequest({{"conditions", json}}, CURRENT_FILE_INFO);
refresh(true);
}
void Rule::setActions(const std::vector<Action>& actions)
{
nlohmann::json json;
for (const Action& a : actions)
{
json.push_back(a.toJson());
}
sendPutRequest({{"actions", json}}, CURRENT_FILE_INFO);
refresh(true);
}
nlohmann::json Rule::sendPutRequest(const nlohmann::json& request, FileInfo fileInfo)
{
return state.getCommandAPI().PUTRequest("/rules/" + std::to_string(id), request, std::move(fileInfo));
}
CreateRule::CreateRule(const std::vector<Condition>& conditions, const std::vector<Action>& actions)
{
nlohmann::json conditionsJson;
for (const Condition& c : conditions)
{
conditionsJson.push_back(c.toJson());
}
request["conditions"] = conditionsJson;
nlohmann::json actionsJson;
for (const Action& a : actions)
{
actionsJson.push_back(a.toJson());
}
request["actions"] = actionsJson;
}
CreateRule& CreateRule::setName(const std::string& name)
{
request["name"] = name;
return *this;
}
CreateRule& CreateRule::setStatus(bool enabled)
{
request["status"] = enabled ? "enabled" : "disabled";
return *this;
}
nlohmann::json CreateRule::getRequest() const
{
return request;
}
} // namespace hueplusplus