This example turns off all lights for 20 seconds, then turns them on again.
1
24
25#include <thread>
26
28
29#ifdef _MSC_VER
31
33
34#else
36
38
39#endif
40
42
43
46
47
49{
51
52 std::vector<hue::BridgeFinder::BridgeIdentification> bridges = finder.
findBridges();
53
54 for (const auto& bridge : bridges)
55 {
56 std::cout << "Bridge: " << bridge.mac << " at " << bridge.ip << '\n';
57 }
58 if (bridges.empty())
59 {
60 std::cout << "Found no bridges\n";
61 throw std::runtime_error("no bridges found");
62 }
63
65 {
66 std::cout << "No bridge given, connecting to first one.\n";
68 }
70 {
72 }
73 auto it = std::find_if(
74 bridges.begin(), bridges.end(), [&](const auto& identification) { return identification.mac == macAddress; });
75 if (it == bridges.end())
76 {
77 std::cout << "Given bridge not found\n";
78 throw std::runtime_error("bridge not found");
79 }
81}
82
83
84
86{
87 std::vector<hue::Light> lights =
hue.lights().getAll();
88
89
90 std::map<int, bool> onMap;
92 {
93 onMap.emplace(l.getId(), l.isOn());
94 l.off();
95 }
96
97
98
99
100
101
102 std::cout << "Turned off all lights\n";
103
104 std::this_thread::sleep_for(std::chrono::seconds(20));
105
106
108 {
109 if (onMap[l.getId()])
110 {
111 l.on();
112 }
113 }
114
115 std::cout << "Turned lights back on\n";
116}
117
118int main(
int argc,
char** argv)
119{
120
121 try
122 {
124
125 std::cout <<
"Connected to bridge. IP: " <<
hue.getBridgeIP() <<
", username: " <<
hue.getUsername() <<
'\n';
126
128 }
129 catch (...)
130 { }
131
132 std::cout << "Press enter to exit\n";
133 std::cin.get();
134
135 return 0;
136}
int main(int argc, char **argv)
Definition BridgeSetup.cpp:85
hueplusplus::LinHttpHandler SystemHttpHandler
Definition BridgeSetup.cpp:39
hue::Bridge connectToBridge()
Definition BridgeSetup.cpp:50
const std::string macAddress
Definition BridgeSetup.cpp:46
const std::string username
Definition BridgeSetup.cpp:47
void lightsOff(hue::Bridge &hue)
Definition LightsOff.cpp:85
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 for Hue Light fixtures.
Definition Light.h:61
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