IoProviderManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Xml;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.IOCore;
  10. using CyberX8_Core;
  11. namespace MECF.Framework.RT.Core.IoProviders
  12. {
  13. public class IoProviderManager : Singleton<IoProviderManager>
  14. {
  15. private List<IIoProvider> _providers = new List<IIoProvider>();
  16. public List<IIoProvider> Providers => _providers;
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. /// <param name="xmlConfigFile">配置文件名,绝对路径</param>
  21. /// <param name="ioMappingPathFile">需要激活的IO Provider名字</param>
  22. public void Initialize(string xmlConfigFile, Dictionary<string, Dictionary<int, string>> ioMappingPathFile)
  23. {
  24. XmlDocument xml = new XmlDocument();
  25. try
  26. {
  27. xml.Load(xmlConfigFile);
  28. XmlNodeList nodeProviders = xml.SelectNodes("IoProviders/IoProvider");
  29. foreach (var nodeProvider in nodeProviders)
  30. {
  31. XmlElement element = nodeProvider as XmlElement;
  32. if (element == null)
  33. continue;
  34. XmlElement nodeParameter = element.SelectSingleNode("Parameter") as XmlElement;
  35. if (nodeParameter == null)
  36. continue;
  37. string module = element.GetAttribute("module").Trim();
  38. string name = element.GetAttribute("name").Trim();
  39. string className = element.GetAttribute("class").Trim();
  40. string assemblyName = element.GetAttribute("assembly").Trim();
  41. string loadCondition = element.GetAttribute("load_condition").Trim();
  42. bool isSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;
  43. //0 : Simualtor, 1:real 2: both
  44. if (isSimulator && loadCondition!="0" && loadCondition != "2")
  45. {
  46. continue;
  47. }
  48. if (!isSimulator && loadCondition != "1" && loadCondition != "2")
  49. {
  50. continue;
  51. }
  52. string source = module + "." + name;
  53. Type t = Assembly.Load(assemblyName).GetType(className);
  54. if (t == null)
  55. {
  56. throw new Exception(string.Format("ioProvider config file class and assembly not valid,"+ source));
  57. }
  58. IIoProvider provider;
  59. try
  60. {
  61. provider = (IIoProvider)Activator.CreateInstance(t);
  62. _providers.Add(provider);
  63. }
  64. catch (Exception ex)
  65. {
  66. LOG.WriteExeption(ex);
  67. throw new Exception(string.Format("ioProvider can not be created," + source));
  68. }
  69. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  70. XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");
  71. foreach (var nodeBlock in nodeBlocks)
  72. {
  73. XmlElement blockElement = nodeBlock as XmlElement;
  74. if (blockElement == null)
  75. continue;
  76. IoBlockItem section = new IoBlockItem();
  77. string type = blockElement.GetAttribute("type");
  78. string offset = blockElement.GetAttribute("offset");
  79. string size = blockElement.GetAttribute("size");
  80. string value_type = blockElement.GetAttribute("value_type");
  81. int result;
  82. if (!int.TryParse(offset, out result))
  83. {
  84. continue;
  85. }
  86. section.Offset = result;
  87. if (!int.TryParse(size, out result))
  88. {
  89. continue;
  90. }
  91. section.Size = result;
  92. switch (type.ToLower())
  93. {
  94. case "ai":
  95. section.Type = IoType.AI;
  96. break;
  97. case "ao":
  98. section.Type = IoType.AO;
  99. break;
  100. case "di":
  101. section.Type = IoType.DI;
  102. break;
  103. case "do":
  104. section.Type = IoType.DO;
  105. break;
  106. default:
  107. continue;
  108. }
  109. lstBuffers.Add(section);
  110. }
  111. if (ioMappingPathFile.ContainsKey(source))
  112. {
  113. provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, ioMappingPathFile[source]);
  114. }
  115. else
  116. {
  117. throw new Exception(string.Format("can not find io map config files," + source));
  118. }
  119. provider.Start();
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
  125. }
  126. }
  127. /// <summary>
  128. ///
  129. /// </summary>
  130. /// <param name="xmlConfigFile">配置文件名,绝对路径</param>
  131. public void Initialize(string xmlConfigFile)
  132. {
  133. XmlDocument xml = new XmlDocument();
  134. try
  135. {
  136. xml.Load(xmlConfigFile);
  137. XmlNodeList nodeProviders = xml.SelectNodes("IoProviders/IoProvider");
  138. foreach (var nodeProvider in nodeProviders)
  139. {
  140. XmlElement element = nodeProvider as XmlElement;
  141. if (element == null)
  142. continue;
  143. XmlElement nodeParameter = element.SelectSingleNode("Parameter") as XmlElement;
  144. if (nodeParameter == null)
  145. continue;
  146. string module = element.GetAttribute("module").Trim();
  147. string name = element.GetAttribute("name").Trim();
  148. string className = element.GetAttribute("class").Trim();
  149. string assemblyName = element.GetAttribute("assembly").Trim();
  150. string loadCondition = element.GetAttribute("load_condition").Trim();
  151. bool isSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;
  152. //0 : Simualtor, 1:real 2: both
  153. if (isSimulator && loadCondition != "0" && loadCondition != "2")
  154. {
  155. continue;
  156. }
  157. if (!isSimulator && loadCondition != "1" && loadCondition != "2")
  158. {
  159. continue;
  160. }
  161. string source = module + "." + name;
  162. Type t = Assembly.Load(assemblyName).GetType(className);
  163. if (t == null)
  164. {
  165. throw new Exception(string.Format("ioProvider config file class and assembly not valid," + source));
  166. }
  167. IIoProvider provider;
  168. try
  169. {
  170. provider = (IIoProvider)Activator.CreateInstance(t);
  171. _providers.Add(provider);
  172. }
  173. catch (Exception ex)
  174. {
  175. LOG.WriteExeption(ex);
  176. throw new Exception(string.Format("ioProvider can not be created," + source));
  177. }
  178. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  179. XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");
  180. foreach (var nodeBlock in nodeBlocks)
  181. {
  182. XmlElement blockElement = nodeBlock as XmlElement;
  183. if (blockElement == null)
  184. continue;
  185. IoBlockItem section = new IoBlockItem();
  186. string type = blockElement.GetAttribute("type");
  187. string offset = blockElement.GetAttribute("offset");
  188. string size = blockElement.GetAttribute("size");
  189. string value_type = blockElement.GetAttribute("value_type");
  190. int result;
  191. if (!int.TryParse(offset, out result))
  192. {
  193. continue;
  194. }
  195. section.Offset = result;
  196. if (!int.TryParse(size, out result))
  197. {
  198. continue;
  199. }
  200. section.Size = result;
  201. switch (type.ToLower())
  202. {
  203. case "ai":
  204. section.Type = IoType.AI;
  205. break;
  206. case "ao":
  207. section.Type = IoType.AO;
  208. break;
  209. case "di":
  210. section.Type = IoType.DI;
  211. break;
  212. case "do":
  213. section.Type = IoType.DO;
  214. break;
  215. default:
  216. continue;
  217. }
  218. lstBuffers.Add(section);
  219. }
  220. string mapModule = element.GetAttribute("map_module").Trim();
  221. string mapFile="";
  222. if (mapModule == "TM"|| mapModule == "SETM")
  223. {
  224. mapFile ="TM"+"\\"+ element.GetAttribute("map_file").Trim();
  225. }
  226. else
  227. {
  228. // JetChamber jetChamber = (JetChamber)Enum.Parse(typeof(JetChamber), SC.GetStringValue($"{mapModule}.ChamberType"));
  229. JetChamber jetChamber = (JetChamber)SC.GetValue<int>($"{mapModule}.ChamberType");
  230. switch (jetChamber)
  231. {
  232. case JetChamber.Venus:
  233. mapFile= "PM" + "\\" + "Venus" + "\\" + "VenusIoDefine.xml";
  234. break;
  235. case JetChamber.Kepler2300:
  236. mapFile = "PM" + "\\" + "Kepler2300" + "\\" + "Kepler2300IoDefine.xml";
  237. break;
  238. case JetChamber.Kepler2200A:
  239. mapFile = "PM" + "\\" + "Kepler2200A" + "\\" + "Kepler2200AIoDefine.xml";
  240. break;
  241. case JetChamber.Kepler2200B:
  242. //mapFile = jetChamber.ToString() + "\\" + "_ioDefineKepler.xml";
  243. mapFile= "PM" + "\\" + "Kepler2200B" + "\\" + "Kepler2200BIoDefine.xml";
  244. break;
  245. case JetChamber.VenusSE:
  246. mapFile = "PM" + "\\" + "VenusSE" + "\\" + "VenusSEIoDefine.xml";
  247. break;
  248. case JetChamber.VenusDE:
  249. mapFile = "PM" + "\\" + "VenusDE" + "\\" + "VenusDEIoDefine.xml";
  250. break;
  251. }
  252. }
  253. FileInfo file = new FileInfo(xmlConfigFile);
  254. string fullPathName = file.Directory.FullName + "\\" + mapFile;
  255. provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, fullPathName, mapModule);
  256. provider.Start();
  257. }
  258. }
  259. catch (Exception ex)
  260. {
  261. throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
  262. }
  263. }
  264. public void Terminate()
  265. {
  266. try
  267. {
  268. foreach (var ioProvider in _providers)
  269. {
  270. ioProvider.Stop();
  271. }
  272. }
  273. catch (Exception ex)
  274. {
  275. LOG.WriteExeption(ex);
  276. }
  277. }
  278. public void Reset()
  279. {
  280. try
  281. {
  282. foreach (var ioProvider in _providers)
  283. {
  284. ioProvider.Reset();
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. LOG.WriteExeption(ex);
  290. }
  291. }
  292. }
  293. }