public static void SaveHueAuthXml(HueAuthSuccess hueAuthSuccess)
        {
            System.Xml.Serialization.XmlSerializer writer =
                new System.Xml.Serialization.XmlSerializer(typeof(HueAuthSuccess));

            var path = Directory.GetCurrentDirectory() + "\\Config\\HueAuthentication.xml";

            System.IO.FileStream file = System.IO.File.Create(path);

            writer.Serialize(file, hueAuthSuccess);
            file.Close();
        }
        public static HueAuthSuccess ReadHueAuthXml()
        {
            // Now we can read the serialized book ...
            System.Xml.Serialization.XmlSerializer reader =
                new System.Xml.Serialization.XmlSerializer(typeof(HueAuthSuccess));

            var path = Directory.GetCurrentDirectory() + "\\Config\\HueAuthentication.xml";

            System.IO.StreamReader file    = new System.IO.StreamReader(path);
            HueAuthSuccess         hueAuth = (HueAuthSuccess)reader.Deserialize(file);

            file.Close();
            return(hueAuth);
        }
        static async Task <bool> ConnectToHueBridge()
        {
            bool connected = false;

            string hueIp = String.Empty;

            try {
                hueIp = await GetHueBridgeIpAddress();
            }
            catch {
                WriteLog("Failed to get Hue Bridge Ip Address.");
                return(false);
            }
            List <BridgeIpAddressResponse> bridgeIpAddressResponse = JsonConvert.DeserializeObject <List <BridgeIpAddressResponse> >(hueIp);

            HueBridgeDetails.ip = bridgeIpAddressResponse.First().internalipaddress;
            HueBridgeDetails.id = bridgeIpAddressResponse.First().id;

            // Try Read from config file for Hue Auth User
            try {
                _hueAuthUser = ReadHueAuthXml();
            }
            catch {
            }

            // If _hueAuthUser hasn't been set yet, try to connect
            if (_hueAuthUser == null)
            {
                string hueAuth = String.Empty;
                hueAuth = await GetHueAuthentication();

                if (hueAuth == String.Empty || hueAuth == null)
                {
                    connected = false;
                }
                else if (hueAuth.ToUpper().Contains("ERROR"))
                {
                    List <HueAuthError> hueAuthErrors = JsonConvert.DeserializeObject <List <HueAuthError> >(hueAuth);
                    showBalloon(title: "Hue Authentication Error", body: hueAuthErrors.First().error.description + ". Press Link Button on Hue Bridge and try again.");
                    WriteLog(hueAuthErrors.First().error.description + ". Press Link Button on Hue Bridge and try again.");
                    connected = false;
                }
                else
                {
                    List <HueAuthSuccess> hueAuthSuccesses = JsonConvert.DeserializeObject <List <HueAuthSuccess> >(hueAuth);
                    _hueAuthUser = hueAuthSuccesses.First();
                    SaveHueAuthXml(_hueAuthUser);
                    connected = true;
                }

                return(connected);
            }
            else
            {
                string hueLights = await GetHueLights();

                if (hueLights.ToUpper().Contains("ERROR"))
                {
                    List <HueAuthError> hueAuthErrors = JsonConvert.DeserializeObject <List <HueAuthError> >(hueLights);
                    showBalloon(title: "Hue Authentication Token Error", body: hueAuthErrors.First().error.description + ".");
                    WriteLog("Hue Authentication Token Error:" + hueAuthErrors.First().error.description + ". Delete Authentication configuration file and try again or update Authentication token.");
                    return(false);
                }
                else
                {
                    _hueBridgeLights = JsonConvert.DeserializeObject <Dictionary <string, HueLightsDto> >(hueLights);
                    _hueUserLights   = ReadHueUserLightsXml();
                    return(true);
                }
            }
        }