SystemConfigViewModel.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Windows;
  7. using System.Windows.Forms;
  8. using System.Windows.Input;
  9. using System.Xml;
  10. using Aitex.Core.RT.Log;
  11. using Aitex.Core.RT.SCCore;
  12. using Aitex.Core.UI.MVVM;
  13. using Aitex.Core.Utilities;
  14. using Aitex.Sorter.Common;
  15. using MECF.Framework.Common.DataCenter;
  16. using MECF.Framework.Common.OperationCenter;
  17. using MessageBox = System.Windows.MessageBox;
  18. namespace Aitex.Sorter.UI.ViewModel
  19. {
  20. public class SystemConfigViewModel : UIViewModelBase
  21. {
  22. [IgnorePropertyChange]
  23. public SCValue ConfigFeedback
  24. {
  25. get;
  26. set;
  27. }
  28. [IgnorePropertyChange]
  29. public SCValue ConfigSetPoint
  30. {
  31. get;
  32. set;
  33. }
  34. [IgnorePropertyChange]
  35. public ICommand SetConfigCommand
  36. {
  37. get;
  38. private set;
  39. }
  40. [IgnorePropertyChange]
  41. public ICommand SetConfig2Command
  42. {
  43. get;
  44. private set;
  45. }
  46. [IgnorePropertyChange]
  47. public ICommand SaveAsConfigCommand
  48. {
  49. get;
  50. private set;
  51. }
  52. [IgnorePropertyChange]
  53. public ICommand LoadConfigCommand
  54. {
  55. get;
  56. private set;
  57. }
  58. [IgnorePropertyChange]
  59. public ICommand SetAllConfigCommand
  60. {
  61. get;
  62. private set;
  63. }
  64. public SystemConfigViewModel()
  65. : base("SystemConfigViewModel")
  66. {
  67. SetConfigCommand = new DelegateCommand<object>(SetConfig);
  68. SetConfig2Command = new DelegateCommand<object>(SetConfig2);
  69. SaveAsConfigCommand = new DelegateCommand<object>(SaveAsConfig);
  70. LoadConfigCommand = new DelegateCommand<object>(LoadConfig);
  71. SetAllConfigCommand = new DelegateCommand<object>(SetAllConfig);
  72. ConfigFeedback = new SCValue();
  73. ConfigSetPoint = new SCValue();
  74. ConfigFeedback.RetrieveAll();
  75. ConfigSetPoint.RetrieveAll();
  76. }
  77. void SetConfig(object param)
  78. {
  79. if (!PerformInvokeFunctionCheck(new string[] { "Config", "Set" })) return;
  80. object[] sc = (object[])param ;
  81. InvokeClient.Instance.Service.DoOperation("System.SetConfig", sc[0].ToString(), sc[1].ToString());
  82. UpdateConfig();
  83. }
  84. void SetConfig2(object param)
  85. {
  86. string stringParam = param.ToString();
  87. string[] values = stringParam.Split(';');
  88. foreach (var value in values)
  89. {
  90. if (string.IsNullOrEmpty(value))
  91. continue;
  92. string[] props = value.Split('.');
  93. if (props.Length != 3)
  94. {
  95. LOG.Error("Error SetConfig parameter" + param);
  96. continue;
  97. }
  98. string scName = props[1] + "_" + props[2];
  99. object scValue = null;
  100. PropertyInfo[] property = typeof(SCValue).GetProperties();
  101. foreach (PropertyInfo fiGroup in property)
  102. {
  103. if (fiGroup.Name == props[1])
  104. {
  105. object objGroup = fiGroup.GetValue(ConfigSetPoint, null);
  106. foreach (PropertyInfo fiItem in objGroup.GetType().GetProperties())
  107. {
  108. if (fiItem.Name == props[2])
  109. {
  110. scValue = fiItem.GetValue(objGroup, null);
  111. InvokeClient.Instance.Service.DoOperation(OperationName.SetConfig , scName, scValue);
  112. break;
  113. }
  114. }
  115. break;
  116. }
  117. }
  118. }
  119. UpdateConfig();
  120. }
  121. private void SaveAsConfig(object param)
  122. {
  123. SaveFileDialog sfd = new SaveFileDialog();
  124. sfd.DefaultExt = ".cfg";
  125. sfd.Filter = ".cfg|*.*";
  126. sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  127. sfd.FileName = string.Format("process_config_{0}.cfg", DateTime.Now.ToString("yyyyMMdd_HHmmssfff"));
  128. if (sfd.ShowDialog() != DialogResult.OK)
  129. return;
  130. XmlDocument xml = new XmlDocument();
  131. xml.LoadXml(@"<?xml version='1.0' encoding='utf-8'?><Aitex><ProcessConfigs></ProcessConfigs></Aitex>");
  132. Dictionary<string, object> items = ConfigFeedback.GetValue();
  133. foreach (var item in items)
  134. {
  135. CreateNode(xml, item.Key, item.Value.ToString());
  136. }
  137. try
  138. {
  139. xml.Save(sfd.FileName);
  140. }
  141. catch (Exception ex)
  142. {
  143. MessageBox.Show(ex.Message, "Save Error", MessageBoxButton.OK, MessageBoxImage.Warning);
  144. }
  145. }
  146. void CreateNode(XmlDocument xmlDoc, string name, string value)
  147. {
  148. XmlNode node = xmlDoc.CreateElement("ProcessConfig");
  149. XmlAttribute attrName = xmlDoc.CreateAttribute("Name");
  150. attrName.Value = name;
  151. node.Attributes.Append(attrName);
  152. XmlAttribute attrValue = xmlDoc.CreateAttribute("Value");
  153. attrValue.Value = value;
  154. node.Attributes.Append(attrValue);
  155. xmlDoc.SelectSingleNode("Aitex/ProcessConfigs").AppendChild(node);
  156. }
  157. private void LoadConfig(object param)
  158. {
  159. OpenFileDialog ofd = new OpenFileDialog();
  160. ofd.DefaultExt = ".cfg";
  161. ofd.Filter = ".cfg|*.*";
  162. ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
  163. if (ofd.ShowDialog() != DialogResult.OK)
  164. return;
  165. XmlDocument xml = new XmlDocument();
  166. try
  167. {
  168. xml.Load(ofd.FileName);
  169. Dictionary<string, object> items = ConfigSetPoint.GetValue();
  170. foreach (var item in items)
  171. {
  172. ConfigSetPoint.Update(item.Key, GetNodeValue(xml, item.Key, item.Value.ToString()));
  173. }
  174. }
  175. catch (Exception ex)
  176. {
  177. MessageBox.Show(ex.Message, "Open Failed", MessageBoxButton.OK, MessageBoxImage.Warning);
  178. return;
  179. }
  180. InvokeAllPropertyChanged();
  181. }
  182. string GetNodeValue(XmlDocument xmlDoc, string configItem, string defaultValue)
  183. {
  184. try
  185. {
  186. XmlNode node = xmlDoc.SelectSingleNode(string.Format("Aitex/ProcessConfigs/ProcessConfig[@Name='{0}']", configItem));
  187. if (node != null)
  188. {
  189. return node.Attributes["Value"].InnerText;
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. LOG.Write(ex);
  195. }
  196. return defaultValue;
  197. }
  198. private void SetAllConfig(object param)
  199. {
  200. Dictionary<string, object> items = ConfigSetPoint.GetValue();
  201. foreach (var item in items)
  202. {
  203. InvokeClient.Instance.Service.DoOperation(OperationName.SetConfig.ToString(), item.Key, item.Value.ToString());
  204. }
  205. UpdateConfig();
  206. }
  207. public void UpdateConfig()
  208. {
  209. ConfigFeedback.Update(QueryDataClient.Instance.Service.PollConfig(ConfigFeedback.GetKeys()));
  210. ConfigSetPoint.Update(QueryDataClient.Instance.Service.PollConfig(ConfigSetPoint.GetKeys()));
  211. InvokeAllPropertyChanged();
  212. }
  213. }
  214. }