using System.Collections.Generic;
using System.Diagnostics;
using Aitex.Core.Util;
namespace Aitex.Core.RT.IOCore.Interlock.Limits;
///
/// 互锁限制条件。
///
public abstract class InterlockLimitBase : IInterlockLimit
where TAccessor : IInterlockLimitDataProvider
where TValue : struct
{
#region Variables
private Dictionary _cultureTip;
private readonly R_TRIG _trigger;
#endregion
#region Constructor
///
/// 互锁限制条件的构造函数。
///
/// 数据供应器对象实例
/// 当前限制条件中的IO状态。
/// 默认语言提示信息。
/// 多国语言提示信息。
/// IO名称错误,前缀不是“DI_”、”DO_“、”AI_“或"AO_".
protected InterlockLimitBase(IInterlockLimitDataProvider dataProvider, string value, string tip,
Dictionary cultureTip)
{
Debug.Assert(dataProvider != null, "The data provider can not be null.");
_trigger = new R_TRIG();
DataProvider = dataProvider;
Tip = tip;
_cultureTip = cultureTip;
UniqueId = $"{dataProvider.Name}.{value}";
}
#endregion
#region Properties
///
/// 放回当前Limit所对应的IO对象。
///
protected IInterlockLimitDataProvider DataProvider { get; }
///
/// 返回互锁限制条件的唯一识别码。
///
/// 该唯一识别码用于创建字典时,作为字典的Key值使用。
///
/// 该值由Name+LimitValue组成。
///
///
public string UniqueId { get; }
///
///
///
public string Name => DataProvider?.Name ?? "";
///
///
///
public string Description => DataProvider?.Description ?? "";
///
/// 返回当前Limit对应的数据值。
///
public abstract TValue CurrentValue { get; }
///
/// 返回当前互锁限制条件触发的原因。
///
public abstract string LimitReason { get; }
///
/// 返回守护条件满足时输出的信息。
///
public abstract string DaemonReason { get; }
///
/// 返回当前互锁条件的信号约束值。
///
public TValue LimitValue { get; protected set; }
///
/// 返回当前互锁条件提示信息。
///
public string Tip { get; }
#endregion
#region Methods
/*
///
/// 判断两个互锁限制条件是否相等。
///
/// 待比较的互锁限制条件。
///
/// True: 相同;False:不同。
///
public bool IsSame(object interlockLimit)
{
if (interlockLimit is not InterlockLimit li)
return false;
return Name == li.Name && li.LimitValue == LimitValue;
}*/
///
/// 返回互锁限制监测的信号当前值和期望值不相等的条件是否触发。
///
/// 捕获当前值和期望值不相等信号的上升沿,当上升沿到达时触发输出Q。
///
///
///
public bool IsTriggered()
{
_trigger.CLK = !CheckInRange();
return _trigger.Q;
}
///
/// 根据互锁条件判断是否允许DO输出。
///
/// 如果禁止DO输出,返回互锁原因。
///
public bool CanDo(out string reason)
{
reason = string.Empty;
if (CheckInRange())
return true;
reason = LimitReason;
return false;
}
///
/// 获取指定互锁限制条件中限制条件的内容。
///
///
public virtual string GetLimitValue()
{
return LimitValue.ToString();
}
///
/// 获取指定互锁限制条件中当前IO状态。
///
///
public virtual string GetCurrentValue()
{
return CurrentValue.ToString();
}
///
/// 检查当前值是否在指定范围内。
///
/// True: 在范围内; False:超出范围。
protected abstract bool CheckInRange();
///
///
///
///
public override string ToString()
{
return $"{Name}, Limit={LimitValue}";
}
#endregion
}