using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.Util;
using PunkHPX8_Core;
using PunkHPX8_RT.Modules;
using PunkHPX8_RT.Modules.SRD;
using PunkHPX8_RT.Modules.Transporter;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Routine;
namespace PunkHPX8_RT.Devices.Safety
{
internal class SafetyAllOnRoutine : RoutineBase, IRoutine
{
private enum SafetyAllOnStep
{
Ready,
SwitchOnProcessTransporter,
WaitSwitchOnProcessTransporter,
SwitchOnLoaderTransporter,
WaitSwitchOnLoaderTransporter,
SwitchOnLoader,
WaitSwitchOnLoader,
SwitchOnPuf1,
WaitSwitchOnPuf1,
SwitchOnPuf2,
WaitSwitchOnPuf2,
SwitchOnSRD1,
WaitSwitchOnSRD1,
SwitchOnSRD2,
WaitSwitchOnSRD2,
End
}
#region 内部变量
private SafetyDevice _device;
///
/// SRD1
///
private SRDEntity _srd1Entity;
///
/// SRD2
///
private SRDEntity _srd2Entity;
#endregion
///
/// 构造函数
///
///
public SafetyAllOnRoutine(string module) : base(module)
{
}
///
/// 中止
///
public void Abort()
{
Runner.Stop($"Safety All On Abort");
}
///
/// 监控
///
///
public RState Monitor()
{
Runner.Run(SafetyAllOnStep.Ready, CheckReady, _delay_1ms)
.RunIf(SafetyAllOnStep.SwitchOnSRD1, _srd1Entity!=null, SwitchOnSRD1, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnSRD1, CheckSwitchOnSRD1Complete, CheckSwitchOnSRD1Error)
.RunIf(SafetyAllOnStep.SwitchOnSRD2, _srd2Entity!=null, SwitchOnSRD2, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnSRD2, CheckSwitchOnSRD2Complete, CheckSwitchOnSRD2Error)
.End(SafetyAllOnStep.End, NullFun, _delay_1ms);
return Runner.Status;
}
private bool CheckReady()
{
_srd1Entity = Singleton.Instance.GetModule(ModuleName.SRD1.ToString());
_srd2Entity = Singleton.Instance.GetModule(ModuleName.SRD2.ToString());
return true;
}
#region SRD1
///
/// Switch on SRD1 Motors
///
///
private bool SwitchOnSRD1()
{
return _srd1Entity.CheckToPostMessage(eEvent.ERR_SRD, ModuleName.SRD1.ToString(), (int)SRDMSG.SwitchOn);
}
///
/// 检查SRD1 Motors switchon是否完成
///
///
private bool CheckSwitchOnSRD1Complete()
{
if(_srd1Entity==null)
{
return true;
}
if (_srd1Entity.IsRotationSwitchOn && _srd1Entity.IsArmSwitchOn)
{
return true;
}
return false;
}
///
/// 检查SRD1 Motors switchon是否出错
///
///
private bool CheckSwitchOnSRD1Error()
{
if (_srd1Entity == null)
{
return false;
}
if (_srd1Entity.IsError)
{
LOG.WriteLog(eEvent.ERR_SAFETY, Module, $"Switching On SRD1 Motors is failed");
return true;
}
return false;
}
#endregion
#region SRD2
///
/// Switch on SRD2 Motors
///
///
private bool SwitchOnSRD2()
{
return _srd2Entity.CheckToPostMessage(eEvent.ERR_SRD, ModuleName.SRD2.ToString(), (int)SRDMSG.SwitchOn);
}
///
/// 检查SRD2 Motors switchon是否完成
///
///
private bool CheckSwitchOnSRD2Complete()
{
if(_srd2Entity == null)
{
return true;
}
if (_srd2Entity.IsRotationSwitchOn && _srd2Entity.IsArmSwitchOn)
{
return true;
}
return false;
}
///
/// 检查SRD2 Motors switchon是否出错
///
///
private bool CheckSwitchOnSRD2Error()
{
if(_srd2Entity == null)
{
return false;
}
if (_srd2Entity.IsError)
{
LOG.WriteLog(eEvent.ERR_SAFETY, Module, $"Switching On SRD2 Motors is failed");
return true;
}
return false;
}
#endregion
///
/// 启动
///
///
///
public RState Start(params object[] objs)
{
_device = DEVICE.GetDevice(Module);
return Runner.Start(Module, $"Safety All On");
}
}
}