SystemConfigViewModel.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using MECF.Framework.Common.OperationCenter;
  4. using OpenSEMI.ClientBase;
  5. using OpenSEMI.ClientBase.Command;
  6. namespace Virgo_DUI.Client.Models.Utility.SystemConfig
  7. {
  8. public class SystemConfigViewModel : BaseModel
  9. {
  10. #region Properties
  11. private List<ConfigNode> _ConfigNodes = new List<ConfigNode>();
  12. public List<ConfigNode> ConfigNodes
  13. {
  14. get { return _ConfigNodes; }
  15. set { _ConfigNodes = value; NotifyOfPropertyChange("ConfigNodes"); }
  16. }
  17. private List<ConfigItem> _configItems = null;
  18. public List<ConfigItem> ConfigItems
  19. {
  20. get { return _configItems; }
  21. set { _configItems = value; NotifyOfPropertyChange("ConfigItems"); }
  22. }
  23. string _CurrentNodeName = string.Empty;
  24. public BaseCommand<ConfigNode> TreeViewSelectedItemChangedCmd { private set; get; }
  25. #endregion
  26. #region Functions
  27. public SystemConfigViewModel()
  28. {
  29. this.DisplayName = "System Config";
  30. TreeViewSelectedItemChangedCmd = new BaseCommand<ConfigNode>(TreeViewSelectedItemChanged);
  31. }
  32. protected override void OnInitialize()
  33. {
  34. base.OnInitialize();
  35. ConfigNodes = SystemConfigProvider.Instance.GetConfigTree().SubNodes;
  36. }
  37. private void TreeViewSelectedItemChanged(ConfigNode node)
  38. {
  39. _CurrentNodeName = string.IsNullOrEmpty(node.Path) ? node.Name : $"{node.Path}.{node.Name}";
  40. ConfigItems = node.Items;
  41. GetDataOfConfigItems();
  42. }
  43. private void GetDataOfConfigItems()
  44. {
  45. if (ConfigItems == null)
  46. return;
  47. for (int i = 0; i < ConfigItems.Count; i++)
  48. {
  49. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", ConfigItems[i].Name);
  50. ConfigItems[i].CurrentValue = SystemConfigProvider.Instance.GetValueByName(key);
  51. if (ConfigItems[i].Type == DataType.Bool)
  52. {
  53. bool value;
  54. if (bool.TryParse(ConfigItems[i].CurrentValue, out value))
  55. ConfigItems[i].BoolValue = value;
  56. }
  57. else
  58. ConfigItems[i].StringValue = ConfigItems[i].CurrentValue;
  59. }
  60. }
  61. public void SetValue(ConfigItem item)
  62. {
  63. //key :System.IsSimulatorMode
  64. //value: true or false 都是字符串
  65. //input check
  66. string value;
  67. if (item.Type == DataType.Bool)
  68. {
  69. value = item.BoolValue.ToString().ToLower();
  70. }
  71. else
  72. {
  73. if (item.TextSaved)
  74. return;
  75. if (item.Type == DataType.Int)
  76. {
  77. int iValue;
  78. if (int.TryParse(item.StringValue, out iValue))
  79. {
  80. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  81. {
  82. if (iValue > item.Max || iValue < item.Min)
  83. {
  84. DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", ((int)item.Min).ToString(), ((int)item.Max).ToString()));
  85. return;
  86. }
  87. }
  88. }
  89. else
  90. {
  91. DialogBox.ShowWarning("Please input valid data.");
  92. return;
  93. }
  94. value = item.StringValue;
  95. }
  96. else if (item.Type == DataType.Double)
  97. {
  98. double fValue;
  99. if (double.TryParse(item.StringValue, out fValue))
  100. {
  101. if (!double.IsNaN(item.Max) && !double.IsNaN(item.Min))
  102. {
  103. if (fValue > item.Max || fValue < item.Min)
  104. {
  105. DialogBox.ShowWarning(string.Format("The value should be between {0} and {1}.", item.Min.ToString(), item.Max.ToString()));
  106. return;
  107. }
  108. }
  109. }
  110. else
  111. {
  112. DialogBox.ShowWarning("Please input valid data.");
  113. return;
  114. }
  115. value = item.StringValue;
  116. }
  117. else
  118. value = item.StringValue;
  119. }
  120. string key = String.Format("{0}{1}{2}", _CurrentNodeName, ".", item.Name);
  121. InvokeClient.Instance.Service.DoOperation("System.SetConfig", key, value);
  122. item.TextSaved = true;
  123. Reload();
  124. }
  125. public void Reload()
  126. {
  127. GetDataOfConfigItems();
  128. }
  129. public void SaveAll()
  130. {
  131. if (ConfigItems == null)
  132. return;
  133. ConfigItems.ForEach(item => SetValue(item));
  134. }
  135. #endregion
  136. }
  137. }