SystemConfig.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Xml;
  5. using System.Linq;
  6. using Aitex.Core.Util;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.RT.Log;
  9. namespace Venus_Simulator.Instances
  10. {
  11. public class SystemConfig : Singleton<SystemConfig>
  12. {
  13. private Dictionary<string, SCConfigItem> _items = new Dictionary<string, SCConfigItem>();
  14. List<string> paths = new List<string>();
  15. public void Initialize()
  16. {
  17. var current_path = Environment.CurrentDirectory;
  18. int nIndesx = current_path.LastIndexOf("Venus\\");
  19. current_path = current_path.Substring(0, nIndesx + 5);
  20. GetConfigFilePath(current_path);
  21. string cfgPath = paths.Find(item => item.Contains("Venus_RT"));
  22. if (cfgPath != null)
  23. {
  24. string config_path = cfgPath;
  25. string system_cfg = config_path.Replace("_sc.data", "System.sccfg");
  26. if(File.Exists(system_cfg))
  27. {
  28. BuildItems(system_cfg);
  29. if (File.Exists(config_path))
  30. {
  31. var cfg_stream = File.Open(config_path, FileMode.Open, FileAccess.Read);
  32. XmlDocument xmlData = new XmlDocument();
  33. xmlData.Load(config_path);
  34. xmlData.Load(stream);
  35. XmlNodeList scdatas = xmlData.SelectNodes("root/scdata");
  36. foreach (XmlElement nodedata in scdatas)
  37. {
  38. string name = nodedata.GetAttribute("name");
  39. if (_items.ContainsKey(name))
  40. {
  41. InitializeItemValue(_items[name], nodedata.GetAttribute("value"));
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. private bool InitializeItemValue(SCConfigItem item, string value)
  49. {
  50. bool changed = false;
  51. switch (item.Type)
  52. {
  53. case "Bool":
  54. bool boolValue;
  55. if (bool.TryParse(value, out boolValue) && boolValue != item.BoolValue)
  56. {
  57. item.BoolValue = boolValue;
  58. changed = true;
  59. }
  60. break;
  61. case "Integer":
  62. int intValue;
  63. if (int.TryParse(value, out intValue) && intValue != item.IntValue)
  64. {
  65. int.TryParse(item.Min, out int min);
  66. int.TryParse(item.Max, out int max);
  67. if (intValue < min || intValue > max)
  68. {
  69. //EV.PostWarningLog(ModuleNameString.System, $"SC {item.PathName} value {intValue} out of setting range ({item.Min}, {item.Max})");
  70. break;
  71. }
  72. item.IntValue = intValue;
  73. changed = true;
  74. }
  75. break;
  76. case "Double":
  77. double doubleValue;
  78. if (double.TryParse(value, out doubleValue) && Math.Abs(doubleValue - item.DoubleValue) > 0.0001)
  79. {
  80. double.TryParse(item.Min, out double min);
  81. double.TryParse(item.Max, out double max);
  82. if (doubleValue < min || doubleValue > max)
  83. {
  84. //EV.PostWarningLog(ModuleNameString.System, $"SC {item.PathName} value {doubleValue} out of setting range ({item.Min}, {item.Max})");
  85. break;
  86. }
  87. item.DoubleValue = doubleValue;
  88. changed = true;
  89. }
  90. break;
  91. case "String":
  92. if (value != item.StringValue)
  93. {
  94. item.StringValue = value;
  95. changed = true;
  96. }
  97. break;
  98. }
  99. return changed;
  100. }
  101. public T GetValue<T>(string name) where T : struct
  102. {
  103. try
  104. {
  105. if (typeof(T) == typeof(bool))
  106. return (T)(object)_items[name].BoolValue;
  107. if (typeof(T) == typeof(int))
  108. return (T)(object)_items[name].IntValue;
  109. if (typeof(T) == typeof(double))
  110. return (T)(object)_items[name].DoubleValue;
  111. }
  112. catch (KeyNotFoundException)
  113. {
  114. return default(T);
  115. }
  116. catch (Exception)
  117. {
  118. return default(T);
  119. }
  120. return default(T);
  121. }
  122. private void BuildItems(string xmlFile)
  123. {
  124. XmlDocument xml = new XmlDocument();
  125. try
  126. {
  127. var stream = File.Open(xmlFile, FileMode.Open, FileAccess.Read);
  128. xml.Load(stream);
  129. XmlNodeList nodeConfigs = xml.SelectNodes("root/configs");
  130. foreach (XmlElement nodeConfig in nodeConfigs)
  131. {
  132. BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  133. }
  134. }
  135. catch (Exception ex)
  136. {
  137. LOG.WriteExeption(ex);
  138. }
  139. }
  140. private void BuildPathConfigs(string parentPath, XmlElement configElement)
  141. {
  142. XmlNodeList nodeConfigsList = configElement.SelectNodes("configs");
  143. foreach (XmlElement nodeConfig in nodeConfigsList)
  144. {
  145. if (string.IsNullOrEmpty(parentPath))
  146. {
  147. BuildPathConfigs(nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  148. }
  149. else
  150. {
  151. BuildPathConfigs(parentPath + "." + nodeConfig.GetAttribute("name"), nodeConfig as XmlElement);
  152. }
  153. }
  154. XmlNodeList nodeConfigs = configElement.SelectNodes("config");
  155. foreach (XmlElement nodeConfig in nodeConfigs)
  156. {
  157. SCConfigItem item = new SCConfigItem()
  158. {
  159. Default = nodeConfig.GetAttribute("default"),
  160. Name = nodeConfig.GetAttribute("name"),
  161. Description = nodeConfig.GetAttribute("description"),
  162. Max = nodeConfig.GetAttribute("max"),
  163. Min = nodeConfig.GetAttribute("min"),
  164. Parameter = nodeConfig.GetAttribute("paramter"),
  165. Path = parentPath,
  166. Tag = nodeConfig.GetAttribute("tag"),
  167. Type = nodeConfig.GetAttribute("type"),
  168. Unit = nodeConfig.GetAttribute("unit"),
  169. };
  170. InitializeItemValue(item, item.Default);
  171. if (_items.ContainsKey(item.PathName))
  172. {
  173. //LOG.Error("Duplicated SC item, "+ item.PathName);
  174. }
  175. _items[item.PathName] = item;
  176. }
  177. }
  178. private void SearchFileInPath(string path, string folderName, string fileName, ref List<string> projectPaths)
  179. {
  180. var dirs = Directory.GetDirectories(path, "*.*", SearchOption.TopDirectoryOnly).ToList(); //获取当前路径下所有文件与文件夹
  181. var desFolders = dirs.FindAll(x => x.Contains(folderName)); //在当前目录中查找目标文件夹
  182. if (desFolders == null || desFolders.Count <= 0)
  183. {
  184. //当前目录未找到目标则递归
  185. foreach (var dir in dirs)
  186. {
  187. SearchFileInPath(dir, folderName, fileName, ref projectPaths);
  188. }
  189. }
  190. else
  191. {
  192. //找到则添加至结果集
  193. projectPaths.Add(path + "\\" + folderName + "\\" + fileName);
  194. }
  195. }
  196. public void GetConfigFilePath(string path)
  197. {
  198. DirectoryInfo dir = new DirectoryInfo(path);
  199. //找到该目录下的文件
  200. FileInfo[] fi = dir.GetFiles();
  201. foreach (FileInfo f in fi)
  202. {
  203. if (f.FullName.Contains("\\Venus_RT\\") && f.FullName.Contains("\\Config\\") && f.Name == "_sc.data")
  204. {
  205. paths.Add(f.FullName);
  206. return;
  207. }
  208. }
  209. //找到该目录下的所有目录再递归
  210. DirectoryInfo[] subDir = dir.GetDirectories();
  211. foreach (DirectoryInfo d in subDir)
  212. {
  213. GetConfigFilePath(d.FullName);
  214. }
  215. }
  216. }
  217. }