SystemConfigProvider.cs 4.8 KB

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