Commit 3939aedac38a20b546b761fa223850e6bf1c93f1

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent 2638bcb8

Add implementation for DaylightSensor.

include/hueplusplus/Sensor.h
... ... @@ -49,6 +49,8 @@ public:
49 49 //! \brief std dtor
50 50 ~Sensor() = default;
51 51  
  52 + bool hasSwupdate() const;
  53 +
52 54 bool hasOn() const;
53 55 // Check whether sensor is on. Does not update when off
54 56 bool isOn() const;
... ... @@ -84,11 +86,14 @@ public:
84 86 nlohmann::json getState() const;
85 87 void setStateAttribute(const std::string& key, const nlohmann::json& value);
86 88  
  89 + nlohmann::json getConfig() const;
  90 + void setConfigAttribute(const std::string& key, const nlohmann::json& value);
  91 +
87 92 bool isCertified() const;
88 93 bool isPrimary() const;
89 94  
90 95 template <typename T>
91   - T asSensorType() const
  96 + T asSensorType() const &
92 97 {
93 98 if (getType() != T::type_str)
94 99 {
... ... @@ -125,17 +130,18 @@ public:
125 130 bool isOn() const;
126 131 void setOn(bool on);
127 132  
128   - bool hasBattery() const;
  133 + bool hasBatteryState() const;
129 134 int getBatteryState() const;
130 135 void setBatteryState(int percent);
131 136  
132   - void setCoordinates(std::string latitude, std::string longitude);
  137 + void setCoordinates(const std::string& latitude, const std::string& longitude);
  138 + bool isConfigured() const;
133 139  
134 140 int getSunriseOffset() const;
135 141 void setSunriseOffset(int minutes);
136 142  
137   - int getSunsetOffeset() const;
138   - void setSunsetOffest(int minutes);
  143 + int getSunsetOffset() const;
  144 + void setSunsetOffset(int minutes);
139 145  
140 146 bool isDaylight() const;
141 147  
... ...
src/Bridge.cpp
... ... @@ -55,13 +55,18 @@ std::vector&lt;BridgeFinder::BridgeIdentification&gt; BridgeFinder::FindBridges() cons
55 55 size_t start = p.first.find("//") + 2;
56 56 size_t length = p.first.find(":", start) - start;
57 57 bridge.ip = p.first.substr(start, length);
58   - std::string desc
59   - = http_handler->GETString("/description.xml", "application/xml", "", bridge.ip, bridge.port);
60   - std::string mac = ParseDescription(desc);
61   - if (!mac.empty())
62   - {
63   - bridge.mac = NormalizeMac(mac);
64   - foundBridges.push_back(std::move(bridge));
  58 + try {
  59 + std::string desc
  60 + = http_handler->GETString("/description.xml", "application/xml", "", bridge.ip, bridge.port);
  61 + std::string mac = ParseDescription(desc);
  62 + if (!mac.empty())
  63 + {
  64 + bridge.mac = NormalizeMac(mac);
  65 + foundBridges.push_back(std::move(bridge));
  66 + }
  67 + }
  68 + catch (const HueException&) {
  69 + // No body found in response, skip this device
65 70 }
66 71 }
67 72 }
... ...
src/Sensor.cpp
... ... @@ -27,6 +27,11 @@
27 27  
28 28 namespace hueplusplus
29 29 {
  30 +bool Sensor::hasSwupdate() const
  31 +{
  32 + return state.getValue().at("config").count("swupdate") != 0;
  33 +}
  34 +
30 35 bool Sensor::hasOn() const
31 36 {
32 37 return state.getValue().at("config").count("on") != 0;
... ... @@ -173,6 +178,16 @@ void Sensor::setStateAttribute(const std::string&amp; key, const nlohmann::json&amp; val
173 178 sendPutRequest("/state", nlohmann::json {{"key", value}}, CURRENT_FILE_INFO);
174 179 }
175 180  
  181 +nlohmann::json Sensor::getConfig() const
  182 +{
  183 + return state.getValue().at("config");
  184 +}
  185 +
  186 +void Sensor::setConfigAttribute(const std::string& key, const nlohmann::json& value)
  187 +{
  188 + sendPutRequest("/config", nlohmann::json {{"key", value}}, CURRENT_FILE_INFO);
  189 +}
  190 +
176 191 bool Sensor::isCertified() const
177 192 {
178 193 nlohmann::json certified = utils::safeGetMember(state.getValue(), "capabilities", "certified");
... ... @@ -188,4 +203,72 @@ bool Sensor::isPrimary() const
188 203 Sensor::Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration)
189 204 : BaseDevice(id, commands, "/sensors/", refreshDuration)
190 205 { }
  206 +
  207 +namespace sensors
  208 +{
  209 +
  210 +bool DaylightSensor::isOn() const
  211 +{
  212 + return state.getValue().at("config").at("on").get<bool>();
  213 +}
  214 +
  215 +void DaylightSensor::setOn(bool on)
  216 +{
  217 + sendPutRequest("/config", {"on", on}, CURRENT_FILE_INFO);
  218 +}
  219 +
  220 +bool DaylightSensor::hasBatteryState() const
  221 +{
  222 + return state.getValue().at("config").count("battery") != 0;
  223 +}
  224 +int DaylightSensor::getBatteryState() const
  225 +{
  226 + return state.getValue().at("config").at("battery").get<int>();
  227 +}
  228 +void DaylightSensor::setBatteryState(int percent)
  229 +{
  230 + sendPutRequest("/config", nlohmann::json {{"battery", percent}}, CURRENT_FILE_INFO);
  231 +}
  232 +void DaylightSensor::setCoordinates(const std::string& latitude, const std::string& longitude)
  233 +{
  234 + nlohmann::json request {{"lat", latitude}, {"long", longitude}};
  235 + // Currently, "none" is supposed to be used for reset; may change to null in the future,
  236 + // so the functionality is implemented already
  237 + if (latitude.empty())
  238 + {
  239 + request["lat"] = nullptr;
  240 + }
  241 + if (longitude.empty())
  242 + {
  243 + request["long"] = nullptr;
  244 + }
  245 + sendPutRequest("/config", request, CURRENT_FILE_INFO);
  246 +}
  247 +bool DaylightSensor::isConfigured() const
  248 +{
  249 + return state.getValue().at("config").at("configured").get<bool>();
  250 +}
  251 +int DaylightSensor::getSunriseOffset() const
  252 +{
  253 + return state.getValue().at("config").at("sunriseoffset").get<int>();
  254 +}
  255 +void DaylightSensor::setSunriseOffset(int minutes)
  256 +{
  257 + sendPutRequest("/config", nlohmann::json {{"sunriseoffset", minutes}}, CURRENT_FILE_INFO);
  258 +}
  259 +
  260 +int DaylightSensor::getSunsetOffset() const
  261 +{
  262 + return state.getValue().at("config").at("sunsetoffset").get<int>();
  263 +}
  264 +void DaylightSensor::setSunsetOffset(int minutes)
  265 +{
  266 + sendPutRequest("/config", nlohmann::json {{"sunsetoffset", minutes}}, CURRENT_FILE_INFO);
  267 +}
  268 +
  269 +bool DaylightSensor::isDaylight() const
  270 +{
  271 + return state.getValue().at("config").at("daylight").get<bool>();
  272 +}
  273 +} // namespace sensors
191 274 } // namespace hueplusplus
... ...