diff --git a/include/hueplusplus/BaseDevice.h b/include/hueplusplus/BaseDevice.h index 5807aee..5a8e108 100644 --- a/include/hueplusplus/BaseDevice.h +++ b/include/hueplusplus/BaseDevice.h @@ -129,15 +129,15 @@ protected: //! \brief Utility function to send a put request to the device. //! - //! \param request A nlohmann::json aka the request to send //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") + //! \param request A nlohmann::json aka the request to send //! \param fileInfo FileInfo from calling function for exception details. //! \return The parsed reply //! \throws std::system_error when system or socket operations fail //! \throws HueException when response contained no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - virtual nlohmann::json sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); + virtual nlohmann::json sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo); protected: int id; //!< holds the id of the device diff --git a/include/hueplusplus/CLIPSensors.h b/include/hueplusplus/CLIPSensors.h index 539cc09..e603694 100644 --- a/include/hueplusplus/CLIPSensors.h +++ b/include/hueplusplus/CLIPSensors.h @@ -64,6 +64,7 @@ public: CLIPOpenClose(Sensor sensor) : BaseCLIP(std::move(sensor)) { } bool isOpen() const; + void setOpen(bool open); static constexpr const char* typeStr = "CLIPOpenClose"; }; @@ -110,8 +111,8 @@ public: int getThresholdOffset() const; void setThresholdOffset(int offset); - void setLightLevel(int level); int getLightLevel() const; + void setLightLevel(int level); bool isDark() const; bool isDaylight() const; diff --git a/include/hueplusplus/Group.h b/include/hueplusplus/Group.h index d599488..c7911b4 100644 --- a/include/hueplusplus/Group.h +++ b/include/hueplusplus/Group.h @@ -283,7 +283,7 @@ protected: //! \throws HueException when response contained no body //! \throws HueAPIResponseException when response contains an error //! \throws nlohmann::json::parse_error when response could not be parsed - nlohmann::json sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); + nlohmann::json sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo); protected: int id; diff --git a/src/BaseDevice.cpp b/src/BaseDevice.cpp index 5017bd8..492325f 100644 --- a/src/BaseDevice.cpp +++ b/src/BaseDevice.cpp @@ -84,7 +84,7 @@ std::string BaseDevice::getSwVersion() const bool BaseDevice::setName(const std::string& name) { nlohmann::json request = { {"name", name} }; - nlohmann::json reply = sendPutRequest(request, "/name", CURRENT_FILE_INFO); + nlohmann::json reply = sendPutRequest("/name", request, CURRENT_FILE_INFO); // Check whether request was successful (returned name is not necessarily the actually set name) // If it already exists, a number is added, if it is too long to be returned, "Updated" is returned @@ -98,7 +98,7 @@ BaseDevice::BaseDevice( state.refresh(); } -nlohmann::json BaseDevice::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) +nlohmann::json BaseDevice::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) { return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); } diff --git a/src/CLIPSensors.cpp b/src/CLIPSensors.cpp new file mode 100644 index 0000000..a87a527 --- /dev/null +++ b/src/CLIPSensors.cpp @@ -0,0 +1,185 @@ +/** + \file CLIPSensors.cpp + Copyright Notice\n + Copyright (C) 2020 Jan Rogall - developer\n + + This file is part of hueplusplus. + + hueplusplus is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + hueplusplus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with hueplusplus. If not, see . + */ + +#include "hueplusplus/CLIPSensors.h" + +#include "hueplusplus/HueExceptionMacro.h" + +namespace hueplusplus +{ +namespace sensors +{ +bool BaseCLIP::isOn() const +{ + return state.getValue().at("config").at("on").get(); +} + +void BaseCLIP::setOn(bool on) +{ + sendPutRequest("/config", nlohmann::json {{"on", on}}, CURRENT_FILE_INFO); +} +bool BaseCLIP::hasBatteryState() const +{ + return state.getValue().at("config").count("battery") != 0; +} +int BaseCLIP::getBatteryState() const +{ + return state.getValue().at("config").at("battery").get(); +} +void BaseCLIP::setBatteryState(int percent) +{ + sendPutRequest("/config", nlohmann::json {{"battery", percent}}, CURRENT_FILE_INFO); +} +bool BaseCLIP::isReachable() const +{ + return state.getValue().at("config").at("reachable").get(); +} + +bool BaseCLIP::hasURL() const +{ + return state.getValue().at("config").count("url") != 0; +} +std::string BaseCLIP::getURL() const +{ + return state.getValue().at("config").at("url").get(); +} +void BaseCLIP::setURL(const std::string& url) +{ + sendPutRequest("/config", nlohmann::json{ {"url", url} }, CURRENT_FILE_INFO); +} + + +time::AbsoluteTime BaseCLIP::getLastUpdated() const +{ + const nlohmann::json& stateJson = state.getValue().at("state"); + auto it = stateJson.find("lastupdated"); + if (it == stateJson.end() || !it->is_string() || *it == "none") + { + return time::AbsoluteTime(std::chrono::system_clock::time_point(std::chrono::seconds{ 0 })); + } + return time::AbsoluteTime::parseUTC(it->get()); +} + +int CLIPSwitch::getButtonEvent() const +{ + return state.getValue().at("state").at("buttonevent").get(); +} +void CLIPSwitch::setButtonEvent(int code) +{ + sendPutRequest("/state", nlohmann::json{ {"buttonevent", code} }, CURRENT_FILE_INFO); +} + +bool CLIPOpenClose::isOpen() const +{ + return state.getValue().at("state").at("open").get(); +} +void CLIPOpenClose::setOpen(bool open) +{ + sendPutRequest("/state", nlohmann::json{ {"open", open} }, CURRENT_FILE_INFO); +} + +bool CLIPPresence::getPresence() const +{ + return state.getValue().at("state").at("presence").get(); +} +void CLIPPresence::setPresence(bool presence) +{ + sendPutRequest("/state", nlohmann::json{ {"presence", presence} }, CURRENT_FILE_INFO); +} + +int CLIPTemperature::getTemperature() const +{ + return state.getValue().at("state").at("temperature").get(); +} +void CLIPTemperature::setTemperature(int temperature) +{ + sendPutRequest("/state", nlohmann::json{ {"temperature", temperature} }, CURRENT_FILE_INFO); +} + + +int CLIPHumidity::getHumidity() const +{ + return state.getValue().at("state").at("humidity").get(); +} +void CLIPHumidity::setHumidity(int humidity) +{ + sendPutRequest("/state", nlohmann::json{ {"humidity", humidity} }, CURRENT_FILE_INFO); +} + +int CLIPLightLevel::getDarkThreshold() const +{ + return state.getValue().at("config").at("tholddark").get(); +} + +void CLIPLightLevel::setDarkThreshold(int threshold) +{ + sendPutRequest("/config", nlohmann::json{ { "tholddark", threshold} }, CURRENT_FILE_INFO); +} +int CLIPLightLevel::getThresholdOffset() const +{ + return state.getValue().at("config").at("tholdoffset").get(); +} + +void CLIPLightLevel::setThresholdOffset(int offset) +{ + sendPutRequest("/config", nlohmann::json{ { "tholdoffset", offset} }, CURRENT_FILE_INFO); +} + +int CLIPLightLevel::getLightLevel() const +{ + return state.getValue().at("state").at("lightlevel").get(); +} + +void CLIPLightLevel::setLightLevel(int level) +{ + sendPutRequest("/state", nlohmann::json{ {"lightlevel", level} }, CURRENT_FILE_INFO); +} + +bool CLIPLightLevel::isDark() const +{ + return state.getValue().at("state").at("dark").get(); +} + +bool CLIPLightLevel::isDaylight() const +{ + return state.getValue().at("state").at("daylight").get(); +} + +bool CLIPGenericFlag::getFlag() const +{ + return state.getValue().at("state").at("flag").get(); +} +void CLIPGenericFlag::setFlag(bool flag) +{ + sendPutRequest("/state", nlohmann::json{ {"flag", flag} }, CURRENT_FILE_INFO); +} + +int CLIPGenericStatus::getStatus() const +{ + return state.getValue().at("config").at("status").get(); +} + +void CLIPGenericStatus::setStatus(int status) +{ + sendPutRequest("/config", nlohmann::json{ { "status", status} }, CURRENT_FILE_INFO); +} +} // namespace sensors +} // namespace hueplusplus \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 144156b..e627326 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,7 +22,7 @@ set(hueplusplus_SOURCES StateTransaction.cpp TimePattern.cpp UPnP.cpp - Utils.cpp "ZLLSensors.cpp") + Utils.cpp "ZLLSensors.cpp" "CLIPSensors.cpp") # on windows we want to compile the WinHttpHandler if(WIN32) diff --git a/src/Group.cpp b/src/Group.cpp index 6827519..70d260e 100644 --- a/src/Group.cpp +++ b/src/Group.cpp @@ -49,7 +49,7 @@ std::vector Group::getLightIds() const void Group::setName(const std::string& name) { nlohmann::json request = {{"name", name}}; - sendPutRequest(request, "", CURRENT_FILE_INFO); + sendPutRequest("", request, CURRENT_FILE_INFO); refresh(); } @@ -60,7 +60,7 @@ void Group::setLights(const std::vector& ids) { lights.push_back(std::to_string(id)); } - sendPutRequest({{"lights", lights}}, "", CURRENT_FILE_INFO); + sendPutRequest("", {{"lights", lights}}, CURRENT_FILE_INFO); refresh(); } @@ -180,7 +180,7 @@ void Group::setColorLoop(bool on, uint8_t transition) void Group::setScene(const std::string& scene) { - sendPutRequest({{"scene", scene}}, "/action", CURRENT_FILE_INFO); + sendPutRequest("/action", {{"scene", scene}}, CURRENT_FILE_INFO); } ScheduleCommand Group::scheduleScene(const std::string& scene) const @@ -191,7 +191,7 @@ ScheduleCommand Group::scheduleScene(const std::string& scene) const return ScheduleCommand(command); } -nlohmann::json Group::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) +nlohmann::json Group::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) { return state.getCommandAPI().PUTRequest("/groups/" + std::to_string(id) + subPath, request, std::move(fileInfo)); } @@ -203,7 +203,7 @@ std::string Group::getRoomType() const void Group::setRoomType(const std::string& type) { - sendPutRequest({{"class", type}}, "", CURRENT_FILE_INFO); + sendPutRequest("", {{"class", type}}, CURRENT_FILE_INFO); refresh(); } diff --git a/src/ZLLSensors.cpp b/src/ZLLSensors.cpp index b380538..d2d625e 100644 --- a/src/ZLLSensors.cpp +++ b/src/ZLLSensors.cpp @@ -209,7 +209,7 @@ bool ZLLLightLevel::isReachable() const } int ZLLLightLevel::getDarkThreshold() const { - return state.getValue().at("config").at("tholddark").get(); + return state.getValue().at("config").at("tholddark").get(); } void ZLLLightLevel::setDarkThreshold(int threshold) @@ -218,7 +218,7 @@ void ZLLLightLevel::setDarkThreshold(int threshold) } int ZLLLightLevel::getThresholdOffset() const { - return state.getValue().at("config").at("tholdoffset").get(); + return state.getValue().at("config").at("tholdoffset").get(); } void ZLLLightLevel::setThresholdOffset(int offset) diff --git a/test/mocks/mock_Light.h b/test/mocks/mock_Light.h index 24112bb..84c5861 100644 --- a/test/mocks/mock_Light.h +++ b/test/mocks/mock_Light.h @@ -124,7 +124,7 @@ public: MOCK_METHOD1(setColorLoop, bool(bool on)); MOCK_METHOD3(sendPutRequest, - nlohmann::json(const nlohmann::json& request, const std::string& subPath, hueplusplus::FileInfo fileInfo)); + nlohmann::json(const std::string& subPath, const nlohmann::json& request,hueplusplus::FileInfo fileInfo)); }; #endif