| 206 | } |
| 207 | |
| 208 | std::string Bridge::requestUsername() |
| 209 | { |
| 210 | std::chrono::steady_clock::duration timeout = Config::instance().getRequestUsernameTimeout(); |
| 211 | std::chrono::steady_clock::duration checkInterval = Config::instance().getRequestUsernameAttemptInterval(); |
| 212 | std::cout << "Please press the link Button! You've got " |
| 213 | << std::chrono::duration_cast<std::chrono::seconds>(timeout).count() << " secs!\n"; |
| 214 | |
| 215 | // when the link button was pressed we got 30 seconds to get our username for control |
| 216 | nlohmann::json request; |
| 217 | request["devicetype"] = "HuePlusPlus#User"; |
| 218 | request["generateclientkey"] = true; |
| 219 | |
| 220 | nlohmann::json answer; |
| 221 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
| 222 | // do-while loop to check at least once when timeout is 0 |
| 223 | do |
| 224 | { |
| 225 | std::this_thread::sleep_for(checkInterval); |
| 226 | answer = http_handler->POSTJson("/api", request, ip, port); |
| 227 | nlohmann::json jsonUser = utils::safeGetMember(answer, 0, "success", "username"); |
| 228 | nlohmann::json jsonKey = utils::safeGetMember(answer, 0, "success", "clientkey"); |
| 229 | if (jsonUser != nullptr) |
| 230 | { |
| 231 | // [{"success":{"username": "<username>"}}] |
| 232 | username = jsonUser.get<std::string>(); |
| 233 | // Update commands with new username and ip |
| 234 | setHttpHandler(http_handler); |
| 235 | std::cout << "Success! Link button was pressed!\n"; |
| 236 | std::cout << "Username is \"" << username << "\"\n"; |
| 237 | |
| 238 | if (jsonKey != nullptr) |
| 239 | { |
| 240 | clientkey = jsonKey.get<std::string>(); |
| 241 | std::cout << "Client key is \"" << clientkey << "\"\n"; |
| 242 | } |
| 243 | break; |
| 244 | } |
| 245 | else if (answer.size() > 0 && answer[0].count("error")) |
| 246 | { |
| 247 | HueAPIResponseException exception = HueAPIResponseException::Create(CURRENT_FILE_INFO, answer[0]); |
| 248 | // All errors except 101: Link button not pressed |
| 249 | if (exception.GetErrorNumber() != 101) |
| 250 | { |
| 251 | throw exception; |
| 252 | } |
| 253 | } |
| 254 | } while (std::chrono::steady_clock::now() - start < timeout); |
| 255 | |
| 256 | return username; |
| 257 | } |
| 258 | |
| 259 | bool Bridge::startStreaming(std::string group_identifier) |
| 260 | { |