diff --git a/src/BaseHttpHandler.cpp b/src/BaseHttpHandler.cpp index c02978b..aeef09b 100644 --- a/src/BaseHttpHandler.cpp +++ b/src/BaseHttpHandler.cpp @@ -24,8 +24,9 @@ #include "hueplusplus/HueExceptionMacro.h" -std::string hueplusplus::BaseHttpHandler::sendGetHTTPBody( - const std::string& msg, const std::string& adr, int port) const +namespace hueplusplus +{ +std::string BaseHttpHandler::sendGetHTTPBody(const std::string& msg, const std::string& adr, int port) const { std::string response = send(msg, adr, port); size_t start = response.find("\r\n\r\n"); @@ -42,7 +43,7 @@ std::string hueplusplus::BaseHttpHandler::sendGetHTTPBody( return response; } -std::string hueplusplus::BaseHttpHandler::sendHTTPRequest(const std::string& method, const std::string& uri, +std::string BaseHttpHandler::sendHTTPRequest(const std::string& method, const std::string& uri, const std::string& contentType, const std::string& body, const std::string& adr, int port) const { std::string request; @@ -69,50 +70,51 @@ std::string hueplusplus::BaseHttpHandler::sendHTTPRequest(const std::string& met return sendGetHTTPBody(request.c_str(), adr, port); } -std::string hueplusplus::BaseHttpHandler::GETString(const std::string& uri, const std::string& contentType, - const std::string& body, const std::string& adr, int port) const +std::string BaseHttpHandler::GETString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port) const { return sendHTTPRequest("GET", uri, contentType, body, adr, port); } -std::string hueplusplus::BaseHttpHandler::POSTString(const std::string& uri, const std::string& contentType, - const std::string& body, const std::string& adr, int port) const +std::string BaseHttpHandler::POSTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port) const { return sendHTTPRequest("POST", uri, contentType, body, adr, port); } -std::string hueplusplus::BaseHttpHandler::PUTString(const std::string& uri, const std::string& contentType, - const std::string& body, const std::string& adr, int port) const +std::string BaseHttpHandler::PUTString(const std::string& uri, const std::string& contentType, const std::string& body, + const std::string& adr, int port) const { return sendHTTPRequest("PUT", uri, contentType, body, adr, port); } -std::string hueplusplus::BaseHttpHandler::DELETEString(const std::string& uri, const std::string& contentType, +std::string BaseHttpHandler::DELETEString(const std::string& uri, const std::string& contentType, const std::string& body, const std::string& adr, int port) const { return sendHTTPRequest("DELETE", uri, contentType, body, adr, port); } -nlohmann::json hueplusplus::BaseHttpHandler::GETJson( +nlohmann::json BaseHttpHandler::GETJson( const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const { return nlohmann::json::parse(GETString(uri, "application/json", body.dump(), adr, port)); } -nlohmann::json hueplusplus::BaseHttpHandler::POSTJson( +nlohmann::json BaseHttpHandler::POSTJson( const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const { return nlohmann::json::parse(POSTString(uri, "application/json", body.dump(), adr, port)); } -nlohmann::json hueplusplus::BaseHttpHandler::PUTJson( +nlohmann::json BaseHttpHandler::PUTJson( const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const { return nlohmann::json::parse(PUTString(uri, "application/json", body.dump(), adr, port)); } -nlohmann::json hueplusplus::BaseHttpHandler::DELETEJson( +nlohmann::json BaseHttpHandler::DELETEJson( const std::string& uri, const nlohmann::json& body, const std::string& adr, int port) const { return nlohmann::json::parse(DELETEString(uri, "application/json", body.dump(), adr, port)); } +} // namespace hueplusplus diff --git a/src/ExtendedColorHueStrategy.cpp b/src/ExtendedColorHueStrategy.cpp index 632ddd7..9546cf7 100644 --- a/src/ExtendedColorHueStrategy.cpp +++ b/src/ExtendedColorHueStrategy.cpp @@ -28,7 +28,9 @@ #include "hueplusplus/HueConfig.h" -bool hueplusplus::ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const +namespace hueplusplus +{ +bool ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -110,7 +112,7 @@ bool hueplusplus::ExtendedColorHueStrategy::alertHueSaturation(uint16_t hue, uin } } -bool hueplusplus::ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const +bool ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -192,7 +194,7 @@ bool hueplusplus::ExtendedColorHueStrategy::alertXY(float x, float y, HueLight& } } -bool hueplusplus::ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const +bool ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -273,3 +275,4 @@ bool hueplusplus::ExtendedColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8 return false; } } +} // namespace hueplusplus diff --git a/src/ExtendedColorTemperatureStrategy.cpp b/src/ExtendedColorTemperatureStrategy.cpp index fbecacb..151ddd6 100644 --- a/src/ExtendedColorTemperatureStrategy.cpp +++ b/src/ExtendedColorTemperatureStrategy.cpp @@ -30,7 +30,9 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool hueplusplus::ExtendedColorTemperatureStrategy::setColorTemperature( +namespace hueplusplus +{ +bool ExtendedColorTemperatureStrategy::setColorTemperature( unsigned int mired, uint8_t transition, HueLight& light) const { light.refreshState(); @@ -68,7 +70,7 @@ bool hueplusplus::ExtendedColorTemperatureStrategy::setColorTemperature( return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::ExtendedColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const +bool ExtendedColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -149,3 +151,4 @@ bool hueplusplus::ExtendedColorTemperatureStrategy::alertTemperature(unsigned in return false; } } +} // namespace hueplusplus diff --git a/src/Hue.cpp b/src/Hue.cpp index b8d8577..6d825b9 100644 --- a/src/Hue.cpp +++ b/src/Hue.cpp @@ -41,9 +41,11 @@ #include "hueplusplus/UPnP.h" #include "hueplusplus/Utils.h" -hueplusplus::HueFinder::HueFinder(std::shared_ptr handler) : http_handler(std::move(handler)) {} +namespace hueplusplus +{ +HueFinder::HueFinder(std::shared_ptr handler) : http_handler(std::move(handler)) {} -std::vector hueplusplus::HueFinder::FindBridges() const +std::vector HueFinder::FindBridges() const { UPnP uplug; std::vector> foundDevices = uplug.getDevices(http_handler); @@ -71,7 +73,7 @@ std::vector hueplusplus::HueFinder::F return foundBridges; } -hueplusplus::Hue hueplusplus::HueFinder::GetBridge(const HueIdentification& identification) +Hue HueFinder::GetBridge(const HueIdentification& identification) { std::string normalizedMac = NormalizeMac(identification.mac); auto pos = usernames.find(normalizedMac); @@ -91,17 +93,17 @@ hueplusplus::Hue hueplusplus::HueFinder::GetBridge(const HueIdentification& iden return bridge; } -void hueplusplus::HueFinder::AddUsername(const std::string& mac, const std::string& username) +void HueFinder::AddUsername(const std::string& mac, const std::string& username) { usernames[NormalizeMac(mac)] = username; } -const std::map& hueplusplus::HueFinder::GetAllUsernames() const +const std::map& HueFinder::GetAllUsernames() const { return usernames; } -std::string hueplusplus::HueFinder::NormalizeMac(std::string input) +std::string HueFinder::NormalizeMac(std::string input) { // Remove any non alphanumeric characters (e.g. ':' and whitespace) input.erase(std::remove_if(input.begin(), input.end(), [](char c) { return !std::isalnum(c, std::locale()); }), @@ -111,7 +113,7 @@ std::string hueplusplus::HueFinder::NormalizeMac(std::string input) return input; } -std::string hueplusplus::HueFinder::ParseDescription(const std::string& description) +std::string HueFinder::ParseDescription(const std::string& description) { const char* model = "Philips hue bridge"; const char* serialBegin = ""; @@ -133,7 +135,7 @@ std::string hueplusplus::HueFinder::ParseDescription(const std::string& descript return std::string(); } -hueplusplus::Hue::Hue( +Hue::Hue( const std::string& ip, const int port, const std::string& username, std::shared_ptr handler) : ip(ip), port(port), @@ -147,17 +149,17 @@ hueplusplus::Hue::Hue( commands(ip, port, username, http_handler) {} -std::string hueplusplus::Hue::getBridgeIP() +std::string Hue::getBridgeIP() { return ip; } -int hueplusplus::Hue::getBridgePort() +int Hue::getBridgePort() { return port; } -std::string hueplusplus::Hue::requestUsername() +std::string Hue::requestUsername() { std::cout << "Please press the link Button! You've got 35 secs!\n"; // when the link // button was @@ -206,22 +208,22 @@ std::string hueplusplus::Hue::requestUsername() return username; } -std::string hueplusplus::Hue::getUsername() +std::string Hue::getUsername() { return username; } -void hueplusplus::Hue::setIP(const std::string& ip) +void Hue::setIP(const std::string& ip) { this->ip = ip; } -void hueplusplus::Hue::setPort(const int port) +void Hue::setPort(const int port) { this->port = port; } -hueplusplus::HueLight& hueplusplus::Hue::getLight(int id) +HueLight& Hue::getLight(int id) { auto pos = lights.find(id); if (pos != lights.end()) @@ -244,7 +246,7 @@ hueplusplus::HueLight& hueplusplus::Hue::getLight(int id) return lights.find(id)->second; } -bool hueplusplus::Hue::removeLight(int id) +bool Hue::removeLight(int id) { nlohmann::json result = commands.DELETERequest("/lights/" + std::to_string(id), nlohmann::json::object(), CURRENT_FILE_INFO); @@ -256,7 +258,7 @@ bool hueplusplus::Hue::removeLight(int id) return success; } -std::vector> hueplusplus::Hue::getAllLights() +std::vector> Hue::getAllLights() { refreshState(); nlohmann::json lightsState = state["lights"]; @@ -272,7 +274,7 @@ std::vector> hueplusplus::Hue::get return result; } -bool hueplusplus::Hue::lightExists(int id) +bool Hue::lightExists(int id) { refreshState(); auto pos = lights.find(id); @@ -287,7 +289,7 @@ bool hueplusplus::Hue::lightExists(int id) return false; } -bool hueplusplus::Hue::lightExists(int id) const +bool Hue::lightExists(int id) const { auto pos = lights.find(id); if (pos != lights.end()) @@ -301,7 +303,7 @@ bool hueplusplus::Hue::lightExists(int id) const return false; } -std::string hueplusplus::Hue::getPictureOfLight(int id) const +std::string Hue::getPictureOfLight(int id) const { std::string ret = ""; auto pos = lights.find(id); @@ -312,7 +314,7 @@ std::string hueplusplus::Hue::getPictureOfLight(int id) const return ret; } -std::string hueplusplus::Hue::getPictureOfModel(const std::string& model_id) const +std::string Hue::getPictureOfModel(const std::string& model_id) const { std::string ret = ""; if (model_id == "LCT001" || model_id == "LCT007" || model_id == "LCT010" || model_id == "LCT014" @@ -436,7 +438,7 @@ std::string hueplusplus::Hue::getPictureOfModel(const std::string& model_id) con return ret; } -void hueplusplus::Hue::refreshState() +void Hue::refreshState() { if (username.empty()) { @@ -454,3 +456,4 @@ void hueplusplus::Hue::refreshState() << answer.dump() << std::endl; } } +} // namespace hueplusplus diff --git a/src/HueCommandAPI.cpp b/src/HueCommandAPI.cpp index b6e6650..3fb79f6 100644 --- a/src/HueCommandAPI.cpp +++ b/src/HueCommandAPI.cpp @@ -26,7 +26,9 @@ #include "hueplusplus/HueExceptionMacro.h" -constexpr std::chrono::steady_clock::duration hueplusplus::HueCommandAPI::minDelay; +namespace hueplusplus +{ +constexpr std::chrono::steady_clock::duration HueCommandAPI::minDelay; namespace { @@ -62,7 +64,7 @@ nlohmann::json RunWithTimeout(std::shared_ptr timeout, std::chrono::ste } } // namespace -hueplusplus::HueCommandAPI::HueCommandAPI( +HueCommandAPI::HueCommandAPI( const std::string& ip, const int port, const std::string& username, std::shared_ptr httpHandler) : ip(ip), port(port), @@ -71,43 +73,43 @@ hueplusplus::HueCommandAPI::HueCommandAPI( timeout(new TimeoutData{std::chrono::steady_clock::now(), {}}) {} -nlohmann::json hueplusplus::HueCommandAPI::PUTRequest(const std::string& path, const nlohmann::json& request) const +nlohmann::json HueCommandAPI::PUTRequest(const std::string& path, const nlohmann::json& request) const { return PUTRequest(path, request, CURRENT_FILE_INFO); } -nlohmann::json hueplusplus::HueCommandAPI::PUTRequest( +nlohmann::json HueCommandAPI::PUTRequest( const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const { return HandleError(fileInfo, RunWithTimeout(timeout, minDelay, [&]() { return httpHandler->PUTJson(CombinedPath(path), request, ip); })); } -nlohmann::json hueplusplus::HueCommandAPI::GETRequest(const std::string& path, const nlohmann::json& request) const +nlohmann::json HueCommandAPI::GETRequest(const std::string& path, const nlohmann::json& request) const { return GETRequest(path, request, CURRENT_FILE_INFO); } -nlohmann::json hueplusplus::HueCommandAPI::GETRequest( +nlohmann::json HueCommandAPI::GETRequest( const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const { return HandleError(fileInfo, RunWithTimeout(timeout, minDelay, [&]() { return httpHandler->GETJson(CombinedPath(path), request, ip); })); } -nlohmann::json hueplusplus::HueCommandAPI::DELETERequest(const std::string& path, const nlohmann::json& request) const +nlohmann::json HueCommandAPI::DELETERequest(const std::string& path, const nlohmann::json& request) const { return DELETERequest(path, request, CURRENT_FILE_INFO); } -nlohmann::json hueplusplus::HueCommandAPI::DELETERequest( +nlohmann::json HueCommandAPI::DELETERequest( const std::string& path, const nlohmann::json& request, FileInfo fileInfo) const { return HandleError(fileInfo, RunWithTimeout(timeout, minDelay, [&]() { return httpHandler->DELETEJson(CombinedPath(path), request, ip); })); } -nlohmann::json hueplusplus::HueCommandAPI::HandleError(FileInfo fileInfo, const nlohmann::json& response) const +nlohmann::json HueCommandAPI::HandleError(FileInfo fileInfo, const nlohmann::json& response) const { if (response.count("error")) { @@ -120,7 +122,7 @@ nlohmann::json hueplusplus::HueCommandAPI::HandleError(FileInfo fileInfo, const return response; } -std::string hueplusplus::HueCommandAPI::CombinedPath(const std::string& path) const +std::string HueCommandAPI::CombinedPath(const std::string& path) const { std::string result = "/api/"; result.append(username); @@ -132,3 +134,4 @@ std::string hueplusplus::HueCommandAPI::CombinedPath(const std::string& path) co result.append(path); return result; } +} // namespace hueplusplus diff --git a/src/HueDeviceTypes.cpp b/src/HueDeviceTypes.cpp index dabb885..63abdac 100644 --- a/src/HueDeviceTypes.cpp +++ b/src/HueDeviceTypes.cpp @@ -26,6 +26,8 @@ #include "hueplusplus/HueExceptionMacro.h" +namespace hueplusplus +{ namespace { const std::set getGamutBTypes() @@ -66,7 +68,7 @@ const std::set getTemperatureLightTypes() } } // namespace -auto hueplusplus::MakeHueLight::operator()(std::string type, int id, HueCommandAPI commands, +auto MakeHueLight::operator()(std::string type, int id, HueCommandAPI commands, std::shared_ptr simpleBrightnessStrategy, std::shared_ptr extendedColorTemperatureStrategy, std::shared_ptr simpleColorTemperatureStrategy, @@ -112,3 +114,4 @@ auto hueplusplus::MakeHueLight::operator()(std::string type, int id, HueCommandA std::cerr << "Could not determine HueLight type:" << type << "!\n"; throw HueException(CURRENT_FILE_INFO, "Could not determine HueLight type!"); } +} // namespace hueplusplus diff --git a/src/HueException.cpp b/src/HueException.cpp index dc0a095..4b10934 100644 --- a/src/HueException.cpp +++ b/src/HueException.cpp @@ -22,21 +22,23 @@ #include "hueplusplus/HueException.h" -hueplusplus::HueException::HueException(FileInfo fileInfo, const std::string& message) +namespace hueplusplus +{ +HueException::HueException(FileInfo fileInfo, const std::string& message) : HueException("HueException", std::move(fileInfo), message) {} -const char* hueplusplus::HueException::what() const noexcept +const char* HueException::what() const noexcept { return whatMessage.c_str(); } -const hueplusplus::FileInfo& hueplusplus::HueException::GetFile() const noexcept +const FileInfo& HueException::GetFile() const noexcept { return fileInfo; } -hueplusplus::HueException::HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message) +HueException::HueException(const char* exceptionName, FileInfo fileInfo, const std::string& message) : fileInfo(std::move(fileInfo)) { whatMessage = exceptionName; @@ -46,7 +48,7 @@ hueplusplus::HueException::HueException(const char* exceptionName, FileInfo file whatMessage.append(message); } -hueplusplus::HueAPIResponseException::HueAPIResponseException( +HueAPIResponseException::HueAPIResponseException( FileInfo fileInfo, int error, std::string address, std::string description) : HueException("HueApiResponseException", std::move(fileInfo), GetMessage(error, address, description)), error(error), @@ -54,23 +56,22 @@ hueplusplus::HueAPIResponseException::HueAPIResponseException( description(std::move(description)) {} -int hueplusplus::HueAPIResponseException::GetErrorNumber() const noexcept +int HueAPIResponseException::GetErrorNumber() const noexcept { return error; } -const std::string& hueplusplus::HueAPIResponseException::GetAddress() const noexcept +const std::string& HueAPIResponseException::GetAddress() const noexcept { return address; } -const std::string& hueplusplus::HueAPIResponseException::GetDescription() const noexcept +const std::string& HueAPIResponseException::GetDescription() const noexcept { return description; } -hueplusplus::HueAPIResponseException hueplusplus::HueAPIResponseException::Create( - FileInfo fileInfo, const nlohmann::json& response) +HueAPIResponseException HueAPIResponseException::Create(FileInfo fileInfo, const nlohmann::json& response) { const nlohmann::json error = response.at("error"); int errorCode = error.value("type", -1); @@ -79,8 +80,7 @@ hueplusplus::HueAPIResponseException hueplusplus::HueAPIResponseException::Creat return HueAPIResponseException(std::move(fileInfo), errorCode, std::move(address), std::move(description)); } -std::string hueplusplus::HueAPIResponseException::GetMessage( - int error, const std::string& addr, const std::string& description) +std::string HueAPIResponseException::GetMessage(int error, const std::string& addr, const std::string& description) { std::string result = std::to_string(error); result.append(" "); @@ -90,7 +90,7 @@ std::string hueplusplus::HueAPIResponseException::GetMessage( return result; } -std::string hueplusplus::FileInfo::ToString() const +std::string FileInfo::ToString() const { if (filename.empty() || line < 0) { @@ -103,3 +103,4 @@ std::string hueplusplus::FileInfo::ToString() const result.append(std::to_string(line)); return result; } +} // namespace hueplusplus diff --git a/src/HueLight.cpp b/src/HueLight.cpp index 588cf33..5aa1375 100644 --- a/src/HueLight.cpp +++ b/src/HueLight.cpp @@ -30,56 +30,58 @@ #include "hueplusplus/Utils.h" #include "json/json.hpp" -bool hueplusplus::HueLight::On(uint8_t transition) +namespace hueplusplus +{ +bool HueLight::On(uint8_t transition) { refreshState(); return OnNoRefresh(transition); } -bool hueplusplus::HueLight::Off(uint8_t transition) +bool HueLight::Off(uint8_t transition) { refreshState(); return OffNoRefresh(transition); } -bool hueplusplus::HueLight::isOn() +bool HueLight::isOn() { refreshState(); return state["state"]["on"].get(); } -bool hueplusplus::HueLight::isOn() const +bool HueLight::isOn() const { return state["state"]["on"].get(); } -int hueplusplus::HueLight::getId() const +int HueLight::getId() const { return id; } -std::string hueplusplus::HueLight::getType() const +std::string HueLight::getType() const { return state["type"].get(); } -std::string hueplusplus::HueLight::getName() +std::string HueLight::getName() { refreshState(); return state["name"].get(); } -std::string hueplusplus::HueLight::getName() const +std::string HueLight::getName() const { return state["name"].get(); } -std::string hueplusplus::HueLight::getModelId() const +std::string HueLight::getModelId() const { return state["modelid"].get(); } -std::string hueplusplus::HueLight::getUId() const +std::string HueLight::getUId() const { if (state.count("uniqueid")) { @@ -88,7 +90,7 @@ std::string hueplusplus::HueLight::getUId() const return std::string(); } -std::string hueplusplus::HueLight::getManufacturername() const +std::string HueLight::getManufacturername() const { if (state.count("manufacturername")) { @@ -97,7 +99,7 @@ std::string hueplusplus::HueLight::getManufacturername() const return std::string(); } -std::string hueplusplus::HueLight::getProductname() const +std::string HueLight::getProductname() const { if (state.count("productname")) { @@ -106,7 +108,7 @@ std::string hueplusplus::HueLight::getProductname() const return std::string(); } -std::string hueplusplus::HueLight::getLuminaireUId() const +std::string HueLight::getLuminaireUId() const { if (state.count("luminaireuniqueid")) { @@ -115,18 +117,18 @@ std::string hueplusplus::HueLight::getLuminaireUId() const return std::string(); } -std::string hueplusplus::HueLight::getSwVersion() +std::string HueLight::getSwVersion() { refreshState(); return state["swversion"].get(); } -std::string hueplusplus::HueLight::getSwVersion() const +std::string HueLight::getSwVersion() const { return state["swversion"].get(); } -bool hueplusplus::HueLight::setName(const std::string& name) +bool HueLight::setName(const std::string& name) { nlohmann::json request = nlohmann::json::object(); request["name"] = name; @@ -136,22 +138,22 @@ bool hueplusplus::HueLight::setName(const std::string& name) return utils::safeGetMember(reply, 0, "success", "/lights/" + std::to_string(id) + "/name") == name; } -hueplusplus::ColorType hueplusplus::HueLight::getColorType() const +ColorType HueLight::getColorType() const { return colorType; } -unsigned int hueplusplus::HueLight::KelvinToMired(unsigned int kelvin) const +unsigned int HueLight::KelvinToMired(unsigned int kelvin) const { return int(0.5f + (1000000 / kelvin)); } -unsigned int hueplusplus::HueLight::MiredToKelvin(unsigned int mired) const +unsigned int HueLight::MiredToKelvin(unsigned int mired) const { return int(0.5f + (1000000 / mired)); } -bool hueplusplus::HueLight::alert() +bool HueLight::alert() { nlohmann::json request; request["alert"] = "select"; @@ -161,12 +163,9 @@ bool hueplusplus::HueLight::alert() return utils::validateReplyForLight(request, reply, id); } -hueplusplus::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) {} -hueplusplus::HueLight::HueLight(int id, const HueCommandAPI& commands, - std::shared_ptr brightnessStrategy, +HueLight::HueLight(int id, const HueCommandAPI& commands, std::shared_ptr brightnessStrategy, std::shared_ptr colorTempStrategy, std::shared_ptr colorHueStrategy) : id(id), @@ -179,7 +178,7 @@ hueplusplus::HueLight::HueLight(int id, const HueCommandAPI& commands, refreshState(); } -bool hueplusplus::HueLight::OnNoRefresh(uint8_t transition) +bool HueLight::OnNoRefresh(uint8_t transition) { nlohmann::json request = nlohmann::json::object(); if (transition != 4) @@ -203,7 +202,7 @@ bool hueplusplus::HueLight::OnNoRefresh(uint8_t transition) return utils::validateReplyForLight(request, reply, id); } -bool hueplusplus::HueLight::OffNoRefresh(uint8_t transition) +bool HueLight::OffNoRefresh(uint8_t transition) { nlohmann::json request = nlohmann::json::object(); if (transition != 4) @@ -227,13 +226,12 @@ bool hueplusplus::HueLight::OffNoRefresh(uint8_t transition) return utils::validateReplyForLight(request, reply, id); } -nlohmann::json hueplusplus::HueLight::SendPutRequest( - const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) +nlohmann::json HueLight::SendPutRequest(const nlohmann::json& request, const std::string& subPath, FileInfo fileInfo) { return commands.PUTRequest("/lights/" + std::to_string(id) + subPath, request, std::move(fileInfo)); } -void hueplusplus::HueLight::refreshState() +void HueLight::refreshState() { // std::chrono::steady_clock::time_point start = // std::chrono::steady_clock::now(); std::cout << "\tRefreshing lampstate of @@ -254,3 +252,4 @@ void hueplusplus::HueLight::refreshState() // std::chrono::duration_cast(std::chrono::steady_clock::now() // - start).count() << "ms" << std::endl; } +} // namespace hueplusplus diff --git a/src/LinHttpHandler.cpp b/src/LinHttpHandler.cpp index dad1785..e833f25 100644 --- a/src/LinHttpHandler.cpp +++ b/src/LinHttpHandler.cpp @@ -38,6 +38,8 @@ #include // socket, connect #include // read, write, close +namespace hueplusplus +{ class SocketCloser { public: @@ -48,7 +50,7 @@ private: int s; }; -std::string hueplusplus::LinHttpHandler::send(const std::string& msg, const std::string& adr, int port) const +std::string LinHttpHandler::send(const std::string& msg, const std::string& adr, int port) const { // create socket int socketFD = socket(AF_INET, SOCK_STREAM, 0); @@ -136,7 +138,7 @@ std::string hueplusplus::LinHttpHandler::send(const std::string& msg, const std: return response; } -std::vector hueplusplus::LinHttpHandler::sendMulticast( +std::vector LinHttpHandler::sendMulticast( const std::string& msg, const std::string& adr, int port, int timeout) const { hostent* server; // host information @@ -221,3 +223,4 @@ std::vector hueplusplus::LinHttpHandler::sendMulticast( } return returnString; } +} // namespace hueplusplus diff --git a/src/SimpleBrightnessStrategy.cpp b/src/SimpleBrightnessStrategy.cpp index 745fb91..a2aaf73 100644 --- a/src/SimpleBrightnessStrategy.cpp +++ b/src/SimpleBrightnessStrategy.cpp @@ -29,7 +29,9 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool hueplusplus::SimpleBrightnessStrategy::setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const +namespace hueplusplus +{ +bool SimpleBrightnessStrategy::setBrightness(unsigned int bri, uint8_t transition, HueLight& light) const { light.refreshState(); if (bri == 0) @@ -76,13 +78,14 @@ bool hueplusplus::SimpleBrightnessStrategy::setBrightness(unsigned int bri, uint } } -unsigned int hueplusplus::SimpleBrightnessStrategy::getBrightness(HueLight& light) const +unsigned int SimpleBrightnessStrategy::getBrightness(HueLight& light) const { light.refreshState(); return light.state["state"]["bri"].get(); } -unsigned int hueplusplus::SimpleBrightnessStrategy::getBrightness(const HueLight& light) const +unsigned int SimpleBrightnessStrategy::getBrightness(const HueLight& light) const { return light.state["state"]["bri"].get(); } +} // namespace hueplusplus diff --git a/src/SimpleColorHueStrategy.cpp b/src/SimpleColorHueStrategy.cpp index 600580e..220cb77 100644 --- a/src/SimpleColorHueStrategy.cpp +++ b/src/SimpleColorHueStrategy.cpp @@ -30,7 +30,9 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool hueplusplus::SimpleColorHueStrategy::setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const +namespace hueplusplus +{ +bool SimpleColorHueStrategy::setColorHue(uint16_t hue, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -60,7 +62,7 @@ bool hueplusplus::SimpleColorHueStrategy::setColorHue(uint16_t hue, uint8_t tran return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::SimpleColorHueStrategy::setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const +bool SimpleColorHueStrategy::setColorSaturation(uint8_t sat, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -93,8 +95,7 @@ bool hueplusplus::SimpleColorHueStrategy::setColorSaturation(uint8_t sat, uint8_ return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::SimpleColorHueStrategy::setColorHueSaturation( - uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const +bool SimpleColorHueStrategy::setColorHueSaturation(uint16_t hue, uint8_t sat, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -133,7 +134,7 @@ bool hueplusplus::SimpleColorHueStrategy::setColorHueSaturation( return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::SimpleColorHueStrategy::setColorXY(float x, float y, uint8_t transition, HueLight& light) const +bool SimpleColorHueStrategy::setColorXY(float x, float y, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -166,8 +167,7 @@ bool hueplusplus::SimpleColorHueStrategy::setColorXY(float x, float y, uint8_t t return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::SimpleColorHueStrategy::setColorRGB( - uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const +bool SimpleColorHueStrategy::setColorRGB(uint8_t r, uint8_t g, uint8_t b, uint8_t transition, HueLight& light) const { if ((r == 0) && (g == 0) && (b == 0)) { @@ -193,7 +193,7 @@ bool hueplusplus::SimpleColorHueStrategy::setColorRGB( return light.setColorXY(x, y, transition); } -bool hueplusplus::SimpleColorHueStrategy::setColorLoop(bool on, HueLight& light) const +bool SimpleColorHueStrategy::setColorLoop(bool on, HueLight& light) const { // colorloop light.refreshState(); @@ -220,7 +220,7 @@ bool hueplusplus::SimpleColorHueStrategy::setColorLoop(bool on, HueLight& light) return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const +bool SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8_t sat, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -279,7 +279,7 @@ bool hueplusplus::SimpleColorHueStrategy::alertHueSaturation(uint16_t hue, uint8 } } -bool hueplusplus::SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const +bool SimpleColorHueStrategy::alertXY(float x, float y, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -338,7 +338,7 @@ bool hueplusplus::SimpleColorHueStrategy::alertXY(float x, float y, HueLight& li } } -bool hueplusplus::SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const +bool SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t b, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -397,24 +397,24 @@ bool hueplusplus::SimpleColorHueStrategy::alertRGB(uint8_t r, uint8_t g, uint8_t } } -std::pair hueplusplus::SimpleColorHueStrategy::getColorHueSaturation(HueLight& light) const +std::pair SimpleColorHueStrategy::getColorHueSaturation(HueLight& light) const { light.refreshState(); return std::make_pair(light.state["state"]["hue"].get(), light.state["state"]["sat"].get()); } -std::pair hueplusplus::SimpleColorHueStrategy::getColorHueSaturation(const HueLight& light) const +std::pair SimpleColorHueStrategy::getColorHueSaturation(const HueLight& light) const { return std::make_pair(light.state["state"]["hue"].get(), light.state["state"]["sat"].get()); } -std::pair hueplusplus::SimpleColorHueStrategy::getColorXY(HueLight& light) const +std::pair SimpleColorHueStrategy::getColorXY(HueLight& light) const { light.refreshState(); return std::make_pair(light.state["state"]["xy"][0].get(), light.state["state"]["xy"][1].get()); } -std::pair hueplusplus::SimpleColorHueStrategy::getColorXY(const HueLight& light) const +std::pair SimpleColorHueStrategy::getColorXY(const HueLight& light) const { return std::make_pair(light.state["state"]["xy"][0].get(), light.state["state"]["xy"][1].get()); } @@ -428,3 +428,4 @@ float t = (x0 * y1 - y0 * x1 + (y0 - y1) * pointx + (x1 - x0) * pointy) * sign; return s > 0 && t > 0 && (s + t) < A * sign; }*/ +} // namespace hueplusplus diff --git a/src/SimpleColorTemperatureStrategy.cpp b/src/SimpleColorTemperatureStrategy.cpp index c3a6041..c28aed0 100644 --- a/src/SimpleColorTemperatureStrategy.cpp +++ b/src/SimpleColorTemperatureStrategy.cpp @@ -30,8 +30,9 @@ #include "hueplusplus/HueExceptionMacro.h" #include "hueplusplus/Utils.h" -bool hueplusplus::SimpleColorTemperatureStrategy::setColorTemperature( - unsigned int mired, uint8_t transition, HueLight& light) const +namespace hueplusplus +{ +bool SimpleColorTemperatureStrategy::setColorTemperature(unsigned int mired, uint8_t transition, HueLight& light) const { light.refreshState(); nlohmann::json request = nlohmann::json::object(); @@ -68,7 +69,7 @@ bool hueplusplus::SimpleColorTemperatureStrategy::setColorTemperature( return utils::validateReplyForLight(request, reply, light.id); } -bool hueplusplus::SimpleColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const +bool SimpleColorTemperatureStrategy::alertTemperature(unsigned int mired, HueLight& light) const { light.refreshState(); std::string cType = light.state["state"]["colormode"].get(); @@ -102,13 +103,14 @@ bool hueplusplus::SimpleColorTemperatureStrategy::alertTemperature(unsigned int } } -unsigned int hueplusplus::SimpleColorTemperatureStrategy::getColorTemperature(HueLight& light) const +unsigned int SimpleColorTemperatureStrategy::getColorTemperature(HueLight& light) const { light.refreshState(); return light.state["state"]["ct"].get(); } -unsigned int hueplusplus::SimpleColorTemperatureStrategy::getColorTemperature(const HueLight& light) const +unsigned int SimpleColorTemperatureStrategy::getColorTemperature(const HueLight& light) const { return light.state["state"]["ct"].get(); } +} // namespace hueplusplus diff --git a/src/UPnP.cpp b/src/UPnP.cpp index 7e42e38..f617864 100644 --- a/src/UPnP.cpp +++ b/src/UPnP.cpp @@ -25,8 +25,9 @@ #include #include -std::vector> hueplusplus::UPnP::getDevices( - std::shared_ptr handler) +namespace hueplusplus +{ +std::vector> UPnP::getDevices(std::shared_ptr handler) { // send UPnP M-Search request std::vector foundDevices @@ -56,3 +57,4 @@ std::vector> hueplusplus::UPnP::getDevices( } return devices; } +} // namespace hueplusplus diff --git a/src/Utils.cpp b/src/Utils.cpp index 56b17ae..a95e67b 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -24,7 +24,11 @@ #include -bool hueplusplus::utils::validateReplyForLight(const nlohmann::json& request, const nlohmann::json& reply, int lightId) +namespace hueplusplus +{ +namespace utils +{ +bool validateReplyForLight(const nlohmann::json& request, const nlohmann::json& reply, int lightId) { bool success = false; std::string path = "/lights/" + std::to_string(lightId) + "/state/"; @@ -75,4 +79,6 @@ bool hueplusplus::utils::validateReplyForLight(const nlohmann::json& request, co } } return success; -} \ No newline at end of file +} +} // namespace utils +} // namespace hueplusplus diff --git a/src/WinHttpHandler.cpp b/src/WinHttpHandler.cpp index c393bbe..82d4b5f 100644 --- a/src/WinHttpHandler.cpp +++ b/src/WinHttpHandler.cpp @@ -32,6 +32,8 @@ #pragma comment(lib, "Ws2_32.lib") +namespace hueplusplus +{ namespace { class AddrInfoFreer @@ -54,7 +56,7 @@ private: }; } // namespace -hueplusplus::WinHttpHandler::WinHttpHandler() +WinHttpHandler::WinHttpHandler() { // Initialize Winsock int return_code = WSAStartup(MAKEWORD(2, 2), &wsaData); @@ -65,12 +67,12 @@ hueplusplus::WinHttpHandler::WinHttpHandler() } } -hueplusplus::WinHttpHandler::~WinHttpHandler() +WinHttpHandler::~WinHttpHandler() { WSACleanup(); } -std::string hueplusplus::WinHttpHandler::send(const std::string& msg, const std::string& adr, int port) const +std::string WinHttpHandler::send(const std::string& msg, const std::string& adr, int port) const { struct addrinfo hints = {}; hints.ai_family = AF_INET; @@ -171,7 +173,7 @@ std::string hueplusplus::WinHttpHandler::send(const std::string& msg, const std: return response; } -std::vector hueplusplus::WinHttpHandler::sendMulticast( +std::vector WinHttpHandler::sendMulticast( const std::string& msg, const std::string& adr, int port, int timeout) const { struct addrinfo hints = {}; @@ -296,3 +298,4 @@ std::vector hueplusplus::WinHttpHandler::sendMulticast( return returnString; } +} // namespace hueplusplus