InterlockManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. namespace Aitex.Core.RT.IOCore
  9. {
  10. class 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, object> ioMap, out string reason)
  15. {
  16. reason = string.Empty;
  17. try
  18. {
  19. XmlDocument xmlConfig = new XmlDocument();
  20. xmlConfig.Load(interlockFile);
  21. //<Action do="DO_RF_power_on_off" value="true" tip="" tip.zh-CN="" tip.en-US="">
  22. // <Limit di="DI_RF_hardware_interlock" value="true" tip="" tip.zh-CN="" tip.en-US="" />
  23. // <Limit di="DI_Chamber_door_sw" value="true" tip="" tip.zh-CN="" tip.en-US="" />
  24. //</Action>
  25. XmlNode nodeInterlock = xmlConfig.SelectSingleNode("Interlock");
  26. foreach (XmlElement actionNode in nodeInterlock.ChildNodes)
  27. {
  28. if (!actionNode.HasAttribute("do") || !actionNode.HasAttribute("value"))
  29. {
  30. reason += "action node has no [do] or [value] attribute \r\n";
  31. continue;
  32. }
  33. string doName = actionNode.GetAttribute("do");
  34. bool value = Convert.ToBoolean(actionNode.GetAttribute("value"));
  35. string tip = string.Empty;
  36. Dictionary<string, string> cultureTip = new Dictionary<string, string>();
  37. List<InterlockLimit> limits = new List<InterlockLimit>();
  38. if (!ioMap.ContainsKey(doName))
  39. {
  40. reason += "action node " + doName + " no such DO defined \r\n";
  41. continue;
  42. }
  43. DOAccessor doItem = ioMap[doName] as DOAccessor;
  44. if (doItem == null)
  45. {
  46. reason += "action node " + doName + " no such DO defined \r\n";
  47. continue;
  48. }
  49. if (actionNode.HasAttribute("tip"))
  50. tip = actionNode.GetAttribute("tip");
  51. if (actionNode.HasAttribute("tip.zh-CN"))
  52. cultureTip["zh-CN"] = actionNode.GetAttribute("tip.zh-CN");
  53. if (actionNode.HasAttribute("tip.en-US"))
  54. cultureTip["en-US"] = actionNode.GetAttribute("tip.en-US");
  55. foreach (XmlElement limitNode in actionNode.ChildNodes)
  56. {
  57. if (!limitNode.HasAttribute("di") || !limitNode.HasAttribute("value"))
  58. {
  59. reason += "limit node lack of di or value attribute \r\n";
  60. continue;
  61. }
  62. string stringValue = limitNode.GetAttribute("value");
  63. if (stringValue.Contains("*"))
  64. {
  65. continue;
  66. }
  67. string diName = limitNode.GetAttribute("di");
  68. bool limitValue = Convert.ToBoolean(limitNode.GetAttribute("value"));
  69. string limitTip = string.Empty;
  70. string limitModule = "System";
  71. Dictionary<string, string> limitCultureTip = new Dictionary<string, string>();
  72. if (!ioMap.ContainsKey(diName))
  73. {
  74. reason += "limit node " + diName + " no such DI defined \r\n";
  75. continue;
  76. }
  77. DIAccessor diItem = ioMap[diName] as DIAccessor;
  78. if (diItem == null)
  79. {
  80. reason += "limit node " + diName + " no such DI defined \r\n";
  81. continue;
  82. }
  83. if (limitNode.HasAttribute("tip"))
  84. limitTip = limitNode.GetAttribute("tip");
  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("module"))
  90. limitModule = limitNode.GetAttribute("module");
  91. limits.Add(new InterlockLimit(diItem, limitValue, limitTip, limitCultureTip, limitModule));
  92. }
  93. InterlockAction action = new InterlockAction(doItem, value, tip, cultureTip, limits);
  94. _actions.Add(action);
  95. foreach (var interlockLimit in limits)
  96. {
  97. bool isExist = false;
  98. foreach (var limit in _dicLimit.Keys)
  99. {
  100. if (interlockLimit.IsSame(limit))
  101. {
  102. _dicLimit[limit].Add(action);
  103. isExist = true;
  104. break;
  105. }
  106. }
  107. if (!isExist)
  108. {
  109. _dicLimit[interlockLimit] = new List<InterlockAction>();
  110. _dicLimit[interlockLimit].Add(action);
  111. }
  112. }
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. reason += ex.Message;
  118. }
  119. if (!string.IsNullOrEmpty(reason))
  120. return false;
  121. return true;
  122. }
  123. public void Monitor()
  124. {
  125. foreach (var limit in _dicLimit)
  126. {
  127. if (!limit.Key.IsTriggered())
  128. continue;
  129. string info = string.Empty;
  130. foreach (var action in limit.Value)
  131. {
  132. string reason;
  133. if (!action.Reverse(out reason))
  134. continue;
  135. if (string.IsNullOrEmpty(info))
  136. {
  137. info = string.Format("Due to the {0}, {1}=[{2}]\n", limit.Key.Tip, limit.Key.Name,
  138. !limit.Key.Value);
  139. }
  140. info += reason + "\n";
  141. }
  142. if (!string.IsNullOrEmpty(info))
  143. {
  144. EV.PostMessage(limit.Key.Module, EventEnum.DefaultAlarm, info.TrimEnd('\n'));
  145. }
  146. }
  147. }
  148. public bool CanSetDo(string doName, bool onOff, out string reason)
  149. {
  150. reason = string.Empty;
  151. foreach (var interlockAction in _actions)
  152. {
  153. if (interlockAction.IsSame(doName, onOff))
  154. {
  155. return interlockAction.CanDo(out reason);
  156. }
  157. }
  158. return true;
  159. }
  160. }
  161. }