Пример #1
0
        /// <summary>
        /// Save Bridge to file.
        /// </summary>
        /// <returns>True or false on success</returns>
        public static bool SaveBridges()
        {
            bool ret = false;

            try
            {
                string result = JsonConvert.SerializeObject(bridges, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented
                });
                log.Debug($@"Saving bridge settings : {bridges}");
                string filepath = Path.Combine(path, "WinHue\\WinHueBridges.set");
                if (File.Exists(filepath))
                {
                    log.Info("Backuping WinHueBridge.set file to WinHueBridge.set.bak");
                    File.Copy(filepath, filepath + ".bak", true);
                }

                if (CreateWinHueDirectory())
                {
                    File.WriteAllText(filepath, result);
                    ret = true;
                }
            }
            catch (Exception ex)
            {
                bridges = new CustomBridges();
                ret     = false;
                log.Error("Error while saving the bridge settings.", ex);
            }
            return(ret);
        }
Пример #2
0
        /// <summary>
        /// Load bridge from file.
        /// </summary>
        /// <returns></returns>
        public static bool LoadBridges()
        {
            bool result = false;

            try
            {
                string filepath = Path.Combine(path, "WinHue\\WinHueBridges.set");
                if (!File.Exists(filepath))
                {
                    return(result);
                }
                log.Debug("Trying to open bridge settings file...");
                StreamReader sr = File.OpenText(filepath);
                log.Debug("File open.");
                string settingsString = sr.ReadToEnd();
                log.Debug($@"Loading bridge settings : {settingsString}");
                sr.Close();
                log.Debug("Deserializing the settings file.");
                bridges = JsonConvert.DeserializeObject <CustomBridges>(settingsString, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore
                });
                result = true;
            }
            catch (Exception ex)
            {
                settings = new CustomSettings();
                result   = false;
                log.Error("Error while loading the bridge settings.", ex);
            }
            return(result);
        }