SystemConfigProvider.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using Aitex.Core.RT.Log;
  5. using MECF.Framework.Common.DataCenter;
  6. using OpenSEMI.ClientBase.ServiceProvider;
  7. namespace VirgoUI.Client.Models.Utility.SystemConfig
  8. {
  9. public class SystemConfigProvider : IProvider
  10. {
  11. private static SystemConfigProvider _Instance = null;
  12. public static SystemConfigProvider Instance
  13. {
  14. get
  15. {
  16. if (_Instance == null)
  17. _Instance = new SystemConfigProvider();
  18. return _Instance;
  19. }
  20. }
  21. public void Create()
  22. {
  23. }
  24. private Dictionary<string, DataType> typeDic = new Dictionary<string, DataType>() {
  25. { "String", DataType.String },
  26. { "Bool", DataType.Bool },
  27. { "Integer", DataType.Int },
  28. { "Double", DataType.Double }
  29. };
  30. public ConfigNode GetConfig()
  31. {
  32. string config = QueryDataClient.Instance.Service.GetConfigFileContent();
  33. if (string.IsNullOrEmpty(config))
  34. return null;
  35. ConfigNode result = new ConfigNode() {Path = string.Empty};
  36. try
  37. {
  38. XmlDocument xml = new XmlDocument();
  39. xml.LoadXml(config);
  40. XmlElement element = xml.SelectSingleNode("root") as XmlElement;
  41. BuildTree(element, result);
  42. }
  43. catch (Exception ex)
  44. {
  45. LOG.Write(ex);
  46. }
  47. return result;
  48. }
  49. public void BuildTree(XmlElement root, ConfigNode item)
  50. {
  51. item.Name = root.GetAttribute("name");
  52. item.NameView = root.GetAttribute("nameView");
  53. item.SubNodes = new List<ConfigNode>();
  54. item.Items = new List<ConfigItem>();
  55. XmlNodeList group = root.SelectNodes("configs");
  56. XmlNodeList single = root.SelectNodes("config");
  57. if (group != null)
  58. {
  59. foreach (var groupItem in group)
  60. {
  61. XmlElement element = groupItem as XmlElement;
  62. string strVisible = element.GetAttribute("visible");
  63. bool bVisible;
  64. if (!string.IsNullOrEmpty(strVisible))
  65. {
  66. if (bool.TryParse(strVisible, out bVisible) && !bVisible)
  67. continue;
  68. }
  69. ConfigNode sc = new ConfigNode() {Path = item.Name};
  70. BuildTree(element, sc);
  71. item.SubNodes.Add(sc);
  72. }
  73. }
  74. if (single != null)
  75. {
  76. foreach (var singleItem in single)
  77. {
  78. XmlElement element = singleItem as XmlElement;
  79. string strVisible = element.GetAttribute("visible");
  80. bool bVisible;
  81. if (!bool.TryParse(strVisible, out bVisible))
  82. bVisible = true;
  83. if (!bVisible) //do not show the item if visible field is false
  84. continue;
  85. ConfigItem config = new ConfigItem()
  86. {
  87. Name = element.GetAttribute("name"),
  88. NameView = element.GetAttribute("nameView"),
  89. DefaultValue = element.GetAttribute("default"),
  90. Description = element.GetAttribute("description"),
  91. Max = ConvertStringToFloat(element.GetAttribute("max")),
  92. Min = ConvertStringToFloat(element.GetAttribute("min")),
  93. Parameter = element.GetAttribute("paramter"),
  94. Unit = element.GetAttribute("unit"),
  95. Tag = element.GetAttribute("tag")
  96. };
  97. string strType = element.GetAttribute("type");
  98. config.Type = typeDic.ContainsKey(strType) ? typeDic[strType] : DataType.Unknown;
  99. config.Visible = bVisible;
  100. item.Items.Add(config);
  101. }
  102. }
  103. }
  104. private float ConvertStringToFloat(string value)
  105. {
  106. float result;
  107. if (!float.TryParse(value, out result))
  108. result = float.NaN;
  109. return result;
  110. }
  111. public ConfigNode GetConfigTree()
  112. {
  113. return GetConfig();
  114. }
  115. public List<string> GetValuesByNode(string node)
  116. {
  117. List<string> values = new List<string>();
  118. //interface to get values
  119. return values;
  120. }
  121. public string GetValueByName(string name)
  122. {
  123. object result = QueryDataClient.Instance.Service.GetConfig(name);
  124. if (result != null)
  125. return result.ToString();
  126. else
  127. return string.Empty;
  128. }
  129. }
  130. }