diff --git a/hueplusplus/ExtendedColorHueStrategy.cpp b/hueplusplus/ExtendedColorHueStrategy.cpp new file mode 100755 index 0000000..856941b --- /dev/null +++ b/hueplusplus/ExtendedColorHueStrategy.cpp @@ -0,0 +1,261 @@ +/** + \file ExtendedColorHueStrategy.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/ExtendedColorHueStrategy.h" + +#include +#include +#include + +bool ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const +{ + light.refreshState(); + std::string cType = light.state["state"]["colormode"].asString(); + bool on = light.state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = light.state["state"]["hue"].asUInt(); + uint8_t oldSat = light.state["state"]["sat"].asUInt(); + if (!light.setColorHueSaturation(hue, sat, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = light.state["state"]["xy"][0].asFloat(); + float oldY = light.state["state"]["xy"][1].asFloat(); + if (!light.setColorHueSaturation(hue, sat, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorXY(oldX, oldY, 1); + } + } + else if (cType == "ct") + { + uint16_t oldCT = light.state["state"]["ct"].asUInt(); + if (!light.setColorHueSaturation(hue, sat, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorTemperature(oldCT, 1); + } + } + else + { + return false; + } +} + +bool ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const +{ + light.refreshState(); + std::string cType = light.state["state"]["colormode"].asString(); + bool on = light.state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = light.state["state"]["hue"].asUInt(); + uint8_t oldSat = light.state["state"]["sat"].asUInt(); + if (!light.setColorXY(x, y, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = light.state["state"]["xy"][0].asFloat(); + float oldY = light.state["state"]["xy"][1].asFloat(); + if (!light.setColorXY(x, y, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorXY(oldX, oldY, 1); + } + } + else if (cType == "ct") + { + uint16_t oldCT = light.state["state"]["ct"].asUInt(); + if (!light.setColorXY(x, y, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorTemperature(oldCT, 1); + } + } + else + { + return false; + } +} + +bool ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const +{ + light.refreshState(); + std::string cType = light.state["state"]["colormode"].asString(); + bool on = light.state["state"]["on"].asBool(); + if (cType == "hs") + { + uint16_t oldHue = light.state["state"]["hue"].asUInt(); + uint8_t oldSat = light.state["state"]["sat"].asUInt(); + if (!light.setColorRGB(r, g, b, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorHueSaturation(oldHue, oldSat, 1); + } + } + else if (cType == "xy") + { + float oldX = light.state["state"]["xy"][0].asFloat(); + float oldY = light.state["state"]["xy"][1].asFloat(); + if (!light.setColorRGB(r, g, b, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorXY(oldX, oldY, 1); + } + } + else if (cType == "ct") + { + uint16_t oldCT = light.state["state"]["ct"].asUInt(); + if (!light.setColorRGB(r, g, b, 1)) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(110)); + if (!light.alert()) + { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1500)); + if (!on) + { + return light.OffNoRefresh(1); + } + else + { + return light.setColorTemperature(oldCT, 1); + } + } + else + { + return false; + } +} \ No newline at end of file diff --git a/hueplusplus/HueExtendedColorLight.cpp b/hueplusplus/ExtendedColorTemperatureStrategy.cpp old mode 100644 new mode 100755 index b67216d..5cc7bdf --- a/hueplusplus/HueExtendedColorLight.cpp +++ b/hueplusplus/ExtendedColorTemperatureStrategy.cpp @@ -1,5 +1,5 @@ /** - \file HueExtendedColorLight.cpp + \file ExtendedColorTemperatureStrategy.cpp Copyright Notice\n Copyright (C) 2017 Jan Rogall - developer\n Copyright (C) 2017 Moritz Wirger - developer\n @@ -17,25 +17,26 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA **/ -#include "include/HueExtendedColorLight.h" +#include "include/ExtendedColorTemperatureStrategy.h" #include #include #include -bool HueExtendedColorLight::setColorTemperature(unsigned int mired, uint8_t transistion) + +bool ExtendedColorTemperatureStrategy::setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const { - refreshState(); + light.refreshState(); Json::Value request(Json::objectValue); - if (transistion != 4) + if (transition != 4) { - request["transitiontime"] = transistion; + request["transitiontime"] = transition; } - if (state["state"]["on"].asBool() != true) + if (light.state["state"]["on"].asBool() != true) { request["on"] = true; } - if (state["state"]["ct"].asUInt() != mired || state["state"]["colormode"].asString() != "ct") + if (light.state["state"]["ct"].asUInt() != mired || light.state["state"]["colormode"].asString() != "ct") { if (mired > 500) { @@ -54,10 +55,10 @@ bool HueExtendedColorLight::setColorTemperature(unsigned int mired, uint8_t tran return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = light.SendPutRequest(request); //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; + std::string path = "/lights/" + std::to_string(light.id) + "/state/"; bool success = true; int i = 0; if (success && request.isMember("transitiontime")) @@ -80,235 +81,77 @@ bool HueExtendedColorLight::setColorTemperature(unsigned int mired, uint8_t tran 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) +bool ExtendedColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const { - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); + light.refreshState(); + std::string cType = light.state["state"]["colormode"].asString(); + bool on = light.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)) + uint16_t oldHue = light.state["state"]["hue"].asUInt(); + uint8_t oldSat = light.state["state"]["sat"].asUInt(); + if (!light.setColorTemperature(mired, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorHueSaturation(oldHue, oldSat, 1); + return light.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)) + float oldX = light.state["state"]["xy"][0].asFloat(); + float oldY = light.state["state"]["xy"][1].asFloat(); + if (!light.setColorTemperature(mired, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorXY(oldX, oldY, 1); + return light.setColorXY(oldX, oldY, 1); } } else if (cType == "ct") { - uint16_t oldCT = state["state"]["ct"].asUInt(); - if (!setColorXY(x, y, 1)) + uint16_t oldCT = light.state["state"]["ct"].asUInt(); + if (!light.setColorTemperature(mired, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorTemperature(oldCT, 1); + return light.setColorTemperature(oldCT, 1); } } else @@ -317,81 +160,3 @@ bool HueExtendedColorLight::alertXY(float x, float y) } } -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/Hue.cpp b/hueplusplus/Hue.cpp old mode 100644 new mode 100755 index c148601..fee2c86 --- a/hueplusplus/Hue.cpp +++ b/hueplusplus/Hue.cpp @@ -19,10 +19,11 @@ #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 "include/SimpleBrightnessStrategy.h" +#include "include/SimpleColorHueStrategy.h" +#include "include/ExtendedColorHueStrategy.h" +#include "include/SimpleColorTemperatureStrategy.h" +#include "include/ExtendedColorTemperatureStrategy.h" #include "include/HttpHandler.h" #include "include/UPnP.h" @@ -147,6 +148,7 @@ std::string HueFinder::RequestUsername(const std::string & ip) const { // [{"success":{"username": "83b7780291a6ceffbe0bd049104df"}}] std::cout << "Success! Link button was pressed!\n"; + std::cout << "Username is \"" << answer[0]["success"]["username"].asString() << "\"\n";; return answer[0]["success"]["username"].asString(); } if (answer[0]["error"] != Json::nullValue) @@ -159,8 +161,15 @@ std::string HueFinder::RequestUsername(const std::string & ip) const } -Hue::Hue(const std::string& ip, const std::string& username) : ip(ip), username(username) +Hue::Hue(const std::string& ip, const std::string& username) : +ip(ip), +username(username) { + _simpleBrightnessStrategy = std::shared_ptr( new SimpleBrightnessStrategy ); + _simpleColorHueStrategy = std::shared_ptr( new SimpleColorHueStrategy ); + _extendedColorHueStrategy = std::shared_ptr( new ExtendedColorHueStrategy ); + _simpleColorTemperatureStrategy = std::shared_ptr( new SimpleColorTemperatureStrategy ); + _extendedColorTemperatureStrategy = std::shared_ptr( new ExtendedColorTemperatureStrategy ); } std::string Hue::getBridgeIP() @@ -233,56 +242,137 @@ void Hue::setIP(const std::string ip) this->ip = ip; } -std::unique_ptr Hue::getLight(int id) +const HueLight& Hue::getLight(int id) { + if(lights.count(id) > 0) + { + return lights.find(id)->second; + } refreshState(); if (state["lights"][std::to_string(id)] == Json::nullValue) { std::cout << "Error in Hue getLight(): light with id " << id << " is not valid\n"; throw(std::runtime_error("Error in Hue getLight(): light id is not valid")); } - std::cout << state["lights"][std::to_string(id)] << std::endl; + //std::cout << state["lights"][std::to_string(id)] << std::endl; std::string type = state["lights"][std::to_string(id)]["modelid"].asString(); - std::cout << type << std::endl; + //std::cout << type << std::endl; if (type == "LCT001" || type == "LCT002" || type == "LCT003" || type == "LCT007" || type == "LLM001") { // HueExtendedColorLight Gamut B - std::unique_ptr light(new HueExtendedColorLight(ip, username, id)); - light->colorType = ColorType::GAMUT_B; - return light; + HueLight light = HueLight(ip, username, id); + light.setBrightnessStrategy(_simpleBrightnessStrategy); + light.setColorHueStrategy(_extendedColorHueStrategy); + light.setColorTemperatureStrategy(_extendedColorTemperatureStrategy); + light.colorType = ColorType::GAMUT_B; + lights.emplace(id, light); + return lights.find(id)->second; } else if (type == "LCT010" || type == "LCT011" || type == "LCT014" || type == "LLC020" || type == "LST002") { // HueExtendedColorLight Gamut C - std::unique_ptr light(new HueExtendedColorLight(ip, username, id)); - light->colorType = ColorType::GAMUT_C; - return light; + HueLight light = HueLight(ip, username, id); + light.setBrightnessStrategy(_simpleBrightnessStrategy); + light.setColorHueStrategy(_extendedColorHueStrategy); + light.setColorTemperatureStrategy(_extendedColorTemperatureStrategy); + light.colorType = ColorType::GAMUT_C; + lights.emplace(id, light); + return lights.find(id)->second; } else if (type == "LST001" || type == "LLC006" || type == "LLC007" || type == "LLC010" || type == "LLC011" || type == "LLC012" || type == "LLC013") { // HueColorLight Gamut A - std::unique_ptr light(new HueColorLight(ip, username, id)); - light->colorType = ColorType::GAMUT_A; - return light; + HueLight light = HueLight(ip, username, id); + light.setBrightnessStrategy(_simpleBrightnessStrategy); + light.setColorHueStrategy(_simpleColorHueStrategy); + light.setColorTemperatureStrategy(_simpleColorTemperatureStrategy); + light.colorType = ColorType::GAMUT_A; + lights.emplace(id, light); + return lights.find(id)->second; } else if (type == "LWB004" || type == "LWB006" || type == "LWB007" || type == "LWB010" || type == "LWB014") { // HueDimmableLight No Color Type - std::unique_ptr light(new HueDimmableLight(ip, username, id)); - light->colorType = ColorType::NONE; - return light; + HueLight light = HueLight(ip, username, id); + light.setBrightnessStrategy(_simpleBrightnessStrategy); + //light.setColorHueStrategy(); + //light.setColorTemperatureStrategy(); + light.colorType = ColorType::NONE; + lights.emplace(id, light); + return lights.find(id)->second; } else if (type == "LLM010" || type == "LLM011" || type == "LLM012" || type == "LTW001" || type == "LTW004" || type == "LTW013" || type == "LTW014") { // HueTemperatureLight - std::unique_ptr light(new HueTemperatureLight(ip, username, id)); - light->colorType = ColorType::TEMPERATURE; - return light; + HueLight light = HueLight(ip, username, id); + light.setBrightnessStrategy(_simpleBrightnessStrategy); + //light.setColorHueStrategy(); + light.setColorTemperatureStrategy(_simpleColorTemperatureStrategy); + light.colorType = ColorType::TEMPERATURE; + lights.emplace(id, light); + return lights.find(id)->second; } std::cout << "Could not determine HueLight type!\n"; throw(std::runtime_error("Could not determine HueLight type!")); } +/*const std::map& Hue::getAllLightTypes() +{ + refreshState(); + for (const auto& name : state["lights"].getMemberNames()) + { + std::string type = state["lights"][name]["modelid"].asString(); + int id = std::stoi(name); + + if (type == "LCT001" || type == "LCT002" || type == "LCT003" || type == "LCT007" || type == "LLM001") + { + // HueExtendedColorLight Gamut B + lights[id].second = ColorType::GAMUT_B; + } + else if (type == "LCT010" || type == "LCT011" || type == "LCT014" || type == "LLC020" || type == "LST002") + { + // HueExtendedColorLight Gamut C + lights[id].second = ColorType::GAMUT_C; + } + else if (type == "LST001" || type == "LLC006" || type == "LLC007" || type == "LLC010" || type == "LLC011" || type == "LLC012" || type == "LLC013") + { + // HueColorLight Gamut A + lights[id].second = ColorType::GAMUT_A; + } + else if (type == "LWB004" || type == "LWB006" || type == "LWB007" || type == "LWB010" || type == "LWB014") + { + // HueDimmableLight No Color Type + lights[id].second = ColorType::NONE; + } + else if (type == "LLM010" || type == "LLM011" || type == "LLM012" || type == "LTW001" || type == "LTW004" || type == "LTW013" || type == "LTW014") + { + // HueTemperatureLight + lights[id].second = ColorType::TEMPERATURE; + } + } + return lights; +}*/ + + +std::vector> Hue::getAllLights() +{ + refreshState(); + for (const auto& name : state["lights"].getMemberNames()) + { + uint8_t id = std::stoi(name); + if(lights.count(id)<=0) + { + getLight(id); + } + } + std::vector> result; + for (const auto& entry : lights) + { + result.emplace_back(entry.second); + } + return result; +} + void Hue::refreshState() { if (username.empty()) @@ -301,13 +391,13 @@ void Hue::refreshState() Json::Value answer; std::string postAnswer = HttpHandler().sendRequestGetBody(get.c_str(), ip, 80); - std::cout <<"\""<< postAnswer << "\"\n" << std::endl; + //std::cout <<"\""<< postAnswer << "\"\n" << std::endl; if (!reader->parse(postAnswer.c_str(), postAnswer.c_str() + postAnswer.length(), &answer, &error)) { std::cout << "Error while parsing JSON in refreshState of Hue: " << error << std::endl; throw(std::runtime_error("Error while parsing JSON in refreshState of Hue")); } - if (answer["lights"] != Json::nullValue) + if (answer.isMember("lights")) { state = answer; } diff --git a/hueplusplus/HueLight.cpp b/hueplusplus/HueLight.cpp old mode 100644 new mode 100755 index d88af32..4d4e0be --- a/hueplusplus/HueLight.cpp +++ b/hueplusplus/HueLight.cpp @@ -27,35 +27,18 @@ #include #include -bool HueLight::On(uint8_t transistion) +bool HueLight::On(uint8_t transition) { std::cout << "Turning lamp with id: " << id << " on\n"; refreshState(); - return OnNoRefresh(transistion); + return OnNoRefresh(transition); } -bool HueLight::Off(uint8_t transistion) +bool HueLight::Off(uint8_t transition) { std::cout << "Turning lamp with id: " << id << " off\n"; refreshState(); - return OffNoRefresh(transistion); -} - -bool HueLight::alert() -{ - std::cout << "alert()\n"; - - Json::Value request; - request["alert"] = "select"; - - Json::Value reply = SendPutRequest(request); - - if (reply[0]["success"]["/lights/" + std::to_string(id) + "/state/alert"].asString() == "select") - { - return true; - } - - return false; + return OffNoRefresh(transition); } std::string HueLight::getName() @@ -74,26 +57,48 @@ unsigned int HueLight::MiredToKelvin(unsigned int mired) return int(0.5f + (1000000 / mired)); } +bool HueLight::alert() +{ + std::cout << "alert()\n"; + + Json::Value request; + request["alert"] = "select"; + + Json::Value reply = SendPutRequest(request); + + if (reply[0]["success"]["/lights/" + std::to_string(id) + "/state/alert"].asString() == "select") + { + return true; + } + + return false; +} -HueLight::HueLight(const std::string& ip, const std::string& username, int id) : ip(ip), username(username), id(id) +HueLight::HueLight(const std::string& ip, const std::string& username, int id) : +ip(ip), +username(username), +id(id), +_brightnessStrategy(nullptr), +_colorTemperatureStrategy(nullptr), +_colorHueStategy(nullptr) { refreshState(); } -bool HueLight::OnNoRefresh(uint8_t transistion) +bool HueLight::OnNoRefresh(uint8_t transition) { std::cout << "\tOnNoRefresh()\n"; Json::Value request(Json::objectValue); - if (transistion != 4) + if (transition != 4) { - request["transistiontime"] = transistion; + request["transitiontime"] = transition; } if (state["state"]["on"].asBool() != true) { request["on"] = true; } - if (!request.isMember("on") && !request.isMember("transistiontime")) + if (!request.isMember("on") && !request.isMember("transitiontime")) { //Nothing needs to be changed return true; @@ -105,10 +110,10 @@ bool HueLight::OnNoRefresh(uint8_t transistion) std::string path = "/lights/" + std::to_string(id) + "/state/"; bool success = true; int i = 0; - if (success && request.isMember("transistiontime")) + 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 + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); ++i; } if (success && request.isMember("on")) @@ -119,20 +124,20 @@ bool HueLight::OnNoRefresh(uint8_t transistion) return success; } -bool HueLight::OffNoRefresh(uint8_t transistion) +bool HueLight::OffNoRefresh(uint8_t transition) { std::cout << "\tOffNoRefresh()\n"; Json::Value request(Json::objectValue); - if (transistion != 4) + if (transition != 4) { - request["transistiontime"] = transistion; + request["transitiontime"] = transition; } if (state["state"]["on"].asBool() != false) { request["on"] = false; } - if (!request.isMember("on") && !request.isMember("transistiontime")) + if (!request.isMember("on") && !request.isMember("transitiontime")) { //Nothing needs to be changed return true; @@ -144,10 +149,10 @@ bool HueLight::OffNoRefresh(uint8_t transistion) std::string path = "/lights/" + std::to_string(id) + "/state/"; bool success = true; int i = 0; - if (success && request.isMember("transistiontime")) + 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 + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); ++i; } if (success && request.isMember("on")) diff --git a/hueplusplus/HueDimmableLight.cpp b/hueplusplus/SimpleBrightnessStrategy.cpp old mode 100644 new mode 100755 index efd0f4d..733ea0d --- a/hueplusplus/HueDimmableLight.cpp +++ b/hueplusplus/SimpleBrightnessStrategy.cpp @@ -1,12 +1,12 @@ /** - \file HueDimmableLight.cpp + \file SimpleBrightnessStrategy.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 + 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 @@ -17,21 +17,21 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA **/ -#include "include/HueDimmableLight.h" +#include "include/SimpleBrightnessStrategy.h" #include #include #include -bool HueDimmableLight::setBrightness(unsigned int bri, uint8_t transistion) +bool SimpleBrightnessStrategy::setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const { - std::cout << "Setting lamp with id: " << id << " to brightness of " << bri << std::endl; - refreshState(); + std::cout << "Setting lamp with id: " << light.id << " to brightness of " << bri << std::endl; + light.refreshState(); if (bri == 0) { - if (state["state"]["on"] == true) + if (light.state["state"]["on"] == true) { - return OffNoRefresh(transistion); + return light.OffNoRefresh(transition); } else { @@ -41,15 +41,15 @@ bool HueDimmableLight::setBrightness(unsigned int bri, uint8_t transistion) else { Json::Value request(Json::objectValue); - if (transistion != 4) + if (transition != 4) { - request["transistiontime"] = transistion; + request["transitiontime"] = transition; } - if (state["state"]["on"].asBool() != true) + if (light.state["state"]["on"].asBool() != true) { request["on"] = true; } - if (state["state"]["bri"].asUInt() != bri) + if (light.state["state"]["bri"].asUInt() != bri) { bri -= 1; if (bri > 254) @@ -65,16 +65,16 @@ bool HueDimmableLight::setBrightness(unsigned int bri, uint8_t transistion) return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = light.SendPutRequest(request); //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; + std::string path = "/lights/" + std::to_string(light.id) + "/state/"; bool success = true; int i = 0; - if (success && request.isMember("transistiontime")) + 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 + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); ++i; } if (success && request.isMember("on")) diff --git a/hueplusplus/HueColorLight.cpp b/hueplusplus/SimpleColorHueStrategy.cpp old mode 100644 new mode 100755 index 882b43b..9817687 --- a/hueplusplus/HueColorLight.cpp +++ b/hueplusplus/SimpleColorHueStrategy.cpp @@ -1,5 +1,5 @@ /** - \file HueColorLight.cpp + \file SimpleColorHueStrategy.cpp Copyright Notice\n Copyright (C) 2017 Jan Rogall - developer\n Copyright (C) 2017 Moritz Wirger - developer\n @@ -17,25 +17,25 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA **/ -#include "include/HueColorLight.h" +#include "include/SimpleColorHueStrategy.h" #include #include #include -bool HueColorLight::setColorHue(uint16_t hue, uint8_t transistion) +bool SimpleColorHueStrategy::setColorHue(uint16_t hue, uint8_t transistion, HueLight& light) const { - refreshState(); + light.refreshState(); Json::Value request(Json::objectValue); if (transistion != 4) { request["transistiontime"] = transistion; } - if (state["state"]["on"].asBool() != true) + if (light.state["state"]["on"].asBool() != true) { request["on"] = true; } - if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs") + if (light.state["state"]["hue"].asUInt() != hue || light.state["state"]["colormode"].asString() != "hs") { hue = hue % 65535; request["hue"] = hue; @@ -47,10 +47,10 @@ bool HueColorLight::setColorHue(uint16_t hue, uint8_t transistion) return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = light.SendPutRequest(request); //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; + std::string path = "/lights/" + std::to_string(light.id) + "/state/"; bool success = true; int i = 0; if (success && request.isMember("transistiontime")) @@ -73,19 +73,19 @@ bool HueColorLight::setColorHue(uint16_t hue, uint8_t transistion) return success; } -bool HueColorLight::setColorSaturation(uint8_t sat, uint8_t transistion) +bool SimpleColorHueStrategy::setColorSaturation(uint8_t sat, uint8_t transistion, HueLight& light) const { - refreshState(); + light.refreshState(); Json::Value request(Json::objectValue); if (transistion != 4) { request["transistiontime"] = transistion; } - if (state["state"]["on"].asBool() != true) + if (light.state["state"]["on"].asBool() != true) { request["on"] = true; } - if (state["state"]["sat"].asUInt() != sat) + if (light.state["state"]["sat"].asUInt() != sat) { if (sat > 254) { @@ -100,10 +100,10 @@ bool HueColorLight::setColorSaturation(uint8_t sat, uint8_t transistion) return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = light.SendPutRequest(request); //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; + std::string path = "/lights/" + std::to_string(light.id) + "/state/"; bool success = true; int i = 0; if (success && request.isMember("transistiontime")) @@ -126,25 +126,25 @@ bool HueColorLight::setColorSaturation(uint8_t sat, uint8_t transistion) return success; } -bool HueColorLight::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transistion) +bool SimpleColorHueStrategy::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transistion, HueLight& light) const { - refreshState(); + light.refreshState(); Json::Value request(Json::objectValue); if (transistion != 4) { request["transitiontime"] = transistion; } - if (state["state"]["on"].asBool() != true) + if (light.state["state"]["on"].asBool() != true) { request["on"] = true; } - if (state["state"]["hue"].asUInt() != hue || state["state"]["colormode"].asString() != "hs") + if (light.state["state"]["hue"].asUInt() != hue || light.state["state"]["colormode"].asString() != "hs") { hue = hue % 65535; request["hue"] = hue; } - if (state["state"]["sat"].asUInt() != sat || state["state"]["colormode"].asString() != "hs") + if (light.state["state"]["sat"].asUInt() != sat || light.state["state"]["colormode"].asString() != "hs") { if (sat > 254) { @@ -159,10 +159,10 @@ bool HueColorLight::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t tra return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = light.SendPutRequest(request); //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; + std::string path = "/lights/" + std::to_string(light.id) + "/state/"; bool success = true; int i = 0; if (success && request.isMember("transitiontime")) @@ -191,20 +191,20 @@ bool HueColorLight::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t tra return success; } -bool HueColorLight::setColorXY(float x, float y, uint8_t transistion) +bool SimpleColorHueStrategy::setColorXY(float x, float y, uint8_t transistion, HueLight& light) const { - refreshState(); + light.refreshState(); Json::Value request(Json::objectValue); if (transistion != 4) { request["transitiontime"] = transistion; } - if (state["state"]["on"].asBool() != true) + if (light.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") + if (light.state["state"]["xy"][0].asFloat() != x || light.state["state"]["xy"][1].asFloat() != y || light.state["state"]["colormode"].asString() != "xy") { request["xy"][0] = x; request["xy"][1] = y; @@ -216,10 +216,10 @@ bool HueColorLight::setColorXY(float x, float y, uint8_t transistion) return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = light.SendPutRequest(request); //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; + std::string path = "/lights/" + std::to_string(light.id) + "/state/"; bool success = true; int i = 0; if (success && request.isMember("transitiontime")) @@ -246,7 +246,7 @@ bool HueColorLight::setColorXY(float x, float y, uint8_t transistion) return success; } -bool HueColorLight::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion) +bool SimpleColorHueStrategy::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transistion, HueLight& light) const { float red = r / 255; float green = g / 255; @@ -264,21 +264,21 @@ bool HueColorLight::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transis float x = X / (X + Y + Z); float y = Y / (X + Y + Z); - return setColorXY(x, y, transistion); + return light.setColorXY(x, y, transistion); } -bool HueColorLight::setColorLoop(bool on) +bool SimpleColorHueStrategy::setColorLoop(bool on, HueLight& light) const { //colorloop - refreshState(); + light.refreshState(); Json::Value request(Json::objectValue); - if (state["state"]["on"].asBool() != true) + if (light.state["state"]["on"].asBool() != true) { request["on"] = true; } std::string effect; - if ((effect = on ? "colorloop" : "none") != state["state"]["effect"].asString()) + if ((effect = on ? "colorloop" : "none") != light.state["state"]["effect"].asString()) { request["effect"] = effect; } @@ -288,10 +288,10 @@ bool HueColorLight::setColorLoop(bool on) return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = light.SendPutRequest(request); //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; + std::string path = "/lights/" + std::to_string(light.id) + "/state/"; bool success = true; int i = 0; if (success && request.isMember("on")) @@ -308,55 +308,55 @@ bool HueColorLight::setColorLoop(bool on) return success; } -bool HueColorLight::alertHueSaturation(uint16_t hue, uint8_t sat) +bool SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const { - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); + light.refreshState(); + std::string cType = light.state["state"]["colormode"].asString(); + bool on = light.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)) + uint16_t oldHue = light.state["state"]["hue"].asUInt(); + uint8_t oldSat = light.state["state"]["sat"].asUInt(); + if (!light.setColorHueSaturation(hue, sat, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorHueSaturation(oldHue, oldSat, 1); + return light.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)) + float oldX = light.state["state"]["xy"][0].asFloat(); + float oldY = light.state["state"]["xy"][1].asFloat(); + if (!light.setColorHueSaturation(hue, sat, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorXY(oldX, oldY, 1); + return light.setColorXY(oldX, oldY, 1); } } else @@ -365,55 +365,55 @@ bool HueColorLight::alertHueSaturation(uint16_t hue, uint8_t sat) } } -bool HueColorLight::alertXY(float x, float y) +bool SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const { - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); + light.refreshState(); + std::string cType = light.state["state"]["colormode"].asString(); + bool on = light.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)) + uint16_t oldHue = light.state["state"]["hue"].asUInt(); + uint8_t oldSat = light.state["state"]["sat"].asUInt(); + if (!light.setColorXY(x, y, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorHueSaturation(oldHue, oldSat, 1); + return light.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)) + float oldX = light.state["state"]["xy"][0].asFloat(); + float oldY = light.state["state"]["xy"][1].asFloat(); + if (!light.setColorXY(x, y, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorXY(oldX, oldY, 1); + return light.setColorXY(oldX, oldY, 1); } } else @@ -422,55 +422,55 @@ bool HueColorLight::alertXY(float x, float y) } } -bool HueColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b) +bool SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const { - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); + light.refreshState(); + std::string cType = light.state["state"]["colormode"].asString(); + bool on = light.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)) + uint16_t oldHue = light.state["state"]["hue"].asUInt(); + uint8_t oldSat = light.state["state"]["sat"].asUInt(); + if (!light.setColorRGB(r, g, b, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorHueSaturation(oldHue, oldSat, 1); + return light.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)) + float oldX = light.state["state"]["xy"][0].asFloat(); + float oldY = light.state["state"]["xy"][1].asFloat(); + if (!light.setColorRGB(r, g, b, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorXY(oldX, oldY, 1); + return light.setColorXY(oldX, oldY, 1); } } else @@ -479,7 +479,7 @@ bool HueColorLight::alertRGB(uint8_t r, uint8_t g, uint8_t b) } } -/*bool HueColorLight::pointInTriangle(float pointx, float pointy, float x0, float y0, float x1, float y1, float x2, float y2) +/*bool SimpleColorHueStrategy::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; diff --git a/hueplusplus/HueTemperatureLight.cpp b/hueplusplus/SimpleColorTemperatureStrategy.cpp old mode 100644 new mode 100755 index b37a8f7..a6decd4 --- a/hueplusplus/HueTemperatureLight.cpp +++ b/hueplusplus/SimpleColorTemperatureStrategy.cpp @@ -1,5 +1,5 @@ /** - \file HueTemperatureLight.cpp + \file SimpleColorTemperatureStrategy.cpp Copyright Notice\n Copyright (C) 2017 Jan Rogall - developer\n Copyright (C) 2017 Moritz Wirger - developer\n @@ -17,26 +17,26 @@ Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA **/ -#include "include/HueTemperatureLight.h" +#include "include/SimpleColorTemperatureStrategy.h" #include #include #include -bool HueTemperatureLight::setColorTemperature(unsigned int mired, uint8_t transistion) +bool SimpleColorTemperatureStrategy::setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const { - refreshState(); + light.refreshState(); Json::Value request(Json::objectValue); - if (transistion != 4) + if (transition != 4) { - request["transistiontime"] = transistion; + request["transitiontime"] = transition; } - if (state["state"]["on"].asBool() != true) + if (light.state["state"]["on"].asBool() != true) { request["on"] = true; } - if (state["state"]["ct"].asUInt() != mired) + if (light.state["state"]["ct"].asUInt() != mired) { if (mired > 500) { @@ -55,16 +55,16 @@ bool HueTemperatureLight::setColorTemperature(unsigned int mired, uint8_t transi return true; } - Json::Value reply = SendPutRequest(request); + Json::Value reply = light.SendPutRequest(request); //Check whether request was successful - std::string path = "/lights/" + std::to_string(id) + "/state/"; + std::string path = "/lights/" + std::to_string(light.id) + "/state/"; bool success = true; int i = 0; - if (success && request.isMember("transistiontime")) + 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 + "transistiontime"].asUInt() == request["transistiontime"].asUInt(); + success = !reply[i].isNull() && reply[i].isMember("success") && reply[i]["success"][path + "transitiontime"].asUInt() == request["transitiontime"].asUInt(); ++i; } if (success && request.isMember("on")) @@ -81,31 +81,31 @@ bool HueTemperatureLight::setColorTemperature(unsigned int mired, uint8_t transi return success; } -bool HueTemperatureLight::alertTemperature(unsigned int mired) +bool SimpleColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const { - refreshState(); - std::string cType = state["state"]["colormode"].asString(); - bool on = state["state"]["on"].asBool(); + light.refreshState(); + std::string cType = light.state["state"]["colormode"].asString(); + bool on = light.state["state"]["on"].asBool(); if (cType == "ct") { - uint16_t oldCT = state["state"]["ct"].asUInt(); - if (!setColorTemperature(mired, 1)) + uint16_t oldCT = light.state["state"]["ct"].asUInt(); + if (!light.setColorTemperature(mired, 1)) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(110)); - if (!alert()) + if (!light.alert()) { return false; } std::this_thread::sleep_for(std::chrono::milliseconds(1500)); if (!on) { - return OffNoRefresh(1); + return light.OffNoRefresh(1); } else { - return setColorTemperature(oldCT, 1); + return light.setColorTemperature(oldCT, 1); } } else diff --git a/hueplusplus/include/BrightnessStrategy.h b/hueplusplus/include/BrightnessStrategy.h new file mode 100755 index 0000000..d46daeb --- /dev/null +++ b/hueplusplus/include/BrightnessStrategy.h @@ -0,0 +1,34 @@ +/** + \file BrightnessStrategy.h + 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 +**/ + +#ifndef _BRIGHTNESS_STRATEGY_H +#define _BRIGHTNESS_STRATEGY_H + +#include + +class HueLight; + +class BrightnessStrategy +{ + public: + virtual bool setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const = 0; + virtual ~BrightnessStrategy() {} +}; + +#endif \ No newline at end of file diff --git a/hueplusplus/include/ColorHueStrategy.h b/hueplusplus/include/ColorHueStrategy.h new file mode 100755 index 0000000..ee7a0e5 --- /dev/null +++ b/hueplusplus/include/ColorHueStrategy.h @@ -0,0 +1,42 @@ +/** + \file ColorHueStrategy.h + 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 +**/ + +#ifndef _COLOR_HUE_STRATEGY_H +#define _COLOR_HUE_STRATEGY_H + +#include + +class HueLight; + +class ColorHueStrategy +{ + public: + virtual bool setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const = 0; + virtual bool setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const = 0; + virtual bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const = 0; + virtual bool setColorXY(float x, float y, uint8_t transition, HueLight& light) const = 0; + virtual bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const = 0; + virtual bool setColorLoop(bool on, HueLight& light) const = 0; + virtual bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const = 0; + virtual bool alertXY(float x, float y, HueLight& light) const = 0; + virtual bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const = 0; + virtual ~ColorHueStrategy() {} +}; + +#endif \ No newline at end of file diff --git a/hueplusplus/include/ColorTemperatureStrategy.h b/hueplusplus/include/ColorTemperatureStrategy.h new file mode 100755 index 0000000..68ad0fa --- /dev/null +++ b/hueplusplus/include/ColorTemperatureStrategy.h @@ -0,0 +1,35 @@ +/** + \file ColorTemperatureStrategy.h + 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 +**/ + +#ifndef _COLOR_TEMPERATURE_STRATEGY_H +#define _COLOR_TEMPERATURE_STRATEGY_H + +#include + +class HueLight; + +class ColorTemperatureStrategy +{ + public: + virtual bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const = 0; + virtual bool alertTemperature(unsigned int mired, HueLight& light) const = 0; + virtual ~ColorTemperatureStrategy() {} +}; + +#endif \ No newline at end of file diff --git a/hueplusplus/include/ExtendedColorHueStrategy.h b/hueplusplus/include/ExtendedColorHueStrategy.h new file mode 100755 index 0000000..27319d8 --- /dev/null +++ b/hueplusplus/include/ExtendedColorHueStrategy.h @@ -0,0 +1,33 @@ +/** + \file ExtendedColorHueStrategy.h + 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 +**/ + +#ifndef _EXTENDED_COLOR_HUE_STRATEGY_H +#define _EXTENDED_COLOR_HUE_STRATEGY_H + +#include "SimpleColorHueStrategy.h" + +class ExtendedColorHueStrategy : public SimpleColorHueStrategy +{ + public: + bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const; + bool alertXY(float x, float y, HueLight& light) const; + bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const; +}; + +#endif \ No newline at end of file diff --git a/hueplusplus/include/ExtendedColorTemperatureStrategy.h b/hueplusplus/include/ExtendedColorTemperatureStrategy.h new file mode 100755 index 0000000..09ef140 --- /dev/null +++ b/hueplusplus/include/ExtendedColorTemperatureStrategy.h @@ -0,0 +1,33 @@ +/** + \file ExtendedColorTemperatureStrategy.h + 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 +**/ + +#ifndef _EXTENDED_COLOR_TEMPERATURE_STRATEGY_H +#define _EXTENDED_COLOR_TEMPERATURE_STRATEGY_H + +#include "ColorTemperatureStrategy.h" +#include "HueLight.h" + +class ExtendedColorTemperatureStrategy : public ColorTemperatureStrategy +{ + public: + bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const; + bool alertTemperature(unsigned int mired, HueLight& light) const; +}; + +#endif \ No newline at end of file diff --git a/hueplusplus/include/Hue.h b/hueplusplus/include/Hue.h old mode 100644 new mode 100755 index b5b0e16..7f68a95 --- a/hueplusplus/include/Hue.h +++ b/hueplusplus/include/Hue.h @@ -21,6 +21,9 @@ #define _HUE_H #include "HueLight.h" +#include "BrightnessStrategy.h" +#include "ColorHueStrategy.h" +#include "ColorTemperatureStrategy.h" #include "json/json.h" @@ -104,7 +107,15 @@ public: //! Function that returns a \HueLight of specified \ref id //! \param id Integer that specifies the ID of a Hue light //! \return \ref HueLight that can be controlled - std::unique_ptr getLight(int id); + const HueLight& getLight(int id); + + //! Function that returns all light types that are associated with this bridge + //! \return A map mapping light id's to light types for every light + //const std::map& getAllLightTypes(); + + //! Function that returns all lights that are associated with this bridge + //! \return A vector containing pointers pointing to every HueLight + std::vector> getAllLights(); private: //! Function that refreshes the local \ref state of the Hue bridge @@ -114,6 +125,13 @@ private: std::string ip; std::string username; Json::Value state; + std::map< uint8_t, HueLight > lights; + + std::shared_ptr _simpleBrightnessStrategy; + std::shared_ptr _simpleColorHueStrategy; + std::shared_ptr _extendedColorHueStrategy; + std::shared_ptr _simpleColorTemperatureStrategy; + std::shared_ptr _extendedColorTemperatureStrategy; }; #endif \ No newline at end of file diff --git a/hueplusplus/include/HueColorLight.h b/hueplusplus/include/HueColorLight.h deleted file mode 100644 index 07020a5..0000000 --- a/hueplusplus/include/HueColorLight.h +++ /dev/null @@ -1,87 +0,0 @@ -#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 deleted file mode 100644 index 8d67cdb..0000000 --- a/hueplusplus/include/HueDimmableLight.h +++ /dev/null @@ -1,24 +0,0 @@ -#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 deleted file mode 100644 index 3d8eece..0000000 --- a/hueplusplus/include/HueExtendedColorLight.h +++ /dev/null @@ -1,52 +0,0 @@ -#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/include/HueLight.h b/hueplusplus/include/HueLight.h old mode 100644 new mode 100755 index a222c60..e3bbc4d --- a/hueplusplus/include/HueLight.h +++ b/hueplusplus/include/HueLight.h @@ -20,7 +20,12 @@ #ifndef _HUE_LIGHT_H #define _HUE_LIGHT_H +#include + #include "json/json.h" +#include "BrightnessStrategy.h" +#include "ColorHueStrategy.h" +#include "ColorTemperatureStrategy.h" /*enum ModelType { @@ -78,32 +83,32 @@ enum ColorType GAMUT_C_TEMPERATURE }; -// supports groups, scenes and on/off control class HueLight { friend class Hue; + friend class SimpleBrightnessStrategy; + friend class SimpleColorHueStrategy; + friend class ExtendedColorHueStrategy; + friend class SimpleColorTemperatureStrategy; + friend class ExtendedColorTemperatureStrategy; public: - //! virtual std dtor - virtual ~HueLight() = default; - - //! virtual function that turns the light on. - //! \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 On(uint8_t transistion = 4); + //! std dtor + ~HueLight() = default; - //! virtual function that turns the light off. - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! Function that turns the light on. + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms //! \return Bool that is true on success - virtual bool Off(uint8_t transistion = 4); + bool On(uint8_t transition = 4); - //! Function that lets the light perform one breath cycle. + //! Function that turns the light off. + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms //! \return Bool that is true on success - virtual bool alert(); + bool Off(uint8_t transition = 4); - //! virtual function that returns the name of the light. + //! F\unction that returns the name of the light. //! \return String containig the name of the light - virtual std::string getName(); + std::string getName(); //! Function that converts Kelvin to Mired. //! \param kelvin Unsigned integer value in Kelvin @@ -115,6 +120,186 @@ public: //! \return Unsigned integer value in Kelvin unsigned int MiredToKelvin(unsigned int mired); + //! Function that sets the brightness of this light if the + //! light has a reference to a specific \ref BrightnessStrategy. + //! The brightness can range from 0=off to 255=fully on. + //! \param bri Unsigned int that specifies the brightness + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + bool setBrightness(unsigned int bri, uint8_t transition = 4) + { + if (_brightnessStrategy) + { + return (*_brightnessStrategy).setBrightness(bri, transition, *this); + } + }; + + //! Fucntion that sets the color temperature of this light in mired if the + //! light has a reference to a specific \ref ColorTemperatureStrategy. + //! The color temperature can range from 153 to 500. + //! \param mired Unsigned int that specifies the color temperature in Mired + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + bool setColorTemperature(unsigned int mired, uint8_t transition = 4) + { + if (_colorTemperatureStrategy) + { + return (*_colorTemperatureStrategy).setColorTemperature(mired, transition, *this); + } + }; + + //! Function to set the color of this light with specified hue if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! The hue can range 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 transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + bool setColorHue(uint16_t hue, uint8_t transition = 4) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).setColorHue(hue, transition, *this); + } + }; + + //! Function to set the saturation of color of this light with specified saturation if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! The saturation can range from 0 to 254, whereas 0 is least saturated (white) and 254 is most saturated. + //! \param sat uint8_t that specifies the saturation + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + bool setColorSaturation(uint8_t sat, uint8_t transition = 4) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).setColorSaturation(sat, transition, *this); + } + }; + + //! Function to set the color of this light with specified hue and saturation if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! \param hue uint16_t that specifies the hue + //! \param sat uint8_t that specifies the saturation + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms. + //! \return Bool that is true on success + bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition = 4) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).setColorHueSaturation(hue, sat, transition, *this); + } + }; + + //! Function to set the color of this light in CIE with specified x y if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! The values of 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 transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + bool setColorXY(float x, float y, uint8_t transition = 4) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).setColorXY(x, y, transition, *this); + } + }; + + //! Function to set the color of this light with red green and blue values if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! The values of red, green and blue are ranging from 0 to 255. + //! \param r uint8_t that specifies the red color value + //! \param g uint8_t that specifies the green color value + //! \param b uint8_t that specifies the blue color value + //! \param transition Optional parameter to set the transition from current state to new, standard is 4 = 400ms + //! \return Bool that is true on success + bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition = 4) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).setColorRGB(r, g, b, transition, *this); + } + }; + + //! Function that lets the light perform one breath cycle. + //! \return bool that is true on success + virtual bool alert(); + + //! Function that lets the light perform one breath cycle in specified color temperature + //! if the light has a reference to a specific \ref ColorTemperatureStrategy. + //! 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) + { + if(_colorTemperatureStrategy) + { + return (*_colorTemperatureStrategy).alertTemperature(mired, *this); + } + }; + + //! Function that lets the light perform one breath cycle in specified color if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! 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) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).alertHueSaturation(hue, sat, *this); + } + }; + + //! Function that lets the light perform one breath cycle in specified color if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! The values of x and y are ranging from 0 to 1. + //! 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) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).alertXY(x, y, *this); + } + }; + + //! Function that lets the light perform one breath cycle in specified color if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! The values of red, green and blue are ranging from 0 to 255. + //! It uses this_thread::sleep_for to accomodate for the time an \ref alert() needs + //! \param r uint8_t that specifies the red color value + //! \param g uint8_t that specifies the green color value + //! \param b uint8_t that specifies the blue color value + //! \return Bool that is true on success + bool alertRGB(uint8_t r, uint8_t g, uint8_t b) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).alertRGB(r, g, b, *this); + } + }; + + //! Function to enable colorloop effect if the + //! light has a reference to a specific \ref ColorHueStrategy. + //! The colorloop effect will loop through all colors on current hue and saturation levels. + //! Notice that none of the setter functions check whether this feature is enabled and + //! the colorloop can only be disabled with 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 + bool setColorLoop(bool on) + { + if(_colorHueStategy) + { + return (*_colorHueStategy).setColorLoop(on, *this); + } + }; + protected: //! protected ctor that is used by \ref Hue class. //! \param ip String that specifies the ip of the Hue bridge @@ -122,15 +307,30 @@ protected: //! \param id Integer that specifies the id of this light HueLight(const std::string& ip, const std::string& username, int id); - //! virtual function that turns the light on without refreshing its state. - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! protected function that sets the brightness strategy which defines how + //! specific commands that deal with brightness control are executed + //! \param strat a strategy of type \ref BrightnessStrategy + void setBrightnessStrategy(std::shared_ptr strat) { _brightnessStrategy = strat; }; + + //! protected function that sets the colorTemperature strategy which defines how + //! specific commands that deal with colortemperature control are executed + //! \param strat a strategy of type \ref ColorTemperatureStrategy + void setColorTemperatureStrategy(std::shared_ptr strat) { _colorTemperatureStrategy = strat; }; + + //! protected function that sets the colorHue strategy which defines how + //! specific commands that deal with color control are executed + //! \param strat a strategy of type \ref ColorHueStrategy + void setColorHueStrategy(std::shared_ptr strat) { _colorHueStategy = strat; }; + + //! Function that turns the light on without refreshing its state. + //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms //! \return Bool that is true on success - virtual bool OnNoRefresh(uint8_t transistion = 4); + bool OnNoRefresh(uint8_t transition = 4); - //! virtual function that turns the light off without refreshing its state. - //! \param transistion Optional parameter to set the transition from current state to new standard is 4 = 400ms + //! Function that turns the light off without refreshing its state. + //! \param transition Optional parameter to set the transition from current state to new standard is 4 = 400ms //! \return Bool that is true on success - virtual bool OffNoRefresh(uint8_t transistion = 4); + bool OffNoRefresh(uint8_t transition = 4); //! utility function to send a put request to the light. //! \throws std::runtime_error if the reply could not be parsed @@ -146,12 +346,10 @@ protected: int id; //!< holds the id of the light Json::Value state; //!< holds the current state of the light updated by \ref refreshState ColorType colorType; //!< holds the \ref ColorType of the light -}; - - - - - + std::shared_ptr _brightnessStrategy; //!< holds a reference to the strategy that handles brighntess commands + std::shared_ptr _colorTemperatureStrategy; //!< holds a reference to the strategy that handles colortemperature commands + std::shared_ptr _colorHueStategy; //!< holds a reference to the strategy that handles all color commands +}; -#endif +#endif \ No newline at end of file diff --git a/hueplusplus/include/HueTemperatureLight.h b/hueplusplus/include/HueTemperatureLight.h deleted file mode 100644 index 74287a5..0000000 --- a/hueplusplus/include/HueTemperatureLight.h +++ /dev/null @@ -1,31 +0,0 @@ -#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/include/SimpleBrightnessStrategy.h b/hueplusplus/include/SimpleBrightnessStrategy.h new file mode 100755 index 0000000..f208647 --- /dev/null +++ b/hueplusplus/include/SimpleBrightnessStrategy.h @@ -0,0 +1,32 @@ +/** + \file SimpleBrightnessStrategy.h + 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 +**/ + +#ifndef _SIMPLE_BRIGHTNESS_STRATEGY_H +#define _SIMPLE_BRIGHTNESS_STRATEGY_H + +#include "BrightnessStrategy.h" +#include "HueLight.h" + +class SimpleBrightnessStrategy : public BrightnessStrategy +{ + public: + bool setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const; +}; + +#endif \ No newline at end of file diff --git a/hueplusplus/include/SimpleColorHueStrategy.h b/hueplusplus/include/SimpleColorHueStrategy.h new file mode 100755 index 0000000..e40d46b --- /dev/null +++ b/hueplusplus/include/SimpleColorHueStrategy.h @@ -0,0 +1,39 @@ +/** + \file SimpleColorHueStrategy.h + 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 +**/ + +#ifndef _SIMPLE_COLOR_HUE_STRATEGY_H +#define _SIMPLE_COLOR_HUE_STRATEGY_H + +#include "HueLight.h" + +class SimpleColorHueStrategy : public ColorHueStrategy +{ + public: + bool setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const; + bool setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const; + bool setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const; + bool setColorXY(float x, float y, uint8_t transition, HueLight& light) const; + bool setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const; + bool setColorLoop(bool on, HueLight& light) const; + bool alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const; + bool alertXY(float x, float y, HueLight& light) const; + bool alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const; +}; + +#endif \ No newline at end of file diff --git a/hueplusplus/include/SimpleColorTemperatureStrategy.h b/hueplusplus/include/SimpleColorTemperatureStrategy.h new file mode 100755 index 0000000..0fcf22f --- /dev/null +++ b/hueplusplus/include/SimpleColorTemperatureStrategy.h @@ -0,0 +1,33 @@ +/** + \file SimpleColorTemperatureStrategy.h + 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 +**/ + +#ifndef _SIMPLE_COLOR_TEMPERATURE_STRATEGY_H +#define _SIMPLE_COLOR_TEMPERATURE_STRATEGY_H + +#include "ColorTemperatureStrategy.h" +#include "HueLight.h" + +class SimpleColorTemperatureStrategy : public ColorTemperatureStrategy +{ + public: + bool setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const; + bool alertTemperature(unsigned int mired, HueLight& light) const; +}; + +#endif \ No newline at end of file