SystemConfigProvider.cs 4.6 KB

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