SystemConfigViewModel.cs 9.4 KB

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