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