IoProviderManager.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. namespace MECF.Framework.RT.Core.IoProviders
  15. {
  16. public class IoProviderManager : Singleton<IoProviderManager>
  17. {
  18. private List<IIoProvider> _providers = new List<IIoProvider>();
  19. private Dictionary<string, IIoProvider> _dicProviders = new Dictionary<string, IIoProvider>();
  20. public List<IIoProvider> Providers => _providers;
  21. public IIoProvider GetProvider(string name)
  22. {
  23. if (_dicProviders.ContainsKey(name))
  24. return _dicProviders[name];
  25. return null;
  26. }
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. /// <param name="xmlConfigFile">配置文件名,绝对路径</param>
  31. /// <param name="ioMappingPathFile">需要激活的IO Provider名字</param>
  32. public void Initialize(string xmlConfigFile, Dictionary<string, Dictionary<int, string>> ioMappingPathFile)
  33. {
  34. XmlDocument xml = new XmlDocument();
  35. try
  36. {
  37. xml.Load(xmlConfigFile);
  38. XmlNodeList nodeProviders = xml.SelectNodes("IoProviders/IoProvider");
  39. foreach (var nodeProvider in nodeProviders)
  40. {
  41. XmlElement element = nodeProvider as XmlElement;
  42. if (element == null)
  43. continue;
  44. XmlElement nodeParameter = element.SelectSingleNode("Parameter") as XmlElement;
  45. if (nodeParameter == null)
  46. continue;
  47. string module = element.GetAttribute("module").Trim();
  48. string name = element.GetAttribute("name").Trim();
  49. string className = element.GetAttribute("class").Trim();
  50. string assemblyName = element.GetAttribute("assembly").Trim();
  51. string loadCondition = element.GetAttribute("load_condition").Trim();
  52. bool isSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;
  53. //0 : Simualtor, 1:real 2: both
  54. if (isSimulator && loadCondition!="0" && loadCondition != "2")
  55. {
  56. continue;
  57. }
  58. if (!isSimulator && loadCondition != "1" && loadCondition != "2")
  59. {
  60. continue;
  61. }
  62. string source = module + "." + name;
  63. Type t = Assembly.Load(assemblyName).GetType(className);
  64. if (t == null)
  65. {
  66. throw new Exception(string.Format("ioProvider config file class and assembly not valid,"+ source));
  67. }
  68. IIoProvider provider;
  69. try
  70. {
  71. provider = (IIoProvider)Activator.CreateInstance(t);
  72. _providers.Add(provider);
  73. _dicProviders[source] = provider;
  74. }
  75. catch (Exception ex)
  76. {
  77. LOG.Write(ex);
  78. throw new Exception(string.Format("ioProvider can not be created," + source));
  79. }
  80. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  81. XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");
  82. foreach (var nodeBlock in nodeBlocks)
  83. {
  84. XmlElement blockElement = nodeBlock as XmlElement;
  85. if (blockElement == null)
  86. continue;
  87. IoBlockItem section = new IoBlockItem();
  88. string type = blockElement.GetAttribute("type");
  89. string offset = blockElement.GetAttribute("offset");
  90. string size = blockElement.GetAttribute("size");
  91. string value_type = blockElement.GetAttribute("value_type");
  92. int result;
  93. if (!int.TryParse(offset, out result))
  94. {
  95. continue;
  96. }
  97. section.Offset = result;
  98. if (!int.TryParse(size, out result))
  99. {
  100. continue;
  101. }
  102. section.Size = (ushort)result;
  103. switch (type.ToLower())
  104. {
  105. case "ai":
  106. section.Type = IoType.AI;
  107. break;
  108. case "ao":
  109. section.Type = IoType.AO;
  110. break;
  111. case "di":
  112. section.Type = IoType.DI;
  113. break;
  114. case "do":
  115. section.Type = IoType.DO;
  116. break;
  117. default:
  118. continue;
  119. }
  120. if (section.Type == IoType.AI || section.Type == IoType.AO)
  121. {
  122. section.AIOType = (string.IsNullOrEmpty(value_type) || value_type.ToLower()!="float") ? typeof(short) : typeof(float);
  123. }
  124. lstBuffers.Add(section);
  125. }
  126. if (ioMappingPathFile.ContainsKey(source))
  127. {
  128. provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, ioMappingPathFile[source]);
  129. }
  130. else
  131. {
  132. throw new Exception(string.Format("can not find io map config files," + source));
  133. }
  134. provider.Start();
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
  140. }
  141. }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. /// <param name="xmlConfigFile">配置文件名,绝对路径</param>
  146. public void Initialize(string xmlConfigFile)
  147. {
  148. XmlDocument xml = new XmlDocument();
  149. try
  150. {
  151. xml.Load(xmlConfigFile);
  152. XmlNodeList nodeProviders = xml.SelectNodes("IoProviders/IoProvider");
  153. foreach (var nodeProvider in nodeProviders)
  154. {
  155. XmlElement element = nodeProvider as XmlElement;
  156. if (element == null)
  157. continue;
  158. XmlElement nodeParameter = element.SelectSingleNode("Parameter") as XmlElement;
  159. if (nodeParameter == null)
  160. continue;
  161. string module = element.GetAttribute("module").Trim();
  162. string name = element.GetAttribute("name").Trim();
  163. string className = element.GetAttribute("class").Trim();
  164. string assemblyName = element.GetAttribute("assembly").Trim();
  165. string loadCondition = element.GetAttribute("load_condition").Trim();
  166. bool isSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;
  167. //0 : Simualtor, 1:real 2: both
  168. if (isSimulator && loadCondition != "0" && loadCondition != "2")
  169. {
  170. continue;
  171. }
  172. if (!isSimulator && loadCondition != "1" && loadCondition != "2")
  173. {
  174. continue;
  175. }
  176. string source = module + "." + name;
  177. Type t = Assembly.Load(assemblyName).GetType(className);
  178. if (t == null)
  179. {
  180. throw new Exception(string.Format("ioProvider config file class and assembly not valid," + source));
  181. }
  182. IIoProvider provider;
  183. try
  184. {
  185. provider = (IIoProvider)Activator.CreateInstance(t);
  186. _providers.Add(provider);
  187. _dicProviders[source] = provider;
  188. }
  189. catch (Exception ex)
  190. {
  191. LOG.Write(ex);
  192. throw new Exception(string.Format("ioProvider can not be created," + source));
  193. }
  194. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  195. XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");
  196. foreach (var nodeBlock in nodeBlocks)
  197. {
  198. XmlElement blockElement = nodeBlock as XmlElement;
  199. if (blockElement == null)
  200. continue;
  201. IoBlockItem section = new IoBlockItem();
  202. string type = blockElement.GetAttribute("type");
  203. string offset = blockElement.GetAttribute("offset");
  204. string size = blockElement.GetAttribute("size");
  205. string value_type = blockElement.GetAttribute("value_type");
  206. int result;
  207. if (!int.TryParse(offset, out result))
  208. {
  209. continue;
  210. }
  211. section.Offset = result;
  212. if (!int.TryParse(size, out result))
  213. {
  214. continue;
  215. }
  216. section.Size = (ushort)result;
  217. switch (type.ToLower())
  218. {
  219. case "ai":
  220. section.Type = IoType.AI;
  221. break;
  222. case "ao":
  223. section.Type = IoType.AO;
  224. break;
  225. case "di":
  226. section.Type = IoType.DI;
  227. break;
  228. case "do":
  229. section.Type = IoType.DO;
  230. break;
  231. default:
  232. continue;
  233. }
  234. if (section.Type == IoType.AI || section.Type == IoType.AO)
  235. {
  236. section.AIOType = (string.IsNullOrEmpty(value_type) || value_type.ToLower() != "float") ? typeof(short) : typeof(float);
  237. }
  238. lstBuffers.Add(section);
  239. }
  240. string mapModule = element.GetAttribute("map_module").Trim();
  241. string mapFile = element.GetAttribute("map_file").Trim();
  242. FileInfo file = new FileInfo(xmlConfigFile);
  243. string fullPathName = file.Directory.FullName + "\\" + mapFile;
  244. provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, fullPathName, mapModule);
  245. provider.Start();
  246. }
  247. }
  248. catch (Exception ex)
  249. {
  250. throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
  251. }
  252. }
  253. /// <summary>
  254. /// 模块化的版本
  255. /// </summary>
  256. /// <param name="xmlConfigFile"></param>
  257. /// <param name="module"></param>
  258. /// <param name="name"></param>
  259. public void Initialize(string xmlConfigFile, string module, string name, bool filterModule=false)
  260. {
  261. XmlDocument xml = new XmlDocument();
  262. try
  263. {
  264. xml.Load(xmlConfigFile);
  265. XmlNodeList nodeProviders = xml.SelectNodes("IoProviders/IoProvider");
  266. foreach (var nodeProvider in nodeProviders)
  267. {
  268. XmlElement element = nodeProvider as XmlElement;
  269. if (element == null)
  270. continue;
  271. XmlElement nodeParameter = element.SelectSingleNode("Parameter") as XmlElement;
  272. if (nodeParameter == null)
  273. continue;
  274. string className = element.GetAttribute("class").Trim();
  275. string assemblyName = element.GetAttribute("assembly").Trim();
  276. string loadCondition = element.GetAttribute("load_condition").Trim();
  277. string configModule = element.GetAttribute("module").Trim();
  278. if (filterModule &&!string.IsNullOrEmpty(configModule) && configModule!=module)
  279. continue;
  280. bool isSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;
  281. //0 : Simualtor, 1:real 2: both
  282. if (isSimulator && loadCondition != "0" && loadCondition != "2")
  283. {
  284. continue;
  285. }
  286. if (!isSimulator && loadCondition != "1" && loadCondition != "2")
  287. {
  288. continue;
  289. }
  290. string source = module + "." + name;
  291. Type t = Assembly.Load(assemblyName).GetType(className);
  292. if (t == null)
  293. {
  294. throw new Exception(string.Format("ioProvider config file class and assembly not valid," + source));
  295. }
  296. IIoProvider provider;
  297. try
  298. {
  299. provider = (IIoProvider)Activator.CreateInstance(t);
  300. _providers.Add(provider);
  301. _dicProviders[source] = provider;
  302. }
  303. catch (Exception ex)
  304. {
  305. LOG.Write(ex);
  306. throw new Exception(string.Format("ioProvider can not be created," + source));
  307. }
  308. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  309. XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");
  310. foreach (var nodeBlock in nodeBlocks)
  311. {
  312. XmlElement blockElement = nodeBlock as XmlElement;
  313. if (blockElement == null)
  314. continue;
  315. IoBlockItem section = new IoBlockItem();
  316. string type = blockElement.GetAttribute("type");
  317. string offset = blockElement.GetAttribute("offset");
  318. string size = blockElement.GetAttribute("size");
  319. string value_type = blockElement.GetAttribute("value_type");
  320. int result;
  321. if (!int.TryParse(offset, out result))
  322. {
  323. continue;
  324. }
  325. section.Offset = result;
  326. if (!int.TryParse(size, out result))
  327. {
  328. continue;
  329. }
  330. section.Size = (ushort)result;
  331. switch (type.ToLower())
  332. {
  333. case "ai":
  334. section.Type = IoType.AI;
  335. break;
  336. case "ao":
  337. section.Type = IoType.AO;
  338. break;
  339. case "di":
  340. section.Type = IoType.DI;
  341. break;
  342. case "do":
  343. section.Type = IoType.DO;
  344. break;
  345. default:
  346. continue;
  347. }
  348. if (section.Type == IoType.AI || section.Type == IoType.AO)
  349. {
  350. section.AIOType = (string.IsNullOrEmpty(value_type) || value_type.ToLower() != "float") ? typeof(short) : typeof(float);
  351. }
  352. lstBuffers.Add(section);
  353. }
  354. string mapFile = element.GetAttribute("map_file").Trim();
  355. FileInfo file = new FileInfo(xmlConfigFile);
  356. string fullPathName = file.Directory.FullName + "\\" + mapFile;
  357. provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, fullPathName, module);
  358. provider.Start();
  359. }
  360. }
  361. catch (Exception ex)
  362. {
  363. throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
  364. }
  365. }
  366. public void Terminate()
  367. {
  368. try
  369. {
  370. _dicProviders.Clear();
  371. foreach (var ioProvider in _providers)
  372. {
  373. if(ioProvider.IsOpened)
  374. ioProvider.Stop();
  375. }
  376. }
  377. catch (Exception ex)
  378. {
  379. LOG.Write(ex);
  380. }
  381. }
  382. public void Reset()
  383. {
  384. try
  385. {
  386. foreach (var ioProvider in _providers)
  387. {
  388. ioProvider.Reset();
  389. }
  390. }
  391. catch (Exception ex)
  392. {
  393. LOG.Write(ex);
  394. }
  395. }
  396. }
  397. }