IoProviderManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 = 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. if (SC.ContainsItem($"System.SetUp.BlockNumber") && SC.GetStringValue($"System.SetUp.BlockNumber") != "4Block" &&
  177. (name.ToUpper() == "SPN5" || name.ToUpper() == "SPN6" || name.ToUpper() == "SPN7" || name.ToUpper() == "SPN8"))
  178. {
  179. continue;
  180. }
  181. string source = module + "." + name;
  182. Type t = Assembly.Load(assemblyName).GetType(className);
  183. if (t == null)
  184. {
  185. throw new Exception(string.Format("ioProvider config file class and assembly not valid," + source));
  186. }
  187. IIoProvider provider;
  188. try
  189. {
  190. provider = (IIoProvider)Activator.CreateInstance(t);
  191. _providers.Add(provider);
  192. _dicProviders[source] = provider;
  193. }
  194. catch (Exception ex)
  195. {
  196. LOG.Write(ex);
  197. throw new Exception(string.Format("ioProvider can not be created," + source));
  198. }
  199. List<IoBlockItem> lstBuffers = new List<IoBlockItem>();
  200. XmlNodeList nodeBlocks = element.SelectNodes("Blocks/Block");
  201. foreach (var nodeBlock in nodeBlocks)
  202. {
  203. XmlElement blockElement = nodeBlock as XmlElement;
  204. if (blockElement == null)
  205. continue;
  206. IoBlockItem section = new IoBlockItem();
  207. string type = blockElement.GetAttribute("type");
  208. string offset = blockElement.GetAttribute("offset");
  209. string size = blockElement.GetAttribute("size");
  210. string value_type = blockElement.GetAttribute("value_type");
  211. int result;
  212. if (!int.TryParse(offset, out result))
  213. {
  214. continue;
  215. }
  216. section.Offset = result;
  217. if (!int.TryParse(size, out result))
  218. {
  219. continue;
  220. }
  221. section.Size = result;
  222. switch (type.ToLower())
  223. {
  224. case "ai":
  225. section.Type = IoType.AI;
  226. break;
  227. case "ao":
  228. section.Type = IoType.AO;
  229. break;
  230. case "di":
  231. section.Type = IoType.DI;
  232. break;
  233. case "do":
  234. section.Type = IoType.DO;
  235. break;
  236. default:
  237. continue;
  238. }
  239. if (section.Type == IoType.AI || section.Type == IoType.AO)
  240. {
  241. section.AIOType = (string.IsNullOrEmpty(value_type) || value_type.ToLower() != "float") ? typeof(short) : typeof(float);
  242. }
  243. lstBuffers.Add(section);
  244. }
  245. string mapModule = element.GetAttribute("map_module").Trim();
  246. string mapFile = element.GetAttribute("map_file").Trim();
  247. FileInfo file = new FileInfo(xmlConfigFile);
  248. string fullPathName = file.Directory.FullName + "\\" + mapFile;
  249. provider.Initialize(module, name, lstBuffers, IoManager.Instance, nodeParameter, fullPathName, mapModule);
  250. provider.Start();
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. throw new ApplicationException("IoProvider configuration not valid," + ex.Message);
  256. }
  257. }
  258. public void Terminate()
  259. {
  260. try
  261. {
  262. foreach (var ioProvider in _providers)
  263. {
  264. ioProvider.Stop();
  265. }
  266. }
  267. catch (Exception ex)
  268. {
  269. LOG.Write(ex);
  270. }
  271. }
  272. public void Reset()
  273. {
  274. try
  275. {
  276. foreach (var ioProvider in _providers)
  277. {
  278. ioProvider.Reset();
  279. }
  280. }
  281. catch (Exception ex)
  282. {
  283. LOG.Write(ex);
  284. }
  285. }
  286. }
  287. }