using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Aitex.Core.RT.IOCore.Interlock.Actions { public abstract class InterlockActionBase : IInterlockAction { #region Variables private readonly List> _logicOrGroups; private readonly List _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 IEnumerable Limits => _limits.AsReadOnly(); /// /// /// public IEnumerable> LogicOrGroups => _logicOrGroups.AsReadOnly(); #endregion #region Methods /// /// /// public void AddLimit(IInterlockLimit limit) { _limits.Add(limit); } /// /// /// public void AddLogicOrGroup(List orGroup) { _logicOrGroups.Add(orGroup); } /// /// /// public bool IsSame(string doName, bool value) { return string.Equals(doName, _do.Name, StringComparison.CurrentCultureIgnoreCase) && _actionValue == value; } /// /// /// 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 } }