예제 #1
0
        private async Task <bool> Register()
        {
            var       retryCount        = 0;
            const int retryMax          = 60;
            const int pauseMilliseconds = 1000;

            while (retryCount < retryMax) // wait a minute, check each second
            {
                var body = "{\"username\": \"" + appname + "\", \"devicetype\":\"" + appname + "\"}";
                var responseFromServer = await HttpRestHelper.Post(Urls.GetRegisterUrl(), body);

                if (responseFromServer.Contains("link button not pressed"))
                {
                    //responseFromServer = "[{\"error\":{\"type\":7,\"address\":\"/username\",\"description\":\"invalid value, winhueapp, for parameter, username\"}},{\"error\":{\"type\":101,\"address\":\"\",\"description\":\"link button not pressed\"}}]"
                    // link button not pressed, inform on first try only
                    if (retryCount == 0 && PushButtonOnBridge != null)
                    {
                        PushButtonOnBridge(this, null);
                    }

                    Thread.Sleep(pauseMilliseconds); // sleep for a second, then retry
                    retryCount++;
                }
                else
                {
                    dynamic obj = DynamicJsonConverter.Parse(responseFromServer);
                    // sample response: [{"error":{"type":7,"address":"/username","description":"invalid value, WinHueApp, for parameter, username"}},{"success":{"username":"******"}}]

                    string key = obj[1].success.username;

                    if (!string.IsNullOrWhiteSpace(key))
                    {
                        Settings.Default.BridgeApiKey = key;
                        Settings.Default.Save();

                        IsAuthenticated = true;
                        return(true);
                    }
                }
            }

            IsAuthenticated = false;
            return(false);
        }