Commit 72f9bfd152927f311a80de2301921f63303a413a
Committed by
Moritz Wirger
1 parent
c6b560fb
Add an example to read a config file for the username.
Showing
4 changed files
with
208 additions
and
3 deletions
examples/BridgeSetup.cpp
| 1 | +/** | |
| 2 | + \file BridgeSetup.cpp | |
| 3 | + Copyright Notice\n | |
| 4 | + Copyright (C) 2021 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 | + \brief This example connects to a bridge with hardcoded mac and username. | |
| 22 | +**/ | |
| 23 | + | |
| 24 | + | |
| 1 | 25 | #include <algorithm> |
| 2 | 26 | #include <iostream> |
| 3 | 27 | |
| ... | ... | @@ -21,6 +45,7 @@ namespace hue = hueplusplus; |
| 21 | 45 | const std::string macAddress = ""; |
| 22 | 46 | const std::string username = ""; |
| 23 | 47 | |
| 48 | +//! \brief Connects to a bridge and returns it. | |
| 24 | 49 | hue::Bridge connectToBridge() |
| 25 | 50 | { |
| 26 | 51 | hue::BridgeFinder finder(std::make_shared<SystemHttpHandler>()); | ... | ... |
examples/CMakeLists.txt
| 1 | -add_executable(bridge_setup BridgeSetup.cpp ) | |
| 1 | +add_executable(bridge_setup BridgeSetup.cpp) | |
| 2 | 2 | set_property(TARGET bridge_setup PROPERTY CXX_STANDARD 14) |
| 3 | 3 | set_property(TARGET bridge_setup PROPERTY CXX_EXTENSIONS OFF) |
| 4 | 4 | target_link_libraries(bridge_setup hueplusplusstatic) |
| 5 | 5 | |
| 6 | -add_executable(lights_off LightsOff.cpp ) | |
| 6 | +add_executable(lights_off LightsOff.cpp) | |
| 7 | 7 | set_property(TARGET lights_off PROPERTY CXX_STANDARD 14) |
| 8 | 8 | set_property(TARGET lights_off PROPERTY CXX_EXTENSIONS OFF) |
| 9 | 9 | target_link_libraries(lights_off hueplusplusstatic) |
| 10 | 10 | |
| 11 | 11 | |
| 12 | +add_executable(username_config UsernameConfig.cpp) | |
| 13 | +set_property(TARGET lights_off PROPERTY CXX_STANDARD 14) | |
| 14 | +set_property(TARGET lights_off PROPERTY CXX_EXTENSIONS OFF) | |
| 15 | +target_link_libraries(username_config hueplusplusstatic) | |
| 16 | + | |
| 12 | 17 | add_custom_target(hueplusplus_examples) |
| 13 | 18 | add_dependencies(hueplusplus_examples bridge_setup lights_off) |
| 14 | 19 | \ No newline at end of file | ... | ... |
examples/LightsOff.cpp
| 1 | +/** | |
| 2 | + \file LightsOff.cpp | |
| 3 | + Copyright Notice\n | |
| 4 | + Copyright (C) 2021 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 | + \brief This example turns off all lights for 20 seconds, then turns them on again. | |
| 22 | +**/ | |
| 23 | + | |
| 1 | 24 | #include <thread> |
| 2 | 25 | |
| 3 | 26 | #include <hueplusplus/Bridge.h> |
| ... | ... | @@ -20,6 +43,7 @@ namespace hue = hueplusplus; |
| 20 | 43 | const std::string macAddress = ""; |
| 21 | 44 | const std::string username = ""; |
| 22 | 45 | |
| 46 | +//! \brief Connects to a bridge and returns it. | |
| 23 | 47 | hue::Bridge connectToBridge() |
| 24 | 48 | { |
| 25 | 49 | hue::BridgeFinder finder(std::make_shared<SystemHttpHandler>()); |
| ... | ... | @@ -55,6 +79,9 @@ hue::Bridge connectToBridge() |
| 55 | 79 | return finder.getBridge(*it); |
| 56 | 80 | } |
| 57 | 81 | |
| 82 | +//! \brief Turns off the lights on the bridge for 20 seconds. | |
| 83 | +//! | |
| 84 | +//! Only turns the lights back on that were on before. | |
| 58 | 85 | void lightsOff(hue::Bridge& hue) |
| 59 | 86 | { |
| 60 | 87 | std::vector<hue::Light> lights = hue.lights().getAll(); |
| ... | ... | @@ -64,7 +91,7 @@ void lightsOff(hue::Bridge& hue) |
| 64 | 91 | for (hue::Light& l : lights) |
| 65 | 92 | { |
| 66 | 93 | onMap.emplace(l.getId(), l.isOn()); |
| 67 | - l.Off(); | |
| 94 | + l.off(); | |
| 68 | 95 | } |
| 69 | 96 | |
| 70 | 97 | // This would be preferrable, but does not work because it also resets the brightness of all lights | ... | ... |
examples/UsernameConfig.cpp
0 โ 100644
| 1 | +/** | |
| 2 | + \file UsernameConfig.cpp | |
| 3 | + Copyright Notice\n | |
| 4 | + Copyright (C) 2021 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 | + \brief This example reads the username and mac address from a config file. | |
| 22 | +**/ | |
| 23 | + | |
| 24 | +#include <algorithm> | |
| 25 | +#include <fstream> | |
| 26 | +#include <iomanip> | |
| 27 | +#include <iostream> | |
| 28 | + | |
| 29 | +#include <hueplusplus/Bridge.h> | |
| 30 | + | |
| 31 | +#ifdef _MSC_VER | |
| 32 | +#include <hueplusplus/WinHttpHandler.h> | |
| 33 | + | |
| 34 | +using SystemHttpHandler = hueplusplus::WinHttpHandler; | |
| 35 | + | |
| 36 | +#else | |
| 37 | +#include <hueplusplus/LinHttpHandler.h> | |
| 38 | + | |
| 39 | +using SystemHttpHandler = hueplusplus::LinHttpHandler; | |
| 40 | + | |
| 41 | +#endif | |
| 42 | + | |
| 43 | +namespace hue = hueplusplus; | |
| 44 | + | |
| 45 | +//! \brief Reads a json config file. | |
| 46 | +//! \param filename Path to the config file | |
| 47 | +//! \returns Parsed json or an empty object if not successful. | |
| 48 | +nlohmann::json readConfigFile(const std::string& filename) | |
| 49 | +{ | |
| 50 | + std::ifstream stream(filename); | |
| 51 | + try | |
| 52 | + { | |
| 53 | + nlohmann::json result = nlohmann::json::object(); | |
| 54 | + if (stream) | |
| 55 | + { | |
| 56 | + stream >> result; | |
| 57 | + } | |
| 58 | + return result; | |
| 59 | + } | |
| 60 | + catch (const nlohmann::json::exception&) | |
| 61 | + { | |
| 62 | + // Ignore parse errors | |
| 63 | + return nlohmann::json::object(); | |
| 64 | + } | |
| 65 | +} | |
| 66 | + | |
| 67 | +//! \brief Saves a json file. | |
| 68 | +//! \param filename Path to the config file | |
| 69 | +//! \param config Json value to save | |
| 70 | +void saveConfigFile(const std::string& filename, const nlohmann::json& config) | |
| 71 | +{ | |
| 72 | + std::ofstream stream(filename); | |
| 73 | + stream << std::setw(4) << config; | |
| 74 | +} | |
| 75 | + | |
| 76 | +//! \brief Connects to a bridge and returns it | |
| 77 | +//! \param username Already existing username, can be left empty. | |
| 78 | +//! \param macAddress MAC address of the bridge, can be left empty. | |
| 79 | +//! \throws std::runtime_error When the bridge was not found. | |
| 80 | +//! \returns A connected bridge. | |
| 81 | +hue::Bridge connectToBridge(const std::string& username, const std::string& macAddress) | |
| 82 | +{ | |
| 83 | + hue::BridgeFinder finder(std::make_shared<SystemHttpHandler>()); | |
| 84 | + | |
| 85 | + std::vector<hue::BridgeFinder::BridgeIdentification> bridges = finder.findBridges(); | |
| 86 | + | |
| 87 | + for (const auto& bridge : bridges) | |
| 88 | + { | |
| 89 | + std::cout << "Bridge: " << bridge.mac << " at " << bridge.ip << '\n'; | |
| 90 | + } | |
| 91 | + if (bridges.empty()) | |
| 92 | + { | |
| 93 | + std::cout << "Found no bridges\n"; | |
| 94 | + throw std::runtime_error("no bridges found"); | |
| 95 | + } | |
| 96 | + | |
| 97 | + if (macAddress.empty()) | |
| 98 | + { | |
| 99 | + std::cout << "No bridge given, connecting to first one.\n"; | |
| 100 | + return finder.getBridge(bridges.front()); | |
| 101 | + } | |
| 102 | + if (!username.empty()) | |
| 103 | + { | |
| 104 | + finder.addUsername(macAddress, username); | |
| 105 | + } | |
| 106 | + auto it = std::find_if( | |
| 107 | + bridges.begin(), bridges.end(), [&](const auto& identification) { return identification.mac == macAddress; }); | |
| 108 | + if (it == bridges.end()) | |
| 109 | + { | |
| 110 | + std::cout << "Given bridge not found\n"; | |
| 111 | + throw std::runtime_error("bridge not found"); | |
| 112 | + } | |
| 113 | + return finder.getBridge(*it); | |
| 114 | +} | |
| 115 | + | |
| 116 | +//! Connects to a bridge. The steps are: | |
| 117 | +//! - read "config.json" for an existing config | |
| 118 | +//! - connect to the bridge | |
| 119 | +//! - save the username to the config file for the next run | |
| 120 | +//! | |
| 121 | +//! Also prints out the IP and username. | |
| 122 | +int main(int argc, char** argv) | |
| 123 | +{ | |
| 124 | + | |
| 125 | + const std::string filename = "config.json"; | |
| 126 | + try | |
| 127 | + { | |
| 128 | + | |
| 129 | + nlohmann::json config = readConfigFile(filename); | |
| 130 | + const std::string username = config.value("username", ""); | |
| 131 | + const std::string macAddress = config.value("mac", ""); | |
| 132 | + hue::Bridge hue = connectToBridge(username, macAddress); | |
| 133 | + | |
| 134 | + // Store updated information into file | |
| 135 | + config["username"] = hue.getUsername(); | |
| 136 | + config["mac"] = hue.config().getMACAddress(); | |
| 137 | + saveConfigFile(filename, config); | |
| 138 | + | |
| 139 | + std::cout << "Connected to bridge. IP: " << hue.getBridgeIP() << ", username: " << hue.getUsername() << '\n'; | |
| 140 | + } | |
| 141 | + catch (...) | |
| 142 | + { } | |
| 143 | + | |
| 144 | + std::cout << "Press enter to exit\n"; | |
| 145 | + std::cin.get(); | |
| 146 | + | |
| 147 | + return 0; | |
| 148 | +} | |
| 0 | 149 | \ No newline at end of file | ... | ... |