IoProviderManager.cs 11 KB

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