SystemConfig.cs 9.1 KB

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