| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 | 
							- using System;
 
- using System.Collections.Generic;
 
- using System.Xml;
 
- using Aitex.Core.RT.Event;
 
- using Aitex.Core.Util;
 
- using Aitex.Core.RT.Log;
 
- 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);
 
-                 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 bool Initialize(string chamberName,string interlockFile, Dictionary<string, DOAccessor> doMap, Dictionary<string, DIAccessor> diMap, out string reason)
 
-         {
 
-             reason = string.Empty;
 
-             try
 
-             {
 
-                 XmlDocument xmlConfig = new XmlDocument();
 
-                 xmlConfig.Load(interlockFile);
 
-                 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 = $"{chamberName}.{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 = $"{chamberName}.{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 = $"{chamberName}.{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 (!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.LimitValue);
 
-                     }
 
-                     info += reason + "\n";
 
-                 }
 
-                 if (!string.IsNullOrEmpty(info))
 
-                 {
 
-                     LOG.Write(eEvent.ERR_INTERLOCK_FAIL, "System", 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;
 
-         }
 
-     }
 
- }
 
 
  |