hueplusplus 1.2.0
Loading...
Searching...
No Matches
BridgeSetup.cpp

This example connects to a bridge with hardcoded mac and username.

1
26#include <algorithm>
27#include <iostream>
28
29#include <hueplusplus/Bridge.h>
30
31#ifdef _MSC_VER
33
35
36#else
38
40
41#endif
42
43namespace hue = hueplusplus;
44
45// Configure existing connections here, or leave empty for new connection
46const std::string macAddress = "";
47const std::string username = "";
48
49// Connects to a bridge and returns it.
51{
52 hue::BridgeFinder finder(std::make_shared<SystemHttpHandler>());
53
54 std::vector<hue::BridgeFinder::BridgeIdentification> bridges = finder.findBridges();
55
56 for (const auto& bridge : bridges)
57 {
58 std::cout << "Bridge: " << bridge.mac << " at " << bridge.ip << '\n';
59 }
60 if (bridges.empty())
61 {
62 std::cout << "Found no bridges\n";
63 throw std::runtime_error("no bridges found");
64 }
65
66 if (macAddress.empty())
67 {
68 std::cout << "No bridge given, connecting to first one.\n";
69 return finder.getBridge(bridges.front());
70 }
71 if (!username.empty())
72 {
74 }
75 auto it = std::find_if(
76 bridges.begin(), bridges.end(), [&](const auto& identification) { return identification.mac == macAddress; });
77 if (it == bridges.end())
78 {
79 std::cout << "Given bridge not found\n";
80 throw std::runtime_error("bridge not found");
81 }
82 return finder.getBridge(*it);
83}
84
85int main(int argc, char** argv)
86{
87
88 try
89 {
91
92 std::cout << "Connected to bridge. IP: " << hue.getBridgeIP() << ", username: " << hue.getUsername() << '\n';
93 }
94 catch (...)
95 { }
96
97 std::cout << "Press enter to exit\n";
98 std::cin.get();
99
100 return 0;
101}
int main(int argc, char **argv)
Definition BridgeSetup.cpp:85
hue::Bridge connectToBridge()
Definition BridgeSetup.cpp:50
const std::string macAddress
Definition BridgeSetup.cpp:46
const std::string username
Definition BridgeSetup.cpp:47
Definition Bridge.h:62
std::vector< BridgeIdentification > findBridges() const
Finds all bridges in the network and returns them.
Definition Bridge.cpp:43
Bridge getBridge(const BridgeIdentification &identification, bool sharedState=false)
Gets a Hue bridge based on its identification.
Definition Bridge.cpp:78
void addUsername(const std::string &mac, const std::string &username)
Function that adds a username to the usernames map.
Definition Bridge.cpp:109
Bridge class for a bridge.
Definition Bridge.h:139
Class to handle http requests and multicast requests on linux systems.
Definition LinHttpHandler.h:37
Class to handle http requests and multicast requests on windows systems.
Definition WinHttpHandler.h:37
Namespace for the hueplusplus library.
Definition Action.h:28