This example reads the username and mac address from a config file.
1
24
25#include <algorithm>
26#include <fstream>
27#include <iomanip>
28#include <iostream>
29
31
32#ifdef _MSC_VER
34
36
37#else
39
41
42#endif
43
45
46
47
48
50{
51 std::ifstream stream(filename);
52 try
53 {
54 nlohmann::json result = nlohmann::json::object();
55 if (stream)
56 {
57 stream >> result;
58 }
59 return result;
60 }
61 catch (const nlohmann::json::exception&)
62 {
63
64 return nlohmann::json::object();
65 }
66}
67
68
69
70
71void saveConfigFile(
const std::string& filename,
const nlohmann::json& config)
72{
73 std::ofstream stream(filename);
74 stream << std::setw(4) << config;
75}
76
77
78
79
80
81
83{
85
86 std::vector<hue::BridgeFinder::BridgeIdentification> bridges = finder.findBridges();
87
88 for (const auto& bridge : bridges)
89 {
90 std::cout << "Bridge: " << bridge.mac << " at " << bridge.ip << '\n';
91 }
92 if (bridges.empty())
93 {
94 std::cout << "Found no bridges\n";
95 throw std::runtime_error("no bridges found");
96 }
97
99 {
100 std::cout << "No bridge given, connecting to first one.\n";
101 return finder.getBridge(bridges.front());
102 }
104 {
106 }
107 auto it = std::find_if(
108 bridges.begin(), bridges.end(), [&](const auto& identification) { return identification.mac == macAddress; });
109 if (it == bridges.end())
110 {
111 std::cout << "Given bridge not found\n";
112 throw std::runtime_error("bridge not found");
113 }
114 return finder.getBridge(*it);
115}
116
117
118
119
120
121
122
123int main(
int argc,
char** argv)
124{
125
126 const std::string filename = "config.json";
127 try
128 {
129
131 const std::string
username = config.value(
"username",
"");
132 const std::string
macAddress = config.value(
"mac",
"");
134
135
136 config[
"username"] =
hue.getUsername();
137 config[
"mac"] =
hue.config().getMACAddress();
139
140 std::cout <<
"Connected to bridge. IP: " <<
hue.getBridgeIP() <<
", username: " <<
hue.getUsername() <<
'\n';
141 }
142 catch (...)
143 { }
144
145 std::cout << "Press enter to exit\n";
146 std::cin.get();
147
148 return 0;
149}
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
nlohmann::json readConfigFile(const std::string &filename)
Definition UsernameConfig.cpp:49
void saveConfigFile(const std::string &filename, const nlohmann::json &config)
Definition UsernameConfig.cpp:71
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