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

This example turns off all lights for 20 seconds, then turns them on again.

1
25#include <thread>
26
27#include <hueplusplus/Bridge.h>
28
29#ifdef _MSC_VER
31
33
34#else
36
38
39#endif
40
41namespace hue = hueplusplus;
42
43// Configure existing connections here, or leave empty for new connection
44const std::string macAddress = "";
45const std::string username = "";
46
47// Connects to a bridge and returns it.
49{
50 hue::BridgeFinder finder(std::make_shared<SystemHttpHandler>());
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
64 if (macAddress.empty())
65 {
66 std::cout << "No bridge given, connecting to first one.\n";
67 return finder.getBridge(bridges.front());
68 }
69 if (!username.empty())
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 }
80 return finder.getBridge(*it);
81}
82
83// Turns off the lights on the bridge for 20 seconds.
84// Only turns the lights back on that were on before.
86{
87 std::vector<hue::Light> lights = hue.lights().getAll();
88
89 // Save current on state of the lights
90 std::map<int, bool> onMap;
91 for (hue::Light& l : lights)
92 {
93 onMap.emplace(l.getId(), l.isOn());
94 l.off();
95 }
96
97 // This would be preferrable, but does not work because it also resets the brightness of all lights
98 // Group 0 contains all lights, turn all off with a transition of 1 second
99 // hue.groups().get(0).setOn(false, 10);
100 // -------------------------------------
101
102 std::cout << "Turned off all lights\n";
103
104 std::this_thread::sleep_for(std::chrono::seconds(20));
105
106 // Restore the original state of the lights
107 for (hue::Light& l : lights)
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
127 lightsOff(hue);
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
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
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 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