Loading...
Searching...
No Matches
BridgeSetup.cpp

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

#include <algorithm>
#include <iostream>
#include <hueplusplus/Bridge.h>
#ifdef _MSC_VER
#include <hueplusplus/WinHttpHandler.h>
using SystemHttpHandler = hueplusplus::WinHttpHandler;
#else
#include <hueplusplus/LinHttpHandler.h>
using SystemHttpHandler = hueplusplus::LinHttpHandler;
#endif
namespace hue = hueplusplus;
// Configure existing connections here, or leave empty for new connection
const std::string macAddress = "";
const std::string username = "";
// Connects to a bridge and returns it.
hue::Bridge connectToBridge()
{
hue::BridgeFinder finder(std::make_shared<SystemHttpHandler>());
std::vector<hue::BridgeFinder::BridgeIdentification> bridges = finder.findBridges();
for (const auto& bridge : bridges)
{
std::cout << "Bridge: " << bridge.mac << " at " << bridge.ip << '\n';
}
if (bridges.empty())
{
std::cout << "Found no bridges\n";
throw std::runtime_error("no bridges found");
}
if (macAddress.empty())
{
std::cout << "No bridge given, connecting to first one.\n";
return finder.getBridge(bridges.front());
}
if (!username.empty())
{
finder.addUsername(macAddress, username);
}
auto it = std::find_if(
bridges.begin(), bridges.end(), [&](const auto& identification) { return identification.mac == macAddress; });
if (it == bridges.end())
{
std::cout << "Given bridge not found\n";
throw std::runtime_error("bridge not found");
}
return finder.getBridge(*it);
}
int main(int argc, char** argv)
{
try
{
hue::Bridge hue = connectToBridge();
std::cout << "Connected to bridge. IP: " << hue.getBridgeIP() << ", username: " << hue.getUsername() << '\n';
}
catch (...)
{ }
std::cout << "Press enter to exit\n";
std::cin.get();
return 0;
}
Bridge.h
main
int main(int argc, char **argv)
Definition BridgeSetup.cpp:85
connectToBridge
hue::Bridge connectToBridge()
Definition BridgeSetup.cpp:50
macAddress
const std::string macAddress
Definition BridgeSetup.cpp:46
username
const std::string username
Definition BridgeSetup.cpp:47
LinHttpHandler.h
WinHttpHandler.h
hueplusplus::BridgeFinder
Definition Bridge.h:62
hueplusplus::BridgeFinder::findBridges
std::vector< BridgeIdentification > findBridges() const
Finds all bridges in the network and returns them.
Definition Bridge.cpp:43
hueplusplus::BridgeFinder::getBridge
Bridge getBridge(const BridgeIdentification &identification, bool sharedState=false)
Gets a Hue bridge based on its identification.
Definition Bridge.cpp:78
hueplusplus::BridgeFinder::addUsername
void addUsername(const std::string &mac, const std::string &username)
Function that adds a username to the usernames map.
Definition Bridge.cpp:109
hueplusplus::Bridge
Bridge class for a bridge.
Definition Bridge.h:139
hueplusplus::LinHttpHandler
Class to handle http requests and multicast requests on linux systems.
Definition LinHttpHandler.h:37
hueplusplus::WinHttpHandler
Class to handle http requests and multicast requests on windows systems.
Definition WinHttpHandler.h:37
hueplusplus
Namespace for the hueplusplus library.
Definition Action.h:28