2023-04-13 11:51:03 +08:00
|
|
|
|
using System.Collections.Generic;
|
2023-04-24 11:53:56 +08:00
|
|
|
|
using System.Text;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
|
|
namespace Aitex.Core.RT.IOCore
|
|
|
|
|
{
|
|
|
|
|
internal class InterlockAction
|
|
|
|
|
{
|
2023-04-21 17:56:29 +08:00
|
|
|
|
private readonly List<IInterlockLimit> _limits;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-04-21 17:56:29 +08:00
|
|
|
|
private readonly DOAccessor _do;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-04-21 17:56:29 +08:00
|
|
|
|
private readonly bool _actionValue;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-04-21 17:56:29 +08:00
|
|
|
|
private readonly string _tip;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-04-21 17:56:29 +08:00
|
|
|
|
private Dictionary<string, string> _cultureTip;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
|
|
public string ActionName => (_do != null) ? _do.Name : "";
|
|
|
|
|
|
2023-04-21 17:56:29 +08:00
|
|
|
|
public InterlockAction(DOAccessor doItem, bool value, string tip, Dictionary<string, string> cultureTip,
|
|
|
|
|
List<IInterlockLimit> limits)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
_do = doItem;
|
|
|
|
|
_actionValue = value;
|
|
|
|
|
_tip = tip;
|
|
|
|
|
_cultureTip = cultureTip;
|
|
|
|
|
_limits = limits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsSame(string doName, bool value)
|
|
|
|
|
{
|
|
|
|
|
return doName == _do.Name && _actionValue == value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 17:56:29 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 如果命中某个互锁限制条件,则将DO电平恢复到Action定义的反向电平。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reason">执行恢复电平动作的信息。</param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// True:执行了电平恢复操作;False:未操作电平
|
|
|
|
|
/// </returns>
|
|
|
|
|
public bool TryReverse(out string reason)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
reason = string.Empty;
|
2023-04-21 17:56:29 +08:00
|
|
|
|
|
|
|
|
|
// 如果DO当前电平不等于Action定义的电平,则啥也不干
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (_do.Value != _actionValue)
|
|
|
|
|
return false;
|
2023-04-21 17:56:29 +08:00
|
|
|
|
|
|
|
|
|
// 如果DO已经输出Action定义的电平,则反向
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (_do.SetValue(!_actionValue, out reason))
|
2023-04-24 11:53:56 +08:00
|
|
|
|
reason = $"Interlock Force set DO-{_do.IoTableIndex}({_do.Name}) = [{((!_actionValue) ? "ON" : "OFF")}]";
|
2023-04-13 11:51:03 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanDo(out string reason)
|
|
|
|
|
{
|
|
|
|
|
reason = string.Empty;
|
2023-04-24 11:53:56 +08:00
|
|
|
|
var sbReason = new StringBuilder();
|
|
|
|
|
var result = true;
|
2023-04-21 17:56:29 +08:00
|
|
|
|
foreach (var limit in _limits)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-04-24 11:53:56 +08:00
|
|
|
|
if (!limit.CanDo(out var limitReason))
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-04-24 11:53:56 +08:00
|
|
|
|
sbReason.AppendLine(limitReason);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
result = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-24 11:53:56 +08:00
|
|
|
|
|
|
|
|
|
// 如果有互锁被触发,则打印互锁信息
|
|
|
|
|
if (!result)
|
|
|
|
|
{
|
|
|
|
|
sbReason.Insert(0,
|
2023-04-24 14:14:08 +08:00
|
|
|
|
$"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');
|
2023-04-24 11:53:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 11:51:03 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-04-24 11:53:56 +08:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"{ActionName}, Action={_actionValue}, Limit Count={_limits.Count}";
|
|
|
|
|
}
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|