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 _actions = new List(); private Dictionary> _dicLimit = new Dictionary>(); public bool Initialize(string interlockFile, Dictionary ioMap, out string reason) { reason = string.Empty; try { XmlDocument xmlConfig = new XmlDocument(); xmlConfig.Load(interlockFile); // // // // 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 cultureTip = new Dictionary(); List limits = new List(); 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 limitCultureTip = new Dictionary(); 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(); _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; } } }