Sic.Framework-Nanjing-Baishi/MECF.Framework.Common/Aitex/Core/RT/IOCore/Interlock/InterlockDaemonAction.cs

94 lines
2.0 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Text;
namespace Aitex.Core.RT.IOCore;
public class InterlockDaemonAction
{
#region Variables
private readonly List<IInterlockLimit> _limits;
private readonly DOAccessor _do;
private readonly bool _targetValue;
private readonly string _tip;
private Dictionary<string, string> _cultureTip;
#endregion
#region Constructors
public InterlockDaemonAction(DOAccessor doItem, bool value, string tip, Dictionary<string, string> cultureTip,
List<IInterlockLimit> limits)
{
_do = doItem;
_targetValue = value;
_tip = tip;
_cultureTip = cultureTip;
_limits = limits;
}
#endregion
#region Properties
/// <summary>
/// 当前动作的名称。
/// </summary>
public string ActionName => (_do != null) ? _do.Name : "";
#endregion
#region Methods
/// <summary>
/// 比较当前动作是否和指定DO的指定输出状态相同。
/// </summary>
/// <param name="doName"></param>
/// <param name="value"></param>
/// <returns></returns>
public bool IsSame(string doName, bool value)
{
return doName == _do.Name && _targetValue == value;
}
/// <summary>
/// 扫描当前动作的条件并设置相关DO的输出状态。
/// </summary>
public bool DaemonMonitor(out string reason)
{
reason = "";
var info = new StringBuilder();
foreach (var limit in _limits)
{
if (!limit.CanDo(out _))
return false;
info.AppendLine($"\t{limit}");
}
// 到这里表示所有的Condition均满足条件
// 如果DO输出已经满足要求则直接退出
if (_do.Value == _targetValue)
return false;
// 设置DO输出
if (_do.SetValue(_targetValue, out reason))
reason = info.ToString().TrimEnd('\r', '\n');
info.Insert(0,
$"Interlock Force set DO-{_do.IoTableIndex}({_do.Name}) = [{((_targetValue) ? "ON" : "OFF")}]\r\n");
return true;
}
public override string ToString()
{
return $"{ActionName}, Action={_targetValue}, Condition Count={_limits.Count}";
}
#endregion
}