2023-04-23 16:43:08 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-04-24 11:53:56 +08:00
|
|
|
|
using System.Diagnostics;
|
2023-04-23 16:43:08 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Aitex.Core.RT.Event;
|
2023-08-16 15:21:02 +08:00
|
|
|
|
using Aitex.Core.RT.IOCore.Interlock.Base;
|
2023-04-23 16:43:08 +08:00
|
|
|
|
using Aitex.Core.RT.SCCore;
|
|
|
|
|
using MECF.Framework.Common.Equipment;
|
|
|
|
|
|
|
|
|
|
namespace Aitex.Core.RT.IOCore
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 互锁管理器。
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// 互锁管理器作为独立工作的设备,被系统的后台循环调度。
|
|
|
|
|
/// <br/>
|
|
|
|
|
/// 当监测到某个InterlockLimit被触发时,和该Limit相关的所有Action中定义的DO均被
|
|
|
|
|
/// 置为Action节点Value属性中定义电平的反向电平。
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// </summary>
|
2023-08-16 15:21:02 +08:00
|
|
|
|
public class InterlockManager : InterlockManagerBase<InterlockAction>
|
2023-04-23 16:43:08 +08:00
|
|
|
|
{
|
|
|
|
|
#region Variables
|
2023-08-09 17:57:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 以Module为单位,当设置该Module所属的DO并触发互锁时,是否输出Info而不是Warning。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Dictionary<ModuleName, bool> _dicModulePostInfo;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 每个Module对应的Limit集合。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly Dictionary<ModuleName, List<IInterlockLimit>> _dicLimitsPerModule;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 每个Limit对应的DoAction集合。
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly Dictionary<IInterlockLimit, List<InterlockAction>> _dicLimitToActionMap;
|
2023-04-23 16:43:08 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 互锁管理器的构造函数。
|
|
|
|
|
/// </summary>
|
2023-05-13 10:13:55 +08:00
|
|
|
|
public InterlockManager()
|
2023-04-23 16:43:08 +08:00
|
|
|
|
{
|
2023-08-17 01:10:47 +08:00
|
|
|
|
RootNodeName = "Interlock";
|
|
|
|
|
|
2023-08-09 17:57:50 +08:00
|
|
|
|
_dicLimitToActionMap = new ();
|
|
|
|
|
_dicLimitsPerModule = new ();
|
|
|
|
|
_dicModulePostInfo = new();
|
2023-04-23 16:43:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2023-08-17 01:10:47 +08:00
|
|
|
|
|
2023-04-23 16:43:08 +08:00
|
|
|
|
#region Methods
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 初始化互锁管理器。
|
|
|
|
|
/// </summary>
|
2023-08-17 01:10:47 +08:00
|
|
|
|
/// <param name="configFie">互锁配置文件。</param>
|
2023-04-23 16:43:08 +08:00
|
|
|
|
/// <param name="doMap">DO点表。</param>
|
|
|
|
|
/// <param name="diMap">DI点表。</param>
|
|
|
|
|
/// <param name="aoMap">AO点表。</param>
|
|
|
|
|
/// <param name="aiMap">AI点表。</param>
|
|
|
|
|
/// <param name="reason">初始化失败原因。</param>
|
|
|
|
|
/// <returns></returns>
|
2023-08-17 01:10:47 +08:00
|
|
|
|
public override bool Initialize(string configFie,
|
2023-04-23 16:43:08 +08:00
|
|
|
|
Dictionary<string, DOAccessor> doMap,
|
|
|
|
|
Dictionary<string, DIAccessor> diMap,
|
|
|
|
|
Dictionary<string, AIAccessor> aiMap,
|
|
|
|
|
Dictionary<string, AOAccessor> aoMap,
|
|
|
|
|
out string reason)
|
|
|
|
|
{
|
|
|
|
|
reason = "";
|
2023-08-17 01:10:47 +08:00
|
|
|
|
var succ = base.Initialize(configFie, doMap, diMap, aiMap, aoMap, out reason);
|
|
|
|
|
if (!succ)
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-04-23 16:43:08 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-17 01:10:47 +08:00
|
|
|
|
foreach (var action in _lstActions)
|
2023-04-23 16:43:08 +08:00
|
|
|
|
{
|
|
|
|
|
// 创建InterlockLimit到被使用的InterlockAction映射字典
|
2023-08-16 15:06:29 +08:00
|
|
|
|
foreach (var limit in action.Limits)
|
2023-04-23 16:43:08 +08:00
|
|
|
|
{
|
|
|
|
|
// 检查InterlockLimit是否已经存在于字典中
|
2023-08-09 17:57:50 +08:00
|
|
|
|
var exists = _dicLimitToActionMap.ContainsKey(limit);
|
|
|
|
|
Debug.Assert(exists, "The limit object is not in the mapping dictionary");
|
2023-04-23 16:43:08 +08:00
|
|
|
|
|
2023-08-09 17:57:50 +08:00
|
|
|
|
// 当前Limit是否反向绑定了其所影响的所有DoAction
|
2023-08-17 01:10:47 +08:00
|
|
|
|
_dicLimitToActionMap[limit].Add(action);
|
2023-04-23 16:43:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-09 17:57:50 +08:00
|
|
|
|
|
|
|
|
|
_dicModulePostInfo = _dicLimitsPerModule.Keys.ToDictionary(x => x, x=>false);
|
|
|
|
|
|
|
|
|
|
Debug.Assert(!_dicActionsPerModule.ContainsKey(ModuleName.UnDefined),
|
|
|
|
|
$"InterlockManager {nameof(_dicActionsPerModule)} contains key {ModuleName.UnDefined}");
|
|
|
|
|
|
|
|
|
|
Debug.Assert(!_dicLimitsPerModule.ContainsKey(ModuleName.UnDefined),
|
|
|
|
|
$"InterlockManager {nameof(_dicLimitsPerModule)} contains key {ModuleName.UnDefined}");
|
2023-04-23 16:43:08 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-08-17 01:10:47 +08:00
|
|
|
|
reason = ex.Message;
|
2023-04-23 16:43:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 01:10:47 +08:00
|
|
|
|
if (reason.Length <= 0)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
reason = reason.TrimEnd('\n', '\r');
|
|
|
|
|
return false;
|
2023-04-23 16:43:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-13 10:13:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 背景扫描线程执行的任务。
|
|
|
|
|
/// </summary>
|
2023-08-17 01:10:47 +08:00
|
|
|
|
public override void Monitor()
|
2023-08-09 17:57:50 +08:00
|
|
|
|
{
|
|
|
|
|
// 按Module扫描Interlock Limit
|
|
|
|
|
foreach (var moduleName in _dicLimitsPerModule.Keys.ToList())
|
|
|
|
|
{
|
|
|
|
|
Debug.Assert(moduleName != ModuleName.UnDefined,
|
|
|
|
|
$"Interlock Manager CanSetDo() undesired module name {ModuleName.UnDefined}");
|
|
|
|
|
|
|
|
|
|
// 检查当前Module是否旁路Interlock
|
|
|
|
|
var isBypassInterlock = GetScBypassInterlockValue(moduleName);
|
|
|
|
|
if (isBypassInterlock)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
foreach (var limit in _dicLimitsPerModule[moduleName].ToList())
|
|
|
|
|
{
|
2023-05-13 10:13:55 +08:00
|
|
|
|
|
2023-08-09 17:57:50 +08:00
|
|
|
|
// 如果互锁没被触发
|
|
|
|
|
if (!limit.IsTriggered())
|
|
|
|
|
continue;
|
2023-04-23 16:43:08 +08:00
|
|
|
|
|
2023-08-09 17:57:50 +08:00
|
|
|
|
var reverseInfo = new StringBuilder();
|
|
|
|
|
var module = "System";
|
2023-05-13 10:13:55 +08:00
|
|
|
|
|
2023-08-09 17:57:50 +08:00
|
|
|
|
var actions = _dicLimitToActionMap[limit].ToList();
|
|
|
|
|
foreach (var action in actions)
|
|
|
|
|
{
|
|
|
|
|
// 尝试根据Action定义复位该互锁限制条件对应的所有DO的电平
|
|
|
|
|
if (action.TryReverse(out var reason))
|
|
|
|
|
{
|
|
|
|
|
var ss = action.ActionName.Split('.');
|
|
|
|
|
if (ss.Length > 1 && ModuleHelper.IsPm(ss[0]))
|
|
|
|
|
module = ss[0];
|
2023-04-23 16:43:08 +08:00
|
|
|
|
|
2023-08-09 17:57:50 +08:00
|
|
|
|
reverseInfo.AppendLine(reason);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-23 16:43:08 +08:00
|
|
|
|
|
2023-08-09 17:57:50 +08:00
|
|
|
|
// 如果PM腔有被恢复的DO,则打印信息并报警。
|
|
|
|
|
if (reverseInfo.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
reverseInfo.Insert(0,
|
2023-08-10 16:44:46 +08:00
|
|
|
|
$"Due to the {limit.Tip}, {limit.Name} is not [{limit.GetLimitValue()}]\r\n");
|
2023-08-09 17:57:50 +08:00
|
|
|
|
|
|
|
|
|
// Post事件的类型, Info还是Warning
|
|
|
|
|
_dicModulePostInfo.TryGetValue(moduleName, out var isPostInfo);
|
|
|
|
|
|
|
|
|
|
if (isPostInfo)
|
|
|
|
|
EV.PostInfoLog(module, reverseInfo.ToString().TrimEnd('\r', '\n'));
|
|
|
|
|
else
|
|
|
|
|
EV.PostWarningLog(module, reverseInfo.ToString().TrimEnd('\r', '\n'));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置指定Module的Interlock打印信息等级。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="module">模组名称</param>
|
|
|
|
|
/// <param name="isInfo">是否以Info等级打印信息</param>
|
|
|
|
|
public void SetEventLevel(string module, bool isInfo)
|
|
|
|
|
{
|
|
|
|
|
var moduleName = ModuleHelper.Converter(module);
|
|
|
|
|
if (_dicModulePostInfo.ContainsKey(moduleName))
|
|
|
|
|
_dicModulePostInfo[moduleName] = isInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-05-13 10:13:55 +08:00
|
|
|
|
/// 对指定的DO的操作是否满足互锁条件。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="doName">待操作的DO名称。</param>
|
|
|
|
|
/// <param name="onOff">
|
|
|
|
|
/// 指定的输出。
|
|
|
|
|
/// <value>True:输出有效电平</value>
|
|
|
|
|
/// <br/>
|
|
|
|
|
/// <value>False:清除有效电平输出</value>
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <param name="reason">
|
|
|
|
|
/// 如果触发互锁限制,输出互锁限制的原因。
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns></returns>
|
2023-04-23 16:43:08 +08:00
|
|
|
|
public bool CanSetDo(string doName, bool onOff, out string reason)
|
|
|
|
|
{
|
|
|
|
|
reason = string.Empty;
|
2023-08-09 17:57:50 +08:00
|
|
|
|
|
|
|
|
|
var moduleName = GetModuleFromIo(doName);
|
|
|
|
|
|
|
|
|
|
Debug.Assert(moduleName != ModuleName.UnDefined,
|
|
|
|
|
$"Interlock Manager CanSetDo() undesired module name {ModuleName.UnDefined}");
|
|
|
|
|
|
|
|
|
|
var isBypassInterlock = GetScBypassInterlockValue(moduleName);
|
|
|
|
|
if (isBypassInterlock)
|
2023-04-23 16:43:08 +08:00
|
|
|
|
return true;
|
2023-05-13 10:13:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var action in _lstActions.Where(action => action.IsSame(doName, onOff)))
|
|
|
|
|
{
|
|
|
|
|
return action.CanDo(out reason);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-23 16:43:08 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-09 17:57:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取系统配置中指定Module的ByPassInterlock参数设置值。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="module">
|
|
|
|
|
/// 模组名称, <see cref="ModuleName"/>
|
|
|
|
|
/// </param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool GetScBypassInterlockValue(ModuleName module)
|
|
|
|
|
{
|
|
|
|
|
return GetScBypassInterlockValue(module.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取系统配置中指定Module的ByPassInterlock参数设置值。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="module">模组名称</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool GetScBypassInterlockValue(string module)
|
|
|
|
|
{
|
|
|
|
|
if (ModuleHelper.IsPm(module))
|
|
|
|
|
return SC.SafeGetValue($"PM.{module}.BypassInterlock", false);
|
|
|
|
|
else
|
|
|
|
|
return SC.SafeGetValue($"{module}.BypassInterlock", false);
|
|
|
|
|
}
|
2023-08-17 01:10:47 +08:00
|
|
|
|
|
2023-04-23 16:43:08 +08:00
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|