SystemConfigProvider.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Xml;
  5. using Aitex.Core.RT.ConfigCenter;
  6. using Aitex.Core.RT.Log;
  7. using MECF.Framework.Common.DataCenter;
  8. using OpenSEMI.ClientBase.ServiceProvider;
  9. namespace MECF.Framework.UI.Client.CenterViews.Configs.SystemConfig
  10. {
  11. public class SystemConfigProvider : IProvider
  12. {
  13. private static SystemConfigProvider _Instance = null;
  14. public static SystemConfigProvider Instance
  15. {
  16. get
  17. {
  18. if (_Instance == null)
  19. _Instance = new SystemConfigProvider();
  20. return _Instance;
  21. }
  22. }
  23. public void Create()
  24. {
  25. }
  26. private Dictionary<string, DataType> typeDic = new Dictionary<string, DataType>() {
  27. { "String", DataType.String },
  28. { "Bool", DataType.Bool },
  29. { "Integer", DataType.Int },
  30. { "Double", DataType.Double }
  31. };
  32. public ConfigNode GetConfig(bool showVisibiable = false)
  33. {
  34. string config = QueryDataClient.Instance.Service.GetConfigFileContent();
  35. if (string.IsNullOrEmpty(config))
  36. return null;
  37. ConfigNode result = new ConfigNode() { Path = string.Empty };
  38. try
  39. {
  40. XmlDocument xml = new XmlDocument();
  41. xml.LoadXml(config);
  42. XmlElement element = xml.SelectSingleNode("root") as XmlElement;
  43. BuildTree(element, result, showVisibiable);
  44. }
  45. catch (Exception ex)
  46. {
  47. LOG.Write(ex);
  48. }
  49. return result;
  50. }
  51. public ConfigNode GetConfig(string module)
  52. {
  53. string config = QueryDataClient.Instance.Service.GetConfigFileContentByModule(module);
  54. if (string.IsNullOrEmpty(config))
  55. return null;
  56. ConfigNode result = new ConfigNode() {Path = string.Empty};
  57. try
  58. {
  59. XmlDocument xml = new XmlDocument();
  60. xml.LoadXml(config);
  61. XmlElement element = xml.SelectSingleNode("root") as XmlElement;
  62. BuildTree(element, result);
  63. }
  64. catch (Exception ex)
  65. {
  66. LOG.Write(ex);
  67. }
  68. return result;
  69. }
  70. public void BuildTree(XmlElement root, ConfigNode item,bool showVisibiable=false)
  71. {
  72. item.Name = root.GetAttribute("name");
  73. item.SubNodes = new List<ConfigNode>();
  74. item.Items = new List<ConfigItem>();
  75. item.IsMatch = true;
  76. item.Display = root.GetAttribute("display");
  77. if (string.IsNullOrEmpty(item.Display))
  78. item.Display = item.Name;
  79. XmlNodeList group = root.SelectNodes("configs");
  80. XmlNodeList single = root.SelectNodes("config");
  81. if (group != null)
  82. {
  83. foreach (var groupItem in group)
  84. {
  85. XmlElement element = groupItem as XmlElement;
  86. if(!showVisibiable)
  87. {
  88. string strVisible = element.GetAttribute("visible");
  89. bool bVisible;
  90. if (!string.IsNullOrEmpty(strVisible))
  91. {
  92. if (bool.TryParse(strVisible, out bVisible) && !bVisible)
  93. continue;
  94. }
  95. }
  96. ConfigNode sc = new ConfigNode() {Path = string.IsNullOrEmpty(item.Path) ? item.Name : item.Path +"."+ item.Name};
  97. sc.IsExpanded = Regex.Matches(sc.Path, ".").Count < 3;
  98. sc.IsMatch = true;
  99. BuildTree(element, sc, showVisibiable);
  100. item.SubNodes.Add(sc);
  101. }
  102. }
  103. if (single != null)
  104. {
  105. foreach (var singleItem in single)
  106. {
  107. XmlElement element = singleItem as XmlElement;
  108. string strVisible = element.GetAttribute("visible");
  109. bool bVisible;
  110. if (!bool.TryParse(strVisible, out bVisible))
  111. bVisible = true;
  112. if (!showVisibiable)
  113. {
  114. if (!bVisible) //do not show the item if visible field is false
  115. continue;
  116. }
  117. ConfigItem config = new ConfigItem()
  118. {
  119. Name = element.GetAttribute("name"),
  120. Display = element.GetAttribute("display"),
  121. DefaultValue = element.GetAttribute("default"),
  122. Description = element.GetAttribute("description"),
  123. Max = ConvertStringToFloat(element.GetAttribute("max")),
  124. Min = ConvertStringToFloat(element.GetAttribute("min")),
  125. Parameter = element.GetAttribute("paramter"),
  126. Unit = element.GetAttribute("unit"),
  127. Tag = element.GetAttribute("tag"),
  128. Path = $"{item.Path}.{item.Name}"
  129. };
  130. if (string.IsNullOrEmpty(config.Display))
  131. {
  132. config.Display = config.Description;
  133. if (string.IsNullOrEmpty(config.Display))
  134. config.Display = config.Name;
  135. }
  136. string strType = element.GetAttribute("type");
  137. config.Type = typeDic.ContainsKey(strType) ? typeDic[strType] : DataType.Unknown;
  138. config.Visible = bVisible;
  139. item.Items.Add(config);
  140. }
  141. }
  142. }
  143. private float ConvertStringToFloat(string value)
  144. {
  145. float result;
  146. if (!float.TryParse(value, out result))
  147. result = float.NaN;
  148. return result;
  149. }
  150. public ConfigNode GetConfigTree()
  151. {
  152. return GetConfig();
  153. }
  154. public ConfigNode GetConfigTree(string module)
  155. {
  156. return GetConfig(module);
  157. }
  158. public List<string> GetValuesByNode(string node)
  159. {
  160. List<string> values = new List<string>();
  161. //interface to get values
  162. return values;
  163. }
  164. public string GetValueByName(string module, string name)
  165. {
  166. object result = QueryDataClient.Instance.Service.GetConfigByModule(module, name);
  167. if (result != null)
  168. return result.ToString();
  169. else
  170. return string.Empty;
  171. }
  172. public string GetValueByName(string name)
  173. {
  174. object result = QueryDataClient.Instance.Service.GetConfig(name);
  175. if (result != null)
  176. return result.ToString();
  177. else
  178. return string.Empty;
  179. }
  180. }
  181. }