123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Xml;
- using Aitex.Core.RT.Event;
- namespace Aitex.Core.RT.IOCore
- {
- class 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, object> ioMap, 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 (XmlElement actionNode in nodeInterlock.ChildNodes)
- {
- 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 (!ioMap.ContainsKey(doName))
- {
- reason += "action node " + doName + " no such DO defined \r\n";
- continue;
- }
- DOAccessor doItem = ioMap[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.HasAttribute("di") || !limitNode.HasAttribute("value"))
- {
- reason += "limit node lack of di or value attribute \r\n";
- continue;
- }
- string stringValue = limitNode.GetAttribute("value");
- if (stringValue.Contains("*"))
- {
- continue;
- }
- string diName = limitNode.GetAttribute("di");
- bool limitValue = Convert.ToBoolean(limitNode.GetAttribute("value"));
- string limitTip = string.Empty;
- string limitModule = "System";
- Dictionary<string, string> limitCultureTip = new Dictionary<string, string>();
- if (!ioMap.ContainsKey(diName))
- {
- reason += "limit node " + diName + " no such DI defined \r\n";
- continue;
- }
- DIAccessor diItem = ioMap[diName] as DIAccessor;
- if (diItem == null)
- {
- reason += "limit node " + diName + " no such DI defined \r\n";
- continue;
- }
- if (limitNode.HasAttribute("tip"))
- limitTip = limitNode.GetAttribute("tip");
- 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("module"))
- limitModule = limitNode.GetAttribute("module");
- limits.Add(new InterlockLimit(diItem, limitValue, limitTip, limitCultureTip, limitModule));
- }
-
- 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 (!limit.Key.IsTriggered())
- continue;
- string info = string.Empty;
- 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.Value);
- }
- info += reason + "\n";
- }
- if (!string.IsNullOrEmpty(info))
- {
- EV.PostMessage(limit.Key.Module, EventEnum.DefaultAlarm, info.TrimEnd('\n'));
- }
- }
- }
- public bool CanSetDo(string doName, bool onOff, out string reason)
- {
- reason = string.Empty;
- foreach (var interlockAction in _actions)
- {
- if (interlockAction.IsSame(doName, onOff))
- {
- return interlockAction.CanDo(out reason);
- }
- }
- return true;
- }
- }
- }
|