SystemConfig.cs 10 KB

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