using System.Collections.Generic;
using System.Diagnostics;
using Aitex.Core.Util;
namespace Aitex.Core.RT.IOCore
{
///
/// 互锁限制条件。
///
public abstract class InterlockLimit : IInterlockLimit
where TAccessor: IIOAccessor where TValue : struct
{
#region Variables
private Dictionary _cultureTip;
private readonly R_TRIG _trigger;
#endregion
#region Constructor
///
/// 互锁限制条件的构造函数。
///
/// IO对象实例。
/// 当前限制条件中的IO状态。
/// 默认语言提示信息。
/// 多国语言提示信息。
/// IO名称错误,前缀不是“DI_”、”DO_“、”AI_“或"AO_".
protected InterlockLimit(TAccessor io, string value, string tip, Dictionary cultureTip)
{
Debug.Assert(io != null, "The IO Accessor object can not be null.");
_trigger = new R_TRIG();
Io = io;
Name = io.Name;
Tip = tip;
_cultureTip = cultureTip;
UniqueId = $"{io.Name}.{value}";
}
#endregion
#region Properties
protected TAccessor Io { get; }
///
/// 返回互锁限制条件的唯一识别码。
///
/// 该唯一识别码用于创建字典时,作为字典的Key值使用。
///
/// 该值由Name+LimitValue组成。
///
///
public string UniqueId { get; }
///
/// 返回互锁限制条件名称。
///
public string Name { get; }
///
/// 返回
///
public abstract TValue CurrentValue { get; }
///
/// 返回当前互锁限制条件触发的原因。
///
public abstract string LimitReason { 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
}
}