async void SearchBridges()
        {
            foundHueBridges.Clear();
            foundHueLights.Clear();
            connectedBridge = null;
            selectedLight   = null;

            UpdateUIGroups();
            FireUIUpdated();

            //search bridges at https://www.meethue.com/api/nupnp
            //IBridgeLocator locator = new HttpBridgeLocator();
            //scans network using multicast SSDP packets
            IBridgeLocator locator = new SSDPBridgeLocator();

            IEnumerable <Q42.HueApi.Models.Bridge.LocatedBridge> locatedBridges = await locator.LocateBridgesAsync(TimeSpan.FromSeconds(5));

            foreach (var locatedBridge in locatedBridges)
            {
                foundHueBridges.Add(new HueBridge(locatedBridge.BridgeId, locatedBridge.BridgeId, locatedBridge.IpAddress));
            }

            string message = "Found Hue bridges count: " + locatedBridges.Count();

            LogMessage(message, null);
            ShowHubToast(message, hueLogoImage, 2);

            UpdateUIGroups();
            FireUIUpdated();
        }
        async void ChangeLightColor(HueLight light, string color)
        {
            var command = new LightCommand();

            command.TurnOn().SetColor(new RGBColor(color));

            SendCommandToClient(command, light.getLightId());
        }
        async void DecreaseBrightness(HueLight light)
        {
            var command = new LightCommand();

            command.BrightnessIncrement = -40;

            SendCommandToClient(command, light.getLightId());
        }
        async void TurnOffLight(HueLight light)
        {
            var command = new LightCommand();

            command.On = false;

            SendCommandToClient(command, light.getLightId());
        }
        void StartColorLoop(HueLight light)
        {
            var command = new LightCommand();

            command.Effect = Effect.ColorLoop;

            SendCommandToClient(command, light.getLightId());
        }
        async void ConnectToBridge(HueBridge bridge)
        {
            client = new LocalHueClient(bridge.getIp());

            try
            {
                appKey = await client.RegisterAsync(appName, deviceName); //Save the app key for later use

                client.Initialize(appKey);
            } catch (System.Exception exp)
            {
                HubText = "ERROR: " + exp.Message;
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                client = null;
            }

            //client = await RegisterApplication(bridge.getIp());

            if (client != null)
            {
                connectedBridge = bridge;

                GetLights();
            }
            else
            {
                connectedBridge = null;
                selectedLight   = null;
                foundHueLights.Clear();
            }

            UpdateUIGroups();
            FireUIUpdated();
        }
        public override void UIElementEvent(UIEventArgs e)
        {
            LogMessage("Plugin Received UI Event: " + e.ElementId.ToString(), null);
            Guid receivedGuid = e.ElementId;

            ButtonPressAction buttonPressAction = ButtonPressActionDictionary[receivedGuid];

            if (buttonPressAction.action == ActionEnum.SEARCH_BRIDGES)
            {
                HubText = "Searching Bridges...";
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                connectedBridge = null;
                selectedLight   = null;

                SearchBridges();
            }
            else if (buttonPressAction.action == ActionEnum.CONNECT_BRIDGE)
            {
                string bridgeId = buttonPressAction.value;

                HubText = "Connecting to Bridge: " + bridgeId;
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                HueBridge bridge = foundHueBridges.First(item => item.getBridgeId() == bridgeId);

                ConnectToBridge(bridge);
            }
            else if (buttonPressAction.action == ActionEnum.SELECT_LIGHT)
            {
                string lightId = buttonPressAction.value;

                HubText = "Selected Light: " + lightId;
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                selectedLight = foundHueLights.First(item => item.getLightId() == lightId);
            }
            else if (buttonPressAction.action == ActionEnum.LIGHT_ON)
            {
                HubText = "Turning on Light: " + selectedLight.getLightId();
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                TurnOnLight(selectedLight);
            }
            else if (buttonPressAction.action == ActionEnum.LIGHT_OFF)
            {
                HubText = "Turning off Light: " + selectedLight.getLightId();
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                TurnOffLight(selectedLight);
            }
            else if (buttonPressAction.action == ActionEnum.LIGHT_INCREASE)
            {
                HubText = "Increasing Brightness of Light: " + selectedLight.getLightId();
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                IncreaseBrightness(selectedLight);
            }
            else if (buttonPressAction.action == ActionEnum.LIGHT_DECREASE)
            {
                HubText = "Decreasing Brightness of Light: : " + selectedLight.getLightId();
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                DecreaseBrightness(selectedLight);
            }
            else if (buttonPressAction.action == ActionEnum.LIGHT_CHANGE_COLOR)
            {
                HubText = "Changing Color of Light " + selectedLight.getLightId() + " to " + buttonPressAction.value;
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                string color = buttonPressAction.value;

                ChangeLightColor(selectedLight, color);
            }
            else if (buttonPressAction.action == ActionEnum.LIGHT_COLOR_LOOP)
            {
                HubText = "Starting Color Loop on Light " + selectedLight.getLightId();
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage(HubText, null);

                StartColorLoop(selectedLight);
            }
            else
            {
                HubText = "Invalid button press action";
                ShowHubToast(HubText, hueLogoImage, 2);
                LogMessage("Invalid button press action requested: " + buttonPressAction.action, null);
            }

            UpdateUIGroups();

            FireHubTextUpdated();
            FireUIUpdated();
        }