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

This example reads the username and mac address from a config file.

1
25#include <algorithm>
26#include <fstream>
27#include <iomanip>
28#include <iostream>
29
30#include <hueplusplus/Bridge.h>
31
32#ifdef _MSC_VER
34
36
37#else
39
41
42#endif
43
44namespace hue = hueplusplus;
45
46// Reads a json config file.
47// filename: Path to the config file
48// returns parsed json or an empty object if not successful.
49nlohmann::json readConfigFile(const std::string& filename)
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 // Ignore parse errors
64 return nlohmann::json::object();
65 }
66}
67
68// Saves a json file.
69// filename: Path to the config file
70// config: Json value to save
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// Connects to a bridge and returns it
78// username: Already existing username, can be left empty.
79// macAddress: MAC address of the bridge, can be left empty.
80// throws std::runtime_error when the bridge was not found.
81// returns a connected bridge.
82hue::Bridge connectToBridge(const std::string& username, const std::string& macAddress)
83{
84 hue::BridgeFinder finder(std::make_shared<SystemHttpHandler>());
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
98 if (macAddress.empty())
99 {
100 std::cout << "No bridge given, connecting to first one.\n";
101 return finder.getBridge(bridges.front());
102 }
103 if (!username.empty())
104 {
105 finder.addUsername(macAddress, username);
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// Connects to a bridge. The steps are:
118// - read "config.json" for an existing config
119// - connect to the bridge
120// - save the username to the config file for the next run
121//
122// Also prints out the IP and username.
123int main(int argc, char** argv)
124{
125
126 const std::string filename = "config.json";
127 try
128 {
129
130 nlohmann::json config = readConfigFile(filename);
131 const std::string username = config.value("username", "");
132 const std::string macAddress = config.value("mac", "");
134
135 // Store updated information into file
136 config["username"] = hue.getUsername();
137 config["mac"] = hue.config().getMACAddress();
138 saveConfigFile(filename, config);
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
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
Definition Bridge.h:62
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