using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.TwinCat;
using CyberX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MECF.Framework.Common.IOCore;
namespace CyberX8_RT.Devices.Safety
{
public class SafetyToggleRoutine : RoutineBase, IRoutine
{
private enum SafetyToggleStep
{
WriteFalse,
WriteTrue,
LastWriteFalse,
End
}
#region 内部变量
///
/// do名称
///
private string _doName = "";
///
/// do写入数值
///
private bool _doWriteValue = false;
///
/// 设备
///
private SafetyDevice _device;
///
/// 超晨
///
private int _timeout = 1000;
#endregion
///
/// 构造函数
///
///
public SafetyToggleRoutine(string module) : base(module)
{
}
///
/// 中止
///
public void Abort()
{
Runner.Stop($"Manual Safty {_doName} Toggle");
}
///
/// 监控
///
///
public RState Monitor()
{
Runner.Run(SafetyToggleStep.WriteFalse, () => { return WriteDoValue(false); }, CheckDoValue, _timeout)
.Run(SafetyToggleStep.WriteTrue, () => { return WriteDoValue(true); }, CheckDoValue, _timeout)
.Run(SafetyToggleStep.LastWriteFalse, () => { return WriteDoValue(false); }, CheckDoValue, _timeout)
.End(SafetyToggleStep.End, NullFun, _delay_1ms);
return Runner.Status;
}
///
/// 写入do数值
///
///
///
private bool WriteDoValue(bool value)
{
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{_doName}");
_doWriteValue=value;
return IOModuleManager.Instance.WriteIoValue(ioName, value);
}
///
/// 检验do的数值
///
///
private bool CheckDoValue()
{
PropertyInfo propertyInfo = _device.SafetyData.GetType().GetProperty(_doName);
if (propertyInfo != null)
{
bool doValue = (bool)propertyInfo.GetValue(_device.SafetyData);
return doValue==_doWriteValue;
}
else
{
LOG.WriteLog(eEvent.ERR_SAFETY, Module, $"{_doName} in not in property");
return false;
}
}
///
/// 启动
///
///
///
public RState Start(params object[] objs)
{
_device = DEVICE.GetDevice(Module);
_doName= objs[0].ToString();
return Runner.Start(Module, $"{_doName} toggle");
}
}
}