Example #1
0
        public static async void HUEInitialize()
        {
            bridgeIPs = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));

            client = new LocalHueClient(bridgeIPs.First());

            if (File.Exists(dirPath + @"\appKey.txt"))
            {
                appKey = File.ReadAllText(dirPath + @"\appKey.txt");
            }

            if (appKey != null)
            {
                client.Initialize(appKey);
                Connected?.Invoke();
                MessageBox.Show("기존의 휴 브릿지에 연결되었습니다.");
            }
            else
            {
                MessageBox.Show("10초 이내에 휴 브릿지의 링크 버튼을 눌러주세요.");

                Timer timer = new Timer();
                timer.Interval = 1000;
                timer.Elapsed += Timer_Elapsed;
                timer.Start();
            }
        }
Example #2
0
        internal async void FindBridge()
        {
            m_BridgeLocator = new SSDPBridgeLocator();
            m_BridgeIPs     = await m_BridgeLocator.LocateBridgesAsync(TimeSpan.FromSeconds(5));

            OnLocateBridgeComplete(new LocateBridgeEventArgs(m_BridgeIPs));
        }
Example #3
0
        public async Task <string> GetBridgeIpForSetup()
        {
            IEnumerable <Q42.HueApi.Models.Bridge.LocatedBridge> bridgeIPs = await _locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));

            var ip = bridgeIPs.FirstOrDefault()?.IpAddress;

            _client = new LocalHueClient(ip);
            return(ip);
        }
Example #4
0
        private async void button4_Click(object sender, EventArgs e)
        {
            bridgeIPs = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(10));

            try
            {
                client = new LocalHueClient(bridgeIPs.First().IpAddress);
                if (!string.IsNullOrEmpty(Properties.Settings.Default.appkey))
                {
                    client.Initialize(Properties.Settings.Default.appkey);
                    logEvent("Connected with previous Hue Auth");
                }
                else
                {
                    string            messageBoxText = "Press the button on your Hue bridge and then click Ok.";
                    string            caption        = "Action Required";
                    MessageBoxButtons button         = MessageBoxButtons.OK;
                    DialogResult      result         = MessageBox.Show(messageBoxText, caption, button);

                    switch (result)
                    {
                    case DialogResult.OK:
                        var appKey = await client.RegisterAsync("HueLightBot", Environment.MachineName);

                        Properties.Settings.Default.appkey = appKey;
                        Properties.Settings.Default.Save();
                        logEvent("Connected with new Hue Auth");
                        break;
                    }
                }

                groups = await client.GetGroupsAsync();

                List <string> groupNames = groups.Select(x => x.Name).ToList();

                comboBox6.DataSource = groupNames;
            }
            catch (Exception exception)
            {
                string messageBoxText;
                string caption;
                if (exception.Message == "Link button not pressed")
                {
                    messageBoxText = "Link Button not pressed or pairing didn't occur in time. Please try again.";
                    caption        = "Error";
                }
                else
                {
                    messageBoxText = string.Format("An uncaught exception occured. Please let the devs know the following: {0}", Environment.NewLine + exception.Message);
                    caption        = "Error";
                }

                MessageBoxButtons button = MessageBoxButtons.OK;
                DialogResult      result = MessageBox.Show(messageBoxText, caption, button);
            }
        }
Example #5
0
        private async void LocateBridgeAction()
        {
            var result = await LocateBridgeDataLoader.LoadAsync(() => httpLocator.LocateBridgesAsync(TimeSpan.FromSeconds(5)));

            HttpBridges = string.Join(", ", result.Select(x => x.IpAddress).ToArray());

            if (result.Count() > 0)
            {
                _hueClient = new LocalHueClient(result.Select(x => x.IpAddress).First());
            }
        }
Example #6
0
        private async void SsdpLocateBridgeAction()
        {
            var result = await SsdpLocateBridgeDataLoader.LoadAsync(() => ssdpLocator.LocateBridgesAsync(TimeSpan.FromSeconds(5)));

            SsdpBridges = string.Join(", ", result.ToArray());

            if (result.Count() > 0)
            {
                _hueClient = new HueClient(result.First());
            }
        }
Example #7
0
 public List <LocatedBridge> GetBridgesSync()
 {
     try
     {
         IEnumerable <LocatedBridge> bridges = AsyncHelper.RunSync(() => locator.LocateBridgesAsync(new TimeSpan(50)));
         return(bridges.ToList());
     }
     catch
     {
     }
     return(null);
 }
        private async Task <BridgeProperties> DiscoverBridgesOnNetwork()
        {
            logger.LogInformation($"Searching for Hue bridges on network...");
            //FIXME: what if no bridges are found?
            var locatedBridges = (await bridgeLocator.LocateBridgesAsync(TimeSpan.FromSeconds(30))).ToList();
            var locatedBridge  = locatedBridges[0];

            if (locatedBridges.Count() > 1)
            {
                logger.LogWarning($"App does not support more than one bridge.");
            }
            logger.LogInformation($"Hue BridgeID: { locatedBridge.BridgeId,-10} IP: {locatedBridge.IpAddress} found.");
            return(new BridgeProperties
            {
                IpAddress = locatedBridge.IpAddress
            });
        }
        private async Task TestBridgeLocatorWithTimeout(IBridgeLocator locator, TimeSpan timeout)
        {
            var startTime = DateTime.Now;
            var bridgeIPs = await locator.LocateBridgesAsync(timeout);

            var elapsed = DateTime.Now.Subtract(startTime);

            Assert.IsTrue(
                elapsed.Subtract(timeout) < TimeSpan.FromMilliseconds(1000),
                $"Must complete inside the timeout specified ±1s (took {elapsed.TotalMilliseconds}ms)");

            Assert.IsNotNull(bridgeIPs,
                             "Must return list");

            Assert.IsTrue(bridgeIPs.Any(),
                          "Must find bridges");
        }
Example #10
0
        public async Task <bool> Start()
        {
            if (string.IsNullOrEmpty(_config.BridgeIpAddress))
            {
                _logger.LogInformation("Searching from Philip Hue bridge...");

                var bridgeIps = await _bridgeLocator.LocateBridgesAsync(TimeSpan.FromSeconds(10));

                if (bridgeIps.Any())
                {
                    _logger.LogInformation($"Found {bridgeIps.Count()} bridges");

                    _hueClient = new LocalHueClient(bridgeIps.ToList()[0].IpAddress);

                    _config.BridgeIpAddress = bridgeIps.ToList()[0].IpAddress;

                    _logger.LogInformation("Button pressed");
                    var appKey = await _hueClient.RegisterAsync("LeonHomeControl", "Leon");

                    _config.ApiKey = appKey;

                    _componentsService.SaveComponentConfig(_config);

                    _hueClient.Initialize(appKey);

                    _logger.LogInformation("Philip Hue Configured");

                    _hueClient = new LocalHueClient(_config.BridgeIpAddress, _config.ApiKey);

                    _logger.LogInformation("Connected to Philip Hue");

                    var lights = await _hueClient.GetLightsAsync();

                    lights.ToList().ForEach(s => { _logger.LogInformation($"{s.Name}"); });
                    var groups = await _hueClient.GetGroupsAsync();

                    groups.ToList().ForEach(g => { _logger.LogInformation($"{g.Name}"); });
                }
            }
            else
            {
                _hueClient = new LocalHueClient(_config.BridgeIpAddress);

                if (string.IsNullOrEmpty(_config.ApiKey))
                {
                    _userInteractionService.AddUserInteractionData(new UserInteractionData
                    {
                        Name   = "Philip hue component",
                        Fields = new List <UserInteractionField>
                        {
                            new UserInteractionField().Build().SetIsRequired(true)
                            .SetFieldType(UserInteractionFieldTypeEnum.BUTTON)
                            .SetFieldName("PHILIP_HUE_BUTTON_PRESS")
                            .SetDescription("Press Philip hue button for link bride")
                        }
                    }, async data => { });
                }
                else
                {
                    _hueClient = new LocalHueClient(_config.BridgeIpAddress, _config.ApiKey);

                    _logger.LogInformation("Connected to Philip Hue");

                    _schedulerService.AddPolling(UpdateEntities, "PhiipHue_UpdateLightEntities",
                                                 SchedulerServicePollingEnum.NORMAL_POLLING);
                }
            }

            return(true);
        }
Example #11
0
 internal async void FindBridge()
 {
     m_BridgeLocator = new SSDPBridgeLocator();
     m_BridgeIPs = await m_BridgeLocator.LocateBridgesAsync(TimeSpan.FromSeconds(5));
     OnLocateBridgeComplete(new LocateBridgeEventArgs(m_BridgeIPs));
 }