InterlockManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 bool Initialize(string chamberName,string interlockFile, Dictionary<string, DOAccessor> doMap, Dictionary<string, DIAccessor> diMap, out string reason)
  154. {
  155. reason = string.Empty;
  156. try
  157. {
  158. XmlDocument xmlConfig = new XmlDocument();
  159. xmlConfig.Load(interlockFile);
  160. XmlNode nodeInterlock = xmlConfig.SelectSingleNode("Interlock");
  161. foreach (XmlNode item in nodeInterlock.ChildNodes)
  162. {
  163. if (item.NodeType == XmlNodeType.Comment)
  164. continue;
  165. XmlElement actionNode = item as XmlElement;
  166. if (actionNode == null) continue;
  167. if (actionNode.Name != "Action")
  168. {
  169. if (actionNode.NodeType != XmlNodeType.Comment)
  170. {
  171. //LOG.Write("interlock config file contains no comments content, " + actionNode.InnerXml);
  172. }
  173. continue;
  174. }
  175. if (!actionNode.HasAttribute("do") || !actionNode.HasAttribute("value"))
  176. {
  177. reason += "action node has no [do] or [value] attribute \r\n";
  178. continue;
  179. }
  180. string doName = $"{chamberName}.{actionNode.GetAttribute("do")}" ;
  181. bool value = Convert.ToBoolean(actionNode.GetAttribute("value"));
  182. string tip = string.Empty;
  183. Dictionary<string, string> cultureTip = new Dictionary<string, string>();
  184. List<InterlockLimit> limits = new List<InterlockLimit>();
  185. if (!doMap.ContainsKey(doName))
  186. {
  187. reason += "action node " + doName + " no such DO defined \r\n";
  188. continue;
  189. }
  190. DOAccessor doItem = doMap[doName] as DOAccessor;
  191. if (doItem == null)
  192. {
  193. reason += "action node " + doName + " no such DO defined \r\n";
  194. continue;
  195. }
  196. if (actionNode.HasAttribute("tip"))
  197. tip = actionNode.GetAttribute("tip");
  198. if (actionNode.HasAttribute("tip.zh-CN"))
  199. cultureTip["zh-CN"] = actionNode.GetAttribute("tip.zh-CN");
  200. if (actionNode.HasAttribute("tip.en-US"))
  201. cultureTip["en-US"] = actionNode.GetAttribute("tip.en-US");
  202. foreach (XmlElement limitNode in actionNode.ChildNodes)
  203. {
  204. if (limitNode.Name != "Limit")
  205. {
  206. if (limitNode.NodeType != XmlNodeType.Comment)
  207. {
  208. //LOG.Write("interlock config file contains no comments content, " + limitNode.InnerXml);
  209. }
  210. continue;
  211. }
  212. if (!(limitNode.HasAttribute("di") || limitNode.HasAttribute("do")) || !limitNode.HasAttribute("value"))
  213. {
  214. reason += "limit node lack of di/do or value attribute \r\n";
  215. continue;
  216. }
  217. string stringValue = limitNode.GetAttribute("value");
  218. if (stringValue.Contains("*"))
  219. {
  220. continue;
  221. }
  222. bool limitValue = Convert.ToBoolean(limitNode.GetAttribute("value"));
  223. string limitTip = string.Empty;
  224. Dictionary<string, string> limitCultureTip = new Dictionary<string, string>();
  225. if (limitNode.HasAttribute("tip"))
  226. limitTip = limitNode.GetAttribute("tip");
  227. if (limitNode.HasAttribute("tip.zh-CN"))
  228. limitCultureTip["zh-CN"] = limitNode.GetAttribute("tip.zh-CN");
  229. if (limitNode.HasAttribute("tip.en-US"))
  230. limitCultureTip["en-US"] = limitNode.GetAttribute("tip.en-US");
  231. if (limitNode.HasAttribute("di"))
  232. {
  233. string diName = $"{chamberName}.{limitNode.GetAttribute("di")}";
  234. if (!diMap.ContainsKey(diName))
  235. {
  236. reason += "limit node " + diName + " no such DI defined \r\n";
  237. continue;
  238. }
  239. DIAccessor diItem = diMap[diName] as DIAccessor;
  240. if (diItem == null)
  241. {
  242. reason += "limit node " + diName + " no such DI defined \r\n";
  243. continue;
  244. }
  245. limits.Add(new DiLimit(diItem, limitValue, limitTip, limitCultureTip));
  246. }
  247. else if (limitNode.HasAttribute("do"))
  248. {
  249. string ioName = $"{chamberName}.{limitNode.GetAttribute("do")}";
  250. if (!doMap.ContainsKey(ioName))
  251. {
  252. reason += "limit node " + ioName + " no such DO defined \r\n";
  253. continue;
  254. }
  255. DOAccessor ioItem = doMap[ioName];
  256. if (ioItem == null)
  257. {
  258. reason += "limit node " + ioName + " no such DO defined \r\n";
  259. continue;
  260. }
  261. limits.Add(new DoLimit(ioItem, limitValue, limitTip, limitCultureTip));
  262. }
  263. }
  264. InterlockAction action = new InterlockAction(doItem, value, tip, cultureTip, limits);
  265. _actions.Add(action);
  266. foreach (var interlockLimit in limits)
  267. {
  268. bool isExist = false;
  269. foreach (var limit in _dicLimit.Keys)
  270. {
  271. if (interlockLimit.IsSame(limit))
  272. {
  273. _dicLimit[limit].Add(action);
  274. isExist = true;
  275. break;
  276. }
  277. }
  278. if (!isExist)
  279. {
  280. _dicLimit[interlockLimit] = new List<InterlockAction>();
  281. _dicLimit[interlockLimit].Add(action);
  282. }
  283. }
  284. }
  285. }
  286. catch (Exception ex)
  287. {
  288. reason += ex.Message;
  289. }
  290. if (!string.IsNullOrEmpty(reason))
  291. return false;
  292. return true;
  293. }
  294. public void Monitor()
  295. {
  296. foreach (var limit in _dicLimit)
  297. {
  298. if (!limit.Key.IsTriggered())
  299. continue;
  300. string info = string.Empty;
  301. foreach (var action in limit.Value)
  302. {
  303. string reason;
  304. if (!action.Reverse(out reason))
  305. continue;
  306. if (string.IsNullOrEmpty(info))
  307. {
  308. info = string.Format("Due to the {0}, {1}=[{2}]\n", limit.Key.Tip, limit.Key.Name,
  309. !limit.Key.LimitValue);
  310. }
  311. info += reason + "\n";
  312. }
  313. if (!string.IsNullOrEmpty(info))
  314. {
  315. EV.PostAlarmLog("System", info.TrimEnd('\n'));
  316. }
  317. }
  318. }
  319. public bool CanSetDo(string doName, bool onOff, out string reason)
  320. {
  321. reason = string.Empty;
  322. foreach (var interlockAction in _actions)
  323. {
  324. if (interlockAction.IsSame(doName, onOff))
  325. {
  326. return interlockAction.CanDo(out reason);
  327. }
  328. }
  329. return true;
  330. }
  331. }
  332. }