private async Task CreateBridgeUser()
        {
            var userCreationRequestParameters = new UserCreationRequestParameters("ChatsPlayHue#devmachine", true);

            var userCreationRequest = new RestRequest("/", DataFormat.Json);

            userCreationRequest.AddJsonBody(userCreationRequestParameters);

            var accountCreationResponse = client.Post <HueAPIResponse <UserCreationResponse>[]>(userCreationRequest);

            while (accountCreationResponse.Data[0].error != null)
            {
                configurationUI.AskUserToPressLinkButton();
                accountCreationResponse = client.Post <HueAPIResponse <UserCreationResponse>[]>(userCreationRequest);
                await Task.Delay(5000);
            }

            configurationUI.NotifyAboutConnectionToBridge();

            credentials = new CredentialsPair(
                accountCreationResponse.Data[0].success.username,
                accountCreationResponse.Data[0].success.clientkey);

            credentialsStorage.StoreCredentials(mac, credentials);
        }
        public async Task Connect()
        {
            var configRequest = new RestRequest("/whatever/config", DataFormat.Json);

            var configurationResponse = client.Get <BridgeConfigurationResponse>(configRequest);

            mac = configurationResponse.Data.mac;

            credentials = credentialsStorage.GetCredentials(this.mac);

            if (credentials == null)
            {
                await CreateBridgeUser();
            }
            else
            {
                // TODO Test credentials pair.
                var testConfigRequest = new RestRequest(string.Format("/{0}/config", credentials.Username), DataFormat.Json);

                var testConfigResponse = client.Get <BridgeConfigurationResponse>(testConfigRequest);

                if (testConfigResponse.Data.whitelist == null)
                {
                    await CreateBridgeUser();
                }
            }
        }
示例#3
0
        public void StoreCredentials(string macAddress, CredentialsPair toStore)
        {
            var settings = configuration.AppSettings.Settings;

            if (settings["HueUsername-" + macAddress] != null)
            {
                settings.Remove("HueUsername-" + macAddress);
            }

            settings.Add("HueUsername-" + macAddress, toStore.Username);


            if (settings["HueClientKey-" + macAddress] != null)
            {
                settings.Remove("HueClientKey-" + macAddress);
            }

            settings.Add("HueClientKey-" + macAddress, toStore.ClientKey);


            configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(configuration.AppSettings.SectionInformation.Name);
        }