Commit 3323e501d1f18d7694de1f530017bfb147d8d85e
Committed by
Moritz Wirger
1 parent
3d172bca
Add sample for connecting to Hue bridge.
Showing
3 changed files
with
82 additions
and
0 deletions
CMakeLists.txt
100755 โ 100644
| ... | ... | @@ -22,6 +22,7 @@ endif() |
| 22 | 22 | |
| 23 | 23 | # options to set |
| 24 | 24 | option(hueplusplus_TESTS "Build tests" OFF) |
| 25 | +option(hueplusplus_SAMPLES "Build examples" OFF) | |
| 25 | 26 | |
| 26 | 27 | option(CLANG_TIDY_FIX "Perform fixes for Clang-Tidy" OFF) |
| 27 | 28 | find_program(CLANG_TIDY_EXE NAMES "clang-tidy" DOC "Path to clang-tidy executable") |
| ... | ... | @@ -98,3 +99,7 @@ add_subdirectory(src) |
| 98 | 99 | if(hueplusplus_TESTS) |
| 99 | 100 | add_subdirectory("test") |
| 100 | 101 | endif() |
| 102 | + | |
| 103 | +if(hueplusplus_SAMPLES) | |
| 104 | + add_subdirectory("examples") | |
| 105 | +endif() | ... | ... |
examples/BridgeSetup.cpp
0 โ 100644
| 1 | +#include <algorithm> | |
| 2 | +#include <iostream> | |
| 3 | + | |
| 4 | +#include <hueplusplus/Bridge.h> | |
| 5 | + | |
| 6 | +#ifdef _MSC_VER | |
| 7 | +#include <hueplusplus/WinHttpHandler.h> | |
| 8 | + | |
| 9 | +using SystemHttpHandler = hueplusplus::WinHttpHandler; | |
| 10 | + | |
| 11 | +#else | |
| 12 | +#include <hueplusplus/LinHttpHandler.h> | |
| 13 | + | |
| 14 | +using SystemHttpHandler = hueplusplus::LinHttpHandler; | |
| 15 | + | |
| 16 | +#endif | |
| 17 | + | |
| 18 | +// Configure existing connections here, or leave empty for new connection | |
| 19 | +const std::string macAddress = ""; | |
| 20 | +const std::string username = ""; | |
| 21 | + | |
| 22 | +hueplusplus::Bridge connectToBridge() | |
| 23 | +{ | |
| 24 | + hueplusplus::BridgeFinder finder(std::make_shared<SystemHttpHandler>()); | |
| 25 | + | |
| 26 | + std::vector<hueplusplus::BridgeFinder::BridgeIdentification> bridges = finder.findBridges(); | |
| 27 | + | |
| 28 | + for (const auto& bridge : bridges) | |
| 29 | + { | |
| 30 | + std::cout << "Bridge: " << bridge.mac << " at " << bridge.ip << '\n'; | |
| 31 | + } | |
| 32 | + if (bridges.empty()) | |
| 33 | + { | |
| 34 | + std::cout << "Found no bridges\n"; | |
| 35 | + throw std::runtime_error("no bridges found"); | |
| 36 | + } | |
| 37 | + | |
| 38 | + if (macAddress.empty()) | |
| 39 | + { | |
| 40 | + std::cout << "No bridge given, connecting to first one.\n"; | |
| 41 | + return finder.getBridge(bridges.front()); | |
| 42 | + } | |
| 43 | + if (!username.empty()) | |
| 44 | + { | |
| 45 | + finder.addUsername(macAddress, username); | |
| 46 | + } | |
| 47 | + auto it = std::find_if( | |
| 48 | + bridges.begin(), bridges.end(), [&](const auto& identification) { return identification.mac == macAddress; }); | |
| 49 | + if (it == bridges.end()) | |
| 50 | + { | |
| 51 | + std::cout << "Given bridge not found\n"; | |
| 52 | + throw std::runtime_error("bridge not found"); | |
| 53 | + } | |
| 54 | + return finder.getBridge(*it); | |
| 55 | +} | |
| 56 | + | |
| 57 | +int main(int argc, char** argv) | |
| 58 | +{ | |
| 59 | + | |
| 60 | + try | |
| 61 | + { | |
| 62 | + hueplusplus::Bridge hue = connectToBridge(); | |
| 63 | + | |
| 64 | + std::cout << "Connected to bridge. IP: " << hue.getBridgeIP() << ", username: " << hue.getUsername() << '\n'; | |
| 65 | + } | |
| 66 | + catch (...) | |
| 67 | + { } | |
| 68 | + | |
| 69 | + std::cout << "Press enter to exit\n"; | |
| 70 | + std::cin.get(); | |
| 71 | + | |
| 72 | + return 0; | |
| 73 | +} | |
| 0 | 74 | \ No newline at end of file | ... | ... |
examples/CMakeLists.txt
0 โ 100644