Commit fa67863820f2afb79d60e401e970c7354b839691
Committed by
Moritz Wirger
1 parent
179a638e
Add implementation for CLIP sensors.
Showing
9 changed files
with
201 additions
and
15 deletions
include/hueplusplus/BaseDevice.h
| @@ -129,15 +129,15 @@ protected: | @@ -129,15 +129,15 @@ protected: | ||
| 129 | 129 | ||
| 130 | //! \brief Utility function to send a put request to the device. | 130 | //! \brief Utility function to send a put request to the device. |
| 131 | //! | 131 | //! |
| 132 | - //! \param request A nlohmann::json aka the request to send | ||
| 133 | //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") | 132 | //! \param subPath A path that is appended to the uri, note it should always start with a slash ("/") |
| 133 | + //! \param request A nlohmann::json aka the request to send | ||
| 134 | //! \param fileInfo FileInfo from calling function for exception details. | 134 | //! \param fileInfo FileInfo from calling function for exception details. |
| 135 | //! \return The parsed reply | 135 | //! \return The parsed reply |
| 136 | //! \throws std::system_error when system or socket operations fail | 136 | //! \throws std::system_error when system or socket operations fail |
| 137 | //! \throws HueException when response contained no body | 137 | //! \throws HueException when response contained no body |
| 138 | //! \throws HueAPIResponseException when response contains an error | 138 | //! \throws HueAPIResponseException when response contains an error |
| 139 | //! \throws nlohmann::json::parse_error when response could not be parsed | 139 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 140 | - virtual nlohmann::json sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); | 140 | + virtual nlohmann::json sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo); |
| 141 | 141 | ||
| 142 | protected: | 142 | protected: |
| 143 | int id; //!< holds the id of the device | 143 | int id; //!< holds the id of the device |
include/hueplusplus/CLIPSensors.h
| @@ -64,6 +64,7 @@ public: | @@ -64,6 +64,7 @@ public: | ||
| 64 | CLIPOpenClose(Sensor sensor) : BaseCLIP(std::move(sensor)) { } | 64 | CLIPOpenClose(Sensor sensor) : BaseCLIP(std::move(sensor)) { } |
| 65 | 65 | ||
| 66 | bool isOpen() const; | 66 | bool isOpen() const; |
| 67 | + void setOpen(bool open); | ||
| 67 | 68 | ||
| 68 | static constexpr const char* typeStr = "CLIPOpenClose"; | 69 | static constexpr const char* typeStr = "CLIPOpenClose"; |
| 69 | }; | 70 | }; |
| @@ -110,8 +111,8 @@ public: | @@ -110,8 +111,8 @@ public: | ||
| 110 | int getThresholdOffset() const; | 111 | int getThresholdOffset() const; |
| 111 | void setThresholdOffset(int offset); | 112 | void setThresholdOffset(int offset); |
| 112 | 113 | ||
| 113 | - void setLightLevel(int level); | ||
| 114 | int getLightLevel() const; | 114 | int getLightLevel() const; |
| 115 | + void setLightLevel(int level); | ||
| 115 | bool isDark() const; | 116 | bool isDark() const; |
| 116 | bool isDaylight() const; | 117 | bool isDaylight() const; |
| 117 | 118 |
include/hueplusplus/Group.h
| @@ -283,7 +283,7 @@ protected: | @@ -283,7 +283,7 @@ protected: | ||
| 283 | //! \throws HueException when response contained no body | 283 | //! \throws HueException when response contained no body |
| 284 | //! \throws HueAPIResponseException when response contains an error | 284 | //! \throws HueAPIResponseException when response contains an error |
| 285 | //! \throws nlohmann::json::parse_error when response could not be parsed | 285 | //! \throws nlohmann::json::parse_error when response could not be parsed |
| 286 | - nlohmann::json sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); | 286 | + nlohmann::json sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo); |
| 287 | 287 | ||
| 288 | protected: | 288 | protected: |
| 289 | int id; | 289 | int id; |
src/BaseDevice.cpp
| @@ -84,7 +84,7 @@ std::string BaseDevice::getSwVersion() const | @@ -84,7 +84,7 @@ std::string BaseDevice::getSwVersion() const | ||
| 84 | bool BaseDevice::setName(const std::string& name) | 84 | bool BaseDevice::setName(const std::string& name) |
| 85 | { | 85 | { |
| 86 | nlohmann::json request = { {"name", name} }; | 86 | nlohmann::json request = { {"name", name} }; |
| 87 | - nlohmann::json reply = sendPutRequest(request, "/name", CURRENT_FILE_INFO); | 87 | + nlohmann::json reply = sendPutRequest("/name", request, CURRENT_FILE_INFO); |
| 88 | 88 | ||
| 89 | // Check whether request was successful (returned name is not necessarily the actually set name) | 89 | // Check whether request was successful (returned name is not necessarily the actually set name) |
| 90 | // If it already exists, a number is added, if it is too long to be returned, "Updated" is returned | 90 | // 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( | @@ -98,7 +98,7 @@ BaseDevice::BaseDevice( | ||
| 98 | state.refresh(); | 98 | state.refresh(); |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | -nlohmann::json BaseDevice::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) | 101 | +nlohmann::json BaseDevice::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) |
| 102 | { | 102 | { |
| 103 | return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); | 103 | return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); |
| 104 | } | 104 | } |
src/CLIPSensors.cpp
0 → 100644
| 1 | +/** | ||
| 2 | + \file CLIPSensors.cpp | ||
| 3 | + Copyright Notice\n | ||
| 4 | + Copyright (C) 2020 Jan Rogall - developer\n | ||
| 5 | + | ||
| 6 | + This file is part of hueplusplus. | ||
| 7 | + | ||
| 8 | + hueplusplus is free software: you can redistribute it and/or modify | ||
| 9 | + it under the terms of the GNU Lesser General Public License as published by | ||
| 10 | + the Free Software Foundation, either version 3 of the License, or | ||
| 11 | + (at your option) any later version. | ||
| 12 | + | ||
| 13 | + hueplusplus is distributed in the hope that it will be useful, | ||
| 14 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | + GNU Lesser General Public License for more details. | ||
| 17 | + | ||
| 18 | + You should have received a copy of the GNU Lesser General Public License | ||
| 19 | + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | + */ | ||
| 21 | + | ||
| 22 | +#include "hueplusplus/CLIPSensors.h" | ||
| 23 | + | ||
| 24 | +#include "hueplusplus/HueExceptionMacro.h" | ||
| 25 | + | ||
| 26 | +namespace hueplusplus | ||
| 27 | +{ | ||
| 28 | +namespace sensors | ||
| 29 | +{ | ||
| 30 | +bool BaseCLIP::isOn() const | ||
| 31 | +{ | ||
| 32 | + return state.getValue().at("config").at("on").get<bool>(); | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +void BaseCLIP::setOn(bool on) | ||
| 36 | +{ | ||
| 37 | + sendPutRequest("/config", nlohmann::json {{"on", on}}, CURRENT_FILE_INFO); | ||
| 38 | +} | ||
| 39 | +bool BaseCLIP::hasBatteryState() const | ||
| 40 | +{ | ||
| 41 | + return state.getValue().at("config").count("battery") != 0; | ||
| 42 | +} | ||
| 43 | +int BaseCLIP::getBatteryState() const | ||
| 44 | +{ | ||
| 45 | + return state.getValue().at("config").at("battery").get<int>(); | ||
| 46 | +} | ||
| 47 | +void BaseCLIP::setBatteryState(int percent) | ||
| 48 | +{ | ||
| 49 | + sendPutRequest("/config", nlohmann::json {{"battery", percent}}, CURRENT_FILE_INFO); | ||
| 50 | +} | ||
| 51 | +bool BaseCLIP::isReachable() const | ||
| 52 | +{ | ||
| 53 | + return state.getValue().at("config").at("reachable").get<bool>(); | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +bool BaseCLIP::hasURL() const | ||
| 57 | +{ | ||
| 58 | + return state.getValue().at("config").count("url") != 0; | ||
| 59 | +} | ||
| 60 | +std::string BaseCLIP::getURL() const | ||
| 61 | +{ | ||
| 62 | + return state.getValue().at("config").at("url").get<std::string>(); | ||
| 63 | +} | ||
| 64 | +void BaseCLIP::setURL(const std::string& url) | ||
| 65 | +{ | ||
| 66 | + sendPutRequest("/config", nlohmann::json{ {"url", url} }, CURRENT_FILE_INFO); | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | + | ||
| 70 | +time::AbsoluteTime BaseCLIP::getLastUpdated() const | ||
| 71 | +{ | ||
| 72 | + const nlohmann::json& stateJson = state.getValue().at("state"); | ||
| 73 | + auto it = stateJson.find("lastupdated"); | ||
| 74 | + if (it == stateJson.end() || !it->is_string() || *it == "none") | ||
| 75 | + { | ||
| 76 | + return time::AbsoluteTime(std::chrono::system_clock::time_point(std::chrono::seconds{ 0 })); | ||
| 77 | + } | ||
| 78 | + return time::AbsoluteTime::parseUTC(it->get<std::string>()); | ||
| 79 | +} | ||
| 80 | + | ||
| 81 | +int CLIPSwitch::getButtonEvent() const | ||
| 82 | +{ | ||
| 83 | + return state.getValue().at("state").at("buttonevent").get<int>(); | ||
| 84 | +} | ||
| 85 | +void CLIPSwitch::setButtonEvent(int code) | ||
| 86 | +{ | ||
| 87 | + sendPutRequest("/state", nlohmann::json{ {"buttonevent", code} }, CURRENT_FILE_INFO); | ||
| 88 | +} | ||
| 89 | + | ||
| 90 | +bool CLIPOpenClose::isOpen() const | ||
| 91 | +{ | ||
| 92 | + return state.getValue().at("state").at("open").get<bool>(); | ||
| 93 | +} | ||
| 94 | +void CLIPOpenClose::setOpen(bool open) | ||
| 95 | +{ | ||
| 96 | + sendPutRequest("/state", nlohmann::json{ {"open", open} }, CURRENT_FILE_INFO); | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +bool CLIPPresence::getPresence() const | ||
| 100 | +{ | ||
| 101 | + return state.getValue().at("state").at("presence").get<bool>(); | ||
| 102 | +} | ||
| 103 | +void CLIPPresence::setPresence(bool presence) | ||
| 104 | +{ | ||
| 105 | + sendPutRequest("/state", nlohmann::json{ {"presence", presence} }, CURRENT_FILE_INFO); | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +int CLIPTemperature::getTemperature() const | ||
| 109 | +{ | ||
| 110 | + return state.getValue().at("state").at("temperature").get<int>(); | ||
| 111 | +} | ||
| 112 | +void CLIPTemperature::setTemperature(int temperature) | ||
| 113 | +{ | ||
| 114 | + sendPutRequest("/state", nlohmann::json{ {"temperature", temperature} }, CURRENT_FILE_INFO); | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | + | ||
| 118 | +int CLIPHumidity::getHumidity() const | ||
| 119 | +{ | ||
| 120 | + return state.getValue().at("state").at("humidity").get<int>(); | ||
| 121 | +} | ||
| 122 | +void CLIPHumidity::setHumidity(int humidity) | ||
| 123 | +{ | ||
| 124 | + sendPutRequest("/state", nlohmann::json{ {"humidity", humidity} }, CURRENT_FILE_INFO); | ||
| 125 | +} | ||
| 126 | + | ||
| 127 | +int CLIPLightLevel::getDarkThreshold() const | ||
| 128 | +{ | ||
| 129 | + return state.getValue().at("config").at("tholddark").get<int>(); | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +void CLIPLightLevel::setDarkThreshold(int threshold) | ||
| 133 | +{ | ||
| 134 | + sendPutRequest("/config", nlohmann::json{ { "tholddark", threshold} }, CURRENT_FILE_INFO); | ||
| 135 | +} | ||
| 136 | +int CLIPLightLevel::getThresholdOffset() const | ||
| 137 | +{ | ||
| 138 | + return state.getValue().at("config").at("tholdoffset").get<int>(); | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +void CLIPLightLevel::setThresholdOffset(int offset) | ||
| 142 | +{ | ||
| 143 | + sendPutRequest("/config", nlohmann::json{ { "tholdoffset", offset} }, CURRENT_FILE_INFO); | ||
| 144 | +} | ||
| 145 | + | ||
| 146 | +int CLIPLightLevel::getLightLevel() const | ||
| 147 | +{ | ||
| 148 | + return state.getValue().at("state").at("lightlevel").get<int>(); | ||
| 149 | +} | ||
| 150 | + | ||
| 151 | +void CLIPLightLevel::setLightLevel(int level) | ||
| 152 | +{ | ||
| 153 | + sendPutRequest("/state", nlohmann::json{ {"lightlevel", level} }, CURRENT_FILE_INFO); | ||
| 154 | +} | ||
| 155 | + | ||
| 156 | +bool CLIPLightLevel::isDark() const | ||
| 157 | +{ | ||
| 158 | + return state.getValue().at("state").at("dark").get<bool>(); | ||
| 159 | +} | ||
| 160 | + | ||
| 161 | +bool CLIPLightLevel::isDaylight() const | ||
| 162 | +{ | ||
| 163 | + return state.getValue().at("state").at("daylight").get<bool>(); | ||
| 164 | +} | ||
| 165 | + | ||
| 166 | +bool CLIPGenericFlag::getFlag() const | ||
| 167 | +{ | ||
| 168 | + return state.getValue().at("state").at("flag").get<bool>(); | ||
| 169 | +} | ||
| 170 | +void CLIPGenericFlag::setFlag(bool flag) | ||
| 171 | +{ | ||
| 172 | + sendPutRequest("/state", nlohmann::json{ {"flag", flag} }, CURRENT_FILE_INFO); | ||
| 173 | +} | ||
| 174 | + | ||
| 175 | +int CLIPGenericStatus::getStatus() const | ||
| 176 | +{ | ||
| 177 | + return state.getValue().at("config").at("status").get<int>(); | ||
| 178 | +} | ||
| 179 | + | ||
| 180 | +void CLIPGenericStatus::setStatus(int status) | ||
| 181 | +{ | ||
| 182 | + sendPutRequest("/config", nlohmann::json{ { "status", status} }, CURRENT_FILE_INFO); | ||
| 183 | +} | ||
| 184 | +} // namespace sensors | ||
| 185 | +} // namespace hueplusplus | ||
| 0 | \ No newline at end of file | 186 | \ No newline at end of file |
src/CMakeLists.txt
| @@ -22,7 +22,7 @@ set(hueplusplus_SOURCES | @@ -22,7 +22,7 @@ set(hueplusplus_SOURCES | ||
| 22 | StateTransaction.cpp | 22 | StateTransaction.cpp |
| 23 | TimePattern.cpp | 23 | TimePattern.cpp |
| 24 | UPnP.cpp | 24 | UPnP.cpp |
| 25 | - Utils.cpp "ZLLSensors.cpp") | 25 | + Utils.cpp "ZLLSensors.cpp" "CLIPSensors.cpp") |
| 26 | 26 | ||
| 27 | # on windows we want to compile the WinHttpHandler | 27 | # on windows we want to compile the WinHttpHandler |
| 28 | if(WIN32) | 28 | if(WIN32) |
src/Group.cpp
| @@ -49,7 +49,7 @@ std::vector<int> Group::getLightIds() const | @@ -49,7 +49,7 @@ std::vector<int> Group::getLightIds() const | ||
| 49 | void Group::setName(const std::string& name) | 49 | void Group::setName(const std::string& name) |
| 50 | { | 50 | { |
| 51 | nlohmann::json request = {{"name", name}}; | 51 | nlohmann::json request = {{"name", name}}; |
| 52 | - sendPutRequest(request, "", CURRENT_FILE_INFO); | 52 | + sendPutRequest("", request, CURRENT_FILE_INFO); |
| 53 | refresh(); | 53 | refresh(); |
| 54 | } | 54 | } |
| 55 | 55 | ||
| @@ -60,7 +60,7 @@ void Group::setLights(const std::vector<int>& ids) | @@ -60,7 +60,7 @@ void Group::setLights(const std::vector<int>& ids) | ||
| 60 | { | 60 | { |
| 61 | lights.push_back(std::to_string(id)); | 61 | lights.push_back(std::to_string(id)); |
| 62 | } | 62 | } |
| 63 | - sendPutRequest({{"lights", lights}}, "", CURRENT_FILE_INFO); | 63 | + sendPutRequest("", {{"lights", lights}}, CURRENT_FILE_INFO); |
| 64 | refresh(); | 64 | refresh(); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| @@ -180,7 +180,7 @@ void Group::setColorLoop(bool on, uint8_t transition) | @@ -180,7 +180,7 @@ void Group::setColorLoop(bool on, uint8_t transition) | ||
| 180 | 180 | ||
| 181 | void Group::setScene(const std::string& scene) | 181 | void Group::setScene(const std::string& scene) |
| 182 | { | 182 | { |
| 183 | - sendPutRequest({{"scene", scene}}, "/action", CURRENT_FILE_INFO); | 183 | + sendPutRequest("/action", {{"scene", scene}}, CURRENT_FILE_INFO); |
| 184 | } | 184 | } |
| 185 | 185 | ||
| 186 | ScheduleCommand Group::scheduleScene(const std::string& scene) const | 186 | ScheduleCommand Group::scheduleScene(const std::string& scene) const |
| @@ -191,7 +191,7 @@ ScheduleCommand Group::scheduleScene(const std::string& scene) const | @@ -191,7 +191,7 @@ ScheduleCommand Group::scheduleScene(const std::string& scene) const | ||
| 191 | return ScheduleCommand(command); | 191 | return ScheduleCommand(command); |
| 192 | } | 192 | } |
| 193 | 193 | ||
| 194 | -nlohmann::json Group::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) | 194 | +nlohmann::json Group::sendPutRequest(const std::string& subPath, const nlohmann::json& request, FileInfo fileInfo) |
| 195 | { | 195 | { |
| 196 | return state.getCommandAPI().PUTRequest("/groups/" + std::to_string(id) + subPath, request, std::move(fileInfo)); | 196 | return state.getCommandAPI().PUTRequest("/groups/" + std::to_string(id) + subPath, request, std::move(fileInfo)); |
| 197 | } | 197 | } |
| @@ -203,7 +203,7 @@ std::string Group::getRoomType() const | @@ -203,7 +203,7 @@ std::string Group::getRoomType() const | ||
| 203 | 203 | ||
| 204 | void Group::setRoomType(const std::string& type) | 204 | void Group::setRoomType(const std::string& type) |
| 205 | { | 205 | { |
| 206 | - sendPutRequest({{"class", type}}, "", CURRENT_FILE_INFO); | 206 | + sendPutRequest("", {{"class", type}}, CURRENT_FILE_INFO); |
| 207 | refresh(); | 207 | refresh(); |
| 208 | } | 208 | } |
| 209 | 209 |
src/ZLLSensors.cpp
| @@ -209,7 +209,7 @@ bool ZLLLightLevel::isReachable() const | @@ -209,7 +209,7 @@ bool ZLLLightLevel::isReachable() const | ||
| 209 | } | 209 | } |
| 210 | int ZLLLightLevel::getDarkThreshold() const | 210 | int ZLLLightLevel::getDarkThreshold() const |
| 211 | { | 211 | { |
| 212 | - return state.getValue().at("config").at("tholddark").get<bool>(); | 212 | + return state.getValue().at("config").at("tholddark").get<int>(); |
| 213 | } | 213 | } |
| 214 | 214 | ||
| 215 | void ZLLLightLevel::setDarkThreshold(int threshold) | 215 | void ZLLLightLevel::setDarkThreshold(int threshold) |
| @@ -218,7 +218,7 @@ void ZLLLightLevel::setDarkThreshold(int threshold) | @@ -218,7 +218,7 @@ void ZLLLightLevel::setDarkThreshold(int threshold) | ||
| 218 | } | 218 | } |
| 219 | int ZLLLightLevel::getThresholdOffset() const | 219 | int ZLLLightLevel::getThresholdOffset() const |
| 220 | { | 220 | { |
| 221 | - return state.getValue().at("config").at("tholdoffset").get<bool>(); | 221 | + return state.getValue().at("config").at("tholdoffset").get<int>(); |
| 222 | } | 222 | } |
| 223 | 223 | ||
| 224 | void ZLLLightLevel::setThresholdOffset(int offset) | 224 | void ZLLLightLevel::setThresholdOffset(int offset) |
test/mocks/mock_Light.h
| @@ -124,7 +124,7 @@ public: | @@ -124,7 +124,7 @@ public: | ||
| 124 | MOCK_METHOD1(setColorLoop, bool(bool on)); | 124 | MOCK_METHOD1(setColorLoop, bool(bool on)); |
| 125 | 125 | ||
| 126 | MOCK_METHOD3(sendPutRequest, | 126 | MOCK_METHOD3(sendPutRequest, |
| 127 | - nlohmann::json(const nlohmann::json& request, const std::string& subPath, hueplusplus::FileInfo fileInfo)); | 127 | + nlohmann::json(const std::string& subPath, const nlohmann::json& request,hueplusplus::FileInfo fileInfo)); |
| 128 | }; | 128 | }; |
| 129 | 129 | ||
| 130 | #endif | 130 | #endif |