SystemConfig.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. using Aitex.Core.Util;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.RT.Log;
  8. namespace Venus_Simulator.Instances
  9. {
  10. public class SystemConfig : Singleton<SystemConfig>
  11. {
  12. private Dictionary<string, SCConfigItem> _items = new Dictionary<string, SCConfigItem>();
  13. public void Initialize()
  14. {
  15. var processs = System.Diagnostics.Process.GetProcessesByName("Venus_RT");
  16. if(processs.Length > 0)
  17. {
  18. var venus_path = processs[0].MainModule.FileName;
  19. var venus_dir = Path.GetDirectoryName(venus_path);
  20. if (venus_dir != null)
  21. {
  22. string config_path = $"{venus_dir}\\Config\\_sc.data";
  23. string system_cfg = $"{venus_dir}\\Config\\System.sccfg";
  24. if(File.Exists(system_cfg))
  25. {
  26. BuildItems(system_cfg);
  27. if (File.Exists(config_path))
  28. {
  29. XmlDocument xmlData = new XmlDocument();
  30. xmlData.Load(config_path);
  31. XmlNodeList scdatas = xmlData.SelectNodes("root/scdata");
  32. foreach (XmlElement nodedata in scdatas)
  33. {
  34. string name = nodedata.GetAttribute("name");
  35. if (_items.ContainsKey(name))
  36. {
  37. InitializeItemValue(_items[name], nodedata.GetAttribute("value"));
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }
  45. private bool InitializeItemValue(SCConfigItem item, string value)
  46. {
  47. bool changed = false;
  48. switch (item.Type)
  49. {
  50. case "Bool":
  51. bool boolValue;
  52. if (bool.TryParse(value, out boolValue) && boolValue != item.BoolValue)
  53. {
  54. item.BoolValue = boolValue;
  55. changed = true;
  56. }
  57. break;
  58. case "Integer":
  59. int intValue;
  60. if (int.TryParse(value, out intValue) && intValue != item.IntValue)
  61. {
  62. int.TryParse(item.Min, out int min);
  63. int.TryParse(item.Max, out int max);
  64. if (intValue < min || intValue > max)
  65. {
  66. //EV.PostWarningLog(ModuleNameString.System, $"SC {item.PathName} value {intValue} out of setting range ({item.Min}, {item.Max})");
  67. break;
  68. }
  69. item.IntValue = intValue;
  70. changed = true;
  71. }
  72. break;
  73. case "Double":
  74. double doubleValue;
  75. if (double.TryParse(value, out doubleValue) && Math.Abs(doubleValue - item.DoubleValue) > 0.0001)
  76. {
  77. double.TryParse(item.Min, out double min);
  78. double.TryParse(item.Max, out double max);
  79. if (doubleValue < min || doubleValue > max)
  80. {
  81. //EV.PostWarningLog(ModuleNameString.System, $"SC {item.PathName} value {doubleValue} out of setting range ({item.Min}, {item.Max})");
  82. break;
  83. }
  84. item.DoubleValue = doubleValue;
  85. changed = true;
  86. }
  87. break;
  88. case "String":
  89. if (value != item.StringValue)
  90. {
  91. item.StringValue = value;
  92. changed = true;
  93. }
  94. break;
  95. }
  96. return changed;
  97. }
  98. public T GetValue<T>(string name) where T : struct
  99. {
  100. try
  101. {
  102. if (typeof(T) == typeof(bool))
  103. return (T)(object)_items[name].BoolValue;
  104. if (typeof(T) == typeof(int))
  105. return (T)(object)_items[name].IntValue;
  106. if (typeof(T) == typeof(double))
  107. return (T)(object)_items[name].DoubleValue;
  108. }
  109. catch (KeyNotFoundException)
  110. {
  111. return default(T);
  112. }
  113. catch (Exception)
  114. {
  115. return default(T);
  116. }
  117. return default(T);
  118. }
  119. private void BuildItems(string xmlFile)
  120. {
  121. XmlDocument xml = new XmlDocument();
  122. try
  123. {
  124. xml.Load(xmlFile);
  125. XmlNodeList nodeConfigs = xml.SelectNodes("root/configs");
  126. foreach (XmlElement nodeConfig in nodeConfigs)
  127. {
  128. BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. LOG.WriteExeption(ex);
  134. }
  135. }
  136. private void BuildPathConfigs(string parentPath, XmlElement configElement)
  137. {
  138. XmlNodeList nodeConfigsList = configElement.SelectNodes("configs");
  139. foreach (XmlElement nodeConfig in nodeConfigsList)
  140. {
  141. if (string.IsNullOrEmpty(parentPath))
  142. {
  143. BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  144. }
  145. else
  146. {
  147. BuildPathConfigs(parentPath + "." + nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  148. }
  149. }
  150. XmlNodeList nodeConfigs = configElement.SelectNodes("config");
  151. foreach (XmlElement nodeConfig in nodeConfigs)
  152. {
  153. SCConfigItem item = new SCConfigItem()
  154. {
  155. Default = nodeConfig.GetAttribute("default"),
  156. Name = nodeConfig.GetAttribute("name"),
  157. Description = nodeConfig.GetAttribute("description"),
  158. Max = nodeConfig.GetAttribute("max"),
  159. Min = nodeConfig.GetAttribute("min"),
  160. Parameter = nodeConfig.GetAttribute("paramter"),
  161. Path = parentPath,
  162. Tag = nodeConfig.GetAttribute("tag"),
  163. Type = nodeConfig.GetAttribute("type"),
  164. Unit = nodeConfig.GetAttribute("unit"),
  165. };
  166. InitializeItemValue(item, item.Default);
  167. if (_items.ContainsKey(item.PathName))
  168. {
  169. //LOG.Error("Duplicated SC item, "+ item.PathName);
  170. }
  171. _items[item.PathName] = item;
  172. }
  173. }
  174. }
  175. }