using System.Collections.Generic; using System.Linq; using System.Text; using TypeDefLimitList = System.Collections.Generic.List; namespace Aitex.Core.RT.IOCore.Interlock.Base { public abstract class InterlockActionBase : IInterlockAction { #region Variables private readonly List _logicOrGroups; private readonly TypeDefLimitList _limits; private readonly string _tip; private Dictionary _cultureTip; protected readonly string _module; protected readonly DOAccessor _do; protected readonly bool _actionValue; #endregion #region Constructors protected InterlockActionBase(string module, DOAccessor doItem, bool value, string tip, Dictionary cultureTip) { _module = module; _logicOrGroups = new(); _limits = new(); _do = doItem; _actionValue = value; _tip = tip; _cultureTip = cultureTip; } #endregion #region Properties public string ActionName => (_do != null) ? _do.Name : ""; public bool IsSame(string doName, bool value) { return doName == _do.Name && _actionValue == value; } public IEnumerable Limits => _limits.AsReadOnly(); public IEnumerable> LogicOrGroups => _logicOrGroups.AsReadOnly(); #endregion #region Methods public void AddLimit(IInterlockLimit limit) { _limits.Add(limit); } public void AddLogicOrGroup(TypeDefLimitList orGroup) { _logicOrGroups.Add(orGroup); } public bool CanDo(out string reason) { reason = string.Empty; var sbReason = new StringBuilder(); var result = true; foreach (var limit in _limits) { if (!limit.CanDo(out var limitReason)) { sbReason.AppendLine(limitReason); result = false; } } // 如果有互锁被触发,则打印互锁信息 if (!result) { sbReason.Insert(0, $"Interlock triggered, DO-{_do.IoTableIndex}({_do.Name}) can not be [{(_actionValue ? "ON" : "OFF")}]{(string.IsNullOrEmpty(_tip) ? "" : $",{_tip}")} \r\n"); reason = sbReason.ToString().TrimEnd('\r', '\n'); } return result; } /// /// 周期性扫描当前对象时执行的任务。 /// /// public abstract void Monitor(); /// public override string ToString() { return $"{ActionName}, Action={_actionValue}, Limit Count={Limits.Count()}, OR Group Count={LogicOrGroups.Count()}"; } #endregion } }