InterlockManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Xml;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.SCCore;
  10. using Aitex.Core.Util;
  11. using MECF.Framework.Common.Equipment;
  12. namespace Aitex.Core.RT.IOCore
  13. {
  14. public class InterlockManager : Singleton<InterlockManager>
  15. {
  16. private List<InterlockAction> _actions = new List<InterlockAction>();
  17. private Dictionary<InterlockLimit, List<InterlockAction>> _dicLimit = new Dictionary<InterlockLimit, List<InterlockAction>>();
  18. public bool Initialize(string interlockFile, Dictionary<string, DOAccessor> doMap, Dictionary<string, DIAccessor> diMap, out string reason)
  19. {
  20. reason = string.Empty;
  21. try
  22. {
  23. XmlDocument xmlConfig = new XmlDocument();
  24. xmlConfig.Load(interlockFile);
  25. //<Action do="DO_RF_power_on_off" value="true" tip="" tip.zh-CN="" tip.en-US="">
  26. // <Limit di="DI_RF_hardware_interlock" value="true" tip="" tip.zh-CN="" tip.en-US="" />
  27. // <Limit di="DI_Chamber_door_sw" value="true" tip="" tip.zh-CN="" tip.en-US="" />
  28. //</Action>
  29. XmlNode nodeInterlock = xmlConfig.SelectSingleNode("Interlock");
  30. foreach (XmlNode item in nodeInterlock.ChildNodes)
  31. {
  32. if (item.NodeType == XmlNodeType.Comment)
  33. continue;
  34. XmlElement actionNode = item as XmlElement;
  35. if (actionNode == null) continue;
  36. if (actionNode.Name != "Action")
  37. {
  38. if (actionNode.NodeType != XmlNodeType.Comment)
  39. {
  40. LOG.Write("interlock config file contains no comments content, " + actionNode.InnerXml);
  41. }
  42. continue;
  43. }
  44. if (!actionNode.HasAttribute("do") || !actionNode.HasAttribute("value"))
  45. {
  46. reason += "action node has no [do] or [value] attribute \r\n";
  47. continue;
  48. }
  49. string doName = actionNode.GetAttribute("do");
  50. bool value = Convert.ToBoolean(actionNode.GetAttribute("value"));
  51. string tip = string.Empty;
  52. Dictionary<string, string> cultureTip = new Dictionary<string, string>();
  53. List<InterlockLimit> limits = new List<InterlockLimit>();
  54. if (!doMap.ContainsKey(doName))
  55. {
  56. reason += "action node " + doName + " no such DO defined \r\n";
  57. continue;
  58. }
  59. DOAccessor doItem = doMap[doName] as DOAccessor;
  60. if (doItem == null)
  61. {
  62. reason += "action node " + doName + " no such DO defined \r\n";
  63. continue;
  64. }
  65. if (actionNode.HasAttribute("tip"))
  66. tip = actionNode.GetAttribute("tip");
  67. if (actionNode.HasAttribute("tip.zh-CN"))
  68. cultureTip["zh-CN"] = actionNode.GetAttribute("tip.zh-CN");
  69. if (actionNode.HasAttribute("tip.en-US"))
  70. cultureTip["en-US"] = actionNode.GetAttribute("tip.en-US");
  71. foreach (XmlElement limitNode in actionNode.ChildNodes)
  72. {
  73. if (limitNode.Name != "Limit")
  74. {
  75. if (limitNode.NodeType != XmlNodeType.Comment)
  76. {
  77. LOG.Write("interlock config file contains no comments content, " + limitNode.InnerXml);
  78. }
  79. continue;
  80. }
  81. if (!(limitNode.HasAttribute("di") || limitNode.HasAttribute("do")) || !limitNode.HasAttribute("value"))
  82. {
  83. reason += "limit node lack of di/do or value attribute \r\n";
  84. continue;
  85. }
  86. string stringValue = limitNode.GetAttribute("value");
  87. if (stringValue.Contains("*"))
  88. {
  89. continue;
  90. }
  91. bool limitValue = Convert.ToBoolean(limitNode.GetAttribute("value"));
  92. string limitTip = string.Empty;
  93. Dictionary<string, string> limitCultureTip = new Dictionary<string, string>();
  94. if (limitNode.HasAttribute("tip"))
  95. limitTip = limitNode.GetAttribute("tip");
  96. if (limitNode.HasAttribute("tip.zh-CN"))
  97. limitCultureTip["zh-CN"] = limitNode.GetAttribute("tip.zh-CN");
  98. if (limitNode.HasAttribute("tip.en-US"))
  99. limitCultureTip["en-US"] = limitNode.GetAttribute("tip.en-US");
  100. if (limitNode.HasAttribute("di"))
  101. {
  102. string diName = limitNode.GetAttribute("di");
  103. if (!diMap.ContainsKey(diName))
  104. {
  105. reason += "limit node " + diName + " no such DI defined \r\n";
  106. continue;
  107. }
  108. DIAccessor diItem = diMap[diName] as DIAccessor;
  109. if (diItem == null)
  110. {
  111. reason += "limit node " + diName + " no such DI defined \r\n";
  112. continue;
  113. }
  114. limits.Add(new DiLimit(diItem, limitValue, limitTip, limitCultureTip));
  115. }
  116. else if (limitNode.HasAttribute("do"))
  117. {
  118. string ioName = limitNode.GetAttribute("do");
  119. if (!doMap.ContainsKey(ioName))
  120. {
  121. reason += "limit node " + ioName + " no such DO defined \r\n";
  122. continue;
  123. }
  124. DOAccessor ioItem = doMap[ioName];
  125. if (ioItem == null)
  126. {
  127. reason += "limit node " + ioName + " no such DO defined \r\n";
  128. continue;
  129. }
  130. limits.Add(new DoLimit(ioItem, limitValue, limitTip, limitCultureTip));
  131. }
  132. }
  133. InterlockAction action = new InterlockAction(doItem, value, tip, cultureTip, limits);
  134. _actions.Add(action);
  135. foreach (var interlockLimit in limits)
  136. {
  137. bool isExist = false;
  138. foreach (var limit in _dicLimit.Keys)
  139. {
  140. if (interlockLimit.IsSame(limit))
  141. {
  142. _dicLimit[limit].Add(action);
  143. isExist = true;
  144. break;
  145. }
  146. }
  147. if (!isExist)
  148. {
  149. _dicLimit[interlockLimit] = new List<InterlockAction>();
  150. _dicLimit[interlockLimit].Add(action);
  151. }
  152. }
  153. }
  154. }
  155. catch (Exception ex)
  156. {
  157. reason += ex.Message;
  158. }
  159. if (!string.IsNullOrEmpty(reason))
  160. return false;
  161. return true;
  162. }
  163. public void Monitor()
  164. {
  165. foreach (var limit in _dicLimit)
  166. {
  167. if (SC.ContainsItem("System.BypassInterlock") && SC.GetValue<bool>("System.BypassInterlock"))
  168. continue;
  169. if (!limit.Key.IsTriggered())
  170. continue;
  171. string info = string.Empty;
  172. string module = "System";
  173. foreach (var action in limit.Value)
  174. {
  175. string reason;
  176. if (!action.Reverse(out reason))
  177. continue;
  178. if (string.IsNullOrEmpty(info))
  179. {
  180. info = string.Format("Due to the {0}, {1}=[{2}]\n", limit.Key.Tip, limit.Key.Name,
  181. !limit.Key.LimitValue);
  182. }
  183. var nameArray = action.ActionName.Split('.');
  184. if (nameArray != null && nameArray.Length > 1 && ModuleHelper.IsPm(nameArray[0]))
  185. module = nameArray[0];
  186. info += reason + "\n";
  187. }
  188. if (!string.IsNullOrEmpty(info))
  189. {
  190. EV.PostAlarmLog(module, info.TrimEnd('\n'));
  191. }
  192. }
  193. }
  194. public bool CanSetDo(string doName, bool onOff, out string reason)
  195. {
  196. reason = string.Empty;
  197. if (SC.ContainsItem("System.BypassInterlock") && SC.GetValue<bool>("System.BypassInterlock"))
  198. return true;
  199. foreach (var interlockAction in _actions)
  200. {
  201. if (interlockAction.IsSame(doName, onOff))
  202. {
  203. return interlockAction.CanDo(out reason);
  204. }
  205. }
  206. return true;
  207. }
  208. }
  209. }