SystemConfig.cs 9.8 KB

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