diff --git a/include/hueplusplus/HueThing.h b/include/hueplusplus/BaseDevice.h index 833333b..5807aee 100644 --- a/include/hueplusplus/HueThing.h +++ b/include/hueplusplus/BaseDevice.h @@ -1,5 +1,5 @@ /** - \file HueThing.h + \file BaseDevice.h Copyright Notice\n Copyright (C) 2017 Jan Rogall - developer\n Copyright (C) 2017 Moritz Wirger - developer\n @@ -25,70 +25,68 @@ #include -#include "HueCommandAPI.h" +#include "APICache.h" #include "json/json.hpp" namespace hueplusplus { -//! -//! Class for Hue Thing fixtures -//! -class HueThing +//! \brief Base class for physical devices connected to the bridge (sensor or light). +class BaseDevice { public: - //! \brief std dtor - ~HueThing() = default; + //! \brief Virtual destructor + virtual ~BaseDevice() = default; - //! \brief Const function that returns the id of this thing + //! \brief Const function that returns the id of this device //! - //! \return integer representing the thing id + //! \return integer representing the device id virtual int getId() const; - //! \brief Const function that returns the thing type + //! \brief Const function that returns the device type //! //! \return String containing the type virtual std::string getType() const; - //! \brief Function that returns the name of the thing. + //! \brief Function that returns the name of the device. //! - //! \return String containig the name of the thing + //! \return String containig the name of the device //! \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 std::string getName(); - //! \brief Const function that returns the name of the thing. + //! \brief Const function that returns the name of the device. //! - //! \note This will not refresh the thing state + //! \note This will not refresh the device state //! \return String containig the name of the thing virtual std::string getName() const; - //! \brief Const function that returns the modelid of the thing + //! \brief Const function that returns the modelid of the device //! - //! \return String conatining the modelid + //! \return String containing the modelid virtual std::string getModelId() const; - //! \brief Const function that returns the uniqueid of the thing + //! \brief Const function that returns the uniqueid of the device //! //! \note Only working on bridges with versions starting at 1.4 //! \return String containing the uniqueid or an empty string when the function is not supported virtual std::string getUId() const; - //! \brief Const function that returns the manufacturername of the thing + //! \brief Const function that returns the manufacturername of the device //! //! \note Only working on bridges with versions starting at 1.7 //! \return String containing the manufacturername or an empty string when the function is not supported virtual std::string getManufacturername() const; - //! \brief Const function that returns the productname of the thing + //! \brief Const function that returns the productname of the device //! //! \note Only working on bridges with versions starting at 1.24 //! \return String containing the productname or an empty string when the function is not supported virtual std::string getProductname() const; - //! \brief Function that returns the software version of the thing + //! \brief Function that returns the software version of the device //! //! \return String containing the software version //! \throws std::system_error when system or socket operations fail @@ -97,13 +95,13 @@ public: //! \throws nlohmann::json::parse_error when response could not be parsed virtual std::string getSwVersion(); - //! \brief Const function that returns the software version of the thing + //! \brief Const function that returns the software version of the device //! - //! \note This will not refresh the thing state + //! \note This will not refresh the device state //! \return String containing the software version virtual std::string getSwVersion() const; - //! \brief Function that sets the name of the thing + //! \brief Function that sets the name of the device //! //! \return Bool that is true on success //! \throws std::system_error when system or socket operations fail @@ -112,24 +110,25 @@ public: //! \throws nlohmann::json::parse_error when response could not be parsed virtual bool setName(const std::string& name); + //! \brief Refreshes internal cached state. + //! \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 void refresh(); + protected: - //! \brief Protected ctor that is used by \ref Hue class. + //! \brief Protected ctor that is used by subclasses. //! - //! \param id Integer that specifies the id of this thing + //! \param id Integer that specifies the id of this device //! \param commands HueCommandAPI for communication with the bridge - //! - //! leaves strategies unset - HueThing(int id, const HueCommandAPI& commands, const std::string& path); + //! \param path Base path for the resource type, ending with a '/'. Example: \c "/lights/" + //! \param refreshDuration Time between refreshing the cached state. + BaseDevice(int id, const HueCommandAPI& commands, const std::string& path, + std::chrono::steady_clock::duration refreshDuration); - //! \brief Protected function that sets the HueCommandAPI. + //! \brief Utility function to send a put request to the device. //! - //! The HueCommandAPI is used for bridge communication - //! \param commandAPI the new HueCommandAPI - virtual void setCommandAPI(const HueCommandAPI& commandAPI) { commands = commandAPI; }; - - //! \brief Utility function to send a put request to the thing. - //! - //! \throws nlohmann::json::parse_error if the reply could not be parsed //! \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 fileInfo FileInfo from calling function for exception details. @@ -138,21 +137,12 @@ 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 - virtual nlohmann::json SendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); - - //! \brief Virtual function that refreshes the \ref state of the thing. - //! \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 void refreshState(); + virtual nlohmann::json sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo); protected: - int id; //!< holds the id of the thing - std::string path; //!< holds the path of the thing - nlohmann::json state; //!< holds the current state of the thing updated by \ref refreshState - - HueCommandAPI commands; //!< A IHttpHandler that is used to communicate with the bridge + int id; //!< holds the id of the device + std::string path; //!< holds the path of the device + APICache state; //!< holds the current state of the device }; } // namespace hueplusplus diff --git a/include/hueplusplus/Hue.h b/include/hueplusplus/Hue.h index 50361b1..d4ea928 100644 --- a/include/hueplusplus/Hue.h +++ b/include/hueplusplus/Hue.h @@ -132,6 +132,7 @@ public: using GroupList = GroupResourceList; using ScheduleList = CreateableResourceList; using SceneList = CreateableResourceList; + using SensorList = ResourceList; public: //! \brief Constructor of Hue class @@ -223,6 +224,12 @@ public: //! \note Does not refresh state. const SceneList& scenes() const; + //! \brief Provides access to the HueSensor%s on the bridge. + SensorList& sensors(); + //! \brief Provides access to the HueSensor%s on the bridge. + //! \note Does not refresh state. + const SensorList& sensors() const; + private: //! \brief Function that sets the HttpHandler and updates the HueCommandAPI. //! \param handler a HttpHandler of type \ref IHttpHandler @@ -247,6 +254,7 @@ private: detail::MakeCopyable groupList; detail::MakeCopyable scheduleList; detail::MakeCopyable sceneList; + detail::MakeCopyable sensorList; detail::MakeCopyable bridgeConfig; }; } // namespace hueplusplus diff --git a/include/hueplusplus/HueLight.h b/include/hueplusplus/HueLight.h index 0fe38e3..a8d116d 100644 --- a/include/hueplusplus/HueLight.h +++ b/include/hueplusplus/HueLight.h @@ -26,11 +26,11 @@ #include #include "APICache.h" +#include "BaseDevice.h" #include "BrightnessStrategy.h" #include "ColorHueStrategy.h" #include "ColorTemperatureStrategy.h" #include "HueCommandAPI.h" -#include "HueThing.h" #include "StateTransaction.h" #include "json/json.hpp" @@ -96,7 +96,7 @@ enum class ColorType //! \brief Class for Hue Light fixtures //! //! Provides methods to query and control lights. -class HueLight : public HueThing +class HueLight : public BaseDevice { friend class HueLightFactory; friend class SimpleBrightnessStrategy; @@ -106,85 +106,9 @@ class HueLight : public HueThing friend class ExtendedColorTemperatureStrategy; public: - //! \brief std dtor - ~HueLight() = default; - //! \name General information ///@{ - //! \brief 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 - //! \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 bool Off(uint8_t transition = 4); - - //! \brief Function to check whether a light is on or off - //! - //! \return Bool that is true, when the light is on and false, when off - //! \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 bool isOn(); - - //! \brief Const function to check whether a light is on or off - //! - //! \note This will not refresh the light state - //! \return Bool that is true, when the light is on and false, when off - virtual bool isOn() const; - - //! \brief Const function that returns the id of this light - //! - //! \return integer representing the light id - virtual int getId() const; - - //! \brief Const function that returns the light type - //! - //! \return String containing the type - virtual std::string getType() const; - - //! \brief Function that returns the name of the light. - //! - //! \return String containig the name of the light - //! \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 std::string getName(); - - //! \brief Const function that returns the name of the light. - //! - //! \note This will not refresh the light state - //! \return String containig the name of the light - virtual std::string getName() const; - - //! \brief Const function that returns the modelid of the light - //! - //! \return String conatining the modelid - virtual std::string getModelId() const; - - //! \brief Const function that returns the uniqueid of the light - //! - //! \note Only working on bridges with versions starting at 1.4 - //! \return String containing the uniqueid or an empty string when the function is not supported - virtual std::string getUId() const; - - //! \brief Const function that returns the manufacturername of the light - //! - //! \note Only working on bridges with versions starting at 1.7 - //! \return String containing the manufacturername or an empty string when the function is not supported - virtual std::string getManufacturername() const; - - //! \brief Const function that returns the productname of the light - //! - //! \note Only working on bridges with versions starting at 1.24 - //! \return String containing the productname or an empty string when the function is not supported - virtual std::string getProductname() const; - //! \brief Const function that returns the luminaireuniqueid of the light //! //! \note Only working on bridges with versions starting at 1.9 @@ -325,7 +249,7 @@ public: return 0; }; - //! \brief Fucntion that sets the color temperature of this light in mired. + //! \brief Function that sets the color temperature of this light in mired. //! //! \note The color temperature will only be set if the light has a reference //! to a specific \ref ColorTemperatureStrategy. The color temperature can @@ -670,13 +594,6 @@ public: ///@} - //! \brief Refreshes internal cached state. - //! \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 void refresh(); - protected: //! \brief Protected ctor that is used by \ref Hue class. //! @@ -734,21 +651,7 @@ protected: colorHueStrategy = std::move(strat); }; - //! \brief Utility function to send a put request to the light. - //! - //! \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 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); - protected: - int id; //!< holds the id of the light - APICache state; //!< holds the current state of the light ColorType colorType; //!< holds the \ref ColorType of the light std::shared_ptr diff --git a/include/hueplusplus/HueSensor.h b/include/hueplusplus/HueSensor.h index 5199b6e..ba646e6 100644 --- a/include/hueplusplus/HueSensor.h +++ b/include/hueplusplus/HueSensor.h @@ -24,8 +24,8 @@ #include +#include "BaseDevice.h" #include "HueCommandAPI.h" -#include "HueThing.h" #include "json/json.hpp" @@ -34,7 +34,7 @@ namespace hueplusplus //! //! Class for Hue Sensor fixtures //! -class HueSensor : public HueThing +class HueSensor : public BaseDevice { friend class Hue; @@ -89,9 +89,8 @@ protected: //! //! \param id Integer that specifies the id of this sensor //! \param commands HueCommandAPI for communication with the bridge - //! - //! leaves strategies unset - HueSensor(int id, const HueCommandAPI& commands); + //! \param refreshDuration Time between refreshing the cached state. + HueSensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); }; } // namespace hueplusplus diff --git a/src/BaseDevice.cpp b/src/BaseDevice.cpp new file mode 100644 index 0000000..5017bd8 --- /dev/null +++ b/src/BaseDevice.cpp @@ -0,0 +1,111 @@ +/** + \file BaseDevice.cpp + Copyright Notice\n + Copyright (C) 2020 Stefan Herbrechtsmeier - 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/BaseDevice.h" + +#include +#include +#include + +#include "hueplusplus/HueExceptionMacro.h" +#include "hueplusplus/Utils.h" +#include "json/json.hpp" + +namespace hueplusplus +{ +int BaseDevice::getId() const +{ + return id; +} + +std::string BaseDevice::getType() const +{ + return state.getValue().at("type").get(); +} + +std::string BaseDevice::getName() +{ + return state.getValue().at("name").get(); +} + +std::string BaseDevice::getName() const +{ + return state.getValue().at("name").get(); +} + +std::string BaseDevice::getModelId() const +{ + return state.getValue().at("modelid").get(); +} + +std::string BaseDevice::getUId() const +{ + return state.getValue().value("uniqueid", ""); +} + +std::string BaseDevice::getManufacturername() const +{ + return state.getValue().value("manufacturername", ""); +} + +std::string BaseDevice::getProductname() const +{ + return state.getValue().value("productname", ""); +} + +std::string BaseDevice::getSwVersion() +{ + return state.getValue().at("swversion").get(); +} + +std::string BaseDevice::getSwVersion() const +{ + return state.getValue().at("swversion").get(); +} + +bool BaseDevice::setName(const std::string& name) +{ + nlohmann::json request = { {"name", name} }; + nlohmann::json reply = sendPutRequest(request, "/name", 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 + return utils::safeGetMember(reply, 0, "success", "/lights/" + std::to_string(id) + "/name").is_string(); +} + +BaseDevice::BaseDevice( + int id, const HueCommandAPI& commands, const std::string& path, std::chrono::steady_clock::duration refreshDuration) + : id(id), state(path + std::to_string(id), commands, refreshDuration), path(path) +{ + state.refresh(); +} + +nlohmann::json BaseDevice::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) +{ + return state.getCommandAPI().PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); +} + +void BaseDevice::refresh() +{ + state.refresh(); +} + +} // namespace hueplusplus diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9749dcb..e31568a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ set(hueplusplus_SOURCES APICache.cpp + BaseDevice.cpp BaseHttpHandler.cpp BridgeConfig.cpp ColorUnits.cpp @@ -11,11 +12,10 @@ set(hueplusplus_SOURCES HueDeviceTypes.cpp HueException.cpp HueLight.cpp + HueSensor.cpp ModelPictures.cpp Scene.cpp Schedule.cpp - HueSensor.cpp - HueThing.cpp SimpleBrightnessStrategy.cpp SimpleColorHueStrategy.cpp SimpleColorTemperatureStrategy.cpp diff --git a/src/Hue.cpp b/src/Hue.cpp index 93834df..82e7301 100644 --- a/src/Hue.cpp +++ b/src/Hue.cpp @@ -145,6 +145,7 @@ Hue::Hue(const std::string& ip, const int port, const std::string& username, groupList(stateCache, "groups", refreshDuration), scheduleList(stateCache, "schedules", refreshDuration), sceneList(stateCache, "scenes", refreshDuration), + sensorList(stateCache, "sensors", refreshDuration), bridgeConfig(stateCache, refreshDuration) { } @@ -231,35 +232,6 @@ const BridgeConfig& Hue::config() const return bridgeConfig; } -HueSensor& Hue::getSensor(int id) -{ - auto pos = sensors.find(id); - if (pos != sensors.end()) - { - pos->second.refreshState(); - return pos->second; - } - refreshState(); - if (!state["sensors"].count(std::to_string(id))) - { - std::cerr << "Error in Hue getSensor(): sensor with id " << id << " is not valid\n"; - throw HueException(CURRENT_FILE_INFO, "Sensor id is not valid"); - } - // std::cout << state["sensors"][std::to_string(id)] << std::endl; - std::string type = state["sensors"][std::to_string(id)]["modelid"]; - // std::cout << type << std::endl; - if (type == "RWL021" || type == "PHDL00" || type == "PHWA01") - { - // Hue dimmer switch - HueSensor sensor = HueSensor(id, commands); - sensors.emplace(id, sensor); - return sensors.find(id)->second; - } - std::cerr << "Could not determine HueSensor type:" << type << "!\n"; - throw HueException(CURRENT_FILE_INFO, "Could not determine HueSensor type!"); -} - - Hue::LightList& Hue::lights() { return lightList; @@ -275,23 +247,7 @@ Hue::GroupList& Hue::groups() return groupList; } -std::vector> Hue::getAllSensors() -{ - refreshState(); - nlohmann::json sensorsState = state["sensors"]; - for (nlohmann::json::iterator it = sensorsState.begin(); it != sensorsState.end(); ++it) - { - getSensor(std::stoi(it.key())); - } - std::vector> result; - for (auto& entry : sensors) - { - result.emplace_back(entry.second); - } - return result; -} - -bool Hue::lightExists(int id) +const Hue::GroupList& Hue::groups() const { return groupList; } @@ -316,6 +272,16 @@ const Hue::SceneList& Hue::scenes() const return sceneList; } +Hue::SensorList& Hue::sensors() +{ + return sensorList; +} + +const Hue::SensorList& Hue::sensors() const +{ + return sensorList; +} + void Hue::setHttpHandler(std::shared_ptr handler) { http_handler = handler; @@ -323,9 +289,10 @@ void Hue::setHttpHandler(std::shared_ptr handler) lightList = ResourceList(stateCache, "lights", refreshDuration, [factory = HueLightFactory(stateCache->getCommandAPI(), refreshDuration)]( int id, const nlohmann::json& state) mutable { return factory.createLight(state, id); }); - groupList = GroupResourceList(stateCache, "groups", refreshDuration); - scheduleList = CreateableResourceList(stateCache, "schedules", refreshDuration); - sceneList = CreateableResourceList(stateCache, "scenes", refreshDuration); + groupList = GroupList(stateCache, "groups", refreshDuration); + scheduleList = ScheduleList(stateCache, "schedules", refreshDuration); + sceneList = SceneList(stateCache, "scenes", refreshDuration); + sensorList = SensorList(stateCache, "sensors", refreshDuration); bridgeConfig = BridgeConfig(stateCache, refreshDuration); stateCache->refresh(); } diff --git a/src/HueLight.cpp b/src/HueLight.cpp index 0f0f1dd..3193cf0 100644 --- a/src/HueLight.cpp +++ b/src/HueLight.cpp @@ -27,7 +27,6 @@ #include #include "hueplusplus/HueExceptionMacro.h" -#include "hueplusplus/HueThing.h" #include "hueplusplus/Utils.h" #include "json/json.hpp" @@ -58,16 +57,6 @@ std::string HueLight::getLuminaireUId() const return state.getValue().value("luminaireuniqueid", std::string()); } -std::string HueLight::getSwVersion() -{ - return state.getValue()["swversion"].get(); -} - -std::string HueLight::getSwVersion() const -{ - return state.getValue()["swversion"].get(); -} - ColorType HueLight::getColorType() const { return colorType; @@ -123,27 +112,16 @@ StateTransaction HueLight::transaction() state.getCommandAPI(), "/lights/" + std::to_string(id) + "/state", &state.getValue().at("state")); } -void HueLight::refresh() -{ - state.refresh(); -} - -HueLight::HueLight(int id, const HueCommandAPI& commands) : HueLight(id, commands, nullptr, nullptr, nullptr) {} +HueLight::HueLight(int id, const HueCommandAPI& commands) : HueLight(id, commands, nullptr, nullptr, nullptr) { } HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr brightnessStrategy, std::shared_ptr colorTempStrategy, - std::shared_ptr colorHueStrategy, chrono::steady_clock::duration refreshDuration) - : HueThing(id, commands, "/lights/"), + std::shared_ptr colorHueStrategy, std::chrono::steady_clock::duration refreshDuration) + : BaseDevice(id, commands, "/lights/", refreshDuration), colorType(ColorType::NONE), brightnessStrategy(std::move(brightnessStrategy)), colorTemperatureStrategy(std::move(colorTempStrategy)), colorHueStrategy(std::move(colorHueStrategy)) { - state.refresh(); -} - -nlohmann::json HueLight::sendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) -{ - return state.getCommandAPI().PUTRequest("/lights/" + std::to_string(id) + subPath, request, std::move(fileInfo)); } } // namespace hueplusplus diff --git a/src/HueSensor.cpp b/src/HueSensor.cpp index c5cafc2..567ac71 100644 --- a/src/HueSensor.cpp +++ b/src/HueSensor.cpp @@ -21,17 +21,15 @@ #include "hueplusplus/HueSensor.h" -#include "hueplusplus/HueThing.h" #include "json/json.hpp" namespace hueplusplus { int HueSensor::getButtonEvent() { - refreshState(); if (hasButtonEvent()) { - return state["state"]["buttonevent"]; + return state.getValue().at("state").at("buttonevent"); } return 0; } @@ -40,17 +38,16 @@ int HueSensor::getButtonEvent() const { if (hasButtonEvent()) { - return state["state"]["buttonevent"]; + return state.getValue().at("state").at("buttonevent"); } return 0; } int HueSensor::getStatus() { - refreshState(); if (hasStatus()) { - return state["state"]["status"]; + return state.getValue().at("state").at("status"); } return 0; } @@ -59,24 +56,22 @@ int HueSensor::getStatus() const { if (hasStatus()) { - return state["state"]["status"]; + return state.getValue().at("state").at("status"); } return 0; } bool HueSensor::hasButtonEvent() const { - return state["state"].count("buttonevent") > 0; + return state.getValue().at("state").count("buttonevent") != 0; } bool HueSensor::hasStatus() const { - return state["state"].count("status") > 0; + return state.getValue().at("state").count("status") != 0; } -HueSensor::HueSensor(int id, const HueCommandAPI& commands) - : HueThing(id, commands, "/sensors/") -{ - refreshState(); -} +HueSensor::HueSensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration) + : BaseDevice(id, commands, "/sensors/", refreshDuration) +{ } } // namespace hueplusplus diff --git a/src/HueThing.cpp b/src/HueThing.cpp deleted file mode 100644 index 2fc7af4..0000000 --- a/src/HueThing.cpp +++ /dev/null @@ -1,136 +0,0 @@ -/** - \file HueThing.cpp - Copyright Notice\n - Copyright (C) 2020 Stefan Herbrechtsmeier - 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/HueThing.h" - -#include -#include -#include - -#include "hueplusplus/HueExceptionMacro.h" -#include "hueplusplus/Utils.h" -#include "json/json.hpp" - -namespace hueplusplus -{ -int HueThing::getId() const -{ - return id; -} - -std::string HueThing::getType() const -{ - return state["type"]; -} - -std::string HueThing::getName() -{ - refreshState(); - return state["name"]; -} - -std::string HueThing::getName() const -{ - return state["name"]; -} - -std::string HueThing::getModelId() const -{ - return state["modelid"]; -} - -std::string HueThing::getUId() const -{ - if (state.count("uniqueid")) - { - return state["uniqueid"]; - } - return std::string(); -} - -std::string HueThing::getManufacturername() const -{ - if (state.count("manufacturername")) - { - return state["manufacturername"]; - } - return std::string(); -} - -std::string HueThing::getProductname() const -{ - if (state.count("productname")) - { - return state["productname"]; - } - return std::string(); -} - -std::string HueThing::getSwVersion() -{ - refreshState(); - return state["swversion"]; -} - -std::string HueThing::getSwVersion() const -{ - return state["swversion"]; -} - -bool HueThing::setName(const std::string& name) -{ - nlohmann::json request = nlohmann::json::object(); - request["name"] = name; - nlohmann::json reply = SendPutRequest(request, "/name", CURRENT_FILE_INFO); - - // Check whether request was successful - return utils::safeGetMember(reply, 0, "success", path + std::to_string(id) + "/name") == name; -} - -HueThing::HueThing(int id, const HueCommandAPI& commands, const std::string& path) - : id(id), - commands(commands), - path(path) -{ - refreshState(); -} - -nlohmann::json HueThing::SendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) -{ - return commands.PUTRequest(path + std::to_string(id) + subPath, request, std::move(fileInfo)); -} - -void HueThing::refreshState() -{ - nlohmann::json answer - = commands.GETRequest(path + std::to_string(id), nlohmann::json::object(), CURRENT_FILE_INFO); - if (answer.count("state")) - { - state = answer; - } - else - { - std::cout << "Answer in HueThing::refreshState of " - "http_handler->GETJson(...) is not expected!\nAnswer:\n\t" - << answer.dump() << std::endl; - } -} -} // namespace hueplusplus