163 lines
3.8 KiB
C#
163 lines
3.8 KiB
C#
using System.Collections.Generic;
|
||
using Aitex.Core.Util;
|
||
|
||
namespace Aitex.Core.RT.IOCore
|
||
{
|
||
/// <summary>
|
||
/// 互锁限制条件。
|
||
/// </summary>
|
||
public abstract class InterlockLimit<T> : IInterlockLimit
|
||
where T : struct
|
||
{
|
||
#region Variables
|
||
|
||
private readonly string _name;
|
||
private Dictionary<string, string> _cultureTip;
|
||
private readonly R_TRIG _trigger;
|
||
|
||
#endregion
|
||
|
||
#region Constructor
|
||
|
||
/// <summary>
|
||
/// 互锁限制条件的构造函数。
|
||
/// </summary>
|
||
/// <param name="name"></param>
|
||
/// <param name="value"></param>
|
||
/// <param name="tip"></param>
|
||
/// <param name="cultureTip"></param>
|
||
protected InterlockLimit(string name, T value, string tip, Dictionary<string, string> cultureTip)
|
||
{
|
||
_trigger = new R_TRIG();
|
||
_name = name;
|
||
LimitValue = value;
|
||
Tip = tip;
|
||
_cultureTip = cultureTip;
|
||
UniqueId = $"{Name}.{LimitValue}";
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region Properties
|
||
|
||
/// <summary>
|
||
/// 返回互锁限制条件的唯一识别码。
|
||
/// <remarks>
|
||
/// 该唯一识别码用于创建字典时,作为字典的Key值使用。
|
||
/// <br/>
|
||
/// 该值由Name+LimitValue组成。
|
||
/// </remarks>
|
||
/// </summary>
|
||
public string UniqueId { get; }
|
||
|
||
/// <summary>
|
||
/// 返回互锁限制条件名称。
|
||
/// </summary>
|
||
public string Name => _name;
|
||
|
||
/// <summary>
|
||
/// 返回
|
||
/// </summary>
|
||
public abstract T CurrentValue { get; }
|
||
|
||
/// <summary>
|
||
/// 返回当前互锁限制条件触发的原因。
|
||
/// </summary>
|
||
public abstract string LimitReason { get; }
|
||
|
||
/// <summary>
|
||
/// 返回当前互锁条件的信号约束值。
|
||
/// </summary>
|
||
public T LimitValue { get; }
|
||
|
||
/// <summary>
|
||
/// 返回当前互锁条件提示信息。
|
||
/// </summary>
|
||
public string Tip { get; }
|
||
|
||
#endregion
|
||
|
||
#region Methods
|
||
|
||
/// <summary>
|
||
/// 判断两个互锁限制条件是否相等。
|
||
/// </summary>
|
||
/// <param name="interlockLimit">待比较的互锁限制条件。</param>
|
||
/// <returns>
|
||
/// <para>True: 相同;False:不同。</para>
|
||
/// </returns>
|
||
public bool IsSame(object interlockLimit)
|
||
{
|
||
if (interlockLimit is not InterlockLimit<T> li)
|
||
return false;
|
||
return Name == li.Name && HandleValueCompare(LimitValue, li.LimitValue);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 返回互锁限制监测的信号当前值和期望值不相等的条件是否触发。
|
||
/// <remarks>
|
||
/// 捕获当前值和期望值不相等信号的上升沿,当上升沿到达时触发输出Q。
|
||
/// </remarks>
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public bool IsTriggered()
|
||
{
|
||
_trigger.CLK = !HandleValueCompare(CurrentValue, LimitValue);
|
||
return _trigger.Q;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据互锁条件判断是否允许DO输出。
|
||
/// </summary>
|
||
/// <param name="reason">如果禁止DO输出,返回互锁原因。</param>
|
||
/// <returns></returns>
|
||
public bool CanDo(out string reason)
|
||
{
|
||
reason = string.Empty;
|
||
if (HandleValueCompare(CurrentValue, LimitValue))
|
||
{
|
||
return true;
|
||
}
|
||
reason = LimitReason;
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取指定互锁限制条件中限制条件的内容。
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public virtual string GetLimitValue()
|
||
{
|
||
return LimitValue.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取指定互锁限制条件中当前IO状态。
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public virtual string GetCurrentValue()
|
||
{
|
||
return CurrentValue.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 比较互锁条件和当前IO状态是否相等。
|
||
/// </summary>
|
||
/// <param name="v1">IO1状态。</param>
|
||
/// <param name="v2">IO2状态。</param>
|
||
/// <returns></returns>
|
||
protected abstract bool HandleValueCompare(T v1, T v2);
|
||
|
||
/// <summary>
|
||
/// <inheritdoc />
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public override string ToString()
|
||
{
|
||
return $"{Name}, Limit={LimitValue}";
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
}
|