public void CanSerializeAndDeserializeModel()
        {
            var pattern = new PatternModel
            {
                DelayInMinutes = 30,
                Force          = true,
                Name           = "SD",
                Operation      = ShutDownOperation.ShutDown
            };
            string s            = pattern.ToSerializableString();
            var    deserialized = PatternModel.Parse(s);

            Assert.AreEqual(pattern.Description, deserialized.Description);
        }
Exemple #2
0
 public PatternModel[] GetPatterns()
 {
     try
     {
         string         text        = File.ReadAllText(_filePath);
         string[]       lines       = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
         PatternModel[] returnValue = new PatternModel[lines.Length];
         for (int i = 0; i < lines.Length; i++)
         {
             returnValue[i] = PatternModel.Parse(lines[i]);
         }
         return(returnValue);
     }
     catch (Exception ex)
     {
         Log.LogErrorAndDisplayMessageBox("Failed to retrieve patterns", ex);
         return(new PatternModel[0]);
     }
 }
Exemple #3
0
        private static void LoadInstance()
        {
            try
            {
                string[] lines = File.ReadAllLines(_filePath)
                                 .Select(x => x.Trim())
                                 .Where(x => x.Length > 0)
                                 .ToArray();

                Instance = new Settings();
                bool blinkIsSet = false;

                foreach (var line in lines)
                {
                    if (line.StartsWith($"{nameof(MinMinutes)}:"))
                    {
                        Instance.MinMinutes = int.Parse(line.Split(':')[1]);
                    }
                    else if (line.StartsWith($"{nameof(MaxMinutes)}:"))
                    {
                        Instance.MaxMinutes = int.Parse(line.Split(':')[1]);
                    }
                    else if (line.StartsWith($"{nameof(DefaultDelay)}:"))
                    {
                        Instance.DefaultDelay = int.Parse(line.Split(':')[1]);
                    }
                    else if (line.StartsWith($"{nameof(DefaultForce)}:"))
                    {
                        Instance.DefaultForce = bool.Parse(line.Split(':')[1]);
                    }
                    else if (line.StartsWith($"{nameof(DefaultOperation)}:"))
                    {
                        Instance.DefaultOperation = (ShutDownOperation)Enum.Parse(typeof(ShutDownOperation), line.Split(':')[1]);
                    }
                    else if (line.StartsWith($"{nameof(CloseToTray)}:"))
                    {
                        Instance.CloseToTray = bool.Parse(line.Split(':')[1]);
                    }
                    else if (line.StartsWith($"{nameof(BlinkTrayIcon)}:"))
                    {
                        Instance.BlinkTrayIcon = bool.Parse(line.Split(':')[1]);
                        blinkIsSet             = true;
                    }
                    else if (line.StartsWith($"{nameof(PreventShutDown)}:"))
                    {
                        Instance.PreventShutDown = bool.Parse(line.Split(':')[1]);
                    }
                    else if (line.StartsWith("p:"))
                    {
                        Instance.Patterns.Add(PatternModel.Parse(line));
                    }
                }

                if (!blinkIsSet)
                {
                    Instance.BlinkTrayIcon = true;
                    Instance.Save();
                }
            }
            catch
            {
                Instance = DefaultSettings();
            }
        }