123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- 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<object>(SetConfig);
- SetConfig2Command = new DelegateCommand<object>(SetConfig2);
- SaveAsConfigCommand = new DelegateCommand<object>(SaveAsConfig);
- LoadConfigCommand = new DelegateCommand<object>(LoadConfig);
- SetAllConfigCommand = new DelegateCommand<object>(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(@"<?xml version='1.0' encoding='utf-8'?><Aitex><ProcessConfigs></ProcessConfigs></Aitex>");
- Dictionary<string, object> 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<string, object> 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<string, object> 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();
- }
- }
- }
|