SystemConfigProvider.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. LOG.WriteExeption(ex);
  50. }
  51. return result;
  52. }
  53. public void BuildTree(XmlElement root, ConfigNode item)
  54. {
  55. item.Name = root.GetAttribute("name");
  56. item.SubNodes = new List<ConfigNode>();
  57. item.Items = new List<ConfigItem>();
  58. XmlNodeList group = root.SelectNodes("configs");
  59. XmlNodeList single = root.SelectNodes("config");
  60. if (group != null)
  61. {
  62. foreach (var groupItem in group)
  63. {
  64. XmlElement element = groupItem as XmlElement;
  65. ConfigNode sc = new ConfigNode() { Path = item.Name };
  66. BuildTree(element, sc);
  67. item.SubNodes.Add(sc);
  68. }
  69. }
  70. if (single != null)
  71. {
  72. foreach (var singleItem in single)
  73. {
  74. XmlElement element = singleItem as XmlElement;
  75. string strVisible = element.GetAttribute("visible");
  76. bool bVisible;
  77. if (!bool.TryParse(strVisible, out bVisible))
  78. bVisible = true;
  79. if (!bVisible) //do not show the item if visible field is false
  80. continue;
  81. ConfigItem config = new ConfigItem()
  82. {
  83. Name = element.GetAttribute("name"),
  84. DefaultValue = element.GetAttribute("default"),
  85. Description = element.GetAttribute("description"),
  86. Max = ConvertStringToFloat(element.GetAttribute("max")),
  87. Min = ConvertStringToFloat(element.GetAttribute("min")),
  88. Parameter = element.GetAttribute("paramter"),
  89. Unit = element.GetAttribute("unit"),
  90. Tag = element.GetAttribute("tag")
  91. };
  92. string strType = element.GetAttribute("type");
  93. config.Type = typeDic.ContainsKey(strType) ? typeDic[strType] : DataType.Unknown;
  94. config.Visible = bVisible;
  95. item.Items.Add(config);
  96. }
  97. }
  98. }
  99. private float ConvertStringToFloat(string value)
  100. {
  101. float result;
  102. if (!float.TryParse(value, out result))
  103. result = float.NaN;
  104. return result;
  105. }
  106. public ConfigNode GetConfigTree()
  107. {
  108. return GetConfig();
  109. }
  110. public List<string> GetValuesByNode(string node)
  111. {
  112. List<string> values = new List<string>();
  113. //interface to get values
  114. return values;
  115. }
  116. public string GetValueByName(string name)
  117. {
  118. object result = QueryDataClient.Instance.Service.GetConfig(name);
  119. if (result != null)
  120. return result.ToString();
  121. else
  122. return string.Empty;
  123. }
  124. }
  125. }