From 3323e501d1f18d7694de1f530017bfb147d8d85e Mon Sep 17 00:00:00 2001 From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com> Date: Sun, 2 Feb 2020 19:00:36 +0100 Subject: [PATCH] Add sample for connecting to Hue bridge. --- CMakeLists.txt | 5 +++++ examples/BridgeSetup.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ examples/CMakeLists.txt | 4 ++++ 3 files changed, 82 insertions(+), 0 deletions(-) mode change 100755 => 100644 CMakeLists.txt create mode 100644 examples/BridgeSetup.cpp create mode 100644 examples/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt old mode 100755 new mode 100644 index db9cc59..890f2cf --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,7 @@ endif() # options to set option(hueplusplus_TESTS "Build tests" OFF) +option(hueplusplus_SAMPLES "Build examples" OFF) option(CLANG_TIDY_FIX "Perform fixes for Clang-Tidy" OFF) find_program(CLANG_TIDY_EXE NAMES "clang-tidy" DOC "Path to clang-tidy executable") @@ -98,3 +99,7 @@ add_subdirectory(src) if(hueplusplus_TESTS) add_subdirectory("test") endif() + +if(hueplusplus_SAMPLES) + add_subdirectory("examples") +endif() diff --git a/examples/BridgeSetup.cpp b/examples/BridgeSetup.cpp new file mode 100644 index 0000000..f5eb78c --- /dev/null +++ b/examples/BridgeSetup.cpp @@ -0,0 +1,73 @@ +#include +#include + +#include + +#ifdef _MSC_VER +#include + +using SystemHttpHandler = hueplusplus::WinHttpHandler; + +#else +#include + +using SystemHttpHandler = hueplusplus::LinHttpHandler; + +#endif + +// Configure existing connections here, or leave empty for new connection +const std::string macAddress = ""; +const std::string username = ""; + +hueplusplus::Bridge connectToBridge() +{ + hueplusplus::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); +} + +int main(int argc, char** argv) +{ + + try + { + hueplusplus::Bridge hue = connectToBridge(); + + 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 diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..d3444c7 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,4 @@ +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) -- libgit2 0.21.4