InterlockManager.cs 9.3 KB

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