Commit 7b1ff18f4d64c62bae84b4cd34f5b057886ca5e9

Authored by Jojo-1000
Committed by Jan
1 parent de24ef18

Implement MAC address normalizing to give the user more freedom in specifying it.

hueplusplus/Hue.cpp
... ... @@ -60,7 +60,7 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const
60 60 std::smatch matchResult;
61 61 if (std::regex_search(desc, manufRegex) && std::regex_search(desc, manURLRegex) && std::regex_search(desc, modelRegex) && std::regex_search(desc, matchResult, serialRegex))
62 62 {
63   - //The string matches
  63 + //The string matcheshttps://github.com/enwi/hueplusplus
64 64 //Get 1st submatch (0 is whole match)
65 65 bridge.mac = matchResult[1].str();
66 66 foundBridges.push_back(std::move(bridge));
... ... @@ -74,7 +74,8 @@ std::vector<HueFinder::HueIdentification> HueFinder::FindBridges() const
74 74  
75 75 Hue HueFinder::GetBridge(const HueIdentification& identification)
76 76 {
77   - auto pos = usernames.find(identification.mac);
  77 + std::string normalizedMac = NormalizeMac(identification.mac);
  78 + auto pos = usernames.find(normalizedMac);
78 79 if (pos != usernames.end())
79 80 {
80 81 return Hue(identification.ip, pos->second, http_handler);
... ... @@ -86,14 +87,14 @@ Hue HueFinder::GetBridge(const HueIdentification& identification)
86 87 std::cerr << "Failed to request username for ip " << identification.ip << std::endl;
87 88 throw std::runtime_error("Failed to request username!");
88 89 }
89   - AddUsername(identification.mac, bridge.getUsername());
  90 + AddUsername(normalizedMac, bridge.getUsername());
90 91  
91 92 return bridge;
92 93 }
93 94  
94 95 void HueFinder::AddUsername(const std::string& mac, const std::string& username)
95 96 {
96   - usernames[mac] = username;
  97 + usernames[NormalizeMac(mac)] = username;
97 98 }
98 99  
99 100 const std::map<std::string, std::string>& HueFinder::GetAllUsernames() const
... ... @@ -101,6 +102,17 @@ const std::map&lt;std::string, std::string&gt;&amp; HueFinder::GetAllUsernames() const
101 102 return usernames;
102 103 }
103 104  
  105 +std::string HueFinder::NormalizeMac(std::string input)
  106 +{
  107 + // Remove any non alphanumeric characters (e.g. ':' and whitespace)
  108 + input.erase(std::remove_if(input.begin(), input.end(),
  109 + [](char c) { return !std::isalnum(c, std::locale()); }), input.end());
  110 + // Convert to lower case
  111 + std::transform(input.begin(), input.end(), input.begin(),
  112 + [](char c) { return std::tolower(c, std::locale()); });
  113 + return input;
  114 +}
  115 +
104 116  
105 117 Hue::Hue(const std::string& ip, const std::string& username, std::shared_ptr<const IHttpHandler> handler)
106 118 : ip(ip),
... ... @@ -284,139 +296,139 @@ bool Hue::lightExists(int id) const
284 296  
285 297 std::string Hue::getPictureOfLight(int id) const
286 298 {
287   - std::string ret = "";
288   - auto pos = lights.find(id);
289   - if (pos != lights.end())
290   - {
291   - ret = getPictureOfModel(pos->second.getModelId());
292   - }
293   - return ret;
  299 + std::string ret = "";
  300 + auto pos = lights.find(id);
  301 + if (pos != lights.end())
  302 + {
  303 + ret = getPictureOfModel(pos->second.getModelId());
  304 + }
  305 + return ret;
294 306 }
295 307  
296 308 std::string Hue::getPictureOfModel(const std::string& model_id) const
297 309 {
298   - std::string ret = "";
299   - if(model_id == "LCT001" || model_id == "LCT007" || model_id == "LCT010" || model_id == "LCT014" ||
300   - model_id == "LTW010" || model_id == "LTW001" || model_id == "LTW004" || model_id == "LTW015" ||
301   - model_id == "LWB004" || model_id == "LWB006")
302   - {
303   - ret.append("e27_waca");
304   - }
305   - else if (model_id == "LWB010" || model_id == "LWB014")
306   - {
307   - ret.append("e27_white");
308   - }
309   - else if (model_id == "LCT012" || model_id == "LTW012")
310   - {
311   - ret.append("e14");
312   - }
313   - else if (model_id == "LCT002")
314   - {
315   - ret.append("br30");
316   - }
317   - else if (model_id == "LCT011" || model_id == "LTW011")
318   - {
319   - ret.append("br30_slim");
320   - }
321   - else if (model_id == "LCT003")
322   - {
323   - ret.append("gu10");
324   - }
325   - else if (model_id == "LTW013")
326   - {
327   - ret.append("gu10_perfectfit");
328   - }
329   - else if (model_id == "LST001" || model_id == "LST002")
330   - {
331   - ret.append("lightstrip");
332   - }
333   - else if (model_id == "LLC006 " || model_id == "LLC010")
334   - {
335   - ret.append("iris");
336   - }
337   - else if (model_id == "LLC005" || model_id == "LLC011" || model_id == "LLC012" ||
338   - model_id == "LLC007")
339   - {
340   - ret.append("bloom");
341   - }
342   - else if (model_id == "LLC014")
343   - {
344   - ret.append("aura");
345   - }
346   - else if (model_id == "LLC013")
347   - {
348   - ret.append("storylight");
349   - }
350   - else if (model_id == "LLC020")
351   - {
352   - ret.append("go");
353   - }
354   - else if (model_id == "HBL001" || model_id == "HBL002" || model_id == "HBL003")
355   - {
356   - ret.append("beyond_ceiling_pendant_table");
357   - }
358   - else if (model_id == "HIL001 " || model_id == "HIL002")
359   - {
360   - ret.append("impulse");
361   - }
362   - else if (model_id == "HEL001 " || model_id == "HEL002")
363   - {
364   - ret.append("entity");
365   - }
366   - else if (model_id == "HML001" || model_id == "HML002" || model_id == "HML003" ||
367   - model_id == "HML004" || model_id == "HML005")
368   - {
369   - ret.append("phoenix_ceiling_pendant_table_wall");
370   - }
371   - else if (model_id == "HML006")
372   - {
373   - ret.append("phoenix_down");
374   - }
375   - else if (model_id == "LTP001" || model_id == "LTP002" || model_id == "LTP003" ||
376   - model_id == "LTP004" || model_id == "LTP005" || model_id == "LTD003")
377   - {
378   - ret.append("pendant");
379   - }
380   - else if (model_id == "LDF002" || model_id == "LTF001" || model_id == "LTF002" ||
381   - model_id == "LTC001" || model_id == "LTC002" || model_id == "LTC003" ||
382   - model_id == "LTC004" || model_id == "LTD001" || model_id == "LTD002" ||
383   - model_id == "LDF001")
384   - {
385   - ret.append("ceiling");
386   - }
387   - else if (model_id == "LDD002 " || model_id == "LFF001")
388   - {
389   - ret.append("floor");
390   - }
391   - else if (model_id == "LDD001 " || model_id == "LTT001")
392   - {
393   - ret.append("table");
394   - }
395   - else if (model_id == "LDT001 " || model_id == "MWM001")
396   - {
397   - ret.append("recessed");
398   - }
399   - else if (model_id == "BSB001")
400   - {
401   - ret.append("bridge_v1");
402   - }
403   - else if (model_id == "BSB002")
404   - {
405   - ret.append("bridge_v2");
406   - }
407   - else if (model_id == "SWT001")
408   - {
409   - ret.append("tap");
410   - }
411   - else if (model_id == "RWL021")
412   - {
413   - ret.append("hds");
414   - }
415   - else if (model_id == "SML001")
416   - {
417   - ret.append("motion_sensor");
418   - }
419   - return ret;
  310 + std::string ret = "";
  311 + if (model_id == "LCT001" || model_id == "LCT007" || model_id == "LCT010" || model_id == "LCT014" ||
  312 + model_id == "LTW010" || model_id == "LTW001" || model_id == "LTW004" || model_id == "LTW015" ||
  313 + model_id == "LWB004" || model_id == "LWB006")
  314 + {
  315 + ret.append("e27_waca");
  316 + }
  317 + else if (model_id == "LWB010" || model_id == "LWB014")
  318 + {
  319 + ret.append("e27_white");
  320 + }
  321 + else if (model_id == "LCT012" || model_id == "LTW012")
  322 + {
  323 + ret.append("e14");
  324 + }
  325 + else if (model_id == "LCT002")
  326 + {
  327 + ret.append("br30");
  328 + }
  329 + else if (model_id == "LCT011" || model_id == "LTW011")
  330 + {
  331 + ret.append("br30_slim");
  332 + }
  333 + else if (model_id == "LCT003")
  334 + {
  335 + ret.append("gu10");
  336 + }
  337 + else if (model_id == "LTW013")
  338 + {
  339 + ret.append("gu10_perfectfit");
  340 + }
  341 + else if (model_id == "LST001" || model_id == "LST002")
  342 + {
  343 + ret.append("lightstrip");
  344 + }
  345 + else if (model_id == "LLC006 " || model_id == "LLC010")
  346 + {
  347 + ret.append("iris");
  348 + }
  349 + else if (model_id == "LLC005" || model_id == "LLC011" || model_id == "LLC012" ||
  350 + model_id == "LLC007")
  351 + {
  352 + ret.append("bloom");
  353 + }
  354 + else if (model_id == "LLC014")
  355 + {
  356 + ret.append("aura");
  357 + }
  358 + else if (model_id == "LLC013")
  359 + {
  360 + ret.append("storylight");
  361 + }
  362 + else if (model_id == "LLC020")
  363 + {
  364 + ret.append("go");
  365 + }
  366 + else if (model_id == "HBL001" || model_id == "HBL002" || model_id == "HBL003")
  367 + {
  368 + ret.append("beyond_ceiling_pendant_table");
  369 + }
  370 + else if (model_id == "HIL001 " || model_id == "HIL002")
  371 + {
  372 + ret.append("impulse");
  373 + }
  374 + else if (model_id == "HEL001 " || model_id == "HEL002")
  375 + {
  376 + ret.append("entity");
  377 + }
  378 + else if (model_id == "HML001" || model_id == "HML002" || model_id == "HML003" ||
  379 + model_id == "HML004" || model_id == "HML005")
  380 + {
  381 + ret.append("phoenix_ceiling_pendant_table_wall");
  382 + }
  383 + else if (model_id == "HML006")
  384 + {
  385 + ret.append("phoenix_down");
  386 + }
  387 + else if (model_id == "LTP001" || model_id == "LTP002" || model_id == "LTP003" ||
  388 + model_id == "LTP004" || model_id == "LTP005" || model_id == "LTD003")
  389 + {
  390 + ret.append("pendant");
  391 + }
  392 + else if (model_id == "LDF002" || model_id == "LTF001" || model_id == "LTF002" ||
  393 + model_id == "LTC001" || model_id == "LTC002" || model_id == "LTC003" ||
  394 + model_id == "LTC004" || model_id == "LTD001" || model_id == "LTD002" ||
  395 + model_id == "LDF001")
  396 + {
  397 + ret.append("ceiling");
  398 + }
  399 + else if (model_id == "LDD002 " || model_id == "LFF001")
  400 + {
  401 + ret.append("floor");
  402 + }
  403 + else if (model_id == "LDD001 " || model_id == "LTT001")
  404 + {
  405 + ret.append("table");
  406 + }
  407 + else if (model_id == "LDT001 " || model_id == "MWM001")
  408 + {
  409 + ret.append("recessed");
  410 + }
  411 + else if (model_id == "BSB001")
  412 + {
  413 + ret.append("bridge_v1");
  414 + }
  415 + else if (model_id == "BSB002")
  416 + {
  417 + ret.append("bridge_v2");
  418 + }
  419 + else if (model_id == "SWT001")
  420 + {
  421 + ret.append("tap");
  422 + }
  423 + else if (model_id == "RWL021")
  424 + {
  425 + ret.append("hds");
  426 + }
  427 + else if (model_id == "SML001")
  428 + {
  429 + ret.append("motion_sensor");
  430 + }
  431 + return ret;
420 432 }
421 433  
422 434 void Hue::refreshState()
... ...
hueplusplus/include/Hue.h
... ... @@ -82,6 +82,10 @@ public:
82 82 const std::map<std::string, std::string>& GetAllUsernames() const;
83 83  
84 84 private:
  85 + //! \brief Normalizes mac address to plain hex number.
  86 + //! returns \c input without separators and whitespace, in upper case.
  87 + static std::string NormalizeMac(std::string input);
  88 +
85 89 std::map<std::string, std::string> usernames; //!< Maps all macs to usernames added by \ref HueFinder::AddUsername
86 90 std::shared_ptr<const IHttpHandler> http_handler;
87 91 };
... ...
hueplusplus/include/Units.h 0 → 100644
  1 +#ifndef _UNITS_H
  2 +#define _UNITS_H
  3 +
  4 +struct Kelvin
  5 +{
  6 + int value;
  7 +};
  8 +
  9 +struct Mired
  10 +{
  11 + int value;
  12 +};
  13 +
  14 +struct Brightness
  15 +{
  16 + int value;
  17 +};
  18 +
  19 +struct HueSaturation
  20 +{
  21 + int hue;
  22 + int saturation;
  23 +};
  24 +
  25 +struct XY
  26 +{
  27 + float x;
  28 + float y;
  29 +};
  30 +
  31 +struct RGB
  32 +{
  33 + uint8_t r;
  34 + uint8_t g;
  35 + uint8_t b;
  36 +};
  37 +
  38 +#endif
... ...