InterlockManager.cs 17 KB

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