using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Forms; using System.Windows.Input; using System.Xml; using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using Aitex.Core.UI.MVVM; using Aitex.Core.Utilities; using Aitex.Sorter.Common; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.OperationCenter; using MessageBox = System.Windows.MessageBox; namespace Aitex.Sorter.UI.ViewModel { public class SystemConfigViewModel : UIViewModelBase { [IgnorePropertyChange] public SCValue ConfigFeedback { get; set; } [IgnorePropertyChange] public SCValue ConfigSetPoint { get; set; } [IgnorePropertyChange] public ICommand SetConfigCommand { get; private set; } [IgnorePropertyChange] public ICommand SetConfig2Command { get; private set; } [IgnorePropertyChange] public ICommand SaveAsConfigCommand { get; private set; } [IgnorePropertyChange] public ICommand LoadConfigCommand { get; private set; } [IgnorePropertyChange] public ICommand SetAllConfigCommand { get; private set; } public SystemConfigViewModel() : base("SystemConfigViewModel") { SetConfigCommand = new DelegateCommand(SetConfig); SetConfig2Command = new DelegateCommand(SetConfig2); SaveAsConfigCommand = new DelegateCommand(SaveAsConfig); LoadConfigCommand = new DelegateCommand(LoadConfig); SetAllConfigCommand = new DelegateCommand(SetAllConfig); ConfigFeedback = new SCValue(); ConfigSetPoint = new SCValue(); ConfigFeedback.RetrieveAll(); ConfigSetPoint.RetrieveAll(); } void SetConfig(object param) { if (!PerformInvokeFunctionCheck(new string[] { "Config", "Set" })) return; object[] sc = (object[])param ; InvokeClient.Instance.Service.DoOperation("System.SetConfig", sc[0].ToString(), sc[1].ToString()); UpdateConfig(); } void SetConfig2(object param) { string stringParam = param.ToString(); string[] values = stringParam.Split(';'); foreach (var value in values) { if (string.IsNullOrEmpty(value)) continue; string[] props = value.Split('.'); if (props.Length != 3) { LOG.Error("Error SetConfig parameter" + param); continue; } string scName = props[1] + "_" + props[2]; object scValue = null; PropertyInfo[] property = typeof(SCValue).GetProperties(); foreach (PropertyInfo fiGroup in property) { if (fiGroup.Name == props[1]) { object objGroup = fiGroup.GetValue(ConfigSetPoint, null); foreach (PropertyInfo fiItem in objGroup.GetType().GetProperties()) { if (fiItem.Name == props[2]) { scValue = fiItem.GetValue(objGroup, null); InvokeClient.Instance.Service.DoOperation(OperationName.SetConfig , scName, scValue); break; } } break; } } } UpdateConfig(); } private void SaveAsConfig(object param) { SaveFileDialog sfd = new SaveFileDialog(); sfd.DefaultExt = ".cfg"; sfd.Filter = ".cfg|*.*"; sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); sfd.FileName = string.Format("process_config_{0}.cfg", DateTime.Now.ToString("yyyyMMdd_HHmmssfff")); if (sfd.ShowDialog() != DialogResult.OK) return; XmlDocument xml = new XmlDocument(); xml.LoadXml(@""); Dictionary items = ConfigFeedback.GetValue(); foreach (var item in items) { CreateNode(xml, item.Key, item.Value.ToString()); } try { xml.Save(sfd.FileName); } catch (Exception ex) { MessageBox.Show(ex.Message, "Save Error", MessageBoxButton.OK, MessageBoxImage.Warning); } } void CreateNode(XmlDocument xmlDoc, string name, string value) { XmlNode node = xmlDoc.CreateElement("ProcessConfig"); XmlAttribute attrName = xmlDoc.CreateAttribute("Name"); attrName.Value = name; node.Attributes.Append(attrName); XmlAttribute attrValue = xmlDoc.CreateAttribute("Value"); attrValue.Value = value; node.Attributes.Append(attrValue); xmlDoc.SelectSingleNode("Aitex/ProcessConfigs").AppendChild(node); } private void LoadConfig(object param) { OpenFileDialog ofd = new OpenFileDialog(); ofd.DefaultExt = ".cfg"; ofd.Filter = ".cfg|*.*"; ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); if (ofd.ShowDialog() != DialogResult.OK) return; XmlDocument xml = new XmlDocument(); try { xml.Load(ofd.FileName); Dictionary items = ConfigSetPoint.GetValue(); foreach (var item in items) { ConfigSetPoint.Update(item.Key, GetNodeValue(xml, item.Key, item.Value.ToString())); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Open Failed", MessageBoxButton.OK, MessageBoxImage.Warning); return; } InvokeAllPropertyChanged(); } string GetNodeValue(XmlDocument xmlDoc, string configItem, string defaultValue) { try { XmlNode node = xmlDoc.SelectSingleNode(string.Format("Aitex/ProcessConfigs/ProcessConfig[@Name='{0}']", configItem)); if (node != null) { return node.Attributes["Value"].InnerText; } } catch (Exception ex) { LOG.Write(ex); } return defaultValue; } private void SetAllConfig(object param) { Dictionary items = ConfigSetPoint.GetValue(); foreach (var item in items) { InvokeClient.Instance.Service.DoOperation(OperationName.SetConfig.ToString(), item.Key, item.Value.ToString()); } UpdateConfig(); } public void UpdateConfig() { ConfigFeedback.Update(QueryDataClient.Instance.Service.PollConfig(ConfigFeedback.GetKeys())); ConfigSetPoint.Update(QueryDataClient.Instance.Service.PollConfig(ConfigSetPoint.GetKeys())); InvokeAllPropertyChanged(); } } }