IoProviderManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.RT.SCCore;
  3. using Aitex.Core.Util;
  4. using MECF.Framework.Common.IOCore;
  5. using MECF.Framework.RT.Core.IoProviders;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Xml;
  14. namespace athosRT.Devices.IODevices
  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. 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 = (ushort)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. //LOG.Write("ioMappingPathFile:" + source);
  114. if (ioMappingPathFile.ContainsKey(source))
  115. {
  116. provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, ioMappingPathFile[source]);
  117. }
  118. else
  119. {
  120. //LOG.Error(string.Format("can not find io map config files," + source));
  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. LOG.Error("IoProvider configuration not valid," + ex.Message);
  129. throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
  130. }
  131. }
  132. /// <summary>
  133. ///
  134. /// </summary>
  135. /// <param name="xmlConfigFile">配置文件名,绝对路径</param>
  136. public void Initialize(string xmlConfigFile)
  137. {
  138. XmlDocument xml = new XmlDocument();
  139. try
  140. {
  141. xml.Load(xmlConfigFile);
  142. XmlNodeList nodeProviders = xml.SelectNodes("IoProviders/IoProvider");
  143. foreach (var nodeProvider in nodeProviders)
  144. {
  145. XmlElement element = nodeProvider as XmlElement;
  146. if (element == null)
  147. continue;
  148. XmlElement nodeParameter = element.SelectSingleNode("Parameter") as XmlElement;
  149. if (nodeParameter == null)
  150. continue;
  151. string module = element.GetAttribute("module").Trim();
  152. string name = element.GetAttribute("name").Trim();
  153. string className = element.GetAttribute("class").Trim();
  154. string assemblyName = element.GetAttribute("assembly").Trim();
  155. string loadCondition = element.GetAttribute("load_condition").Trim();
  156. bool isSimulator = SC.GetConfigItem("System.IsSimulatorMode").BoolValue;
  157. //0 : Simualtor, 1:real 2: both
  158. if (isSimulator && loadCondition != "0" && loadCondition != "2")
  159. {
  160. continue;
  161. }
  162. if (!isSimulator && loadCondition != "1" && loadCondition != "2")
  163. {
  164. continue;
  165. }
  166. string source = module + "." + name;
  167. Type t = Assembly.Load(assemblyName).GetType(className);
  168. if (t == null)
  169. {
  170. throw new Exception(string.Format("ioProvider config file class and assembly not valid," + source));
  171. }
  172. IIoProvider provider;
  173. try
  174. {
  175. provider = (IIoProvider)Activator.CreateInstance(t);
  176. _providers.Add(provider);
  177. }
  178. catch (Exception ex)
  179. {
  180. //LOG.WriteExeption(ex);
  181. throw new Exception(string.Format("ioProvider can not be created," + source));
  182. }
  183. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  184. XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");
  185. foreach (var nodeBlock in nodeBlocks)
  186. {
  187. XmlElement blockElement = nodeBlock as XmlElement;
  188. if (blockElement == null)
  189. continue;
  190. IoBlockItem section = new IoBlockItem();
  191. string type = blockElement.GetAttribute("type");
  192. string offset = blockElement.GetAttribute("offset");
  193. string size = blockElement.GetAttribute("size");
  194. string value_type = blockElement.GetAttribute("value_type");
  195. int result;
  196. if (!int.TryParse(offset, out result))
  197. {
  198. continue;
  199. }
  200. section.Offset = result;
  201. if (!int.TryParse(size, out result))
  202. {
  203. continue;
  204. }
  205. section.Size = (ushort)result;
  206. switch (type.ToLower())
  207. {
  208. case "ai":
  209. section.Type = IoType.AI;
  210. break;
  211. case "ao":
  212. section.Type = IoType.AO;
  213. break;
  214. case "di":
  215. section.Type = IoType.DI;
  216. break;
  217. case "do":
  218. section.Type = IoType.DO;
  219. break;
  220. default:
  221. continue;
  222. }
  223. lstBuffers.Add(section);
  224. }
  225. string mapModule = element.GetAttribute("map_module").Trim();
  226. string mapFile = element.GetAttribute("map_file").Trim();
  227. FileInfo file = new FileInfo(xmlConfigFile);
  228. string fullPathName = file.Directory.FullName + "\\" + mapFile;
  229. provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, fullPathName, mapModule);
  230. provider.Start();
  231. }
  232. }
  233. catch (Exception ex)
  234. {
  235. throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
  236. }
  237. }
  238. public void Terminate()
  239. {
  240. try
  241. {
  242. foreach (var ioProvider in _providers)
  243. {
  244. ioProvider.Stop();
  245. }
  246. }
  247. catch (Exception ex)
  248. {
  249. //LOG.WriteExeption(ex);
  250. }
  251. }
  252. public void Reset()
  253. {
  254. try
  255. {
  256. foreach (var ioProvider in _providers)
  257. {
  258. ioProvider.Reset();
  259. }
  260. }
  261. catch (Exception ex)
  262. {
  263. //LOG.WriteExeption(ex);
  264. }
  265. }
  266. }
  267. }