IoProviderManager.cs 12 KB

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