IoProviderManager.cs 12 KB

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