SystemConfig.cs 7.0 KB

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