Hue.cpp
14.3 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
\file Hue.cpp
Copyright Notice\n
Copyright (C) 2017 Jan Rogall - developer\n
Copyright (C) 2017 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/Hue.h"
#include <algorithm>
#include <cctype>
#include <chrono>
#include <cstring>
#include <iostream>
#include <locale>
#include <stdexcept>
#include <thread>
#include "hueplusplus/HueDeviceTypes.h"
#include "hueplusplus/ExtendedColorHueStrategy.h"
#include "hueplusplus/ExtendedColorTemperatureStrategy.h"
#include "hueplusplus/HueExceptionMacro.h"
#include "hueplusplus/SimpleBrightnessStrategy.h"
#include "hueplusplus/SimpleColorHueStrategy.h"
#include "hueplusplus/SimpleColorTemperatureStrategy.h"
#include "hueplusplus/UPnP.h"
#include "hueplusplus/Utils.h"
hueplusplus::HueFinder::HueFinder(std::shared_ptr<const IHttpHandler> handler) : http_handler(std::move(handler)) {}
std::vector<hueplusplus::HueFinder::HueIdentification> hueplusplus::HueFinder::FindBridges() const
{
UPnP uplug;
std::vector<std::pair<std::string, std::string>> foundDevices = uplug.getDevices(http_handler);
std::vector<HueIdentification> foundBridges;
for (const std::pair<std::string, std::string>& p : foundDevices)
{
size_t found = p.second.find("IpBridge");
if (found != std::string::npos)
{
HueIdentification bridge;
size_t start = p.first.find("//") + 2;
size_t length = p.first.find(":", start) - start;
bridge.ip = p.first.substr(start, length);
std::string desc
= http_handler->GETString("/description.xml", "application/xml", "", bridge.ip, bridge.port);
std::string mac = ParseDescription(desc);
if (!mac.empty())
{
bridge.mac = NormalizeMac(mac);
foundBridges.push_back(std::move(bridge));
}
}
}
return foundBridges;
}
hueplusplus::Hue hueplusplus::HueFinder::GetBridge(const HueIdentification& identification)
{
std::string normalizedMac = NormalizeMac(identification.mac);
auto pos = usernames.find(normalizedMac);
if (pos != usernames.end())
{
return Hue(identification.ip, identification.port, pos->second, http_handler);
}
Hue bridge(identification.ip, identification.port, "", http_handler);
bridge.requestUsername();
if (bridge.getUsername().empty())
{
std::cerr << "Failed to request username for ip " << identification.ip << std::endl;
throw HueException(CURRENT_FILE_INFO, "Failed to request username!");
}
AddUsername(normalizedMac, bridge.getUsername());
return bridge;
}
void hueplusplus::HueFinder::AddUsername(const std::string& mac, const std::string& username)
{
usernames[NormalizeMac(mac)] = username;
}
const std::map<std::string, std::string>& hueplusplus::HueFinder::GetAllUsernames() const
{
return usernames;
}
std::string hueplusplus::HueFinder::NormalizeMac(std::string input)
{
// Remove any non alphanumeric characters (e.g. ':' and whitespace)
input.erase(std::remove_if(input.begin(), input.end(), [](char c) { return !std::isalnum(c, std::locale()); }),
input.end());
// Convert to lower case
std::transform(input.begin(), input.end(), input.begin(), [](char c) { return std::tolower(c, std::locale()); });
return input;
}
std::string hueplusplus::HueFinder::ParseDescription(const std::string& description)
{
const char* model = "<modelName>Philips hue bridge";
const char* serialBegin = "<serialNumber>";
const char* serialEnd = "</serialNumber>";
if (description.find(model) != std::string::npos)
{
std::size_t begin = description.find(serialBegin);
std::size_t end = description.find(serialEnd, begin);
if (begin != std::string::npos && end != std::string::npos)
{
begin += std::strlen(serialBegin);
if (begin < description.size())
{
std::string result = description.substr(begin, end - begin);
return result;
}
}
}
return std::string();
}
hueplusplus::Hue::Hue(
const std::string& ip, const int port, const std::string& username, std::shared_ptr<const IHttpHandler> handler)
: ip(ip),
port(port),
username(username),
simpleBrightnessStrategy(std::make_shared<SimpleBrightnessStrategy>()),
simpleColorHueStrategy(std::make_shared<SimpleColorHueStrategy>()),
extendedColorHueStrategy(std::make_shared<ExtendedColorHueStrategy>()),
simpleColorTemperatureStrategy(std::make_shared<SimpleColorTemperatureStrategy>()),
extendedColorTemperatureStrategy(std::make_shared<ExtendedColorTemperatureStrategy>()),
http_handler(std::move(handler)),
commands(ip, port, username, http_handler)
{}
std::string hueplusplus::Hue::getBridgeIP()
{
return ip;
}
int hueplusplus::Hue::getBridgePort()
{
return port;
}
std::string hueplusplus::Hue::requestUsername()
{
std::cout << "Please press the link Button! You've got 35 secs!\n"; // when the link
// button was
// pressed we
// got 30
// seconds to
// get our
// username for
// control
nlohmann::json request;
request["devicetype"] = "HuePlusPlus#User";
nlohmann::json answer;
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point lastCheck;
while (std::chrono::steady_clock::now() - start < std::chrono::seconds(35))
{
if (std::chrono::steady_clock::now() - lastCheck > std::chrono::seconds(1))
{
lastCheck = std::chrono::steady_clock::now();
answer = http_handler->POSTJson("/api", request, ip, port);
nlohmann::json jsonUser = utils::safeGetMember(answer, 0, "success", "username");
if (jsonUser != nullptr)
{
// [{"success":{"username": "<username>"}}]
username = jsonUser.get<std::string>();
// Update commands with new username and ip
commands = HueCommandAPI(ip, port, username, http_handler);
std::cout << "Success! Link button was pressed!\n";
std::cout << "Username is \"" << username << "\"\n";
break;
}
else if (answer.size() > 0 && answer[0].count("error"))
{
// All errors except 101: Link button not pressed
if (utils::safeGetMember(answer, 0, "error", "type") != 101)
{
throw HueAPIResponseException::Create(CURRENT_FILE_INFO, answer[0]);
}
}
std::this_thread::sleep_until(lastCheck + std::chrono::seconds(1));
}
}
return username;
}
std::string hueplusplus::Hue::getUsername()
{
return username;
}
void hueplusplus::Hue::setIP(const std::string& ip)
{
this->ip = ip;
}
void hueplusplus::Hue::setPort(const int port)
{
this->port = port;
}
hueplusplus::HueLight& hueplusplus::Hue::getLight(int id)
{
auto pos = lights.find(id);
if (pos != lights.end())
{
pos->second.refreshState();
return pos->second;
}
refreshState();
if (!state["lights"].count(std::to_string(id)))
{
std::cerr << "Error in Hue getLight(): light with id " << id << " is not valid\n";
throw HueException(CURRENT_FILE_INFO, "Light id is not valid");
}
// std::cout << state["lights"][std::to_string(id)] << std::endl;
std::string type = state["lights"][std::to_string(id)]["modelid"].get<std::string>();
// std::cout << type << std::endl;
auto light = MakeHueLight()(type, id, commands, simpleBrightnessStrategy,
extendedColorTemperatureStrategy, simpleColorTemperatureStrategy, extendedColorHueStrategy,
simpleColorHueStrategy);
lights.emplace(id, light);
return lights.find(id)->second;
}
bool hueplusplus::Hue::removeLight(int id)
{
nlohmann::json result
= commands.DELETERequest("/lights/" + std::to_string(id), nlohmann::json::object(), CURRENT_FILE_INFO);
bool success = utils::safeGetMember(result, 0, "success") == "/lights/" + std::to_string(id) + " deleted";
if (success && lights.count(id) != 0)
{
lights.erase(id);
}
return success;
}
std::vector<std::reference_wrapper<hueplusplus::HueLight>> hueplusplus::Hue::getAllLights()
{
refreshState();
nlohmann::json lightsState = state["lights"];
for (nlohmann::json::iterator it = lightsState.begin(); it != lightsState.end(); ++it)
{
getLight(std::stoi(it.key()));
}
std::vector<std::reference_wrapper<HueLight>> result;
for (auto& entry : lights)
{
result.emplace_back(entry.second);
}
return result;
}
bool hueplusplus::Hue::lightExists(int id)
{
refreshState();
auto pos = lights.find(id);
if (pos != lights.end())
{
return true;
}
if (state["lights"].count(std::to_string(id)))
{
return true;
}
return false;
}
bool hueplusplus::Hue::lightExists(int id) const
{
auto pos = lights.find(id);
if (pos != lights.end())
{
return true;
}
if (state["lights"].count(std::to_string(id)))
{
return true;
}
return false;
}
std::string hueplusplus::Hue::getPictureOfLight(int id) const
{
std::string ret = "";
auto pos = lights.find(id);
if (pos != lights.end())
{
ret = getPictureOfModel(pos->second.getModelId());
}
return ret;
}
std::string hueplusplus::Hue::getPictureOfModel(const std::string& model_id) const
{
std::string ret = "";
if (model_id == "LCT001" || model_id == "LCT007" || model_id == "LCT010" || model_id == "LCT014"
|| model_id == "LTW010" || model_id == "LTW001" || model_id == "LTW004" || model_id == "LTW015"
|| model_id == "LWB004" || model_id == "LWB006")
{
ret.append("e27_waca");
}
else if (model_id == "LWB010" || model_id == "LWB014")
{
ret.append("e27_white");
}
else if (model_id == "LCT012" || model_id == "LTW012")
{
ret.append("e14");
}
else if (model_id == "LCT002")
{
ret.append("br30");
}
else if (model_id == "LCT011" || model_id == "LTW011")
{
ret.append("br30_slim");
}
else if (model_id == "LCT003")
{
ret.append("gu10");
}
else if (model_id == "LTW013")
{
ret.append("gu10_perfectfit");
}
else if (model_id == "LST001" || model_id == "LST002")
{
ret.append("lightstrip");
}
else if (model_id == "LLC006 " || model_id == "LLC010")
{
ret.append("iris");
}
else if (model_id == "LLC005" || model_id == "LLC011" || model_id == "LLC012" || model_id == "LLC007")
{
ret.append("bloom");
}
else if (model_id == "LLC014")
{
ret.append("aura");
}
else if (model_id == "LLC013")
{
ret.append("storylight");
}
else if (model_id == "LLC020")
{
ret.append("go");
}
else if (model_id == "HBL001" || model_id == "HBL002" || model_id == "HBL003")
{
ret.append("beyond_ceiling_pendant_table");
}
else if (model_id == "HIL001 " || model_id == "HIL002")
{
ret.append("impulse");
}
else if (model_id == "HEL001 " || model_id == "HEL002")
{
ret.append("entity");
}
else if (model_id == "HML001" || model_id == "HML002" || model_id == "HML003" || model_id == "HML004"
|| model_id == "HML005")
{
ret.append("phoenix_ceiling_pendant_table_wall");
}
else if (model_id == "HML006")
{
ret.append("phoenix_down");
}
else if (model_id == "LTP001" || model_id == "LTP002" || model_id == "LTP003" || model_id == "LTP004"
|| model_id == "LTP005" || model_id == "LTD003")
{
ret.append("pendant");
}
else if (model_id == "LDF002" || model_id == "LTF001" || model_id == "LTF002" || model_id == "LTC001"
|| model_id == "LTC002" || model_id == "LTC003" || model_id == "LTC004" || model_id == "LTD001"
|| model_id == "LTD002" || model_id == "LDF001")
{
ret.append("ceiling");
}
else if (model_id == "LDD002 " || model_id == "LFF001")
{
ret.append("floor");
}
else if (model_id == "LDD001 " || model_id == "LTT001")
{
ret.append("table");
}
else if (model_id == "LDT001 " || model_id == "MWM001")
{
ret.append("recessed");
}
else if (model_id == "BSB001")
{
ret.append("bridge_v1");
}
else if (model_id == "BSB002")
{
ret.append("bridge_v2");
}
else if (model_id == "SWT001")
{
ret.append("tap");
}
else if (model_id == "RWL021")
{
ret.append("hds");
}
else if (model_id == "SML001")
{
ret.append("motion_sensor");
}
return ret;
}
void hueplusplus::Hue::refreshState()
{
if (username.empty())
{
return;
}
nlohmann::json answer = commands.GETRequest("", nlohmann::json::object(), CURRENT_FILE_INFO);
if (answer.is_object() && answer.count("lights"))
{
state = answer;
}
else
{
std::cout << "Answer in Hue::refreshState of http_handler->GETJson(...) is "
"not expected!\nAnswer:\n\t"
<< answer.dump() << std::endl;
}
}