Exemplo n.º 1
0
        private async Task <SuccesfulBridgeConnectionModel> TryConnect(string endpoint, string userName)
        {
            var httpClient = _httpClientFactory.CreateClient();

            StringContent content = new StringContent("{\"devicetype\": \"huecli#" + userName + "\"}", Encoding.UTF8, "application/json");

            while (true)
            {
                HttpResponseMessage response = await httpClient.PostAsync(endpoint, content);

                response.EnsureSuccessStatusCode();

                string responseMessage = await response.Content.ReadAsStringAsync();

                if (responseMessage.Contains("button not pressed") == false)
                {
                    SuccesfulBridgeConnectionModel succesfulBridgeConnectionModel =
                        JsonConvert.DeserializeObject <IEnumerable <SuccesfulBridgeConnectionModel> >(responseMessage)
                        .FirstOrDefault();

                    Console.WriteLine($"Succesful connection. New user: {succesfulBridgeConnectionModel.Success.UserName}");
                    return(succesfulBridgeConnectionModel);
                }
                else
                {
                    Console.WriteLine("Press the button on the Bridge.");
                }

                Thread.Sleep(3000);
            }
        }
Exemplo n.º 2
0
        public async Task EstablishConnection()
        {
            var httpClient = _httpClientFactory.CreateClient();

            _bridgeModel = await TryReadFromCache();

            if (_bridgeModel == null)
            {
                Console.WriteLine("Lets try to find your Bridge...");

                HttpResponseMessage response = await httpClient.GetAsync(_bridgeDiscoveryEndpoint);

                response.EnsureSuccessStatusCode();

                string responseContent = await response.Content.ReadAsStringAsync();

                IEnumerable <FoundBridgeModel> bridges =
                    JsonConvert.DeserializeObject <IEnumerable <FoundBridgeModel> >(responseContent);

                AuthorizedBridgeModel authorizedBridgeModel = null;
                foreach (FoundBridgeModel bridge in bridges)
                {
                    if (bridge == null)
                    {
                        continue;
                    }

                    string bridgeEndpoint = $"http://{bridge.IpAddress}/api";
                    try
                    {
                        SuccesfulBridgeConnectionModel succesfulBridgeConnectionModel = await TryConnect(bridgeEndpoint, _tempUserName);

                        authorizedBridgeModel = new AuthorizedBridgeModel
                        {
                            Id        = bridge.Id,
                            IpAddress = bridge.IpAddress,
                            UserName  = succesfulBridgeConnectionModel.Success.UserName
                        };

                        break;
                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                }

                if (authorizedBridgeModel == null)
                {
                    throw new Exception("Could not find the Bridge. Try again.");
                }

                _bridgeModel = authorizedBridgeModel;
                await Cache(_bridgeModel);
            }
        }