| 76 | } |
| 77 | |
| 78 | Bridge BridgeFinder::getBridge(const BridgeIdentification& identification, bool sharedState) |
| 79 | { |
| 80 | std::string normalizedMac = normalizeMac(identification.mac); |
| 81 | auto pos = usernames.find(normalizedMac); |
| 82 | auto key = clientkeys.find(normalizedMac); |
| 83 | if (pos != usernames.end()) |
| 84 | { |
| 85 | if (key != clientkeys.end()) |
| 86 | { |
| 87 | return Bridge(identification.ip, identification.port, pos->second, http_handler, key->second, |
| 88 | std::chrono::seconds(10), sharedState); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | return Bridge(identification.ip, identification.port, pos->second, http_handler, "", |
| 93 | std::chrono::seconds(10), sharedState); |
| 94 | } |
| 95 | } |
| 96 | Bridge bridge(identification.ip, identification.port, "", http_handler, "", std::chrono::seconds(10), sharedState); |
| 97 | bridge.requestUsername(); |
| 98 | if (bridge.getUsername().empty()) |
| 99 | { |
| 100 | std::cerr << "Failed to request username for ip " << identification.ip << std::endl; |
| 101 | throw HueException(CURRENT_FILE_INFO, "Failed to request username!"); |
| 102 | } |
| 103 | addUsername(normalizedMac, bridge.getUsername()); |
| 104 | addClientKey(normalizedMac, bridge.getClientKey()); |
| 105 | |
| 106 | return bridge; |
| 107 | } |
| 108 | |
| 109 | void BridgeFinder::addUsername(const std::string& mac, const std::string& username) |
| 110 | { |