diff --git a/hueplusplus/HttpHandler.cpp b/hueplusplus/HttpHandler.cpp index 189a3d9..c6cb9fc 100644 --- a/hueplusplus/HttpHandler.cpp +++ b/hueplusplus/HttpHandler.cpp @@ -17,7 +17,7 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA **/ -#include "HttpHandler.h" +#include "include/HttpHandler.h" #include #include #include // printf, sprintf @@ -120,7 +120,7 @@ std::string HttpHandler::sendRequest(const std::string & msg, const std::string std::string HttpHandler::sendRequestGetBody(const std::string & msg, const std::string & adr, int port) { std::string response = sendRequest(msg, adr, port); - unsigned int start = response.find("\r\n\r\n"); + size_t start = response.find("\r\n\r\n"); if (start == std::string::npos) { std::cerr << "Failed to find body in response\n"; @@ -194,7 +194,7 @@ std::vector HttpHandler::sendMulticast(const std::string & msg, con // construct return vector std::vector returnString; - unsigned int pos = response.find("\r\n\r\n"); + size_t pos = response.find("\r\n\r\n"); unsigned int prevpos = 0; while (pos != std::string::npos) { @@ -205,4 +205,4 @@ std::vector HttpHandler::sendMulticast(const std::string & msg, con } return returnString; -} \ No newline at end of file +} diff --git a/hueplusplus/Hue.cpp b/hueplusplus/Hue.cpp index fcc5794..c148601 100644 --- a/hueplusplus/Hue.cpp +++ b/hueplusplus/Hue.cpp @@ -17,11 +17,15 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA **/ -#include "Hue.h" +#include "include/Hue.h" +#include "include/HueLight.h" +#include "include/HueColorLight.h" +#include "include/HueDimmableLight.h" +#include "include/HueExtendedColorLight.h" +#include "include/HueTemperatureLight.h" -#include "HttpHandler.h" -#include "json/json.h" -#include "UPnP.h" +#include "include/HttpHandler.h" +#include "include/UPnP.h" #include #include @@ -44,7 +48,7 @@ std::vector HueFinder::FindBridges() const std::vector foundBridges; for (const std::pair &p : foundDevices) { - unsigned int found = p.second.find("IpBridge"); + size_t found = p.second.find("IpBridge"); if (found != std::string::npos) { HueIdentification bridge; diff --git a/hueplusplus/HueColorLight.cpp b/hueplusplus/HueColorLight.cpp new file mode 100644 index 0000000..882b43b --- /dev/null +++ b/hueplusplus/HueColorLight.cpp @@ -0,0 +1,490 @@ +/** + \file HueColorLight.cpp + Copyright Notice\n + Copyright (C) 2017 Jan Rogall - developer\n + Copyright (C) 2017 Moritz Wirger - developer\n + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +**/ + +#include "include/HueColorLight.h" + +#include +#include +#include + +bool HueColorLight::setColorHue(uint16_t hue, uint8_t transistion) +{ + refreshState(); + Json::Value request(Json::objectValue); + if (transistion != 4) + { + request["transistiontime"] = transistion; + } + if (state["state"]["on"].asBool() != true) + { + request["on"] = true; + } + if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs") + { + hue = hue % 65535; + request["hue"] = hue; + } + + if (!request.isMember("on") && !request.isMember("hue")) + { + //Nothing needs to be changed + return true; + } + + Json::Value reply = SendPutRequest(request); + + //Check whether request was successful + std::string path = "/lights/" + std::to_string(id) + "/state/"; + bool success = true; + int i = 0; + if (success && request.isMember("transistiontime")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); + ++i; + } + if (success && request.isMember("on")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); + ++i; + } + if (success && request.isMember("hue")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "hue"].asUInt() == request["hue"].asUInt(); + } + return success; +} + +bool HueColorLight::setColorSaturation(uint8_t sat, uint8_t transistion) +{ + refreshState(); + Json::Value request(Json::objectValue); + if (transistion != 4) + { + request["transistiontime"] = transistion; + } + if (state["state"]["on"].asBool() != true) + { + request["on"] = true; + } + if (state["state"]["sat"].asUInt() != sat) + { + if (sat > 254) + { + sat = 254; + } + request["sat"] = sat; + } + + if (!request.isMember("on") && !request.isMember("sat")) + { + //Nothing needs to be changed + return true; + } + + Json::Value reply = SendPutRequest(request); + + //Check whether request was successful + std::string path = "/lights/" + std::to_string(id) + "/state/"; + bool success = true; + int i = 0; + if (success && request.isMember("transistiontime")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); + ++i; + } + if (success && request.isMember("on")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); + ++i; + } + if (success && request.isMember("sat")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "sat"].asUInt() == request["sat"].asUInt(); + } + return success; +} + +bool HueColorLight::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transistion) +{ + refreshState(); + Json::Value request(Json::objectValue); + + if (transistion != 4) + { + request["transitiontime"] = transistion; + } + if (state["state"]["on"].asBool() != true) + { + request["on"] = true; + } + if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs") + { + hue = hue % 65535; + request["hue"] = hue; + } + if (state["state"]["sat"].asUInt() != sat || state["state"]["colormode"].asString() != "hs") + { + if (sat > 254) + { + sat = 254; + } + request["sat"] = sat; + } + + if (!request.isMember("on") && !request.isMember("hue") && !request.isMember("sat")) + { + //Nothing needs to be changed + return true; + } + + Json::Value reply = SendPutRequest(request); + + //Check whether request was successful + std::string path = "/lights/" + std::to_string(id) + "/state/"; + bool success = true; + int i = 0; + if (success && request.isMember("transitiontime")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); + ++i; + } + if (success && request.isMember("on")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); + ++i; + } + if (success && request.isMember("hue")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "hue"].asUInt() == request["hue"].asUInt(); + ++i; + } + if (success && request.isMember("sat")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "sat"].asUInt() == request["sat"].asUInt(); + } + return success; +} + +bool HueColorLight::setColorXY(float x, float y, uint8_t transistion) +{ + refreshState(); + Json::Value request(Json::objectValue); + + if (transistion != 4) + { + request["transitiontime"] = transistion; + } + if (state["state"]["on"].asBool() != true) + { + request["on"] = true; + } + if (state["state"]["xy"][0].asFloat() != x || state["state"]["xy"][1].asFloat() != y || state["state"]["colormode"].asString() != "xy") + { + request["xy"][0] = x; + request["xy"][1] = y; + } + + if (!request.isMember("on") && !request.isMember("xy")) + { + //Nothing needs to be changed + return true; + } + + Json::Value reply = SendPutRequest(request); + + //Check whether request was successful + std::string path = "/lights/" + std::to_string(id) + "/state/"; + bool success = true; + int i = 0; + if (success && request.isMember("transitiontime")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); + ++i; + } + if (success && request.isMember("on")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); + ++i; + } + if (success && request.isMember("xy")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "xy"][0].asFloat() == request["xy"][0].asFloat(); + if (success) + { + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "xy"][1].asFloat() == request["xy"][1].asFloat(); + } + } + return success; +} + +bool HueColorLight::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion) +{ + float red = r / 255; + float green = g / 255; + float blue = b / 255; + + // gamma correction + red = (red > 0.04045f) ? pow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f); + green = (green > 0.04045f) ? pow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f); + blue = (blue > 0.04045f) ? pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f); + + float X = red * 0.664511f + green * 0.154324f + blue * 0.162028f; + float Y = red * 0.283881f + green * 0.668433f + blue * 0.047685f; + float Z = red * 0.000088f + green * 0.072310f + blue * 0.986039f; + + float x = X / (X + Y + Z); + float y = Y / (X + Y + Z); + + return setColorXY(x, y, transistion); +} + +bool HueColorLight::setColorLoop(bool on) +{ + //colorloop + refreshState(); + Json::Value request(Json::objectValue); + + if (state["state"]["on"].asBool() != true) + { + request["on"] = true; + } + std::string effect; + if ((effect = on ? "colorloop" : "none") != state["state"]["effect"].asString()) + { + request["effect"] = effect; + } + if (!request.isMember("on") && !request.isMember("effect")) + { + //Nothing needs to be changed + return true; + } + + Json::Value reply = SendPutRequest(request); + + //Check whether request was successful + std::string path = "/lights/" + std::to_string(id) + "/state/"; + bool success = true; + int i = 0; + if (success && request.isMember("on")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); + ++i; + } + if (success && request.isMember("effect")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "effect"].asString() == request["effect"].asString(); + } + return success; +} + +bool HueColorLight::alertHueSaturation(uint16_t hue, uint8_t sat) +{ + refreshState(); + std::string cType = state["state"]["colormode"].asString(); + bool on = state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = state["state"]["hue"].asUInt(); + uint8_t oldSat = state["state"]["sat"].asUInt(); + if (!setColorHueSaturation(hue, sat, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = state["state"]["xy"][0].asFloat(); + float oldY = state["state"]["xy"][1].asFloat(); + if (!setColorHueSaturation(hue, sat, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorXY(oldX, oldY, 1); + } + } + else + { + return false; + } +} + +bool HueColorLight::alertXY(float x, float y) +{ + refreshState(); + std::string cType = state["state"]["colormode"].asString(); + bool on = state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = state["state"]["hue"].asUInt(); + uint8_t oldSat = state["state"]["sat"].asUInt(); + if (!setColorXY(x, y, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = state["state"]["xy"][0].asFloat(); + float oldY = state["state"]["xy"][1].asFloat(); + if (!setColorXY(x, y, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorXY(oldX, oldY, 1); + } + } + else + { + return false; + } +} + +bool HueColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b) +{ + refreshState(); + std::string cType = state["state"]["colormode"].asString(); + bool on = state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = state["state"]["hue"].asUInt(); + uint8_t oldSat = state["state"]["sat"].asUInt(); + if (!setColorRGB(r, g, b, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = state["state"]["xy"][0].asFloat(); + float oldY = state["state"]["xy"][1].asFloat(); + if (!setColorRGB(r, g, b, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorXY(oldX, oldY, 1); + } + } + else + { + return false; + } +} + +/*bool HueColorLight::pointInTriangle(float pointx, float pointy, float x0, float y0, float x1, float y1, float x2, float y2) +{ +float A = (-y1 * x2 + y0*(-x1 + x2) + x0*(y1 - y2) + x1 * y1); +int8_t sign = A < 0 ? -1 : 1; +float s = (y0 * x2 - x0 * y2 + (y2 - y0) * pointx + (x0 - x2) * pointy) * sign; +float t = (x0 * y1 - y0 * x1 + (y0 - y1) * pointx + (x1 - x0) * pointy) * sign; + +return s > 0 && t > 0 && (s + t) < A * sign; +}*/ diff --git a/hueplusplus/HueDimmableLight.cpp b/hueplusplus/HueDimmableLight.cpp new file mode 100644 index 0000000..efd0f4d --- /dev/null +++ b/hueplusplus/HueDimmableLight.cpp @@ -0,0 +1,93 @@ +/** + \file HueDimmableLight.cpp + Copyright Notice\n + Copyright (C) 2017 Jan Rogall - developer\n + Copyright (C) 2017 Moritz Wirger - developer\n + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free SofHueDimmableLighttware Foundation; either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +**/ + +#include "include/HueDimmableLight.h" + +#include +#include +#include + +bool HueDimmableLight::setBrightness(unsigned int bri, uint8_t transistion) +{ + std::cout << "Setting lamp with id: " << id << " to brightness of " << bri << std::endl; + refreshState(); + if (bri == 0) + { + if (state["state"]["on"] == true) + { + return OffNoRefresh(transistion); + } + else + { + return true; + } + } + else + { + Json::Value request(Json::objectValue); + if (transistion != 4) + { + request["transistiontime"] = transistion; + } + if (state["state"]["on"].asBool() != true) + { + request["on"] = true; + } + if (state["state"]["bri"].asUInt() != bri) + { + bri -= 1; + if (bri > 254) + { + bri = 254; + } + request["bri"] = bri; + } + + if (!request.isMember("on") && !request.isMember("bri")) + { + //Nothing needs to be changed + return true; + } + + Json::Value reply = SendPutRequest(request); + + //Check whether request was successful + std::string path = "/lights/" + std::to_string(id) + "/state/"; + bool success = true; + int i = 0; + if (success && request.isMember("transistiontime")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); + ++i; + } + if (success && request.isMember("on")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); + ++i; + } + if (success && request.isMember("bri")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "bri"].asUInt() == request["bri"].asUInt(); + } + return success; + } +} diff --git a/hueplusplus/HueExtendedColorLight.cpp b/hueplusplus/HueExtendedColorLight.cpp new file mode 100644 index 0000000..b67216d --- /dev/null +++ b/hueplusplus/HueExtendedColorLight.cpp @@ -0,0 +1,397 @@ +/** + \file HueExtendedColorLight.cpp + Copyright Notice\n + Copyright (C) 2017 Jan Rogall - developer\n + Copyright (C) 2017 Moritz Wirger - developer\n + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +**/ + +#include "include/HueExtendedColorLight.h" + +#include +#include +#include + +bool HueExtendedColorLight::setColorTemperature(unsigned int mired, uint8_t transistion) +{ + refreshState(); + Json::Value request(Json::objectValue); + if (transistion != 4) + { + request["transitiontime"] = transistion; + } + if (state["state"]["on"].asBool() != true) + { + request["on"] = true; + } + if (state["state"]["ct"].asUInt() != mired || state["state"]["colormode"].asString() != "ct") + { + if (mired > 500) + { + mired = 500; + } + if (mired < 153) + { + mired = 153; + } + request["ct"] = mired; + } + + if (!request.isMember("on") && !request.isMember("ct")) + { + //Nothing needs to be changed + return true; + } + + Json::Value reply = SendPutRequest(request); + + //Check whether request was successful + std::string path = "/lights/" + std::to_string(id) + "/state/"; + bool success = true; + int i = 0; + if (success && request.isMember("transitiontime")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); + ++i; + } + if (success && request.isMember("on")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); + ++i; + } + if (success && request.isMember("ct")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "ct"].asUInt() == request["ct"].asUInt(); + } + return success; +} + +bool HueExtendedColorLight::alertTemperature(unsigned int mired) +{ + refreshState(); + std::string cType = state["state"]["colormode"].asString(); + bool on = state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = state["state"]["hue"].asUInt(); + uint8_t oldSat = state["state"]["sat"].asUInt(); + if (!setColorTemperature(mired, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = state["state"]["xy"][0].asFloat(); + float oldY = state["state"]["xy"][1].asFloat(); + if (!setColorTemperature(mired, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorXY(oldX, oldY, 1); + } + } + else if (cType == "ct") + { + uint16_t oldCT = state["state"]["ct"].asUInt(); + if (!setColorTemperature(mired, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorTemperature(oldCT, 1); + } + } + else + { + return false; + } +} + +bool HueExtendedColorLight::alertHueSaturation(uint16_t hue, uint8_t sat) +{ + refreshState(); + std::string cType = state["state"]["colormode"].asString(); + bool on = state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = state["state"]["hue"].asUInt(); + uint8_t oldSat = state["state"]["sat"].asUInt(); + if (!setColorHueSaturation(hue, sat, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = state["state"]["xy"][0].asFloat(); + float oldY = state["state"]["xy"][1].asFloat(); + if (!setColorHueSaturation(hue, sat, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorXY(oldX, oldY, 1); + } + } + else if (cType == "ct") + { + uint16_t oldCT = state["state"]["ct"].asUInt(); + if (!setColorHueSaturation(hue, sat, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorTemperature(oldCT, 1); + } + } + else + { + return false; + } +} + +bool HueExtendedColorLight::alertXY(float x, float y) +{ + refreshState(); + std::string cType = state["state"]["colormode"].asString(); + bool on = state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = state["state"]["hue"].asUInt(); + uint8_t oldSat = state["state"]["sat"].asUInt(); + if (!setColorXY(x, y, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = state["state"]["xy"][0].asFloat(); + float oldY = state["state"]["xy"][1].asFloat(); + if (!setColorXY(x, y, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorXY(oldX, oldY, 1); + } + } + else if (cType == "ct") + { + uint16_t oldCT = state["state"]["ct"].asUInt(); + if (!setColorXY(x, y, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorTemperature(oldCT, 1); + } + } + else + { + return false; + } +} + +bool HueExtendedColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b) +{ + refreshState(); + std::string cType = state["state"]["colormode"].asString(); + bool on = state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = state["state"]["hue"].asUInt(); + uint8_t oldSat = state["state"]["sat"].asUInt(); + if (!setColorRGB(r, g, b, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = state["state"]["xy"][0].asFloat(); + float oldY = state["state"]["xy"][1].asFloat(); + if (!setColorRGB(r, g, b, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorXY(oldX, oldY, 1); + } + } + else if (cType == "ct") + { + uint16_t oldCT = state["state"]["ct"].asUInt(); + if (!setColorRGB(r, g, b, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorTemperature(oldCT, 1); + } + } + else + { + return false; + } +} diff --git a/hueplusplus/HueLight.cpp b/hueplusplus/HueLight.cpp index 34556a1..d88af32 100644 --- a/hueplusplus/HueLight.cpp +++ b/hueplusplus/HueLight.cpp @@ -17,10 +17,11 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA **/ -#include "HueLight.h" +#include "include/HueLight.h" -#include "HttpHandler.h" -#include "json/json.h" + +#include "include/HttpHandler.h" +#include "include/json/json.h" #include #include @@ -210,1001 +211,3 @@ void HueLight::refreshState() } std::cout << "\tRefresh state took: " << std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count() << "ms" << std::endl; } - -bool HueDimmableLight::setBrightness(unsigned int bri, uint8_t transistion) -{ - std::cout << "Setting lamp with id: " << id << " to brightness of " << bri << std::endl; - refreshState(); - if (bri == 0) - { - if (state["state"]["on"] == true) - { - return OffNoRefresh(transistion); - } - else - { - return true; - } - } - else - { - Json::Value request(Json::objectValue); - if (transistion != 4) - { - request["transistiontime"] = transistion; - } - if (state["state"]["on"].asBool() != true) - { - request["on"] = true; - } - if (state["state"]["bri"].asUInt() != bri) - { - bri -= 1; - if (bri > 254) - { - bri = 254; - } - request["bri"] = bri; - } - - if (!request.isMember("on") && !request.isMember("bri")) - { - //Nothing needs to be changed - return true; - } - - Json::Value reply = SendPutRequest(request); - - //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; - bool success = true; - int i = 0; - if (success && request.isMember("transistiontime")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); - ++i; - } - if (success && request.isMember("on")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); - ++i; - } - if (success && request.isMember("bri")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "bri"].asUInt() == request["bri"].asUInt(); - } - return success; - } -} - -bool HueTemperatureLight::setColorTemperature(unsigned int mired, uint8_t transistion) -{ - refreshState(); - Json::Value request(Json::objectValue); - if (transistion != 4) - { - request["transistiontime"] = transistion; - } - if (state["state"]["on"].asBool() != true) - { - request["on"] = true; - } - if (state["state"]["ct"].asUInt() != mired) - { - if (mired > 500) - { - mired = 500; - } - if (mired < 153) - { - mired = 153; - } - request["ct"] = mired; - } - - if (!request.isMember("on") && !request.isMember("ct")) - { - //Nothing needs to be changed - return true; - } - - Json::Value reply = SendPutRequest(request); - - //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; - bool success = true; - int i = 0; - if (success && request.isMember("transistiontime")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); - ++i; - } - if (success && request.isMember("on")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); - ++i; - } - if (success && request.isMember("ct")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "ct"].asUInt() == request["ct"].asUInt(); - } - return success; -} - -bool HueTemperatureLight::alertTemperature(unsigned int mired) -{ - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); - if (cType == "ct") - { - uint16_t oldCT = state["state"]["ct"].asUInt(); - if (!setColorTemperature(mired, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorTemperature(oldCT, 1); - } - } - else - { - return false; - } -} - -bool HueColorLight::setColorHue(uint16_t hue, uint8_t transistion) -{ - refreshState(); - Json::Value request(Json::objectValue); - if (transistion != 4) - { - request["transistiontime"] = transistion; - } - if (state["state"]["on"].asBool() != true) - { - request["on"] = true; - } - if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs") - { - hue = hue % 65535; - request["hue"] = hue; - } - - if (!request.isMember("on") && !request.isMember("hue")) - { - //Nothing needs to be changed - return true; - } - - Json::Value reply = SendPutRequest(request); - - //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; - bool success = true; - int i = 0; - if (success && request.isMember("transistiontime")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); - ++i; - } - if (success && request.isMember("on")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); - ++i; - } - if (success && request.isMember("hue")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "hue"].asUInt() == request["hue"].asUInt(); - } - return success; -} - -bool HueColorLight::setColorSaturation(uint8_t sat, uint8_t transistion) -{ - refreshState(); - Json::Value request(Json::objectValue); - if (transistion != 4) - { - request["transistiontime"] = transistion; - } - if (state["state"]["on"].asBool() != true) - { - request["on"] = true; - } - if (state["state"]["sat"].asUInt() != sat) - { - if (sat > 254) - { - sat = 254; - } - request["sat"] = sat; - } - - if (!request.isMember("on") && !request.isMember("sat")) - { - //Nothing needs to be changed - return true; - } - - Json::Value reply = SendPutRequest(request); - - //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; - bool success = true; - int i = 0; - if (success && request.isMember("transistiontime")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); - ++i; - } - if (success && request.isMember("on")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); - ++i; - } - if (success && request.isMember("sat")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "sat"].asUInt() == request["sat"].asUInt(); - } - return success; -} - -bool HueColorLight::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transistion) -{ - refreshState(); - Json::Value request(Json::objectValue); - - if (transistion != 4) - { - request["transitiontime"] = transistion; - } - if (state["state"]["on"].asBool() != true) - { - request["on"] = true; - } - if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs") - { - hue = hue % 65535; - request["hue"] = hue; - } - if (state["state"]["sat"].asUInt() != sat || state["state"]["colormode"].asString() != "hs") - { - if (sat > 254) - { - sat = 254; - } - request["sat"] = sat; - } - - if (!request.isMember("on") && !request.isMember("hue") && !request.isMember("sat")) - { - //Nothing needs to be changed - return true; - } - - Json::Value reply = SendPutRequest(request); - - //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; - bool success = true; - int i = 0; - if (success && request.isMember("transitiontime")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); - ++i; - } - if (success && request.isMember("on")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); - ++i; - } - if (success && request.isMember("hue")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "hue"].asUInt() == request["hue"].asUInt(); - ++i; - } - if (success && request.isMember("sat")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "sat"].asUInt() == request["sat"].asUInt(); - } - return success; -} - -bool HueColorLight::setColorXY(float x, float y, uint8_t transistion) -{ - refreshState(); - Json::Value request(Json::objectValue); - - if (transistion != 4) - { - request["transitiontime"] = transistion; - } - if (state["state"]["on"].asBool() != true) - { - request["on"] = true; - } - if (state["state"]["xy"][0].asFloat() != x || state["state"]["xy"][1].asFloat() != y || state["state"]["colormode"].asString() != "xy") - { - request["xy"][0] = x; - request["xy"][1] = y; - } - - if (!request.isMember("on") && !request.isMember("xy")) - { - //Nothing needs to be changed - return true; - } - - Json::Value reply = SendPutRequest(request); - - //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; - bool success = true; - int i = 0; - if (success && request.isMember("transitiontime")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); - ++i; - } - if (success && request.isMember("on")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); - ++i; - } - if (success && request.isMember("xy")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "xy"][0].asFloat() == request["xy"][0].asFloat(); - if (success) - { - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "xy"][1].asFloat() == request["xy"][1].asFloat(); - } - } - return success; -} - -bool HueColorLight::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion) -{ - float red = r / 255; - float green = g / 255; - float blue = b / 255; - - // gamma correction - red = (red > 0.04045f) ? pow((red + 0.055f) / (1.0f + 0.055f), 2.4f) : (red / 12.92f); - green = (green > 0.04045f) ? pow((green + 0.055f) / (1.0f + 0.055f), 2.4f) : (green / 12.92f); - blue = (blue > 0.04045f) ? pow((blue + 0.055f) / (1.0f + 0.055f), 2.4f) : (blue / 12.92f); - - float X = red * 0.664511f + green * 0.154324f + blue * 0.162028f; - float Y = red * 0.283881f + green * 0.668433f + blue * 0.047685f; - float Z = red * 0.000088f + green * 0.072310f + blue * 0.986039f; - - float x = X / (X + Y + Z); - float y = Y / (X + Y + Z); - - return setColorXY(x, y, transistion); -} - -bool HueColorLight::setColorLoop(bool on) -{ - //colorloop - refreshState(); - Json::Value request(Json::objectValue); - - if (state["state"]["on"].asBool() != true) - { - request["on"] = true; - } - std::string effect; - if ((effect = on ? "colorloop" : "none") != state["state"]["effect"].asString()) - { - request["effect"] = effect; - } - if (!request.isMember("on") && !request.isMember("effect")) - { - //Nothing needs to be changed - return true; - } - - Json::Value reply = SendPutRequest(request); - - //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; - bool success = true; - int i = 0; - if (success && request.isMember("on")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); - ++i; - } - if (success && request.isMember("effect")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "effect"].asString() == request["effect"].asString(); - } - return success; -} - -bool HueColorLight::alertHueSaturation(uint16_t hue, uint8_t sat) -{ - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); - if (cType == "hs") - { - uint16_t oldHue = state["state"]["hue"].asUInt(); - uint8_t oldSat = state["state"]["sat"].asUInt(); - if (!setColorHueSaturation(hue, sat, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorHueSaturation(oldHue, oldSat, 1); - } - } - else if (cType == "xy") - { - float oldX = state["state"]["xy"][0].asFloat(); - float oldY = state["state"]["xy"][1].asFloat(); - if (!setColorHueSaturation(hue, sat, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorXY(oldX, oldY, 1); - } - } - else - { - return false; - } -} - -bool HueColorLight::alertXY(float x, float y) -{ - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); - if (cType == "hs") - { - uint16_t oldHue = state["state"]["hue"].asUInt(); - uint8_t oldSat = state["state"]["sat"].asUInt(); - if (!setColorXY(x, y, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorHueSaturation(oldHue, oldSat, 1); - } - } - else if (cType == "xy") - { - float oldX = state["state"]["xy"][0].asFloat(); - float oldY = state["state"]["xy"][1].asFloat(); - if (!setColorXY(x, y, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorXY(oldX, oldY, 1); - } - } - else - { - return false; - } -} - -bool HueColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b) -{ - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); - if (cType == "hs") - { - uint16_t oldHue = state["state"]["hue"].asUInt(); - uint8_t oldSat = state["state"]["sat"].asUInt(); - if (!setColorRGB(r, g, b, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorHueSaturation(oldHue, oldSat, 1); - } - } - else if (cType == "xy") - { - float oldX = state["state"]["xy"][0].asFloat(); - float oldY = state["state"]["xy"][1].asFloat(); - if (!setColorRGB(r, g, b, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorXY(oldX, oldY, 1); - } - } - else - { - return false; - } -} - -/*bool HueColorLight::pointInTriangle(float pointx, float pointy, float x0, float y0, float x1, float y1, float x2, float y2) -{ -float A = (-y1 * x2 + y0*(-x1 + x2) + x0*(y1 - y2) + x1 * y1); -int8_t sign = A < 0 ? -1 : 1; -float s = (y0 * x2 - x0 * y2 + (y2 - y0) * pointx + (x0 - x2) * pointy) * sign; -float t = (x0 * y1 - y0 * x1 + (y0 - y1) * pointx + (x1 - x0) * pointy) * sign; - -return s > 0 && t > 0 && (s + t) < A * sign; -}*/ - -bool HueExtendedColorLight::setColorTemperature(unsigned int mired, uint8_t transistion) -{ - refreshState(); - Json::Value request(Json::objectValue); - if (transistion != 4) - { - request["transitiontime"] = transistion; - } - if (state["state"]["on"].asBool() != true) - { - request["on"] = true; - } - if (state["state"]["ct"].asUInt() != mired || state["state"]["colormode"].asString() != "ct") - { - if (mired > 500) - { - mired = 500; - } - if (mired < 153) - { - mired = 153; - } - request["ct"] = mired; - } - - if (!request.isMember("on") && !request.isMember("ct")) - { - //Nothing needs to be changed - return true; - } - - Json::Value reply = SendPutRequest(request); - - //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; - bool success = true; - int i = 0; - if (success && request.isMember("transitiontime")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); - ++i; - } - if (success && request.isMember("on")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); - ++i; - } - if (success && request.isMember("ct")) - { - //Check if success was sent and the value was changed - success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "ct"].asUInt() == request["ct"].asUInt(); - } - return success; -} - -bool HueExtendedColorLight::alertTemperature(unsigned int mired) -{ - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); - if (cType == "hs") - { - uint16_t oldHue = state["state"]["hue"].asUInt(); - uint8_t oldSat = state["state"]["sat"].asUInt(); - if (!setColorTemperature(mired, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorHueSaturation(oldHue, oldSat, 1); - } - } - else if (cType == "xy") - { - float oldX = state["state"]["xy"][0].asFloat(); - float oldY = state["state"]["xy"][1].asFloat(); - if (!setColorTemperature(mired, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorXY(oldX, oldY, 1); - } - } - else if (cType == "ct") - { - uint16_t oldCT = state["state"]["ct"].asUInt(); - if (!setColorTemperature(mired, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorTemperature(oldCT, 1); - } - } - else - { - return false; - } -} - -bool HueExtendedColorLight::alertHueSaturation(uint16_t hue, uint8_t sat) -{ - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); - if (cType == "hs") - { - uint16_t oldHue = state["state"]["hue"].asUInt(); - uint8_t oldSat = state["state"]["sat"].asUInt(); - if (!setColorHueSaturation(hue, sat, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorHueSaturation(oldHue, oldSat, 1); - } - } - else if (cType == "xy") - { - float oldX = state["state"]["xy"][0].asFloat(); - float oldY = state["state"]["xy"][1].asFloat(); - if (!setColorHueSaturation(hue, sat, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorXY(oldX, oldY, 1); - } - } - else if (cType == "ct") - { - uint16_t oldCT = state["state"]["ct"].asUInt(); - if (!setColorHueSaturation(hue, sat, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorTemperature(oldCT, 1); - } - } - else - { - return false; - } -} - -bool HueExtendedColorLight::alertXY(float x, float y) -{ - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); - if (cType == "hs") - { - uint16_t oldHue = state["state"]["hue"].asUInt(); - uint8_t oldSat = state["state"]["sat"].asUInt(); - if (!setColorXY(x, y, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorHueSaturation(oldHue, oldSat, 1); - } - } - else if (cType == "xy") - { - float oldX = state["state"]["xy"][0].asFloat(); - float oldY = state["state"]["xy"][1].asFloat(); - if (!setColorXY(x, y, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorXY(oldX, oldY, 1); - } - } - else if (cType == "ct") - { - uint16_t oldCT = state["state"]["ct"].asUInt(); - if (!setColorXY(x, y, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorTemperature(oldCT, 1); - } - } - else - { - return false; - } -} - -bool HueExtendedColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b) -{ - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); - if (cType == "hs") - { - uint16_t oldHue = state["state"]["hue"].asUInt(); - uint8_t oldSat = state["state"]["sat"].asUInt(); - if (!setColorRGB(r, g, b, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorHueSaturation(oldHue, oldSat, 1); - } - } - else if (cType == "xy") - { - float oldX = state["state"]["xy"][0].asFloat(); - float oldY = state["state"]["xy"][1].asFloat(); - if (!setColorRGB(r, g, b, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorXY(oldX, oldY, 1); - } - } - else if (cType == "ct") - { - uint16_t oldCT = state["state"]["ct"].asUInt(); - if (!setColorRGB(r, g, b, 1)) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) - { - return false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); - if (!on) - { - return OffNoRefresh(1); - } - else - { - return setColorTemperature(oldCT, 1); - } - } - else - { - return false; - } -} diff --git a/hueplusplus/HueTemperatureLight.cpp b/hueplusplus/HueTemperatureLight.cpp new file mode 100644 index 0000000..b37a8f7 --- /dev/null +++ b/hueplusplus/HueTemperatureLight.cpp @@ -0,0 +1,116 @@ +/** + \file HueTemperatureLight.cpp + Copyright Notice\n + Copyright (C) 2017 Jan Rogall - developer\n + Copyright (C) 2017 Moritz Wirger - developer\n + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +**/ + +#include "include/HueTemperatureLight.h" + +#include +#include +#include + + +bool HueTemperatureLight::setColorTemperature(unsigned int mired, uint8_t transistion) +{ + refreshState(); + Json::Value request(Json::objectValue); + if (transistion != 4) + { + request["transistiontime"] = transistion; + } + if (state["state"]["on"].asBool() != true) + { + request["on"] = true; + } + if (state["state"]["ct"].asUInt() != mired) + { + if (mired > 500) + { + mired = 500; + } + if (mired < 153) + { + mired = 153; + } + request["ct"] = mired; + } + + if (!request.isMember("on") && !request.isMember("ct")) + { + //Nothing needs to be changed + return true; + } + + Json::Value reply = SendPutRequest(request); + + //Check whether request was successful + std::string path = "/lights/" + std::to_string(id) + "/state/"; + bool success = true; + int i = 0; + if (success && request.isMember("transistiontime")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); + ++i; + } + if (success && request.isMember("on")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "on"].asBool() == request["on"].asBool(); + ++i; + } + if (success && request.isMember("ct")) + { + //Check if success was sent and the value was changed + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "ct"].asUInt() == request["ct"].asUInt(); + } + return success; +} + +bool HueTemperatureLight::alertTemperature(unsigned int mired) +{ + refreshState(); + std::string cType = state["state"]["colormode"].asString(); + bool on = state["state"]["on"].asBool(); + if (cType == "ct") + { + uint16_t oldCT = state["state"]["ct"].asUInt(); + if (!setColorTemperature(mired, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return OffNoRefresh(1); + } + else + { + return setColorTemperature(oldCT, 1); + } + } + else + { + return false; + } +} + diff --git a/hueplusplus/UPnP.cpp b/hueplusplus/UPnP.cpp index 39d3bac..32140df 100644 --- a/hueplusplus/UPnP.cpp +++ b/hueplusplus/UPnP.cpp @@ -1,5 +1,5 @@ -#include "UPnP.h" -#include "HttpHandler.h" +#include "include/UPnP.h" +#include "include/HttpHandler.h" #include #include diff --git a/hueplusplus/HttpHandler.h b/hueplusplus/include/HttpHandler.h index 97e04a2..97e04a2 100644 --- a/hueplusplus/HttpHandler.h +++ b/hueplusplus/include/HttpHandler.h diff --git a/hueplusplus/Hue.h b/hueplusplus/include/Hue.h index b5b0e16..b5b0e16 100644 --- a/hueplusplus/Hue.h +++ b/hueplusplus/include/Hue.h diff --git a/hueplusplus/include/HueColorLight.h b/hueplusplus/include/HueColorLight.h new file mode 100644 index 0000000..07020a5 --- /dev/null +++ b/hueplusplus/include/HueColorLight.h @@ -0,0 +1,87 @@ +#ifndef _HUE_COLOR_LIGHT +#define _HUE_COLOR_LIGHT + +#include "HueDimmableLight.h" + +// xy beats ct beats hue/sat. +// supports groups, scenes, on / off, dimming and color control(hue / saturation, enhanced hue, color loop and XY) +class HueColorLight : public HueDimmableLight +{ + friend class Hue; + +public: + //! 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. + //! \param hue uint16_t that specifies the hue + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + virtual bool setColorHue(uint16_t hue, uint8_t transistion = 4); + + //! 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. + //! \param sat uint8_t that specifies the saturation + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + virtual bool setColorSaturation(uint8_t sat, uint8_t transistion = 4); + + //! Function to set the color of this light with specified hue and saturation. + //! \param hue uint16_t that specifies the hue + //! \param sat uint8_t that specifies the saturation + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms. + //! \return Bool that is true on success + virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4); + + //! Function to set the color of this light in CIE with specified x y. Where x and y are ranging from 0 to 1. + //! \param x float that specifies the x coordinate in CIE + //! \param y float that specifies the y coordinate in CIE + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + virtual bool setColorXY(float x, float y, uint8_t transistion = 4); + + //! 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. + //! \param r uint8_t that specifies the red color percentage + //! \param g uint8_t that specifies the green color percentage + //! \param b uint8_t that specifies the blue color percentage + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion = 4); + + //! Function to enable colorloop effect. The colorloop effect will loop through all colors on current hue and saturation levels. + //! Notice that none of the setter functions check for this and the colorloop can only be disabled via this function. + //! or by simply calling Off()/OffNoRefresh() and then On()/OnNoRefresh(), + //! so you could alternatively call Off() and then use any of the setter functions + //! \param on bool that enables this feature when true and disables it when false + //! \return Bool that is true on success + virtual bool setColorLoop(bool on); + + //! Function that lets the light perform one breath cycle in specified color. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param hue uint16_t that specifies the hue + //! \param sat uint8_t that specifies the saturation + //! \return Bool that is true on success + bool alertHueSaturation(uint16_t hue, uint8_t sat); + + //! Function that lets the light perform one breath cycle in specified color. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param x float that specifies the x coordinate in CIE + //! \param y float that specifies the y coordinate in CIE + //! \return Bool that is true on success + bool alertXY(float x, float y); + + //! Function that lets the light perform one breath cycle in specified color. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param r uint8_t that specifies the red color percentage + //! \param g uint8_t that specifies the green color percentage + //! \param b uint8_t that specifies the blue color percentage + //! \return Bool that is true on success + bool alertRGB(uint8_t r, uint8_t g, uint8_t b); + +protected: + //! protected ctor that is used by \ref Hue class. + //! \param ip String that specifies the ip of the Hue bridge + //! \param username String that specifies the username used to control the bridge + //! \param id Integer that specifies the id of this light + HueColorLight(const std::string& ip, const std::string& username, int id) : HueDimmableLight(ip, username, id) {}; + + /*private: + 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*/ +}; +#endif diff --git a/hueplusplus/include/HueDimmableLight.h b/hueplusplus/include/HueDimmableLight.h new file mode 100644 index 0000000..8d67cdb --- /dev/null +++ b/hueplusplus/include/HueDimmableLight.h @@ -0,0 +1,24 @@ +#ifndef _HUE_DIMMABLE_LIGHT_H +#define _HUE_DIMMABLE_LIGHT_H +#include "HueLight.h" + +// supports groups, scenes, on/off and dimming +class HueDimmableLight : public HueLight +{ + friend class Hue; + +public: + //! virtual function that sets the brightness of this light. Ranging from 0=off to 255=fully on + //! \param bri Unsigned int that specifies the brightness + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + virtual bool setBrightness(unsigned int bri, uint8_t transistion = 4); + +protected: + //! protected ctor that is used by \ref Hue class. + //! \param ip String that specifies the ip of the Hue bridge + //! \param username String that specifies the username used to control the bridge + //! \param id Integer that specifies the id of this light + HueDimmableLight(const std::string& ip, const std::string& username, int id) : HueLight(ip, username, id) {}; +}; +#endif diff --git a/hueplusplus/include/HueExtendedColorLight.h b/hueplusplus/include/HueExtendedColorLight.h new file mode 100644 index 0000000..3d8eece --- /dev/null +++ b/hueplusplus/include/HueExtendedColorLight.h @@ -0,0 +1,52 @@ +#ifndef _HUE_EXTENDED_COLOR_LIGHT +#define _HUE_EXTENDED_COLOR_LIGHT + +#include "HueColorLight.h" +// supports same as Color light, but which supports additional setting of color temperature +class HueExtendedColorLight : public HueColorLight +{ + friend class Hue; + +public: + //! Fucntion that sets the color temperature of this light in mired. Ranging from 153 to 500. + //! \param mired Unsigned int that specifies the color temperature in Mired + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + virtual bool setColorTemperature(unsigned int mired, uint8_t transistion = 4); + + //! Function that lets the light perform one breath cycle in specified color temperature. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param mired Color temperature in mired + //! \return Bool that is true on success + bool alertTemperature(unsigned int mired); + + //! Function that lets the light perform one breath cycle in specified color. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param hue uint16_t that specifies the hue + //! \param sat uint8_t that specifies the saturation + //! \return Bool that is true on success + bool alertHueSaturation(uint16_t hue, uint8_t sat); + + //! Function that lets the light perform one breath cycle in specified color. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param x float that specifies the x coordinate in CIE + //! \param y float that specifies the y coordinate in CIE + //! \return Bool that is true on success + bool alertXY(float x, float y); + + //! Function that lets the light perform one breath cycle in specified color. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param r uint8_t that specifies the red color percentage + //! \param g uint8_t that specifies the green color percentage + //! \param b uint8_t that specifies the blue color percentage + //! \return Bool that is true on success + bool alertRGB(uint8_t r, uint8_t g, uint8_t b); + +protected: + //! protected ctor that is used by \ref Hue class. + //! \param ip String that specifies the ip of the Hue bridge + //! \param username String that specifies the username used to control the bridge + //! \param id Integer that specifies the id of this light + HueExtendedColorLight(const std::string& ip, const std::string& username, int id) : HueColorLight(ip, username, id) {}; +}; +#endif diff --git a/hueplusplus/HueLight.h b/hueplusplus/include/HueLight.h index 0887749..a222c60 100644 --- a/hueplusplus/HueLight.h +++ b/hueplusplus/include/HueLight.h @@ -148,180 +148,10 @@ protected: ColorType colorType; //!< holds the \ref ColorType of the light }; -// supports groups, scenes, on/off and dimming -class HueDimmableLight : public HueLight -{ - friend class Hue; - -public: - //! virtual function that sets the brightness of this light. Ranging from 0=off to 255=fully on - //! \param bri Unsigned int that specifies the brightness - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - virtual bool setBrightness(unsigned int bri, uint8_t transistion = 4); - -protected: - //! protected ctor that is used by \ref Hue class. - //! \param ip String that specifies the ip of the Hue bridge - //! \param username String that specifies the username used to control the bridge - //! \param id Integer that specifies the id of this light - HueDimmableLight(const std::string& ip, const std::string& username, int id) : HueLight(ip, username, id) {}; -}; - -// supports groups, scenes, on/off, dimming, and setting of color temperature -class HueTemperatureLight : public HueDimmableLight -{ - friend class Hue; - -public: - //! Fucntion that sets the color temperature of this light in mired. Ranging from 153 to 500 - //! \param mired Unsigned int that specifies the color temperature in Mired - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - virtual bool setColorTemperature(unsigned int mired, uint8_t transistion = 4); - - //! Function that lets the light perform one breath cycle in specified color temperature. - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs - //! \param mired Color temperature in mired - //! \return Bool that is true on success - bool alertTemperature(unsigned int mired); - -protected: - //! protected ctor that is used by \ref Hue class. - //! \param ip String that specifies the ip of the Hue bridge - //! \param username String that specifies the username used to control the bridge - //! \param id Integer that specifies the id of this light - HueTemperatureLight(const std::string& ip, const std::string& username, int id) : HueDimmableLight(ip, username, id) {}; -}; - -// xy beats ct beats hue/sat. -// supports groups, scenes, on / off, dimming and color control(hue / saturation, enhanced hue, color loop and XY) -class HueColorLight : public HueDimmableLight -{ - friend class Hue; - -public: - //! 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. - //! \param hue uint16_t that specifies the hue - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - virtual bool setColorHue(uint16_t hue, uint8_t transistion = 4); - - //! 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. - //! \param sat uint8_t that specifies the saturation - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - virtual bool setColorSaturation(uint8_t sat, uint8_t transistion = 4); - - //! Function to set the color of this light with specified hue and saturation. - //! \param hue uint16_t that specifies the hue - //! \param sat uint8_t that specifies the saturation - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms. - //! \return Bool that is true on success - virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4); - - //! Function to set the color of this light in CIE with specified x y. Where x and y are ranging from 0 to 1. - //! \param x float that specifies the x coordinate in CIE - //! \param y float that specifies the y coordinate in CIE - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - virtual bool setColorXY(float x, float y, uint8_t transistion = 4); - - //! 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. - //! \param r uint8_t that specifies the red color percentage - //! \param g uint8_t that specifies the green color percentage - //! \param b uint8_t that specifies the blue color percentage - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion = 4); - - //! Function to enable colorloop effect. The colorloop effect will loop through all colors on current hue and saturation levels. - //! Notice that none of the setter functions check for this and the colorloop can only be disabled via this function. - //! or by simply calling Off()/OffNoRefresh() and then On()/OnNoRefresh(), - //! so you could alternatively call Off() and then use any of the setter functions - //! \param on bool that enables this feature when true and disables it when false - //! \return Bool that is true on success - virtual bool setColorLoop(bool on); - - //! Function that lets the light perform one breath cycle in specified color. - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs - //! \param hue uint16_t that specifies the hue - //! \param sat uint8_t that specifies the saturation - //! \return Bool that is true on success - bool alertHueSaturation(uint16_t hue, uint8_t sat); - - //! Function that lets the light perform one breath cycle in specified color. - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs - //! \param x float that specifies the x coordinate in CIE - //! \param y float that specifies the y coordinate in CIE - //! \return Bool that is true on success - bool alertXY(float x, float y); - - //! Function that lets the light perform one breath cycle in specified color. - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs - //! \param r uint8_t that specifies the red color percentage - //! \param g uint8_t that specifies the green color percentage - //! \param b uint8_t that specifies the blue color percentage - //! \return Bool that is true on success - bool alertRGB(uint8_t r, uint8_t g, uint8_t b); - -protected: - //! protected ctor that is used by \ref Hue class. - //! \param ip String that specifies the ip of the Hue bridge - //! \param username String that specifies the username used to control the bridge - //! \param id Integer that specifies the id of this light - HueColorLight(const std::string& ip, const std::string& username, int id) : HueDimmableLight(ip, username, id) {}; - - /*private: - 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*/ -}; - -// supports same as Color light, but which supports additional setting of color temperature -class HueExtendedColorLight : public HueColorLight -{ - friend class Hue; - -public: - //! Fucntion that sets the color temperature of this light in mired. Ranging from 153 to 500. - //! \param mired Unsigned int that specifies the color temperature in Mired - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms - //! \return Bool that is true on success - virtual bool setColorTemperature(unsigned int mired, uint8_t transistion = 4); - //! Function that lets the light perform one breath cycle in specified color temperature. - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs - //! \param mired Color temperature in mired - //! \return Bool that is true on success - bool alertTemperature(unsigned int mired); - //! Function that lets the light perform one breath cycle in specified color. - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs - //! \param hue uint16_t that specifies the hue - //! \param sat uint8_t that specifies the saturation - //! \return Bool that is true on success - bool alertHueSaturation(uint16_t hue, uint8_t sat); - //! Function that lets the light perform one breath cycle in specified color. - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs - //! \param x float that specifies the x coordinate in CIE - //! \param y float that specifies the y coordinate in CIE - //! \return Bool that is true on success - bool alertXY(float x, float y); - //! Function that lets the light perform one breath cycle in specified color. - //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs - //! \param r uint8_t that specifies the red color percentage - //! \param g uint8_t that specifies the green color percentage - //! \param b uint8_t that specifies the blue color percentage - //! \return Bool that is true on success - bool alertRGB(uint8_t r, uint8_t g, uint8_t b); -protected: - //! protected ctor that is used by \ref Hue class. - //! \param ip String that specifies the ip of the Hue bridge - //! \param username String that specifies the username used to control the bridge - //! \param id Integer that specifies the id of this light - HueExtendedColorLight(const std::string& ip, const std::string& username, int id) : HueColorLight(ip, username, id) {}; -}; -#endif \ No newline at end of file +#endif diff --git a/hueplusplus/include/HueTemperatureLight.h b/hueplusplus/include/HueTemperatureLight.h new file mode 100644 index 0000000..74287a5 --- /dev/null +++ b/hueplusplus/include/HueTemperatureLight.h @@ -0,0 +1,31 @@ +#ifndef _HUE_TEMPAERATURE_LIGHT +#define _HUE_TEMPAERATURE_LIGHT + +#include "HueDimmableLight.h" + +// supports groups, scenes, on/off, dimming, and setting of color temperature +class HueTemperatureLight : public HueDimmableLight +{ + friend class Hue; + +public: + //! Fucntion that sets the color temperature of this light in mired. Ranging from 153 to 500 + //! \param mired Unsigned int that specifies the color temperature in Mired + //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! \return Bool that is true on success + virtual bool setColorTemperature(unsigned int mired, uint8_t transistion = 4); + + //! Function that lets the light perform one breath cycle in specified color temperature. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param mired Color temperature in mired + //! \return Bool that is true on success + bool alertTemperature(unsigned int mired); + +protected: + //! protected ctor that is used by \ref Hue class. + //! \param ip String that specifies the ip of the Hue bridge + //! \param username String that specifies the username used to control the bridge + //! \param id Integer that specifies the id of this light + HueTemperatureLight(const std::string& ip, const std::string& username, int id) : HueDimmableLight(ip, username, id) {}; +}; +#endif diff --git a/hueplusplus/UPnP.h b/hueplusplus/include/UPnP.h index ad60297..ad60297 100644 --- a/hueplusplus/UPnP.h +++ b/hueplusplus/include/UPnP.h diff --git a/hueplusplus/json/json.h b/hueplusplus/include/json/json.h index 94a1e2f..94a1e2f 100644 --- a/hueplusplus/json/json.h +++ b/hueplusplus/include/json/json.h