SystemConfigProvider.cs 4.5 KB

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