SystemConfigViewModel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Aitex.Core.RT.Log;
  5. using ExcelLibrary.SpreadSheet;
  6. using MECF.Framework.Common.OperationCenter;
  7. using OpenSEMI.ClientBase;
  8. using OpenSEMI.ClientBase.Command;
  9. namespace VirgoUI.Client.Models.Utility.SystemConfig
  10. {
  11. public class SystemConfigViewModel : BaseModel
  12. {
  13. private int MenuPermission;
  14. #region Properties
  15. private List<ConfigNode> _ConfigNodes = new List<ConfigNode>();
  16. public List<ConfigNode> ConfigNodes
  17. {
  18. get { return _ConfigNodes; }
  19. set { _ConfigNodes = value; NotifyOfPropertyChange("ConfigNodes"); }
  20. }
  21. private List<ConfigItem> _configItems = null;
  22. public List<ConfigItem> ConfigItems
  23. {
  24. get { return _configItems; }
  25. set { _configItems = value; NotifyOfPropertyChange("ConfigItems"); }
  26. }
  27. string _CurrentNodeName = string.Empty;
  28. public string CurrentNodeName
  29. {
  30. get
  31. {
  32. return _CurrentNodeName;
  33. }
  34. set
  35. {
  36. _CurrentNodeName = value;
  37. NotifyOfPropertyChange("CurrentNodeName");
  38. }
  39. }
  40. public BaseCommand<ConfigNode> TreeViewSelectedItemChangedCmd { private set; get; }
  41. #endregion
  42. #region Functions
  43. public SystemConfigViewModel()
  44. {
  45. this.DisplayName = "System Config";
  46. TreeViewSelectedItemChangedCmd = new BaseCommand<ConfigNode>(TreeViewSelectedItemChanged);
  47. }
  48. protected override void OnInitialize()
  49. {
  50. MenuPermission = ClientApp.Instance.GetPermission("Config");
  51. base.OnInitialize();
  52. ConfigNodes = SystemConfigProvider.Instance.GetConfigTree().SubNodes;
  53. }
  54. private void TreeViewSelectedItemChanged(ConfigNode node)
  55. {
  56. CurrentNodeName = string.IsNullOrEmpty(node.Path) ? node.Name : $"{node.Path}.{node.Name}";
  57. ConfigItems = node.Items;
  58. GetDataOfConfigItems();
  59. }
  60. private void GetDataOfConfigItems()
  61. {
  62. if (ConfigItems == null)
  63. return;
  64. for (int i = 0; i < ConfigItems.Count; i++)
  65. {
  66. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", ConfigItems[i].Name);
  67. ConfigItems[i].CurrentValue = SystemConfigProvider.Instance.GetValueByName(key);
  68. if (ConfigItems[i].Type == DataType.Bool)
  69. {
  70. bool value;
  71. if (bool.TryParse(ConfigItems[i].CurrentValue, out value))
  72. ConfigItems[i].BoolValue = value;
  73. }
  74. else
  75. ConfigItems[i].StringValue = ConfigItems[i].CurrentValue;
  76. }
  77. }
  78. public void SetValue(ConfigItem item)
  79. {
  80. if (MenuPermission != 3) return;
  81. //key :System.IsSimulatorMode
  82. //value: true or false 都是字符串
  83. //input check
  84. string value;
  85. if (item.Type == DataType.Bool)
  86. {
  87. value = item.BoolValue.ToString().ToLower();
  88. }
  89. else
  90. {
  91. if (item.TextSaved)
  92. return;
  93. if (item.Type == DataType.Int)
  94. {
  95. int iValue;
  96. if (int.TryParse(item.StringValue, out iValue))
  97. {
  98. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  99. {
  100. if (iValue > item.Max || iValue < item.Min)
  101. {
  102. DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", ((int)item.Min).ToString(), ((int)item.Max).ToString()));
  103. return;
  104. }
  105. }
  106. }
  107. else
  108. {
  109. DialogBox.ShowWarning("Please input valid data.");
  110. return;
  111. }
  112. value = item.StringValue;
  113. }
  114. else if (item.Type == DataType.Double)
  115. {
  116. double fValue;
  117. if (double.TryParse(item.StringValue, out fValue))
  118. {
  119. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  120. {
  121. if (fValue > item.Max || fValue < item.Min)
  122. {
  123. DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", item.Min.ToString(), item.Max.ToString()));
  124. return;
  125. }
  126. }
  127. }
  128. else
  129. {
  130. DialogBox.ShowWarning("Please input valid data.");
  131. return;
  132. }
  133. value = item.StringValue;
  134. }
  135. else
  136. value = item.StringValue;
  137. }
  138. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", item.Name);
  139. InvokeClient.Instance.Service.DoOperation("System.SetConfig", key, value);
  140. item.TextSaved = true;
  141. Reload();
  142. }
  143. public void Reload()
  144. {
  145. if (MenuPermission != 3) return;
  146. GetDataOfConfigItems();
  147. }
  148. public void SaveAll()
  149. {
  150. if (MenuPermission != 3) return;
  151. if (ConfigItems == null)
  152. return;
  153. ConfigItems.ForEach(item => SetValue(item));
  154. }
  155. public void ExportData()
  156. {
  157. if (MenuPermission != 3) return;
  158. try
  159. {
  160. if (ConfigNodes == null || ConfigNodes.Count == 0) return;
  161. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  162. dlg.DefaultExt = ".xls"; // Default file extension
  163. dlg.Filter = "数据表格文件|*.xls"; // Filter files by extension
  164. dlg.FileName = string.Format("ConfigList{0}", DateTime.Now.ToString("yyyyMMdd"));
  165. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  166. if (result != true) // Process open file dialog box results
  167. return;
  168. if (File.Exists(dlg.FileName))
  169. {
  170. File.Delete(dlg.FileName);
  171. }
  172. Workbook workbook = new Workbook();
  173. Worksheet worksheet = new Worksheet(DateTime.Now.ToString("yyyyMMdd"));
  174. int colCount = 1;
  175. worksheet.Cells[0, 1] = new Cell("NameView");
  176. worksheet.Cells[0, 2] = new Cell("Unit");
  177. worksheet.Cells[0, 3] = new Cell("Min");
  178. worksheet.Cells[0, 4] = new Cell("Max");
  179. worksheet.Cells[0, 5] = new Cell("DefaultValue");
  180. worksheet.Cells[0, 6] = new Cell("CurrentValue");
  181. for (int i = 0; i < ConfigNodes.Count; i++)
  182. {
  183. for (int j = 0; j < ConfigNodes[i].Items.Count; j++)
  184. {
  185. worksheet.Cells[colCount, 0] = new Cell(ConfigNodes[i].NameView);
  186. worksheet.Cells[colCount, 1] = new Cell(ConfigNodes[i].Items[j].NameView);
  187. worksheet.Cells[colCount, 2] = new Cell(ConfigNodes[i].Items[j].Unit);
  188. worksheet.Cells[colCount, 3] = new Cell(ConfigNodes[i].Items[j].Min);
  189. worksheet.Cells[colCount, 4] = new Cell(ConfigNodes[i].Items[j].Max);
  190. worksheet.Cells[colCount, 5] = new Cell(ConfigNodes[i].Items[j].DefaultValue);
  191. worksheet.Cells[colCount, 6] = new Cell(SystemConfigProvider.Instance.GetValueByName(String.Format("{0}{1}{2}", ConfigNodes[i].Name, ".", ConfigNodes[i].Items[j].Name)));
  192. colCount++;
  193. }
  194. if (ConfigNodes[i].SubNodes.Count != 0)
  195. {
  196. for (int k = 0; k < ConfigNodes[i].SubNodes.Count; k++)
  197. {
  198. for (int l = 0; l < ConfigNodes[i].SubNodes[k].Items.Count; l++)
  199. {
  200. worksheet.Cells[colCount, 0] = new Cell(ConfigNodes[i].SubNodes[k].NameView);
  201. worksheet.Cells[colCount, 1] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].NameView);
  202. worksheet.Cells[colCount, 2] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].Unit);
  203. worksheet.Cells[colCount, 3] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].Min);
  204. worksheet.Cells[colCount, 4] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].Max);
  205. worksheet.Cells[colCount, 5] = new Cell(ConfigNodes[i].SubNodes[k].Items[l].DefaultValue);
  206. worksheet.Cells[colCount, 6] = new Cell(SystemConfigProvider.Instance.GetValueByName(String.Format("{0}{1}{2}{3}{4}", ConfigNodes[i].Name, ".", ConfigNodes[i].SubNodes[k].Name, ".", ConfigNodes[i].SubNodes[k].Items[l].Name)));
  207. colCount++;
  208. }
  209. }
  210. }
  211. }
  212. workbook.Worksheets.Add(worksheet);
  213. workbook.Save(dlg.FileName);
  214. }
  215. catch (Exception ex)
  216. {
  217. LOG.Error(ex.Message, ex);
  218. }
  219. }
  220. #endregion
  221. }
  222. }