InterlockManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using Aitex.Core.RT.Event;
  5. using Aitex.Core.Util;
  6. using Aitex.Core.RT.Log;
  7. namespace Aitex.Core.RT.IOCore
  8. {
  9. public class InterlockManager : Singleton<InterlockManager>
  10. {
  11. private List<InterlockAction> _actions = new List<InterlockAction>();
  12. private Dictionary<InterlockLimit, List<InterlockAction>> _dicLimit = new Dictionary<InterlockLimit, List<InterlockAction>>();
  13. public bool Initialize(string interlockFile, Dictionary<string, DOAccessor> doMap, Dictionary<string, DIAccessor> diMap, out string reason)
  14. {
  15. reason = string.Empty;
  16. try
  17. {
  18. XmlDocument xmlConfig = new XmlDocument();
  19. xmlConfig.Load(interlockFile);
  20. XmlNode nodeInterlock = xmlConfig.SelectSingleNode("Interlock");
  21. foreach (XmlNode item in nodeInterlock.ChildNodes)
  22. {
  23. if (item.NodeType == XmlNodeType.Comment)
  24. continue;
  25. XmlElement actionNode = item as XmlElement;
  26. if (actionNode == null) continue;
  27. if (actionNode.Name != "Action")
  28. {
  29. if (actionNode.NodeType != XmlNodeType.Comment)
  30. {
  31. //LOG.Write("interlock config file contains no comments content, " + actionNode.InnerXml);
  32. }
  33. continue;
  34. }
  35. if (!actionNode.HasAttribute("do") || !actionNode.HasAttribute("value"))
  36. {
  37. reason += "action node has no [do] or [value] attribute \r\n";
  38. continue;
  39. }
  40. string doName = actionNode.GetAttribute("do");
  41. bool value = Convert.ToBoolean(actionNode.GetAttribute("value"));
  42. string tip = string.Empty;
  43. Dictionary<string, string> cultureTip = new Dictionary<string, string>();
  44. List<InterlockLimit> limits = new List<InterlockLimit>();
  45. if (!doMap.ContainsKey(doName))
  46. {
  47. reason += "action node " + doName + " no such DO defined \r\n";
  48. continue;
  49. }
  50. DOAccessor doItem = doMap[doName] as DOAccessor;
  51. if (doItem == null)
  52. {
  53. reason += "action node " + doName + " no such DO defined \r\n";
  54. continue;
  55. }
  56. if (actionNode.HasAttribute("tip"))
  57. tip = actionNode.GetAttribute("tip");
  58. if (actionNode.HasAttribute("tip.zh-CN"))
  59. cultureTip["zh-CN"] = actionNode.GetAttribute("tip.zh-CN");
  60. if (actionNode.HasAttribute("tip.en-US"))
  61. cultureTip["en-US"] = actionNode.GetAttribute("tip.en-US");
  62. foreach (XmlElement limitNode in actionNode.ChildNodes)
  63. {
  64. if (limitNode.Name != "Limit")
  65. {
  66. if (limitNode.NodeType != XmlNodeType.Comment)
  67. {
  68. //LOG.Write("interlock config file contains no comments content, " + limitNode.InnerXml);
  69. }
  70. continue;
  71. }
  72. if (!(limitNode.HasAttribute("di") || limitNode.HasAttribute("do")) || !limitNode.HasAttribute("value"))
  73. {
  74. reason += "limit node lack of di/do or value attribute \r\n";
  75. continue;
  76. }
  77. string stringValue = limitNode.GetAttribute("value");
  78. if (stringValue.Contains("*"))
  79. {
  80. continue;
  81. }
  82. bool limitValue = Convert.ToBoolean(limitNode.GetAttribute("value"));
  83. string limitTip = string.Empty;
  84. Dictionary<string, string> limitCultureTip = new Dictionary<string, string>();
  85. if (limitNode.HasAttribute("tip"))
  86. limitTip = limitNode.GetAttribute("tip");
  87. if (limitNode.HasAttribute("tip.zh-CN"))
  88. limitCultureTip["zh-CN"] = limitNode.GetAttribute("tip.zh-CN");
  89. if (limitNode.HasAttribute("tip.en-US"))
  90. limitCultureTip["en-US"] = limitNode.GetAttribute("tip.en-US");
  91. if (limitNode.HasAttribute("di"))
  92. {
  93. string diName = limitNode.GetAttribute("di");
  94. if (!diMap.ContainsKey(diName))
  95. {
  96. reason += "limit node " + diName + " no such DI defined \r\n";
  97. continue;
  98. }
  99. DIAccessor diItem = diMap[diName] as DIAccessor;
  100. if (diItem == null)
  101. {
  102. reason += "limit node " + diName + " no such DI defined \r\n";
  103. continue;
  104. }
  105. limits.Add(new DiLimit(diItem, limitValue, limitTip, limitCultureTip));
  106. }
  107. else if (limitNode.HasAttribute("do"))
  108. {
  109. string ioName = limitNode.GetAttribute("do");
  110. if (!doMap.ContainsKey(ioName))
  111. {
  112. reason += "limit node " + ioName + " no such DO defined \r\n";
  113. continue;
  114. }
  115. DOAccessor ioItem = doMap[ioName];
  116. if (ioItem == null)
  117. {
  118. reason += "limit node " + ioName + " no such DO defined \r\n";
  119. continue;
  120. }
  121. limits.Add(new DoLimit(ioItem, limitValue, limitTip, limitCultureTip));
  122. }
  123. }
  124. InterlockAction action = new InterlockAction(doItem, value, tip, cultureTip, limits);
  125. _actions.Add(action);
  126. foreach (var interlockLimit in limits)
  127. {
  128. bool isExist = false;
  129. foreach (var limit in _dicLimit.Keys)
  130. {
  131. if (interlockLimit.IsSame(limit))
  132. {
  133. _dicLimit[limit].Add(action);
  134. isExist = true;
  135. break;
  136. }
  137. }
  138. if (!isExist)
  139. {
  140. _dicLimit[interlockLimit] = new List<InterlockAction>();
  141. _dicLimit[interlockLimit].Add(action);
  142. }
  143. }
  144. }
  145. }
  146. catch (Exception ex)
  147. {
  148. reason += ex.Message;
  149. }
  150. if (!string.IsNullOrEmpty(reason))
  151. return false;
  152. return true;
  153. }
  154. public bool Initialize(string chamberName,string interlockFile, Dictionary<string, DOAccessor> doMap, Dictionary<string, DIAccessor> diMap, out string reason)
  155. {
  156. reason = string.Empty;
  157. try
  158. {
  159. XmlDocument xmlConfig = new XmlDocument();
  160. xmlConfig.Load(interlockFile);
  161. XmlNode nodeInterlock = xmlConfig.SelectSingleNode("Interlock");
  162. foreach (XmlNode item in nodeInterlock.ChildNodes)
  163. {
  164. if (item.NodeType == XmlNodeType.Comment)
  165. continue;
  166. XmlElement actionNode = item as XmlElement;
  167. if (actionNode == null) continue;
  168. if (actionNode.Name != "Action")
  169. {
  170. if (actionNode.NodeType != XmlNodeType.Comment)
  171. {
  172. //LOG.Write("interlock config file contains no comments content, " + actionNode.InnerXml);
  173. }
  174. continue;
  175. }
  176. if (!actionNode.HasAttribute("do") || !actionNode.HasAttribute("value"))
  177. {
  178. reason += "action node has no [do] or [value] attribute \r\n";
  179. continue;
  180. }
  181. string doName = $"{chamberName}.{actionNode.GetAttribute("do")}" ;
  182. bool value = Convert.ToBoolean(actionNode.GetAttribute("value"));
  183. string tip = string.Empty;
  184. Dictionary<string, string> cultureTip = new Dictionary<string, string>();
  185. List<InterlockLimit> limits = new List<InterlockLimit>();
  186. if (!doMap.ContainsKey(doName))
  187. {
  188. reason += "action node " + doName + " no such DO defined \r\n";
  189. continue;
  190. }
  191. DOAccessor doItem = doMap[doName] as DOAccessor;
  192. if (doItem == null)
  193. {
  194. reason += "action node " + doName + " no such DO defined \r\n";
  195. continue;
  196. }
  197. if (actionNode.HasAttribute("tip"))
  198. tip = actionNode.GetAttribute("tip");
  199. if (actionNode.HasAttribute("tip.zh-CN"))
  200. cultureTip["zh-CN"] = actionNode.GetAttribute("tip.zh-CN");
  201. if (actionNode.HasAttribute("tip.en-US"))
  202. cultureTip["en-US"] = actionNode.GetAttribute("tip.en-US");
  203. foreach (XmlElement limitNode in actionNode.ChildNodes)
  204. {
  205. if (limitNode.Name != "Limit")
  206. {
  207. if (limitNode.NodeType != XmlNodeType.Comment)
  208. {
  209. //LOG.Write("interlock config file contains no comments content, " + limitNode.InnerXml);
  210. }
  211. continue;
  212. }
  213. if (!(limitNode.HasAttribute("di") || limitNode.HasAttribute("do")) || !limitNode.HasAttribute("value"))
  214. {
  215. reason += "limit node lack of di/do or value attribute \r\n";
  216. continue;
  217. }
  218. string stringValue = limitNode.GetAttribute("value");
  219. if (stringValue.Contains("*"))
  220. {
  221. continue;
  222. }
  223. bool limitValue = Convert.ToBoolean(limitNode.GetAttribute("value"));
  224. string limitTip = string.Empty;
  225. Dictionary<string, string> limitCultureTip = new Dictionary<string, string>();
  226. if (limitNode.HasAttribute("tip"))
  227. limitTip = limitNode.GetAttribute("tip");
  228. if (limitNode.HasAttribute("tip.zh-CN"))
  229. limitCultureTip["zh-CN"] = limitNode.GetAttribute("tip.zh-CN");
  230. if (limitNode.HasAttribute("tip.en-US"))
  231. limitCultureTip["en-US"] = limitNode.GetAttribute("tip.en-US");
  232. if (limitNode.HasAttribute("di"))
  233. {
  234. string diName = $"{chamberName}.{limitNode.GetAttribute("di")}";
  235. if (!diMap.ContainsKey(diName))
  236. {
  237. reason += "limit node " + diName + " no such DI defined \r\n";
  238. continue;
  239. }
  240. DIAccessor diItem = diMap[diName] as DIAccessor;
  241. if (diItem == null)
  242. {
  243. reason += "limit node " + diName + " no such DI defined \r\n";
  244. continue;
  245. }
  246. limits.Add(new DiLimit(diItem, limitValue, limitTip, limitCultureTip));
  247. }
  248. else if (limitNode.HasAttribute("do"))
  249. {
  250. string ioName = $"{chamberName}.{limitNode.GetAttribute("do")}";
  251. if (!doMap.ContainsKey(ioName))
  252. {
  253. reason += "limit node " + ioName + " no such DO defined \r\n";
  254. continue;
  255. }
  256. DOAccessor ioItem = doMap[ioName];
  257. if (ioItem == null)
  258. {
  259. reason += "limit node " + ioName + " no such DO defined \r\n";
  260. continue;
  261. }
  262. limits.Add(new DoLimit(ioItem, limitValue, limitTip, limitCultureTip));
  263. }
  264. }
  265. InterlockAction action = new InterlockAction(doItem, value, tip, cultureTip, limits);
  266. _actions.Add(action);
  267. foreach (var interlockLimit in limits)
  268. {
  269. bool isExist = false;
  270. foreach (var limit in _dicLimit.Keys)
  271. {
  272. if (interlockLimit.IsSame(limit))
  273. {
  274. _dicLimit[limit].Add(action);
  275. isExist = true;
  276. break;
  277. }
  278. }
  279. if (!isExist)
  280. {
  281. _dicLimit[interlockLimit] = new List<InterlockAction>();
  282. _dicLimit[interlockLimit].Add(action);
  283. }
  284. }
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. reason += ex.Message;
  290. }
  291. if (!string.IsNullOrEmpty(reason))
  292. return false;
  293. return true;
  294. }
  295. public void Monitor()
  296. {
  297. foreach (var limit in _dicLimit)
  298. {
  299. if (!limit.Key.IsTriggered())
  300. continue;
  301. string info = string.Empty;
  302. foreach (var action in limit.Value)
  303. {
  304. string reason;
  305. if (!action.Reverse(out reason))
  306. continue;
  307. if (string.IsNullOrEmpty(info))
  308. {
  309. info = string.Format("Due to the {0}, {1}=[{2}]\n", limit.Key.Tip, limit.Key.Name,
  310. !limit.Key.LimitValue);
  311. }
  312. info += reason + "\n";
  313. }
  314. if (!string.IsNullOrEmpty(info))
  315. {
  316. LOG.Write(eEvent.ERR_INTERLOCK_FAIL, "System", info.TrimEnd('\n'));
  317. }
  318. }
  319. }
  320. public bool CanSetDo(string doName, bool onOff, out string reason)
  321. {
  322. reason = string.Empty;
  323. foreach (var interlockAction in _actions)
  324. {
  325. if (interlockAction.IsSame(doName, onOff))
  326. {
  327. return interlockAction.CanDo(out reason);
  328. }
  329. }
  330. return true;
  331. }
  332. }
  333. }