123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Xml;
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.Log;
- using Aitex.Core.RT.SCCore;
- using Aitex.Core.Util;
- using MECF.Framework.Common.Equipment;
- namespace Aitex.Core.RT.IOCore
- {
- public class InterlockManager : Singleton<InterlockManager>
- {
- private List<InterlockAction> _actions = new List<InterlockAction>();
- private Dictionary<InterlockLimit, List<InterlockAction>> _dicLimit = new Dictionary<InterlockLimit, List<InterlockAction>>();
-
- public bool Initialize(string interlockFile, Dictionary<string, DOAccessor> doMap, Dictionary<string, DIAccessor> diMap, out string reason)
- {
- reason = string.Empty;
- try
- {
- XmlDocument xmlConfig = new XmlDocument();
- xmlConfig.Load(interlockFile);
- //<Action do="DO_RF_power_on_off" value="true" tip="" tip.zh-CN="" tip.en-US="">
- // <Limit di="DI_RF_hardware_interlock" value="true" tip="" tip.zh-CN="" tip.en-US="" />
- // <Limit di="DI_Chamber_door_sw" value="true" tip="" tip.zh-CN="" tip.en-US="" />
- //</Action>
- XmlNode nodeInterlock = xmlConfig.SelectSingleNode("Interlock");
- foreach (XmlNode item in nodeInterlock.ChildNodes)
- {
- if (item.NodeType == XmlNodeType.Comment)
- continue;
- XmlElement actionNode = item as XmlElement;
- if (actionNode == null) continue;
- if (actionNode.Name != "Action")
- {
- if (actionNode.NodeType != XmlNodeType.Comment)
- {
- LOG.Write("interlock config file contains no comments content, " + actionNode.InnerXml);
- }
- continue;
- }
- if (!actionNode.HasAttribute("do") || !actionNode.HasAttribute("value"))
- {
- reason += "action node has no [do] or [value] attribute \r\n";
- continue;
- }
- string doName = actionNode.GetAttribute("do");
- bool value = Convert.ToBoolean(actionNode.GetAttribute("value"));
- string tip = string.Empty;
- Dictionary<string, string> cultureTip = new Dictionary<string, string>();
- List<InterlockLimit> limits = new List<InterlockLimit>();
- if (!doMap.ContainsKey(doName))
- {
- reason += "action node " + doName + " no such DO defined \r\n";
- continue;
- }
- DOAccessor doItem = doMap[doName] as DOAccessor;
- if (doItem == null)
- {
- reason += "action node " + doName + " no such DO defined \r\n";
- continue;
- }
- if (actionNode.HasAttribute("tip"))
- tip = actionNode.GetAttribute("tip");
- if (actionNode.HasAttribute("tip.zh-CN"))
- cultureTip["zh-CN"] = actionNode.GetAttribute("tip.zh-CN");
- if (actionNode.HasAttribute("tip.en-US"))
- cultureTip["en-US"] = actionNode.GetAttribute("tip.en-US");
- foreach (XmlElement limitNode in actionNode.ChildNodes)
- {
- if (limitNode.Name != "Limit")
- {
- if (limitNode.NodeType != XmlNodeType.Comment)
- {
- LOG.Write("interlock config file contains no comments content, " + limitNode.InnerXml);
- }
- continue;
- }
- if (!(limitNode.HasAttribute("di") || limitNode.HasAttribute("do")) || !limitNode.HasAttribute("value"))
- {
- reason += "limit node lack of di/do or value attribute \r\n";
- continue;
- }
- string stringValue = limitNode.GetAttribute("value");
- if (stringValue.Contains("*"))
- {
- continue;
- }
- bool limitValue = Convert.ToBoolean(limitNode.GetAttribute("value"));
- string limitTip = string.Empty;
- Dictionary<string, string> limitCultureTip = new Dictionary<string, string>();
- if (limitNode.HasAttribute("tip"))
- limitTip = limitNode.GetAttribute("tip");
- if (limitNode.HasAttribute("tip.zh-CN"))
- limitCultureTip["zh-CN"] = limitNode.GetAttribute("tip.zh-CN");
- if (limitNode.HasAttribute("tip.en-US"))
- limitCultureTip["en-US"] = limitNode.GetAttribute("tip.en-US");
- if (limitNode.HasAttribute("di"))
- {
- string diName = limitNode.GetAttribute("di");
- if (!diMap.ContainsKey(diName))
- {
- reason += "limit node " + diName + " no such DI defined \r\n";
- continue;
- }
- DIAccessor diItem = diMap[diName] as DIAccessor;
- if (diItem == null)
- {
- reason += "limit node " + diName + " no such DI defined \r\n";
- continue;
- }
- limits.Add(new DiLimit(diItem, limitValue, limitTip, limitCultureTip));
- }
- else if (limitNode.HasAttribute("do"))
- {
- string ioName = limitNode.GetAttribute("do");
- if (!doMap.ContainsKey(ioName))
- {
- reason += "limit node " + ioName + " no such DO defined \r\n";
- continue;
- }
- DOAccessor ioItem = doMap[ioName];
- if (ioItem == null)
- {
- reason += "limit node " + ioName + " no such DO defined \r\n";
- continue;
- }
- limits.Add(new DoLimit(ioItem, limitValue, limitTip, limitCultureTip));
- }
- }
-
- InterlockAction action = new InterlockAction(doItem, value, tip, cultureTip, limits);
- _actions.Add(action);
- foreach (var interlockLimit in limits)
- {
- bool isExist = false;
- foreach (var limit in _dicLimit.Keys)
- {
- if (interlockLimit.IsSame(limit))
- {
- _dicLimit[limit].Add(action);
- isExist = true;
- break;
- }
- }
- if (!isExist)
- {
- _dicLimit[interlockLimit] = new List<InterlockAction>();
- _dicLimit[interlockLimit].Add(action);
- }
-
- }
- }
- }
- catch (Exception ex)
- {
- reason += ex.Message;
- }
- if (!string.IsNullOrEmpty(reason))
- return false;
- return true;
- }
- public void Monitor()
- {
- foreach (var limit in _dicLimit)
- {
- if (SC.ContainsItem("System.BypassInterlock") && SC.GetValue<bool>("System.BypassInterlock"))
- continue;
- if (!limit.Key.IsTriggered())
- continue;
- string info = string.Empty;
- string module = "System";
- foreach (var action in limit.Value)
- {
- string reason;
- if (!action.Reverse(out reason))
- continue;
- if (string.IsNullOrEmpty(info))
- {
- info = string.Format("Due to the {0}, {1}=[{2}]\n", limit.Key.Tip, limit.Key.Name,
- !limit.Key.LimitValue);
- }
- var nameArray = action.ActionName.Split('.');
- if (nameArray != null && nameArray.Length > 1 && ModuleHelper.IsPm(nameArray[0]))
- module = nameArray[0];
- info += reason + "\n";
- }
- if (!string.IsNullOrEmpty(info))
- {
- EV.PostAlarmLog(module, info.TrimEnd('\n'));
- }
- }
- }
- public bool CanSetDo(string doName, bool onOff, out string reason)
- {
- reason = string.Empty;
- if (SC.ContainsItem("System.BypassInterlock") && SC.GetValue<bool>("System.BypassInterlock"))
- return true;
- foreach (var interlockAction in _actions)
- {
- if (interlockAction.IsSame(doName, onOff))
- {
- return interlockAction.CanDo(out reason);
- }
- }
- return true;
- }
- }
- }
|