IoProviderManager.cs 13 KB

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