using Aitex.Core.Util; using Aitex.Sorter.Common; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Linq; using System.Text; using System.Xml.Linq; namespace Aitex.Sorter.UI.Config { public class SignalTowerManager : Singleton { public IEnumerable Load(string fileName) { if (File.Exists(fileName)) { var doc = XDocument.Load(fileName); var result = doc.Root.Elements("Event").Select(x => new SignalTowerEventItem() { Name = x.Attribute("Name").Value, RedAction = (SignalTowerAction)Enum.Parse(typeof(SignalTowerAction), x.Attribute("Red").Value), BlueAction = (SignalTowerAction)Enum.Parse(typeof(SignalTowerAction), x.Attribute("Blue").Value), GreenAction = (SignalTowerAction)Enum.Parse(typeof(SignalTowerAction), x.Attribute("Green").Value), BuzzerAction = (SignalTowerAction)Enum.Parse(typeof(SignalTowerAction), x.Attribute("Red").Value), }); return result; } return null; } public void Save(IEnumerable events, string fileName) { var result = events.Select(x => new XElement("Event", new XAttribute("Name", x.Name), new XAttribute("Red", x.RedAction), new XAttribute("Blue", x.BlueAction), new XAttribute("Green", x.GreenAction), new XAttribute("Buzzer", x.BuzzerAction) )); var doc = new XDocument(new XElement("Events", result)); doc.Save(fileName); } } public class SignalTowerEventItem : NotifyPropertyChangedBase { private string name; public string Name { get { return name; } set { name = value; OnPropertyChanged("Name"); } } private SignalTowerAction redAction; public SignalTowerAction RedAction { get { return redAction; } set { redAction = value; OnPropertyChanged("RedAction"); } } private SignalTowerAction blueAction; public SignalTowerAction BlueAction { get { return blueAction; } set { blueAction = value; OnPropertyChanged("BlueAction"); } } private SignalTowerAction greenAction; public SignalTowerAction GreenAction { get { return greenAction; } set { greenAction = value; OnPropertyChanged("GreenAction"); } } private SignalTowerAction buzzerAction; public SignalTowerAction BuzzerAction { get { return buzzerAction; } set { buzzerAction = value; OnPropertyChanged("BuzzerAction"); } } } public enum SignalTowerAction { Off, On, Blink, } }