SystemConfig.cs 10 KB

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