SystemConfig.cs 10 KB

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