Sic.Framework-Nanjing-Baishi/MECF.Framework.Common/Aitex/Core/RT/IOCore/InterlockLimit.cs

163 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}
}