Ejemplo n.º 1
0
        public async Task Init()
        {
            var configs = await _db.Getconfig();

            if (configs == null)
            {
                StatusUpdate?.Invoke(this, new HueEventArgs {
                    Status = "Searching"
                });
                var bridge = await GetBridge();

                if (bridge == null)
                {
                    StatusUpdate?.Invoke(this, new HueEventArgs {
                        Status = "No Bridge found"
                    });
                    return;
                }

                _client = new LocalHueClient(bridge.IpAddress);

                while (true)
                {
                    StatusUpdate?.Invoke(this, new HueEventArgs {
                        Status = "Press the Button"
                    });
                    await Task.Factory.StartNew(() => Thread.Sleep(TimeSpan.FromSeconds(5)));

                    try
                    {
                        var appkey = await _client.RegisterAsync("ImageHue", System.Environment.MachineName.ToString());

                        await _db.Setconfig(new Config { IP = bridge.IpAddress, Key = appkey });

                        break;
                    }
                    catch (Exception e)
                    {
                        System.Console.WriteLine(e.Message);
                    }
                }
                StatusUpdate?.Invoke(this, new HueEventArgs {
                    Status = "Found IP: " + bridge.IpAddress
                });
            }
            else
            {
                _client = new LocalHueClient(configs.IP);
                _client.Initialize(configs.Key);
            }

            try
            {
                var test = await _client.GetCapabilitiesAsync();

                StatusUpdate?.Invoke(this, new HueEventArgs {
                    Status = "Connection Established"
                });
            }
            catch (Exception e)
            {
                StatusUpdate?.Invoke(this, new HueEventArgs {
                    Status = "Error, retry"
                });
                await _db.Deleteconfig();

                _initcounter++;

                if (_initcounter < 5)
                {
                    await Init();
                }
            }
        }