InterlockManager.cs 8.9 KB

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