diff --git a/examples/BridgeSetup.cpp b/examples/BridgeSetup.cpp index 0c92780..ebc950a 100644 --- a/examples/BridgeSetup.cpp +++ b/examples/BridgeSetup.cpp @@ -1,3 +1,27 @@ +/** + \file BridgeSetup.cpp + Copyright Notice\n + Copyright (C) 2021 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 . + + \brief This example connects to a bridge with hardcoded mac and username. +**/ + + #include #include @@ -21,6 +45,7 @@ namespace hue = hueplusplus; const std::string macAddress = ""; const std::string username = ""; +//! \brief Connects to a bridge and returns it. hue::Bridge connectToBridge() { hue::BridgeFinder finder(std::make_shared()); diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 7dccbf0..c0ed8b1 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,13 +1,18 @@ -add_executable(bridge_setup BridgeSetup.cpp ) +add_executable(bridge_setup BridgeSetup.cpp) set_property(TARGET bridge_setup PROPERTY CXX_STANDARD 14) set_property(TARGET bridge_setup PROPERTY CXX_EXTENSIONS OFF) target_link_libraries(bridge_setup hueplusplusstatic) -add_executable(lights_off LightsOff.cpp ) +add_executable(lights_off LightsOff.cpp) set_property(TARGET lights_off PROPERTY CXX_STANDARD 14) set_property(TARGET lights_off PROPERTY CXX_EXTENSIONS OFF) target_link_libraries(lights_off hueplusplusstatic) +add_executable(username_config UsernameConfig.cpp) +set_property(TARGET lights_off PROPERTY CXX_STANDARD 14) +set_property(TARGET lights_off PROPERTY CXX_EXTENSIONS OFF) +target_link_libraries(username_config hueplusplusstatic) + add_custom_target(hueplusplus_examples) add_dependencies(hueplusplus_examples bridge_setup lights_off) \ No newline at end of file diff --git a/examples/LightsOff.cpp b/examples/LightsOff.cpp index 9c75a8f..3396ce8 100644 --- a/examples/LightsOff.cpp +++ b/examples/LightsOff.cpp @@ -1,3 +1,26 @@ +/** + \file LightsOff.cpp + Copyright Notice\n + Copyright (C) 2021 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 . + + \brief This example turns off all lights for 20 seconds, then turns them on again. +**/ + #include #include @@ -20,6 +43,7 @@ namespace hue = hueplusplus; const std::string macAddress = ""; const std::string username = ""; +//! \brief Connects to a bridge and returns it. hue::Bridge connectToBridge() { hue::BridgeFinder finder(std::make_shared()); @@ -55,6 +79,9 @@ hue::Bridge connectToBridge() return finder.getBridge(*it); } +//! \brief Turns off the lights on the bridge for 20 seconds. +//! +//! Only turns the lights back on that were on before. void lightsOff(hue::Bridge& hue) { std::vector lights = hue.lights().getAll(); @@ -64,7 +91,7 @@ void lightsOff(hue::Bridge& hue) for (hue::Light& l : lights) { onMap.emplace(l.getId(), l.isOn()); - l.Off(); + l.off(); } // This would be preferrable, but does not work because it also resets the brightness of all lights diff --git a/examples/UsernameConfig.cpp b/examples/UsernameConfig.cpp new file mode 100644 index 0000000..2643404 --- /dev/null +++ b/examples/UsernameConfig.cpp @@ -0,0 +1,148 @@ +/** + \file UsernameConfig.cpp + Copyright Notice\n + Copyright (C) 2021 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 . + + \brief This example reads the username and mac address from a config file. +**/ + +#include +#include +#include +#include + +#include + +#ifdef _MSC_VER +#include + +using SystemHttpHandler = hueplusplus::WinHttpHandler; + +#else +#include + +using SystemHttpHandler = hueplusplus::LinHttpHandler; + +#endif + +namespace hue = hueplusplus; + +//! \brief Reads a json config file. +//! \param filename Path to the config file +//! \returns Parsed json or an empty object if not successful. +nlohmann::json readConfigFile(const std::string& filename) +{ + std::ifstream stream(filename); + try + { + nlohmann::json result = nlohmann::json::object(); + if (stream) + { + stream >> result; + } + return result; + } + catch (const nlohmann::json::exception&) + { + // Ignore parse errors + return nlohmann::json::object(); + } +} + +//! \brief Saves a json file. +//! \param filename Path to the config file +//! \param config Json value to save +void saveConfigFile(const std::string& filename, const nlohmann::json& config) +{ + std::ofstream stream(filename); + stream << std::setw(4) << config; +} + +//! \brief Connects to a bridge and returns it +//! \param username Already existing username, can be left empty. +//! \param macAddress MAC address of the bridge, can be left empty. +//! \throws std::runtime_error When the bridge was not found. +//! \returns A connected bridge. +hue::Bridge connectToBridge(const std::string& username, const std::string& macAddress) +{ + hue::BridgeFinder finder(std::make_shared()); + + std::vector bridges = finder.findBridges(); + + for (const auto& bridge : bridges) + { + std::cout << "Bridge: " << bridge.mac << " at " << bridge.ip << '\n'; + } + if (bridges.empty()) + { + std::cout << "Found no bridges\n"; + throw std::runtime_error("no bridges found"); + } + + if (macAddress.empty()) + { + std::cout << "No bridge given, connecting to first one.\n"; + return finder.getBridge(bridges.front()); + } + if (!username.empty()) + { + finder.addUsername(macAddress, username); + } + auto it = std::find_if( + bridges.begin(), bridges.end(), [&](const auto& identification) { return identification.mac == macAddress; }); + if (it == bridges.end()) + { + std::cout << "Given bridge not found\n"; + throw std::runtime_error("bridge not found"); + } + return finder.getBridge(*it); +} + +//! Connects to a bridge. The steps are: +//! - read "config.json" for an existing config +//! - connect to the bridge +//! - save the username to the config file for the next run +//! +//! Also prints out the IP and username. +int main(int argc, char** argv) +{ + + const std::string filename = "config.json"; + try + { + + nlohmann::json config = readConfigFile(filename); + const std::string username = config.value("username", ""); + const std::string macAddress = config.value("mac", ""); + hue::Bridge hue = connectToBridge(username, macAddress); + + // Store updated information into file + config["username"] = hue.getUsername(); + config["mac"] = hue.config().getMACAddress(); + saveConfigFile(filename, config); + + std::cout << "Connected to bridge. IP: " << hue.getBridgeIP() << ", username: " << hue.getUsername() << '\n'; + } + catch (...) + { } + + std::cout << "Press enter to exit\n"; + std::cin.get(); + + return 0; +} \ No newline at end of file