Commit 249eb0a6a31a09f6afd072e175566ea9d80bb554

Authored by Jan Wulkop
1 parent b6c1545c

Split hue classes into different header and source files

hueplusplus/HttpHandler.cpp
... ... @@ -17,7 +17,7 @@
17 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 18 **/
19 19  
20   -#include "HttpHandler.h"
  20 +#include "include/HttpHandler.h"
21 21 #include <iostream>
22 22 #include <stdexcept>
23 23 #include <stdio.h> // printf, sprintf
... ... @@ -120,7 +120,7 @@ std::string HttpHandler::sendRequest(const std::string &amp; msg, const std::string
120 120 std::string HttpHandler::sendRequestGetBody(const std::string & msg, const std::string & adr, int port)
121 121 {
122 122 std::string response = sendRequest(msg, adr, port);
123   - unsigned int start = response.find("\r\n\r\n");
  123 + size_t start = response.find("\r\n\r\n");
124 124 if (start == std::string::npos)
125 125 {
126 126 std::cerr << "Failed to find body in response\n";
... ... @@ -194,7 +194,7 @@ std::vector&lt;std::string&gt; HttpHandler::sendMulticast(const std::string &amp; msg, con
194 194  
195 195 // construct return vector
196 196 std::vector<std::string> returnString;
197   - unsigned int pos = response.find("\r\n\r\n");
  197 + size_t pos = response.find("\r\n\r\n");
198 198 unsigned int prevpos = 0;
199 199 while (pos != std::string::npos)
200 200 {
... ... @@ -205,4 +205,4 @@ std::vector&lt;std::string&gt; HttpHandler::sendMulticast(const std::string &amp; msg, con
205 205 }
206 206  
207 207 return returnString;
208   -}
209 208 \ No newline at end of file
  209 +}
... ...
hueplusplus/Hue.cpp
... ... @@ -17,11 +17,15 @@
17 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 18 **/
19 19  
20   -#include "Hue.h"
  20 +#include "include/Hue.h"
  21 +#include "include/HueLight.h"
  22 +#include "include/HueColorLight.h"
  23 +#include "include/HueDimmableLight.h"
  24 +#include "include/HueExtendedColorLight.h"
  25 +#include "include/HueTemperatureLight.h"
21 26  
22   -#include "HttpHandler.h"
23   -#include "json/json.h"
24   -#include "UPnP.h"
  27 +#include "include/HttpHandler.h"
  28 +#include "include/UPnP.h"
25 29  
26 30 #include <chrono>
27 31 #include <iostream>
... ... @@ -44,7 +48,7 @@ std::vector&lt;HueFinder::HueIdentification&gt; HueFinder::FindBridges() const
44 48 std::vector<HueIdentification> foundBridges;
45 49 for (const std::pair<std::string, std::string> &p : foundDevices)
46 50 {
47   - unsigned int found = p.second.find("IpBridge");
  51 + size_t found = p.second.find("IpBridge");
48 52 if (found != std::string::npos)
49 53 {
50 54 HueIdentification bridge;
... ...
hueplusplus/HueColorLight.cpp 0 → 100644
  1 +/**
  2 + \file HueColorLight.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2017 Jan Rogall - developer\n
  5 + Copyright (C) 2017 Moritz Wirger - developer\n
  6 +
  7 + This program is free software; you can redistribute it and/or modify
  8 + it under the terms of the GNU General Public License as published by
  9 + the Free Software Foundation; either version 3 of the License, or
  10 + (at your option) any later version.
  11 + This program is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 + You should have received a copy of the GNU General Public License
  16 + along with this program; if not, write to the Free Software Foundation,
  17 + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18 +**/
  19 +
  20 +#include "include/HueColorLight.h"
  21 +
  22 +#include <cmath>
  23 +#include <iostream>
  24 +#include <thread>
  25 +
  26 +bool HueColorLight::setColorHue(uint16_t hue, uint8_t transistion)
  27 +{
  28 + refreshState();
  29 + Json::Value request(Json::objectValue);
  30 + if (transistion != 4)
  31 + {
  32 + request["transistiontime"] = transistion;
  33 + }
  34 + if (state["state"]["on"].asBool() != true)
  35 + {
  36 + request["on"] = true;
  37 + }
  38 + if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs")
  39 + {
  40 + hue = hue % 65535;
  41 + request["hue"] = hue;
  42 + }
  43 +
  44 + if (!request.isMember("on") && !request.isMember("hue"))
  45 + {
  46 + //Nothing needs to be changed
  47 + return true;
  48 + }
  49 +
  50 + Json::Value reply = SendPutRequest(request);
  51 +
  52 + //Check whether request was successful
  53 + std::string path = "/lights/" + std::to_string(id) + "/state/";
  54 + bool success = true;
  55 + int i = 0;
  56 + if (success && request.isMember("transistiontime"))
  57 + {
  58 + //Check if success was sent and the value was changed
  59 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt();
  60 + ++i;
  61 + }
  62 + if (success && request.isMember("on"))
  63 + {
  64 + //Check if success was sent and the value was changed
  65 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
  66 + ++i;
  67 + }
  68 + if (success && request.isMember("hue"))
  69 + {
  70 + //Check if success was sent and the value was changed
  71 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "hue"].asUInt() == request["hue"].asUInt();
  72 + }
  73 + return success;
  74 +}
  75 +
  76 +bool HueColorLight::setColorSaturation(uint8_t sat, uint8_t transistion)
  77 +{
  78 + refreshState();
  79 + Json::Value request(Json::objectValue);
  80 + if (transistion != 4)
  81 + {
  82 + request["transistiontime"] = transistion;
  83 + }
  84 + if (state["state"]["on"].asBool() != true)
  85 + {
  86 + request["on"] = true;
  87 + }
  88 + if (state["state"]["sat"].asUInt() != sat)
  89 + {
  90 + if (sat > 254)
  91 + {
  92 + sat = 254;
  93 + }
  94 + request["sat"] = sat;
  95 + }
  96 +
  97 + if (!request.isMember("on") && !request.isMember("sat"))
  98 + {
  99 + //Nothing needs to be changed
  100 + return true;
  101 + }
  102 +
  103 + Json::Value reply = SendPutRequest(request);
  104 +
  105 + //Check whether request was successful
  106 + std::string path = "/lights/" + std::to_string(id) + "/state/";
  107 + bool success = true;
  108 + int i = 0;
  109 + if (success && request.isMember("transistiontime"))
  110 + {
  111 + //Check if success was sent and the value was changed
  112 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt();
  113 + ++i;
  114 + }
  115 + if (success && request.isMember("on"))
  116 + {
  117 + //Check if success was sent and the value was changed
  118 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
  119 + ++i;
  120 + }
  121 + if (success && request.isMember("sat"))
  122 + {
  123 + //Check if success was sent and the value was changed
  124 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "sat"].asUInt() == request["sat"].asUInt();
  125 + }
  126 + return success;
  127 +}
  128 +
  129 +bool HueColorLight::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transistion)
  130 +{
  131 + refreshState();
  132 + Json::Value request(Json::objectValue);
  133 +
  134 + if (transistion != 4)
  135 + {
  136 + request["transitiontime"] = transistion;
  137 + }
  138 + if (state["state"]["on"].asBool() != true)
  139 + {
  140 + request["on"] = true;
  141 + }
  142 + if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs")
  143 + {
  144 + hue = hue % 65535;
  145 + request["hue"] = hue;
  146 + }
  147 + if (state["state"]["sat"].asUInt() != sat || state["state"]["colormode"].asString() != "hs")
  148 + {
  149 + if (sat > 254)
  150 + {
  151 + sat = 254;
  152 + }
  153 + request["sat"] = sat;
  154 + }
  155 +
  156 + if (!request.isMember("on") && !request.isMember("hue") && !request.isMember("sat"))
  157 + {
  158 + //Nothing needs to be changed
  159 + return true;
  160 + }
  161 +
  162 + Json::Value reply = SendPutRequest(request);
  163 +
  164 + //Check whether request was successful
  165 + std::string path = "/lights/" + std::to_string(id) + "/state/";
  166 + bool success = true;
  167 + int i = 0;
  168 + if (success && request.isMember("transitiontime"))
  169 + {
  170 + //Check if success was sent and the value was changed
  171 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt();
  172 + ++i;
  173 + }
  174 + if (success && request.isMember("on"))
  175 + {
  176 + //Check if success was sent and the value was changed
  177 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
  178 + ++i;
  179 + }
  180 + if (success && request.isMember("hue"))
  181 + {
  182 + //Check if success was sent and the value was changed
  183 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "hue"].asUInt() == request["hue"].asUInt();
  184 + ++i;
  185 + }
  186 + if (success && request.isMember("sat"))
  187 + {
  188 + //Check if success was sent and the value was changed
  189 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "sat"].asUInt() == request["sat"].asUInt();
  190 + }
  191 + return success;
  192 +}
  193 +
  194 +bool HueColorLight::setColorXY(float x, float y, uint8_t transistion)
  195 +{
  196 + refreshState();
  197 + Json::Value request(Json::objectValue);
  198 +
  199 + if (transistion != 4)
  200 + {
  201 + request["transitiontime"] = transistion;
  202 + }
  203 + if (state["state"]["on"].asBool() != true)
  204 + {
  205 + request["on"] = true;
  206 + }
  207 + if (state["state"]["xy"][0].asFloat() != x || state["state"]["xy"][1].asFloat() != y || state["state"]["colormode"].asString() != "xy")
  208 + {
  209 + request["xy"][0] = x;
  210 + request["xy"][1] = y;
  211 + }
  212 +
  213 + if (!request.isMember("on") && !request.isMember("xy"))
  214 + {
  215 + //Nothing needs to be changed
  216 + return true;
  217 + }
  218 +
  219 + Json::Value reply = SendPutRequest(request);
  220 +
  221 + //Check whether request was successful
  222 + std::string path = "/lights/" + std::to_string(id) + "/state/";
  223 + bool success = true;
  224 + int i = 0;
  225 + if (success && request.isMember("transitiontime"))
  226 + {
  227 + //Check if success was sent and the value was changed
  228 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt();
  229 + ++i;
  230 + }
  231 + if (success && request.isMember("on"))
  232 + {
  233 + //Check if success was sent and the value was changed
  234 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
  235 + ++i;
  236 + }
  237 + if (success && request.isMember("xy"))
  238 + {
  239 + //Check if success was sent and the value was changed
  240 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "xy"][0].asFloat() == request["xy"][0].asFloat();
  241 + if (success)
  242 + {
  243 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "xy"][1].asFloat() == request["xy"][1].asFloat();
  244 + }
  245 + }
  246 + return success;
  247 +}
  248 +
  249 +bool HueColorLight::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion)
  250 +{
  251 + float red = r / 255;
  252 + float green = g / 255;
  253 + float blue = b / 255;
  254 +
  255 + // gamma correction
  256 + red = (red > 0.04045f) ? pow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
  257 + green = (green > 0.04045f) ? pow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
  258 + blue = (blue > 0.04045f) ? pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);
  259 +
  260 + float X = red * 0.664511f + green * 0.154324f + blue * 0.162028f;
  261 + float Y = red * 0.283881f + green * 0.668433f + blue * 0.047685f;
  262 + float Z = red * 0.000088f + green * 0.072310f + blue * 0.986039f;
  263 +
  264 + float x = X / (X + Y + Z);
  265 + float y = Y / (X + Y + Z);
  266 +
  267 + return setColorXY(x, y, transistion);
  268 +}
  269 +
  270 +bool HueColorLight::setColorLoop(bool on)
  271 +{
  272 + //colorloop
  273 + refreshState();
  274 + Json::Value request(Json::objectValue);
  275 +
  276 + if (state["state"]["on"].asBool() != true)
  277 + {
  278 + request["on"] = true;
  279 + }
  280 + std::string effect;
  281 + if ((effect = on ? "colorloop" : "none") != state["state"]["effect"].asString())
  282 + {
  283 + request["effect"] = effect;
  284 + }
  285 + if (!request.isMember("on") && !request.isMember("effect"))
  286 + {
  287 + //Nothing needs to be changed
  288 + return true;
  289 + }
  290 +
  291 + Json::Value reply = SendPutRequest(request);
  292 +
  293 + //Check whether request was successful
  294 + std::string path = "/lights/" + std::to_string(id) + "/state/";
  295 + bool success = true;
  296 + int i = 0;
  297 + if (success && request.isMember("on"))
  298 + {
  299 + //Check if success was sent and the value was changed
  300 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
  301 + ++i;
  302 + }
  303 + if (success && request.isMember("effect"))
  304 + {
  305 + //Check if success was sent and the value was changed
  306 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "effect"].asString() == request["effect"].asString();
  307 + }
  308 + return success;
  309 +}
  310 +
  311 +bool HueColorLight::alertHueSaturation(uint16_t hue, uint8_t sat)
  312 +{
  313 + refreshState();
  314 + std::string cType = state["state"]["colormode"].asString();
  315 + bool on = state["state"]["on"].asBool();
  316 + if (cType == "hs")
  317 + {
  318 + uint16_t oldHue = state["state"]["hue"].asUInt();
  319 + uint8_t oldSat = state["state"]["sat"].asUInt();
  320 + if (!setColorHueSaturation(hue, sat, 1))
  321 + {
  322 + return false;
  323 + }
  324 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  325 + if (!alert())
  326 + {
  327 + return false;
  328 + }
  329 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  330 + if (!on)
  331 + {
  332 + return OffNoRefresh(1);
  333 + }
  334 + else
  335 + {
  336 + return setColorHueSaturation(oldHue, oldSat, 1);
  337 + }
  338 + }
  339 + else if (cType == "xy")
  340 + {
  341 + float oldX = state["state"]["xy"][0].asFloat();
  342 + float oldY = state["state"]["xy"][1].asFloat();
  343 + if (!setColorHueSaturation(hue, sat, 1))
  344 + {
  345 + return false;
  346 + }
  347 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  348 + if (!alert())
  349 + {
  350 + return false;
  351 + }
  352 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  353 + if (!on)
  354 + {
  355 + return OffNoRefresh(1);
  356 + }
  357 + else
  358 + {
  359 + return setColorXY(oldX, oldY, 1);
  360 + }
  361 + }
  362 + else
  363 + {
  364 + return false;
  365 + }
  366 +}
  367 +
  368 +bool HueColorLight::alertXY(float x, float y)
  369 +{
  370 + refreshState();
  371 + std::string cType = state["state"]["colormode"].asString();
  372 + bool on = state["state"]["on"].asBool();
  373 + if (cType == "hs")
  374 + {
  375 + uint16_t oldHue = state["state"]["hue"].asUInt();
  376 + uint8_t oldSat = state["state"]["sat"].asUInt();
  377 + if (!setColorXY(x, y, 1))
  378 + {
  379 + return false;
  380 + }
  381 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  382 + if (!alert())
  383 + {
  384 + return false;
  385 + }
  386 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  387 + if (!on)
  388 + {
  389 + return OffNoRefresh(1);
  390 + }
  391 + else
  392 + {
  393 + return setColorHueSaturation(oldHue, oldSat, 1);
  394 + }
  395 + }
  396 + else if (cType == "xy")
  397 + {
  398 + float oldX = state["state"]["xy"][0].asFloat();
  399 + float oldY = state["state"]["xy"][1].asFloat();
  400 + if (!setColorXY(x, y, 1))
  401 + {
  402 + return false;
  403 + }
  404 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  405 + if (!alert())
  406 + {
  407 + return false;
  408 + }
  409 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  410 + if (!on)
  411 + {
  412 + return OffNoRefresh(1);
  413 + }
  414 + else
  415 + {
  416 + return setColorXY(oldX, oldY, 1);
  417 + }
  418 + }
  419 + else
  420 + {
  421 + return false;
  422 + }
  423 +}
  424 +
  425 +bool HueColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b)
  426 +{
  427 + refreshState();
  428 + std::string cType = state["state"]["colormode"].asString();
  429 + bool on = state["state"]["on"].asBool();
  430 + if (cType == "hs")
  431 + {
  432 + uint16_t oldHue = state["state"]["hue"].asUInt();
  433 + uint8_t oldSat = state["state"]["sat"].asUInt();
  434 + if (!setColorRGB(r, g, b, 1))
  435 + {
  436 + return false;
  437 + }
  438 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  439 + if (!alert())
  440 + {
  441 + return false;
  442 + }
  443 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  444 + if (!on)
  445 + {
  446 + return OffNoRefresh(1);
  447 + }
  448 + else
  449 + {
  450 + return setColorHueSaturation(oldHue, oldSat, 1);
  451 + }
  452 + }
  453 + else if (cType == "xy")
  454 + {
  455 + float oldX = state["state"]["xy"][0].asFloat();
  456 + float oldY = state["state"]["xy"][1].asFloat();
  457 + if (!setColorRGB(r, g, b, 1))
  458 + {
  459 + return false;
  460 + }
  461 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  462 + if (!alert())
  463 + {
  464 + return false;
  465 + }
  466 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  467 + if (!on)
  468 + {
  469 + return OffNoRefresh(1);
  470 + }
  471 + else
  472 + {
  473 + return setColorXY(oldX, oldY, 1);
  474 + }
  475 + }
  476 + else
  477 + {
  478 + return false;
  479 + }
  480 +}
  481 +
  482 +/*bool HueColorLight::pointInTriangle(float pointx, float pointy, float x0, float y0, float x1, float y1, float x2, float y2)
  483 +{
  484 +float A = (-y1 * x2 + y0*(-x1 + x2) + x0*(y1 - y2) + x1 * y1);
  485 +int8_t sign = A < 0 ? -1 : 1;
  486 +float s = (y0 * x2 - x0 * y2 + (y2 - y0) * pointx + (x0 - x2) * pointy) * sign;
  487 +float t = (x0 * y1 - y0 * x1 + (y0 - y1) * pointx + (x1 - x0) * pointy) * sign;
  488 +
  489 +return s > 0 && t > 0 && (s + t) < A * sign;
  490 +}*/
... ...
hueplusplus/HueDimmableLight.cpp 0 → 100644
  1 +/**
  2 + \file HueDimmableLight.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2017 Jan Rogall - developer\n
  5 + Copyright (C) 2017 Moritz Wirger - developer\n
  6 +
  7 + This program is free software; you can redistribute it and/or modify
  8 + it under the terms of the GNU General Public License as published by
  9 + the Free SofHueDimmableLighttware Foundation; either version 3 of the License, or
  10 + (at your option) any later version.
  11 + This program is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 + You should have received a copy of the GNU General Public License
  16 + along with this program; if not, write to the Free Software Foundation,
  17 + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18 +**/
  19 +
  20 +#include "include/HueDimmableLight.h"
  21 +
  22 +#include <cmath>
  23 +#include <iostream>
  24 +#include <thread>
  25 +
  26 +bool HueDimmableLight::setBrightness(unsigned int bri, uint8_t transistion)
  27 +{
  28 + std::cout << "Setting lamp with id: " << id << " to brightness of " << bri << std::endl;
  29 + refreshState();
  30 + if (bri == 0)
  31 + {
  32 + if (state["state"]["on"] == true)
  33 + {
  34 + return OffNoRefresh(transistion);
  35 + }
  36 + else
  37 + {
  38 + return true;
  39 + }
  40 + }
  41 + else
  42 + {
  43 + Json::Value request(Json::objectValue);
  44 + if (transistion != 4)
  45 + {
  46 + request["transistiontime"] = transistion;
  47 + }
  48 + if (state["state"]["on"].asBool() != true)
  49 + {
  50 + request["on"] = true;
  51 + }
  52 + if (state["state"]["bri"].asUInt() != bri)
  53 + {
  54 + bri -= 1;
  55 + if (bri > 254)
  56 + {
  57 + bri = 254;
  58 + }
  59 + request["bri"] = bri;
  60 + }
  61 +
  62 + if (!request.isMember("on") && !request.isMember("bri"))
  63 + {
  64 + //Nothing needs to be changed
  65 + return true;
  66 + }
  67 +
  68 + Json::Value reply = SendPutRequest(request);
  69 +
  70 + //Check whether request was successful
  71 + std::string path = "/lights/" + std::to_string(id) + "/state/";
  72 + bool success = true;
  73 + int i = 0;
  74 + if (success && request.isMember("transistiontime"))
  75 + {
  76 + //Check if success was sent and the value was changed
  77 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt();
  78 + ++i;
  79 + }
  80 + if (success && request.isMember("on"))
  81 + {
  82 + //Check if success was sent and the value was changed
  83 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
  84 + ++i;
  85 + }
  86 + if (success && request.isMember("bri"))
  87 + {
  88 + //Check if success was sent and the value was changed
  89 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "bri"].asUInt() == request["bri"].asUInt();
  90 + }
  91 + return success;
  92 + }
  93 +}
... ...
hueplusplus/HueExtendedColorLight.cpp 0 → 100644
  1 +/**
  2 + \file HueExtendedColorLight.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2017 Jan Rogall - developer\n
  5 + Copyright (C) 2017 Moritz Wirger - developer\n
  6 +
  7 + This program is free software; you can redistribute it and/or modify
  8 + it under the terms of the GNU General Public License as published by
  9 + the Free Software Foundation; either version 3 of the License, or
  10 + (at your option) any later version.
  11 + This program is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 + You should have received a copy of the GNU General Public License
  16 + along with this program; if not, write to the Free Software Foundation,
  17 + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18 +**/
  19 +
  20 +#include "include/HueExtendedColorLight.h"
  21 +
  22 +#include <cmath>
  23 +#include <iostream>
  24 +#include <thread>
  25 +
  26 +bool HueExtendedColorLight::setColorTemperature(unsigned int mired, uint8_t transistion)
  27 +{
  28 + refreshState();
  29 + Json::Value request(Json::objectValue);
  30 + if (transistion != 4)
  31 + {
  32 + request["transitiontime"] = transistion;
  33 + }
  34 + if (state["state"]["on"].asBool() != true)
  35 + {
  36 + request["on"] = true;
  37 + }
  38 + if (state["state"]["ct"].asUInt() != mired || state["state"]["colormode"].asString() != "ct")
  39 + {
  40 + if (mired > 500)
  41 + {
  42 + mired = 500;
  43 + }
  44 + if (mired < 153)
  45 + {
  46 + mired = 153;
  47 + }
  48 + request["ct"] = mired;
  49 + }
  50 +
  51 + if (!request.isMember("on") && !request.isMember("ct"))
  52 + {
  53 + //Nothing needs to be changed
  54 + return true;
  55 + }
  56 +
  57 + Json::Value reply = SendPutRequest(request);
  58 +
  59 + //Check whether request was successful
  60 + std::string path = "/lights/" + std::to_string(id) + "/state/";
  61 + bool success = true;
  62 + int i = 0;
  63 + if (success && request.isMember("transitiontime"))
  64 + {
  65 + //Check if success was sent and the value was changed
  66 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt();
  67 + ++i;
  68 + }
  69 + if (success && request.isMember("on"))
  70 + {
  71 + //Check if success was sent and the value was changed
  72 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
  73 + ++i;
  74 + }
  75 + if (success && request.isMember("ct"))
  76 + {
  77 + //Check if success was sent and the value was changed
  78 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "ct"].asUInt() == request["ct"].asUInt();
  79 + }
  80 + return success;
  81 +}
  82 +
  83 +bool HueExtendedColorLight::alertTemperature(unsigned int mired)
  84 +{
  85 + refreshState();
  86 + std::string cType = state["state"]["colormode"].asString();
  87 + bool on = state["state"]["on"].asBool();
  88 + if (cType == "hs")
  89 + {
  90 + uint16_t oldHue = state["state"]["hue"].asUInt();
  91 + uint8_t oldSat = state["state"]["sat"].asUInt();
  92 + if (!setColorTemperature(mired, 1))
  93 + {
  94 + return false;
  95 + }
  96 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  97 + if (!alert())
  98 + {
  99 + return false;
  100 + }
  101 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  102 + if (!on)
  103 + {
  104 + return OffNoRefresh(1);
  105 + }
  106 + else
  107 + {
  108 + return setColorHueSaturation(oldHue, oldSat, 1);
  109 + }
  110 + }
  111 + else if (cType == "xy")
  112 + {
  113 + float oldX = state["state"]["xy"][0].asFloat();
  114 + float oldY = state["state"]["xy"][1].asFloat();
  115 + if (!setColorTemperature(mired, 1))
  116 + {
  117 + return false;
  118 + }
  119 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  120 + if (!alert())
  121 + {
  122 + return false;
  123 + }
  124 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  125 + if (!on)
  126 + {
  127 + return OffNoRefresh(1);
  128 + }
  129 + else
  130 + {
  131 + return setColorXY(oldX, oldY, 1);
  132 + }
  133 + }
  134 + else if (cType == "ct")
  135 + {
  136 + uint16_t oldCT = state["state"]["ct"].asUInt();
  137 + if (!setColorTemperature(mired, 1))
  138 + {
  139 + return false;
  140 + }
  141 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  142 + if (!alert())
  143 + {
  144 + return false;
  145 + }
  146 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  147 + if (!on)
  148 + {
  149 + return OffNoRefresh(1);
  150 + }
  151 + else
  152 + {
  153 + return setColorTemperature(oldCT, 1);
  154 + }
  155 + }
  156 + else
  157 + {
  158 + return false;
  159 + }
  160 +}
  161 +
  162 +bool HueExtendedColorLight::alertHueSaturation(uint16_t hue, uint8_t sat)
  163 +{
  164 + refreshState();
  165 + std::string cType = state["state"]["colormode"].asString();
  166 + bool on = state["state"]["on"].asBool();
  167 + if (cType == "hs")
  168 + {
  169 + uint16_t oldHue = state["state"]["hue"].asUInt();
  170 + uint8_t oldSat = state["state"]["sat"].asUInt();
  171 + if (!setColorHueSaturation(hue, sat, 1))
  172 + {
  173 + return false;
  174 + }
  175 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  176 + if (!alert())
  177 + {
  178 + return false;
  179 + }
  180 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  181 + if (!on)
  182 + {
  183 + return OffNoRefresh(1);
  184 + }
  185 + else
  186 + {
  187 + return setColorHueSaturation(oldHue, oldSat, 1);
  188 + }
  189 + }
  190 + else if (cType == "xy")
  191 + {
  192 + float oldX = state["state"]["xy"][0].asFloat();
  193 + float oldY = state["state"]["xy"][1].asFloat();
  194 + if (!setColorHueSaturation(hue, sat, 1))
  195 + {
  196 + return false;
  197 + }
  198 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  199 + if (!alert())
  200 + {
  201 + return false;
  202 + }
  203 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  204 + if (!on)
  205 + {
  206 + return OffNoRefresh(1);
  207 + }
  208 + else
  209 + {
  210 + return setColorXY(oldX, oldY, 1);
  211 + }
  212 + }
  213 + else if (cType == "ct")
  214 + {
  215 + uint16_t oldCT = state["state"]["ct"].asUInt();
  216 + if (!setColorHueSaturation(hue, sat, 1))
  217 + {
  218 + return false;
  219 + }
  220 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  221 + if (!alert())
  222 + {
  223 + return false;
  224 + }
  225 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  226 + if (!on)
  227 + {
  228 + return OffNoRefresh(1);
  229 + }
  230 + else
  231 + {
  232 + return setColorTemperature(oldCT, 1);
  233 + }
  234 + }
  235 + else
  236 + {
  237 + return false;
  238 + }
  239 +}
  240 +
  241 +bool HueExtendedColorLight::alertXY(float x, float y)
  242 +{
  243 + refreshState();
  244 + std::string cType = state["state"]["colormode"].asString();
  245 + bool on = state["state"]["on"].asBool();
  246 + if (cType == "hs")
  247 + {
  248 + uint16_t oldHue = state["state"]["hue"].asUInt();
  249 + uint8_t oldSat = state["state"]["sat"].asUInt();
  250 + if (!setColorXY(x, y, 1))
  251 + {
  252 + return false;
  253 + }
  254 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  255 + if (!alert())
  256 + {
  257 + return false;
  258 + }
  259 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  260 + if (!on)
  261 + {
  262 + return OffNoRefresh(1);
  263 + }
  264 + else
  265 + {
  266 + return setColorHueSaturation(oldHue, oldSat, 1);
  267 + }
  268 + }
  269 + else if (cType == "xy")
  270 + {
  271 + float oldX = state["state"]["xy"][0].asFloat();
  272 + float oldY = state["state"]["xy"][1].asFloat();
  273 + if (!setColorXY(x, y, 1))
  274 + {
  275 + return false;
  276 + }
  277 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  278 + if (!alert())
  279 + {
  280 + return false;
  281 + }
  282 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  283 + if (!on)
  284 + {
  285 + return OffNoRefresh(1);
  286 + }
  287 + else
  288 + {
  289 + return setColorXY(oldX, oldY, 1);
  290 + }
  291 + }
  292 + else if (cType == "ct")
  293 + {
  294 + uint16_t oldCT = state["state"]["ct"].asUInt();
  295 + if (!setColorXY(x, y, 1))
  296 + {
  297 + return false;
  298 + }
  299 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  300 + if (!alert())
  301 + {
  302 + return false;
  303 + }
  304 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  305 + if (!on)
  306 + {
  307 + return OffNoRefresh(1);
  308 + }
  309 + else
  310 + {
  311 + return setColorTemperature(oldCT, 1);
  312 + }
  313 + }
  314 + else
  315 + {
  316 + return false;
  317 + }
  318 +}
  319 +
  320 +bool HueExtendedColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b)
  321 +{
  322 + refreshState();
  323 + std::string cType = state["state"]["colormode"].asString();
  324 + bool on = state["state"]["on"].asBool();
  325 + if (cType == "hs")
  326 + {
  327 + uint16_t oldHue = state["state"]["hue"].asUInt();
  328 + uint8_t oldSat = state["state"]["sat"].asUInt();
  329 + if (!setColorRGB(r, g, b, 1))
  330 + {
  331 + return false;
  332 + }
  333 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  334 + if (!alert())
  335 + {
  336 + return false;
  337 + }
  338 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  339 + if (!on)
  340 + {
  341 + return OffNoRefresh(1);
  342 + }
  343 + else
  344 + {
  345 + return setColorHueSaturation(oldHue, oldSat, 1);
  346 + }
  347 + }
  348 + else if (cType == "xy")
  349 + {
  350 + float oldX = state["state"]["xy"][0].asFloat();
  351 + float oldY = state["state"]["xy"][1].asFloat();
  352 + if (!setColorRGB(r, g, b, 1))
  353 + {
  354 + return false;
  355 + }
  356 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  357 + if (!alert())
  358 + {
  359 + return false;
  360 + }
  361 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  362 + if (!on)
  363 + {
  364 + return OffNoRefresh(1);
  365 + }
  366 + else
  367 + {
  368 + return setColorXY(oldX, oldY, 1);
  369 + }
  370 + }
  371 + else if (cType == "ct")
  372 + {
  373 + uint16_t oldCT = state["state"]["ct"].asUInt();
  374 + if (!setColorRGB(r, g, b, 1))
  375 + {
  376 + return false;
  377 + }
  378 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  379 + if (!alert())
  380 + {
  381 + return false;
  382 + }
  383 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  384 + if (!on)
  385 + {
  386 + return OffNoRefresh(1);
  387 + }
  388 + else
  389 + {
  390 + return setColorTemperature(oldCT, 1);
  391 + }
  392 + }
  393 + else
  394 + {
  395 + return false;
  396 + }
  397 +}
... ...
hueplusplus/HueLight.cpp
... ... @@ -17,10 +17,11 @@
17 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 18 **/
19 19  
20   -#include "HueLight.h"
  20 +#include "include/HueLight.h"
21 21  
22   -#include "HttpHandler.h"
23   -#include "json/json.h"
  22 +
  23 +#include "include/HttpHandler.h"
  24 +#include "include/json/json.h"
24 25  
25 26 #include <cmath>
26 27 #include <iostream>
... ... @@ -210,1001 +211,3 @@ void HueLight::refreshState()
210 211 }
211 212 std::cout << "\tRefresh state took: " << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count() << "ms" << std::endl;
212 213 }
213   -
214   -bool HueDimmableLight::setBrightness(unsigned int bri, uint8_t transistion)
215   -{
216   - std::cout << "Setting lamp with id: " << id << " to brightness of " << bri << std::endl;
217   - refreshState();
218   - if (bri == 0)
219   - {
220   - if (state["state"]["on"] == true)
221   - {
222   - return OffNoRefresh(transistion);
223   - }
224   - else
225   - {
226   - return true;
227   - }
228   - }
229   - else
230   - {
231   - Json::Value request(Json::objectValue);
232   - if (transistion != 4)
233   - {
234   - request["transistiontime"] = transistion;
235   - }
236   - if (state["state"]["on"].asBool() != true)
237   - {
238   - request["on"] = true;
239   - }
240   - if (state["state"]["bri"].asUInt() != bri)
241   - {
242   - bri -= 1;
243   - if (bri > 254)
244   - {
245   - bri = 254;
246   - }
247   - request["bri"] = bri;
248   - }
249   -
250   - if (!request.isMember("on") && !request.isMember("bri"))
251   - {
252   - //Nothing needs to be changed
253   - return true;
254   - }
255   -
256   - Json::Value reply = SendPutRequest(request);
257   -
258   - //Check whether request was successful
259   - std::string path = "/lights/" + std::to_string(id) + "/state/";
260   - bool success = true;
261   - int i = 0;
262   - if (success && request.isMember("transistiontime"))
263   - {
264   - //Check if success was sent and the value was changed
265   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt();
266   - ++i;
267   - }
268   - if (success && request.isMember("on"))
269   - {
270   - //Check if success was sent and the value was changed
271   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
272   - ++i;
273   - }
274   - if (success && request.isMember("bri"))
275   - {
276   - //Check if success was sent and the value was changed
277   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "bri"].asUInt() == request["bri"].asUInt();
278   - }
279   - return success;
280   - }
281   -}
282   -
283   -bool HueTemperatureLight::setColorTemperature(unsigned int mired, uint8_t transistion)
284   -{
285   - refreshState();
286   - Json::Value request(Json::objectValue);
287   - if (transistion != 4)
288   - {
289   - request["transistiontime"] = transistion;
290   - }
291   - if (state["state"]["on"].asBool() != true)
292   - {
293   - request["on"] = true;
294   - }
295   - if (state["state"]["ct"].asUInt() != mired)
296   - {
297   - if (mired > 500)
298   - {
299   - mired = 500;
300   - }
301   - if (mired < 153)
302   - {
303   - mired = 153;
304   - }
305   - request["ct"] = mired;
306   - }
307   -
308   - if (!request.isMember("on") && !request.isMember("ct"))
309   - {
310   - //Nothing needs to be changed
311   - return true;
312   - }
313   -
314   - Json::Value reply = SendPutRequest(request);
315   -
316   - //Check whether request was successful
317   - std::string path = "/lights/" + std::to_string(id) + "/state/";
318   - bool success = true;
319   - int i = 0;
320   - if (success && request.isMember("transistiontime"))
321   - {
322   - //Check if success was sent and the value was changed
323   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt();
324   - ++i;
325   - }
326   - if (success && request.isMember("on"))
327   - {
328   - //Check if success was sent and the value was changed
329   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
330   - ++i;
331   - }
332   - if (success && request.isMember("ct"))
333   - {
334   - //Check if success was sent and the value was changed
335   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "ct"].asUInt() == request["ct"].asUInt();
336   - }
337   - return success;
338   -}
339   -
340   -bool HueTemperatureLight::alertTemperature(unsigned int mired)
341   -{
342   - refreshState();
343   - std::string cType = state["state"]["colormode"].asString();
344   - bool on = state["state"]["on"].asBool();
345   - if (cType == "ct")
346   - {
347   - uint16_t oldCT = state["state"]["ct"].asUInt();
348   - if (!setColorTemperature(mired, 1))
349   - {
350   - return false;
351   - }
352   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
353   - if (!alert())
354   - {
355   - return false;
356   - }
357   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
358   - if (!on)
359   - {
360   - return OffNoRefresh(1);
361   - }
362   - else
363   - {
364   - return setColorTemperature(oldCT, 1);
365   - }
366   - }
367   - else
368   - {
369   - return false;
370   - }
371   -}
372   -
373   -bool HueColorLight::setColorHue(uint16_t hue, uint8_t transistion)
374   -{
375   - refreshState();
376   - Json::Value request(Json::objectValue);
377   - if (transistion != 4)
378   - {
379   - request["transistiontime"] = transistion;
380   - }
381   - if (state["state"]["on"].asBool() != true)
382   - {
383   - request["on"] = true;
384   - }
385   - if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs")
386   - {
387   - hue = hue % 65535;
388   - request["hue"] = hue;
389   - }
390   -
391   - if (!request.isMember("on") && !request.isMember("hue"))
392   - {
393   - //Nothing needs to be changed
394   - return true;
395   - }
396   -
397   - Json::Value reply = SendPutRequest(request);
398   -
399   - //Check whether request was successful
400   - std::string path = "/lights/" + std::to_string(id) + "/state/";
401   - bool success = true;
402   - int i = 0;
403   - if (success && request.isMember("transistiontime"))
404   - {
405   - //Check if success was sent and the value was changed
406   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt();
407   - ++i;
408   - }
409   - if (success && request.isMember("on"))
410   - {
411   - //Check if success was sent and the value was changed
412   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
413   - ++i;
414   - }
415   - if (success && request.isMember("hue"))
416   - {
417   - //Check if success was sent and the value was changed
418   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "hue"].asUInt() == request["hue"].asUInt();
419   - }
420   - return success;
421   -}
422   -
423   -bool HueColorLight::setColorSaturation(uint8_t sat, uint8_t transistion)
424   -{
425   - refreshState();
426   - Json::Value request(Json::objectValue);
427   - if (transistion != 4)
428   - {
429   - request["transistiontime"] = transistion;
430   - }
431   - if (state["state"]["on"].asBool() != true)
432   - {
433   - request["on"] = true;
434   - }
435   - if (state["state"]["sat"].asUInt() != sat)
436   - {
437   - if (sat > 254)
438   - {
439   - sat = 254;
440   - }
441   - request["sat"] = sat;
442   - }
443   -
444   - if (!request.isMember("on") && !request.isMember("sat"))
445   - {
446   - //Nothing needs to be changed
447   - return true;
448   - }
449   -
450   - Json::Value reply = SendPutRequest(request);
451   -
452   - //Check whether request was successful
453   - std::string path = "/lights/" + std::to_string(id) + "/state/";
454   - bool success = true;
455   - int i = 0;
456   - if (success && request.isMember("transistiontime"))
457   - {
458   - //Check if success was sent and the value was changed
459   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt();
460   - ++i;
461   - }
462   - if (success && request.isMember("on"))
463   - {
464   - //Check if success was sent and the value was changed
465   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
466   - ++i;
467   - }
468   - if (success && request.isMember("sat"))
469   - {
470   - //Check if success was sent and the value was changed
471   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "sat"].asUInt() == request["sat"].asUInt();
472   - }
473   - return success;
474   -}
475   -
476   -bool HueColorLight::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transistion)
477   -{
478   - refreshState();
479   - Json::Value request(Json::objectValue);
480   -
481   - if (transistion != 4)
482   - {
483   - request["transitiontime"] = transistion;
484   - }
485   - if (state["state"]["on"].asBool() != true)
486   - {
487   - request["on"] = true;
488   - }
489   - if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs")
490   - {
491   - hue = hue % 65535;
492   - request["hue"] = hue;
493   - }
494   - if (state["state"]["sat"].asUInt() != sat || state["state"]["colormode"].asString() != "hs")
495   - {
496   - if (sat > 254)
497   - {
498   - sat = 254;
499   - }
500   - request["sat"] = sat;
501   - }
502   -
503   - if (!request.isMember("on") && !request.isMember("hue") && !request.isMember("sat"))
504   - {
505   - //Nothing needs to be changed
506   - return true;
507   - }
508   -
509   - Json::Value reply = SendPutRequest(request);
510   -
511   - //Check whether request was successful
512   - std::string path = "/lights/" + std::to_string(id) + "/state/";
513   - bool success = true;
514   - int i = 0;
515   - if (success && request.isMember("transitiontime"))
516   - {
517   - //Check if success was sent and the value was changed
518   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt();
519   - ++i;
520   - }
521   - if (success && request.isMember("on"))
522   - {
523   - //Check if success was sent and the value was changed
524   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
525   - ++i;
526   - }
527   - if (success && request.isMember("hue"))
528   - {
529   - //Check if success was sent and the value was changed
530   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "hue"].asUInt() == request["hue"].asUInt();
531   - ++i;
532   - }
533   - if (success && request.isMember("sat"))
534   - {
535   - //Check if success was sent and the value was changed
536   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "sat"].asUInt() == request["sat"].asUInt();
537   - }
538   - return success;
539   -}
540   -
541   -bool HueColorLight::setColorXY(float x, float y, uint8_t transistion)
542   -{
543   - refreshState();
544   - Json::Value request(Json::objectValue);
545   -
546   - if (transistion != 4)
547   - {
548   - request["transitiontime"] = transistion;
549   - }
550   - if (state["state"]["on"].asBool() != true)
551   - {
552   - request["on"] = true;
553   - }
554   - if (state["state"]["xy"][0].asFloat() != x || state["state"]["xy"][1].asFloat() != y || state["state"]["colormode"].asString() != "xy")
555   - {
556   - request["xy"][0] = x;
557   - request["xy"][1] = y;
558   - }
559   -
560   - if (!request.isMember("on") && !request.isMember("xy"))
561   - {
562   - //Nothing needs to be changed
563   - return true;
564   - }
565   -
566   - Json::Value reply = SendPutRequest(request);
567   -
568   - //Check whether request was successful
569   - std::string path = "/lights/" + std::to_string(id) + "/state/";
570   - bool success = true;
571   - int i = 0;
572   - if (success && request.isMember("transitiontime"))
573   - {
574   - //Check if success was sent and the value was changed
575   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt();
576   - ++i;
577   - }
578   - if (success && request.isMember("on"))
579   - {
580   - //Check if success was sent and the value was changed
581   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
582   - ++i;
583   - }
584   - if (success && request.isMember("xy"))
585   - {
586   - //Check if success was sent and the value was changed
587   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "xy"][0].asFloat() == request["xy"][0].asFloat();
588   - if (success)
589   - {
590   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "xy"][1].asFloat() == request["xy"][1].asFloat();
591   - }
592   - }
593   - return success;
594   -}
595   -
596   -bool HueColorLight::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion)
597   -{
598   - float red = r / 255;
599   - float green = g / 255;
600   - float blue = b / 255;
601   -
602   - // gamma correction
603   - red = (red > 0.04045f) ? pow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f);
604   - green = (green > 0.04045f) ? pow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f);
605   - blue = (blue > 0.04045f) ? pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f);
606   -
607   - float X = red * 0.664511f + green * 0.154324f + blue * 0.162028f;
608   - float Y = red * 0.283881f + green * 0.668433f + blue * 0.047685f;
609   - float Z = red * 0.000088f + green * 0.072310f + blue * 0.986039f;
610   -
611   - float x = X / (X + Y + Z);
612   - float y = Y / (X + Y + Z);
613   -
614   - return setColorXY(x, y, transistion);
615   -}
616   -
617   -bool HueColorLight::setColorLoop(bool on)
618   -{
619   - //colorloop
620   - refreshState();
621   - Json::Value request(Json::objectValue);
622   -
623   - if (state["state"]["on"].asBool() != true)
624   - {
625   - request["on"] = true;
626   - }
627   - std::string effect;
628   - if ((effect = on ? "colorloop" : "none") != state["state"]["effect"].asString())
629   - {
630   - request["effect"] = effect;
631   - }
632   - if (!request.isMember("on") && !request.isMember("effect"))
633   - {
634   - //Nothing needs to be changed
635   - return true;
636   - }
637   -
638   - Json::Value reply = SendPutRequest(request);
639   -
640   - //Check whether request was successful
641   - std::string path = "/lights/" + std::to_string(id) + "/state/";
642   - bool success = true;
643   - int i = 0;
644   - if (success && request.isMember("on"))
645   - {
646   - //Check if success was sent and the value was changed
647   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
648   - ++i;
649   - }
650   - if (success && request.isMember("effect"))
651   - {
652   - //Check if success was sent and the value was changed
653   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "effect"].asString() == request["effect"].asString();
654   - }
655   - return success;
656   -}
657   -
658   -bool HueColorLight::alertHueSaturation(uint16_t hue, uint8_t sat)
659   -{
660   - refreshState();
661   - std::string cType = state["state"]["colormode"].asString();
662   - bool on = state["state"]["on"].asBool();
663   - if (cType == "hs")
664   - {
665   - uint16_t oldHue = state["state"]["hue"].asUInt();
666   - uint8_t oldSat = state["state"]["sat"].asUInt();
667   - if (!setColorHueSaturation(hue, sat, 1))
668   - {
669   - return false;
670   - }
671   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
672   - if (!alert())
673   - {
674   - return false;
675   - }
676   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
677   - if (!on)
678   - {
679   - return OffNoRefresh(1);
680   - }
681   - else
682   - {
683   - return setColorHueSaturation(oldHue, oldSat, 1);
684   - }
685   - }
686   - else if (cType == "xy")
687   - {
688   - float oldX = state["state"]["xy"][0].asFloat();
689   - float oldY = state["state"]["xy"][1].asFloat();
690   - if (!setColorHueSaturation(hue, sat, 1))
691   - {
692   - return false;
693   - }
694   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
695   - if (!alert())
696   - {
697   - return false;
698   - }
699   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
700   - if (!on)
701   - {
702   - return OffNoRefresh(1);
703   - }
704   - else
705   - {
706   - return setColorXY(oldX, oldY, 1);
707   - }
708   - }
709   - else
710   - {
711   - return false;
712   - }
713   -}
714   -
715   -bool HueColorLight::alertXY(float x, float y)
716   -{
717   - refreshState();
718   - std::string cType = state["state"]["colormode"].asString();
719   - bool on = state["state"]["on"].asBool();
720   - if (cType == "hs")
721   - {
722   - uint16_t oldHue = state["state"]["hue"].asUInt();
723   - uint8_t oldSat = state["state"]["sat"].asUInt();
724   - if (!setColorXY(x, y, 1))
725   - {
726   - return false;
727   - }
728   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
729   - if (!alert())
730   - {
731   - return false;
732   - }
733   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
734   - if (!on)
735   - {
736   - return OffNoRefresh(1);
737   - }
738   - else
739   - {
740   - return setColorHueSaturation(oldHue, oldSat, 1);
741   - }
742   - }
743   - else if (cType == "xy")
744   - {
745   - float oldX = state["state"]["xy"][0].asFloat();
746   - float oldY = state["state"]["xy"][1].asFloat();
747   - if (!setColorXY(x, y, 1))
748   - {
749   - return false;
750   - }
751   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
752   - if (!alert())
753   - {
754   - return false;
755   - }
756   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
757   - if (!on)
758   - {
759   - return OffNoRefresh(1);
760   - }
761   - else
762   - {
763   - return setColorXY(oldX, oldY, 1);
764   - }
765   - }
766   - else
767   - {
768   - return false;
769   - }
770   -}
771   -
772   -bool HueColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b)
773   -{
774   - refreshState();
775   - std::string cType = state["state"]["colormode"].asString();
776   - bool on = state["state"]["on"].asBool();
777   - if (cType == "hs")
778   - {
779   - uint16_t oldHue = state["state"]["hue"].asUInt();
780   - uint8_t oldSat = state["state"]["sat"].asUInt();
781   - if (!setColorRGB(r, g, b, 1))
782   - {
783   - return false;
784   - }
785   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
786   - if (!alert())
787   - {
788   - return false;
789   - }
790   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
791   - if (!on)
792   - {
793   - return OffNoRefresh(1);
794   - }
795   - else
796   - {
797   - return setColorHueSaturation(oldHue, oldSat, 1);
798   - }
799   - }
800   - else if (cType == "xy")
801   - {
802   - float oldX = state["state"]["xy"][0].asFloat();
803   - float oldY = state["state"]["xy"][1].asFloat();
804   - if (!setColorRGB(r, g, b, 1))
805   - {
806   - return false;
807   - }
808   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
809   - if (!alert())
810   - {
811   - return false;
812   - }
813   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
814   - if (!on)
815   - {
816   - return OffNoRefresh(1);
817   - }
818   - else
819   - {
820   - return setColorXY(oldX, oldY, 1);
821   - }
822   - }
823   - else
824   - {
825   - return false;
826   - }
827   -}
828   -
829   -/*bool HueColorLight::pointInTriangle(float pointx, float pointy, float x0, float y0, float x1, float y1, float x2, float y2)
830   -{
831   -float A = (-y1 * x2 + y0*(-x1 + x2) + x0*(y1 - y2) + x1 * y1);
832   -int8_t sign = A < 0 ? -1 : 1;
833   -float s = (y0 * x2 - x0 * y2 + (y2 - y0) * pointx + (x0 - x2) * pointy) * sign;
834   -float t = (x0 * y1 - y0 * x1 + (y0 - y1) * pointx + (x1 - x0) * pointy) * sign;
835   -
836   -return s > 0 && t > 0 && (s + t) < A * sign;
837   -}*/
838   -
839   -bool HueExtendedColorLight::setColorTemperature(unsigned int mired, uint8_t transistion)
840   -{
841   - refreshState();
842   - Json::Value request(Json::objectValue);
843   - if (transistion != 4)
844   - {
845   - request["transitiontime"] = transistion;
846   - }
847   - if (state["state"]["on"].asBool() != true)
848   - {
849   - request["on"] = true;
850   - }
851   - if (state["state"]["ct"].asUInt() != mired || state["state"]["colormode"].asString() != "ct")
852   - {
853   - if (mired > 500)
854   - {
855   - mired = 500;
856   - }
857   - if (mired < 153)
858   - {
859   - mired = 153;
860   - }
861   - request["ct"] = mired;
862   - }
863   -
864   - if (!request.isMember("on") && !request.isMember("ct"))
865   - {
866   - //Nothing needs to be changed
867   - return true;
868   - }
869   -
870   - Json::Value reply = SendPutRequest(request);
871   -
872   - //Check whether request was successful
873   - std::string path = "/lights/" + std::to_string(id) + "/state/";
874   - bool success = true;
875   - int i = 0;
876   - if (success && request.isMember("transitiontime"))
877   - {
878   - //Check if success was sent and the value was changed
879   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt();
880   - ++i;
881   - }
882   - if (success && request.isMember("on"))
883   - {
884   - //Check if success was sent and the value was changed
885   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
886   - ++i;
887   - }
888   - if (success && request.isMember("ct"))
889   - {
890   - //Check if success was sent and the value was changed
891   - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "ct"].asUInt() == request["ct"].asUInt();
892   - }
893   - return success;
894   -}
895   -
896   -bool HueExtendedColorLight::alertTemperature(unsigned int mired)
897   -{
898   - refreshState();
899   - std::string cType = state["state"]["colormode"].asString();
900   - bool on = state["state"]["on"].asBool();
901   - if (cType == "hs")
902   - {
903   - uint16_t oldHue = state["state"]["hue"].asUInt();
904   - uint8_t oldSat = state["state"]["sat"].asUInt();
905   - if (!setColorTemperature(mired, 1))
906   - {
907   - return false;
908   - }
909   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
910   - if (!alert())
911   - {
912   - return false;
913   - }
914   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
915   - if (!on)
916   - {
917   - return OffNoRefresh(1);
918   - }
919   - else
920   - {
921   - return setColorHueSaturation(oldHue, oldSat, 1);
922   - }
923   - }
924   - else if (cType == "xy")
925   - {
926   - float oldX = state["state"]["xy"][0].asFloat();
927   - float oldY = state["state"]["xy"][1].asFloat();
928   - if (!setColorTemperature(mired, 1))
929   - {
930   - return false;
931   - }
932   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
933   - if (!alert())
934   - {
935   - return false;
936   - }
937   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
938   - if (!on)
939   - {
940   - return OffNoRefresh(1);
941   - }
942   - else
943   - {
944   - return setColorXY(oldX, oldY, 1);
945   - }
946   - }
947   - else if (cType == "ct")
948   - {
949   - uint16_t oldCT = state["state"]["ct"].asUInt();
950   - if (!setColorTemperature(mired, 1))
951   - {
952   - return false;
953   - }
954   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
955   - if (!alert())
956   - {
957   - return false;
958   - }
959   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
960   - if (!on)
961   - {
962   - return OffNoRefresh(1);
963   - }
964   - else
965   - {
966   - return setColorTemperature(oldCT, 1);
967   - }
968   - }
969   - else
970   - {
971   - return false;
972   - }
973   -}
974   -
975   -bool HueExtendedColorLight::alertHueSaturation(uint16_t hue, uint8_t sat)
976   -{
977   - refreshState();
978   - std::string cType = state["state"]["colormode"].asString();
979   - bool on = state["state"]["on"].asBool();
980   - if (cType == "hs")
981   - {
982   - uint16_t oldHue = state["state"]["hue"].asUInt();
983   - uint8_t oldSat = state["state"]["sat"].asUInt();
984   - if (!setColorHueSaturation(hue, sat, 1))
985   - {
986   - return false;
987   - }
988   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
989   - if (!alert())
990   - {
991   - return false;
992   - }
993   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
994   - if (!on)
995   - {
996   - return OffNoRefresh(1);
997   - }
998   - else
999   - {
1000   - return setColorHueSaturation(oldHue, oldSat, 1);
1001   - }
1002   - }
1003   - else if (cType == "xy")
1004   - {
1005   - float oldX = state["state"]["xy"][0].asFloat();
1006   - float oldY = state["state"]["xy"][1].asFloat();
1007   - if (!setColorHueSaturation(hue, sat, 1))
1008   - {
1009   - return false;
1010   - }
1011   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
1012   - if (!alert())
1013   - {
1014   - return false;
1015   - }
1016   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
1017   - if (!on)
1018   - {
1019   - return OffNoRefresh(1);
1020   - }
1021   - else
1022   - {
1023   - return setColorXY(oldX, oldY, 1);
1024   - }
1025   - }
1026   - else if (cType == "ct")
1027   - {
1028   - uint16_t oldCT = state["state"]["ct"].asUInt();
1029   - if (!setColorHueSaturation(hue, sat, 1))
1030   - {
1031   - return false;
1032   - }
1033   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
1034   - if (!alert())
1035   - {
1036   - return false;
1037   - }
1038   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
1039   - if (!on)
1040   - {
1041   - return OffNoRefresh(1);
1042   - }
1043   - else
1044   - {
1045   - return setColorTemperature(oldCT, 1);
1046   - }
1047   - }
1048   - else
1049   - {
1050   - return false;
1051   - }
1052   -}
1053   -
1054   -bool HueExtendedColorLight::alertXY(float x, float y)
1055   -{
1056   - refreshState();
1057   - std::string cType = state["state"]["colormode"].asString();
1058   - bool on = state["state"]["on"].asBool();
1059   - if (cType == "hs")
1060   - {
1061   - uint16_t oldHue = state["state"]["hue"].asUInt();
1062   - uint8_t oldSat = state["state"]["sat"].asUInt();
1063   - if (!setColorXY(x, y, 1))
1064   - {
1065   - return false;
1066   - }
1067   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
1068   - if (!alert())
1069   - {
1070   - return false;
1071   - }
1072   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
1073   - if (!on)
1074   - {
1075   - return OffNoRefresh(1);
1076   - }
1077   - else
1078   - {
1079   - return setColorHueSaturation(oldHue, oldSat, 1);
1080   - }
1081   - }
1082   - else if (cType == "xy")
1083   - {
1084   - float oldX = state["state"]["xy"][0].asFloat();
1085   - float oldY = state["state"]["xy"][1].asFloat();
1086   - if (!setColorXY(x, y, 1))
1087   - {
1088   - return false;
1089   - }
1090   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
1091   - if (!alert())
1092   - {
1093   - return false;
1094   - }
1095   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
1096   - if (!on)
1097   - {
1098   - return OffNoRefresh(1);
1099   - }
1100   - else
1101   - {
1102   - return setColorXY(oldX, oldY, 1);
1103   - }
1104   - }
1105   - else if (cType == "ct")
1106   - {
1107   - uint16_t oldCT = state["state"]["ct"].asUInt();
1108   - if (!setColorXY(x, y, 1))
1109   - {
1110   - return false;
1111   - }
1112   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
1113   - if (!alert())
1114   - {
1115   - return false;
1116   - }
1117   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
1118   - if (!on)
1119   - {
1120   - return OffNoRefresh(1);
1121   - }
1122   - else
1123   - {
1124   - return setColorTemperature(oldCT, 1);
1125   - }
1126   - }
1127   - else
1128   - {
1129   - return false;
1130   - }
1131   -}
1132   -
1133   -bool HueExtendedColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b)
1134   -{
1135   - refreshState();
1136   - std::string cType = state["state"]["colormode"].asString();
1137   - bool on = state["state"]["on"].asBool();
1138   - if (cType == "hs")
1139   - {
1140   - uint16_t oldHue = state["state"]["hue"].asUInt();
1141   - uint8_t oldSat = state["state"]["sat"].asUInt();
1142   - if (!setColorRGB(r, g, b, 1))
1143   - {
1144   - return false;
1145   - }
1146   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
1147   - if (!alert())
1148   - {
1149   - return false;
1150   - }
1151   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
1152   - if (!on)
1153   - {
1154   - return OffNoRefresh(1);
1155   - }
1156   - else
1157   - {
1158   - return setColorHueSaturation(oldHue, oldSat, 1);
1159   - }
1160   - }
1161   - else if (cType == "xy")
1162   - {
1163   - float oldX = state["state"]["xy"][0].asFloat();
1164   - float oldY = state["state"]["xy"][1].asFloat();
1165   - if (!setColorRGB(r, g, b, 1))
1166   - {
1167   - return false;
1168   - }
1169   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
1170   - if (!alert())
1171   - {
1172   - return false;
1173   - }
1174   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
1175   - if (!on)
1176   - {
1177   - return OffNoRefresh(1);
1178   - }
1179   - else
1180   - {
1181   - return setColorXY(oldX, oldY, 1);
1182   - }
1183   - }
1184   - else if (cType == "ct")
1185   - {
1186   - uint16_t oldCT = state["state"]["ct"].asUInt();
1187   - if (!setColorRGB(r, g, b, 1))
1188   - {
1189   - return false;
1190   - }
1191   - std::this_thread::sleep_for(std::chrono::milliseconds(110));
1192   - if (!alert())
1193   - {
1194   - return false;
1195   - }
1196   - std::this_thread::sleep_for(std::chrono::milliseconds(1500));
1197   - if (!on)
1198   - {
1199   - return OffNoRefresh(1);
1200   - }
1201   - else
1202   - {
1203   - return setColorTemperature(oldCT, 1);
1204   - }
1205   - }
1206   - else
1207   - {
1208   - return false;
1209   - }
1210   -}
... ...
hueplusplus/HueTemperatureLight.cpp 0 → 100644
  1 +/**
  2 + \file HueTemperatureLight.cpp
  3 + Copyright Notice\n
  4 + Copyright (C) 2017 Jan Rogall - developer\n
  5 + Copyright (C) 2017 Moritz Wirger - developer\n
  6 +
  7 + This program is free software; you can redistribute it and/or modify
  8 + it under the terms of the GNU General Public License as published by
  9 + the Free Software Foundation; either version 3 of the License, or
  10 + (at your option) any later version.
  11 + This program is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 + You should have received a copy of the GNU General Public License
  16 + along with this program; if not, write to the Free Software Foundation,
  17 + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18 +**/
  19 +
  20 +#include "include/HueTemperatureLight.h"
  21 +
  22 +#include <cmath>
  23 +#include <iostream>
  24 +#include <thread>
  25 +
  26 +
  27 +bool HueTemperatureLight::setColorTemperature(unsigned int mired, uint8_t transistion)
  28 +{
  29 + refreshState();
  30 + Json::Value request(Json::objectValue);
  31 + if (transistion != 4)
  32 + {
  33 + request["transistiontime"] = transistion;
  34 + }
  35 + if (state["state"]["on"].asBool() != true)
  36 + {
  37 + request["on"] = true;
  38 + }
  39 + if (state["state"]["ct"].asUInt() != mired)
  40 + {
  41 + if (mired > 500)
  42 + {
  43 + mired = 500;
  44 + }
  45 + if (mired < 153)
  46 + {
  47 + mired = 153;
  48 + }
  49 + request["ct"] = mired;
  50 + }
  51 +
  52 + if (!request.isMember("on") && !request.isMember("ct"))
  53 + {
  54 + //Nothing needs to be changed
  55 + return true;
  56 + }
  57 +
  58 + Json::Value reply = SendPutRequest(request);
  59 +
  60 + //Check whether request was successful
  61 + std::string path = "/lights/" + std::to_string(id) + "/state/";
  62 + bool success = true;
  63 + int i = 0;
  64 + if (success && request.isMember("transistiontime"))
  65 + {
  66 + //Check if success was sent and the value was changed
  67 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt();
  68 + ++i;
  69 + }
  70 + if (success && request.isMember("on"))
  71 + {
  72 + //Check if success was sent and the value was changed
  73 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool();
  74 + ++i;
  75 + }
  76 + if (success && request.isMember("ct"))
  77 + {
  78 + //Check if success was sent and the value was changed
  79 + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "ct"].asUInt() == request["ct"].asUInt();
  80 + }
  81 + return success;
  82 +}
  83 +
  84 +bool HueTemperatureLight::alertTemperature(unsigned int mired)
  85 +{
  86 + refreshState();
  87 + std::string cType = state["state"]["colormode"].asString();
  88 + bool on = state["state"]["on"].asBool();
  89 + if (cType == "ct")
  90 + {
  91 + uint16_t oldCT = state["state"]["ct"].asUInt();
  92 + if (!setColorTemperature(mired, 1))
  93 + {
  94 + return false;
  95 + }
  96 + std::this_thread::sleep_for(std::chrono::milliseconds(110));
  97 + if (!alert())
  98 + {
  99 + return false;
  100 + }
  101 + std::this_thread::sleep_for(std::chrono::milliseconds(1500));
  102 + if (!on)
  103 + {
  104 + return OffNoRefresh(1);
  105 + }
  106 + else
  107 + {
  108 + return setColorTemperature(oldCT, 1);
  109 + }
  110 + }
  111 + else
  112 + {
  113 + return false;
  114 + }
  115 +}
  116 +
... ...
hueplusplus/UPnP.cpp
1   -#include "UPnP.h"
2   -#include "HttpHandler.h"
  1 +#include "include/UPnP.h"
  2 +#include "include/HttpHandler.h"
3 3 #include <algorithm>
4 4 #include <iostream>
5 5  
... ...
hueplusplus/HttpHandler.h renamed to hueplusplus/include/HttpHandler.h
hueplusplus/Hue.h renamed to hueplusplus/include/Hue.h
hueplusplus/include/HueColorLight.h 0 → 100644
  1 +#ifndef _HUE_COLOR_LIGHT
  2 +#define _HUE_COLOR_LIGHT
  3 +
  4 +#include "HueDimmableLight.h"
  5 +
  6 +// xy beats ct beats hue/sat.
  7 +// supports groups, scenes, on / off, dimming and color control(hue / saturation, enhanced hue, color loop and XY)
  8 +class HueColorLight : public HueDimmableLight
  9 +{
  10 + friend class Hue;
  11 +
  12 +public:
  13 + //! Function to set the color of this light with specified hue. Ranging from 0 to 65535, whereas 65535 and 0 are red, 25500 is green and 46920 is blue.
  14 + //! \param hue uint16_t that specifies the hue
  15 + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
  16 + //! \return Bool that is true on success
  17 + virtual bool setColorHue(uint16_t hue, uint8_t transistion = 4);
  18 +
  19 + //! Function to set the saturation of color of this light with specified saturation. Ranging from 0 to 254, whereas 0 is least saturated (white) and 254 is most saturated.
  20 + //! \param sat uint8_t that specifies the saturation
  21 + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
  22 + //! \return Bool that is true on success
  23 + virtual bool setColorSaturation(uint8_t sat, uint8_t transistion = 4);
  24 +
  25 + //! Function to set the color of this light with specified hue and saturation.
  26 + //! \param hue uint16_t that specifies the hue
  27 + //! \param sat uint8_t that specifies the saturation
  28 + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms.
  29 + //! \return Bool that is true on success
  30 + virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4);
  31 +
  32 + //! Function to set the color of this light in CIE with specified x y. Where x and y are ranging from 0 to 1.
  33 + //! \param x float that specifies the x coordinate in CIE
  34 + //! \param y float that specifies the y coordinate in CIE
  35 + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
  36 + //! \return Bool that is true on success
  37 + virtual bool setColorXY(float x, float y, uint8_t transistion = 4);
  38 +
  39 + //! Function to set the color of this light with red green and blue values. Where red, green and blue are ranging from 0 to 255.
  40 + //! \param r uint8_t that specifies the red color percentage
  41 + //! \param g uint8_t that specifies the green color percentage
  42 + //! \param b uint8_t that specifies the blue color percentage
  43 + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
  44 + //! \return Bool that is true on success
  45 + virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion = 4);
  46 +
  47 + //! Function to enable colorloop effect. The colorloop effect will loop through all colors on current hue and saturation levels.
  48 + //! Notice that none of the setter functions check for this and the colorloop can only be disabled via this function.
  49 + //! or by simply calling Off()/OffNoRefresh() and then On()/OnNoRefresh(),
  50 + //! so you could alternatively call Off() and then use any of the setter functions
  51 + //! \param on bool that enables this feature when true and disables it when false
  52 + //! \return Bool that is true on success
  53 + virtual bool setColorLoop(bool on);
  54 +
  55 + //! Function that lets the light perform one breath cycle in specified color.
  56 + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
  57 + //! \param hue uint16_t that specifies the hue
  58 + //! \param sat uint8_t that specifies the saturation
  59 + //! \return Bool that is true on success
  60 + bool alertHueSaturation(uint16_t hue, uint8_t sat);
  61 +
  62 + //! Function that lets the light perform one breath cycle in specified color.
  63 + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
  64 + //! \param x float that specifies the x coordinate in CIE
  65 + //! \param y float that specifies the y coordinate in CIE
  66 + //! \return Bool that is true on success
  67 + bool alertXY(float x, float y);
  68 +
  69 + //! Function that lets the light perform one breath cycle in specified color.
  70 + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
  71 + //! \param r uint8_t that specifies the red color percentage
  72 + //! \param g uint8_t that specifies the green color percentage
  73 + //! \param b uint8_t that specifies the blue color percentage
  74 + //! \return Bool that is true on success
  75 + bool alertRGB(uint8_t r, uint8_t g, uint8_t b);
  76 +
  77 +protected:
  78 + //! protected ctor that is used by \ref Hue class.
  79 + //! \param ip String that specifies the ip of the Hue bridge
  80 + //! \param username String that specifies the username used to control the bridge
  81 + //! \param id Integer that specifies the id of this light
  82 + HueColorLight(const std::string& ip, const std::string& username, int id) : HueDimmableLight(ip, username, id) {};
  83 +
  84 + /*private:
  85 + bool pointInTriangle(float pointx, float pointy, float x0, float y0, float x1, float y1, float x2, float y2); // currently unused because Hue bridge handles this*/
  86 +};
  87 +#endif
... ...
hueplusplus/include/HueDimmableLight.h 0 → 100644
  1 +#ifndef _HUE_DIMMABLE_LIGHT_H
  2 +#define _HUE_DIMMABLE_LIGHT_H
  3 +#include "HueLight.h"
  4 +
  5 +// supports groups, scenes, on/off and dimming
  6 +class HueDimmableLight : public HueLight
  7 +{
  8 + friend class Hue;
  9 +
  10 +public:
  11 + //! virtual function that sets the brightness of this light. Ranging from 0=off to 255=fully on
  12 + //! \param bri Unsigned int that specifies the brightness
  13 + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
  14 + //! \return Bool that is true on success
  15 + virtual bool setBrightness(unsigned int bri, uint8_t transistion = 4);
  16 +
  17 +protected:
  18 + //! protected ctor that is used by \ref Hue class.
  19 + //! \param ip String that specifies the ip of the Hue bridge
  20 + //! \param username String that specifies the username used to control the bridge
  21 + //! \param id Integer that specifies the id of this light
  22 + HueDimmableLight(const std::string& ip, const std::string& username, int id) : HueLight(ip, username, id) {};
  23 +};
  24 +#endif
... ...
hueplusplus/include/HueExtendedColorLight.h 0 → 100644
  1 +#ifndef _HUE_EXTENDED_COLOR_LIGHT
  2 +#define _HUE_EXTENDED_COLOR_LIGHT
  3 +
  4 +#include "HueColorLight.h"
  5 +// supports same as Color light, but which supports additional setting of color temperature
  6 +class HueExtendedColorLight : public HueColorLight
  7 +{
  8 + friend class Hue;
  9 +
  10 +public:
  11 + //! Fucntion that sets the color temperature of this light in mired. Ranging from 153 to 500.
  12 + //! \param mired Unsigned int that specifies the color temperature in Mired
  13 + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
  14 + //! \return Bool that is true on success
  15 + virtual bool setColorTemperature(unsigned int mired, uint8_t transistion = 4);
  16 +
  17 + //! Function that lets the light perform one breath cycle in specified color temperature.
  18 + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
  19 + //! \param mired Color temperature in mired
  20 + //! \return Bool that is true on success
  21 + bool alertTemperature(unsigned int mired);
  22 +
  23 + //! Function that lets the light perform one breath cycle in specified color.
  24 + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
  25 + //! \param hue uint16_t that specifies the hue
  26 + //! \param sat uint8_t that specifies the saturation
  27 + //! \return Bool that is true on success
  28 + bool alertHueSaturation(uint16_t hue, uint8_t sat);
  29 +
  30 + //! Function that lets the light perform one breath cycle in specified color.
  31 + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
  32 + //! \param x float that specifies the x coordinate in CIE
  33 + //! \param y float that specifies the y coordinate in CIE
  34 + //! \return Bool that is true on success
  35 + bool alertXY(float x, float y);
  36 +
  37 + //! Function that lets the light perform one breath cycle in specified color.
  38 + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
  39 + //! \param r uint8_t that specifies the red color percentage
  40 + //! \param g uint8_t that specifies the green color percentage
  41 + //! \param b uint8_t that specifies the blue color percentage
  42 + //! \return Bool that is true on success
  43 + bool alertRGB(uint8_t r, uint8_t g, uint8_t b);
  44 +
  45 +protected:
  46 + //! protected ctor that is used by \ref Hue class.
  47 + //! \param ip String that specifies the ip of the Hue bridge
  48 + //! \param username String that specifies the username used to control the bridge
  49 + //! \param id Integer that specifies the id of this light
  50 + HueExtendedColorLight(const std::string& ip, const std::string& username, int id) : HueColorLight(ip, username, id) {};
  51 +};
  52 +#endif
... ...
hueplusplus/HueLight.h renamed to hueplusplus/include/HueLight.h
... ... @@ -148,180 +148,10 @@ protected:
148 148 ColorType colorType; //!< holds the \ref ColorType of the light
149 149 };
150 150  
151   -// supports groups, scenes, on/off and dimming
152   -class HueDimmableLight : public HueLight
153   -{
154   - friend class Hue;
155   -
156   -public:
157   - //! virtual function that sets the brightness of this light. Ranging from 0=off to 255=fully on
158   - //! \param bri Unsigned int that specifies the brightness
159   - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
160   - //! \return Bool that is true on success
161   - virtual bool setBrightness(unsigned int bri, uint8_t transistion = 4);
162   -
163   -protected:
164   - //! protected ctor that is used by \ref Hue class.
165   - //! \param ip String that specifies the ip of the Hue bridge
166   - //! \param username String that specifies the username used to control the bridge
167   - //! \param id Integer that specifies the id of this light
168   - HueDimmableLight(const std::string& ip, const std::string& username, int id) : HueLight(ip, username, id) {};
169   -};
170   -
171   -// supports groups, scenes, on/off, dimming, and setting of color temperature
172   -class HueTemperatureLight : public HueDimmableLight
173   -{
174   - friend class Hue;
175   -
176   -public:
177   - //! Fucntion that sets the color temperature of this light in mired. Ranging from 153 to 500
178   - //! \param mired Unsigned int that specifies the color temperature in Mired
179   - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
180   - //! \return Bool that is true on success
181   - virtual bool setColorTemperature(unsigned int mired, uint8_t transistion = 4);
182   -
183   - //! Function that lets the light perform one breath cycle in specified color temperature.
184   - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
185   - //! \param mired Color temperature in mired
186   - //! \return Bool that is true on success
187   - bool alertTemperature(unsigned int mired);
188   -
189   -protected:
190   - //! protected ctor that is used by \ref Hue class.
191   - //! \param ip String that specifies the ip of the Hue bridge
192   - //! \param username String that specifies the username used to control the bridge
193   - //! \param id Integer that specifies the id of this light
194   - HueTemperatureLight(const std::string& ip, const std::string& username, int id) : HueDimmableLight(ip, username, id) {};
195   -};
196   -
197   -// xy beats ct beats hue/sat.
198   -// supports groups, scenes, on / off, dimming and color control(hue / saturation, enhanced hue, color loop and XY)
199   -class HueColorLight : public HueDimmableLight
200   -{
201   - friend class Hue;
202   -
203   -public:
204   - //! Function to set the color of this light with specified hue. Ranging from 0 to 65535, whereas 65535 and 0 are red, 25500 is green and 46920 is blue.
205   - //! \param hue uint16_t that specifies the hue
206   - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
207   - //! \return Bool that is true on success
208   - virtual bool setColorHue(uint16_t hue, uint8_t transistion = 4);
209   -
210   - //! Function to set the saturation of color of this light with specified saturation. Ranging from 0 to 254, whereas 0 is least saturated (white) and 254 is most saturated.
211   - //! \param sat uint8_t that specifies the saturation
212   - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
213   - //! \return Bool that is true on success
214   - virtual bool setColorSaturation(uint8_t sat, uint8_t transistion = 4);
215   -
216   - //! Function to set the color of this light with specified hue and saturation.
217   - //! \param hue uint16_t that specifies the hue
218   - //! \param sat uint8_t that specifies the saturation
219   - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms.
220   - //! \return Bool that is true on success
221   - virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4);
222   -
223   - //! Function to set the color of this light in CIE with specified x y. Where x and y are ranging from 0 to 1.
224   - //! \param x float that specifies the x coordinate in CIE
225   - //! \param y float that specifies the y coordinate in CIE
226   - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
227   - //! \return Bool that is true on success
228   - virtual bool setColorXY(float x, float y, uint8_t transistion = 4);
229   -
230   - //! Function to set the color of this light with red green and blue values. Where red, green and blue are ranging from 0 to 255.
231   - //! \param r uint8_t that specifies the red color percentage
232   - //! \param g uint8_t that specifies the green color percentage
233   - //! \param b uint8_t that specifies the blue color percentage
234   - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
235   - //! \return Bool that is true on success
236   - virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion = 4);
237   -
238   - //! Function to enable colorloop effect. The colorloop effect will loop through all colors on current hue and saturation levels.
239   - //! Notice that none of the setter functions check for this and the colorloop can only be disabled via this function.
240   - //! or by simply calling Off()/OffNoRefresh() and then On()/OnNoRefresh(),
241   - //! so you could alternatively call Off() and then use any of the setter functions
242   - //! \param on bool that enables this feature when true and disables it when false
243   - //! \return Bool that is true on success
244   - virtual bool setColorLoop(bool on);
245   -
246   - //! Function that lets the light perform one breath cycle in specified color.
247   - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
248   - //! \param hue uint16_t that specifies the hue
249   - //! \param sat uint8_t that specifies the saturation
250   - //! \return Bool that is true on success
251   - bool alertHueSaturation(uint16_t hue, uint8_t sat);
252   -
253   - //! Function that lets the light perform one breath cycle in specified color.
254   - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
255   - //! \param x float that specifies the x coordinate in CIE
256   - //! \param y float that specifies the y coordinate in CIE
257   - //! \return Bool that is true on success
258   - bool alertXY(float x, float y);
259   -
260   - //! Function that lets the light perform one breath cycle in specified color.
261   - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
262   - //! \param r uint8_t that specifies the red color percentage
263   - //! \param g uint8_t that specifies the green color percentage
264   - //! \param b uint8_t that specifies the blue color percentage
265   - //! \return Bool that is true on success
266   - bool alertRGB(uint8_t r, uint8_t g, uint8_t b);
267   -
268   -protected:
269   - //! protected ctor that is used by \ref Hue class.
270   - //! \param ip String that specifies the ip of the Hue bridge
271   - //! \param username String that specifies the username used to control the bridge
272   - //! \param id Integer that specifies the id of this light
273   - HueColorLight(const std::string& ip, const std::string& username, int id) : HueDimmableLight(ip, username, id) {};
274   -
275   - /*private:
276   - bool pointInTriangle(float pointx, float pointy, float x0, float y0, float x1, float y1, float x2, float y2); // currently unused because Hue bridge handles this*/
277   -};
278   -
279   -// supports same as Color light, but which supports additional setting of color temperature
280   -class HueExtendedColorLight : public HueColorLight
281   -{
282   - friend class Hue;
283   -
284   -public:
285   - //! Fucntion that sets the color temperature of this light in mired. Ranging from 153 to 500.
286   - //! \param mired Unsigned int that specifies the color temperature in Mired
287   - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
288   - //! \return Bool that is true on success
289   - virtual bool setColorTemperature(unsigned int mired, uint8_t transistion = 4);
290 151  
291   - //! Function that lets the light perform one breath cycle in specified color temperature.
292   - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
293   - //! \param mired Color temperature in mired
294   - //! \return Bool that is true on success
295   - bool alertTemperature(unsigned int mired);
296 152  
297   - //! Function that lets the light perform one breath cycle in specified color.
298   - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
299   - //! \param hue uint16_t that specifies the hue
300   - //! \param sat uint8_t that specifies the saturation
301   - //! \return Bool that is true on success
302   - bool alertHueSaturation(uint16_t hue, uint8_t sat);
303 153  
304   - //! Function that lets the light perform one breath cycle in specified color.
305   - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
306   - //! \param x float that specifies the x coordinate in CIE
307   - //! \param y float that specifies the y coordinate in CIE
308   - //! \return Bool that is true on success
309   - bool alertXY(float x, float y);
310 154  
311   - //! Function that lets the light perform one breath cycle in specified color.
312   - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
313   - //! \param r uint8_t that specifies the red color percentage
314   - //! \param g uint8_t that specifies the green color percentage
315   - //! \param b uint8_t that specifies the blue color percentage
316   - //! \return Bool that is true on success
317   - bool alertRGB(uint8_t r, uint8_t g, uint8_t b);
318 155  
319   -protected:
320   - //! protected ctor that is used by \ref Hue class.
321   - //! \param ip String that specifies the ip of the Hue bridge
322   - //! \param username String that specifies the username used to control the bridge
323   - //! \param id Integer that specifies the id of this light
324   - HueExtendedColorLight(const std::string& ip, const std::string& username, int id) : HueColorLight(ip, username, id) {};
325   -};
326 156  
327   -#endif
328 157 \ No newline at end of file
  158 +#endif
... ...
hueplusplus/include/HueTemperatureLight.h 0 → 100644
  1 +#ifndef _HUE_TEMPAERATURE_LIGHT
  2 +#define _HUE_TEMPAERATURE_LIGHT
  3 +
  4 +#include "HueDimmableLight.h"
  5 +
  6 +// supports groups, scenes, on/off, dimming, and setting of color temperature
  7 +class HueTemperatureLight : public HueDimmableLight
  8 +{
  9 + friend class Hue;
  10 +
  11 +public:
  12 + //! Fucntion that sets the color temperature of this light in mired. Ranging from 153 to 500
  13 + //! \param mired Unsigned int that specifies the color temperature in Mired
  14 + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms
  15 + //! \return Bool that is true on success
  16 + virtual bool setColorTemperature(unsigned int mired, uint8_t transistion = 4);
  17 +
  18 + //! Function that lets the light perform one breath cycle in specified color temperature.
  19 + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs
  20 + //! \param mired Color temperature in mired
  21 + //! \return Bool that is true on success
  22 + bool alertTemperature(unsigned int mired);
  23 +
  24 +protected:
  25 + //! protected ctor that is used by \ref Hue class.
  26 + //! \param ip String that specifies the ip of the Hue bridge
  27 + //! \param username String that specifies the username used to control the bridge
  28 + //! \param id Integer that specifies the id of this light
  29 + HueTemperatureLight(const std::string& ip, const std::string& username, int id) : HueDimmableLight(ip, username, id) {};
  30 +};
  31 +#endif
... ...
hueplusplus/UPnP.h renamed to hueplusplus/include/UPnP.h
hueplusplus/json/json.h renamed to hueplusplus/include/json/json.h