using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.Util;
using CyberX8_Core;
using CyberX8_RT.Modules;
using CyberX8_RT.Modules.Loader;
using CyberX8_RT.Modules.PUF;
using CyberX8_RT.Modules.SRD;
using CyberX8_RT.Modules.Transporter;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.Utilities;
using System.Runtime.InteropServices;
namespace CyberX8_RT.Devices.Safety
{
internal class SafetyNewAllOnRoutine : 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;
private TransporterSwitchOnRoutine _loaderTransporterSwitchOnRoutine;
private TransporterSwitchOnRoutine _processTransporterSwitchOnRoutine;
private LoaderSwitchAllOnRoutine _loaderSwitchAllOnRoutine;
private PufSwitchOnRoutine _puf1SwitchOnRoutine;
private PufSwitchOnRoutine _puf2SwitchOnRoutine;
private SRDSwitchOnRoutine _srd1SwitchOnRoutine;
private SRDSwitchOnRoutine _srd2SwitchOnRoutine;
#endregion
///
/// 构造函数
///
///
public SafetyNewAllOnRoutine(string module) : base(module)
{
}
///
/// 中止
///
public void Abort()
{
Runner.Stop($"Safety All On Abort");
}
///
/// 监控
///
///
public RState Monitor()
{
Runner
.RunIf(SafetyAllOnStep.SwitchOnProcessTransporter,ModuleHelper.IsInstalled(ModuleName.Transporter1),SwitchOnProcessTransporter, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnProcessTransporter, CheckSwitchOnProcessTransporterComplete, CheckSwitchOnProcessTransporterError)
.RunIf(SafetyAllOnStep.SwitchOnLoaderTransporter,ModuleHelper.IsInstalled(ModuleName.Transporter2), SwitchOnLoaderTransporter, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnLoaderTransporter, CheckSwitchOnLoaderTransporterComplete, CheckSwitchOnLoaderTransporterError)
.RunIf(SafetyAllOnStep.SwitchOnPuf1,ModuleHelper.IsInstalled(ModuleName.PUF1), SwitchOnPuf1, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnPuf1, CheckSwitchOnPuf1Complete, CheckSwitchOnPuf1Error)
.RunIf(SafetyAllOnStep.SwitchOnPuf2,ModuleHelper.IsInstalled(ModuleName.PUF2), SwitchOnPuf2, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnPuf2, CheckSwitchOnPuf2Complete, CheckSwitchOnPuf2Error)
.RunIf(SafetyAllOnStep.SwitchOnLoader,ModuleHelper.IsInstalled(ModuleName.Loader1), SwitchOnLoader, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnLoader, CheckSwitchOnLoaderComplete, CheckSwitchOnLoaderError)
.RunIf(SafetyAllOnStep.SwitchOnSRD1, ModuleHelper.IsInstalled(ModuleName.SRD1), SwitchOnSRD1, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnSRD1, CheckSwitchOnSRD1Complete, CheckSwitchOnSRD1Error)
.RunIf(SafetyAllOnStep.SwitchOnSRD2, ModuleHelper.IsInstalled(ModuleName.SRD2), SwitchOnSRD2, _delay_1ms)
.WaitWithStopCondition(SafetyAllOnStep.WaitSwitchOnSRD2, CheckSwitchOnSRD2Complete, CheckSwitchOnSRD2Error)
.End(SafetyAllOnStep.End, NullFun, _delay_1ms);
return Runner.Status;
}
#region ProcessTransporter
///
/// Switch on ProcessTransporter Motors
///
///
private bool SwitchOnProcessTransporter()
{
return _processTransporterSwitchOnRoutine.Start()==RState.Running;
}
///
/// 检查ProcessTransporter Motors switchon是否完成
///
///
private bool CheckSwitchOnProcessTransporterComplete()
{
if (!ModuleHelper.IsInstalled(ModuleName.Transporter1))
{
return true;
}
return CommonFunction.CheckRoutineEndState(_processTransporterSwitchOnRoutine);
}
///
/// 检查ProcessTransporter Motors switchon是否出错
///
///
private bool CheckSwitchOnProcessTransporterError()
{
if (!ModuleHelper.IsInstalled(ModuleName.Transporter1))
{
return false;
}
return CommonFunction.CheckRoutineStopState(_processTransporterSwitchOnRoutine);
}
#endregion
#region LoaderTransporter
///
/// Switch on LoaderTransporter Motors
///
///
private bool SwitchOnLoaderTransporter()
{
return _loaderTransporterSwitchOnRoutine.Start() == RState.Running;
}
///
/// 检查LoaderTransporter Motors switchon是否完成
///
///
private bool CheckSwitchOnLoaderTransporterComplete()
{
if (!ModuleHelper.IsInstalled(ModuleName.Transporter2))
{
return true;
}
return CommonFunction.CheckRoutineEndState(_loaderTransporterSwitchOnRoutine);
}
///
/// 检查LoaderTransporter Motors switchon是否出错
///
///
private bool CheckSwitchOnLoaderTransporterError()
{
if (!ModuleHelper.IsInstalled(ModuleName.Transporter2))
{
return false;
}
return CommonFunction.CheckRoutineStopState(_loaderTransporterSwitchOnRoutine);
}
#endregion
#region Puf1
///
/// Switch on Puf1 Motors
///
///
private bool SwitchOnPuf1()
{
return _puf1SwitchOnRoutine.Start()==RState.Running;
}
///
/// 检查Puf1 Motors switchon是否完成
///
///
private bool CheckSwitchOnPuf1Complete()
{
if (!ModuleHelper.IsInstalled(ModuleName.PUF1))
{
return true;
}
return CommonFunction.CheckRoutineEndState(_puf1SwitchOnRoutine);
}
///
/// 检查Puf1 Motors switchon是否出错
///
///
private bool CheckSwitchOnPuf1Error()
{
if (!ModuleHelper.IsInstalled(ModuleName.PUF1))
{
return false;
}
return CommonFunction.CheckRoutineStopState(_puf1SwitchOnRoutine);
}
#endregion
#region Puf2
///
/// Switch on Puf2 Motors
///
///
private bool SwitchOnPuf2()
{
return _puf2SwitchOnRoutine.Start()==RState.Running;
}
///
/// 检查Puf1 Motors switchon是否完成
///
///
private bool CheckSwitchOnPuf2Complete()
{
if (!ModuleHelper.IsInstalled(ModuleName.PUF2))
{
return true;
}
return CommonFunction.CheckRoutineEndState(_puf2SwitchOnRoutine);
}
///
/// 检查Puf2 Motors switchon是否出错
///
///
private bool CheckSwitchOnPuf2Error()
{
if (!ModuleHelper.IsInstalled(ModuleName.Transporter1))
{
return false;
}
return CommonFunction.CheckRoutineStopState(_puf2SwitchOnRoutine);
}
#endregion
#region Loader
///
/// Switch on Loader Motors
///
///
private bool SwitchOnLoader()
{
return _loaderSwitchAllOnRoutine.Start() == RState.Running;
}
///
/// 检查Loader Motors switchon是否完成
///
///
private bool CheckSwitchOnLoaderComplete()
{
if (!ModuleHelper.IsInstalled(ModuleName.Loader1))
{
return true;
}
return CommonFunction.CheckRoutineEndState(_loaderSwitchAllOnRoutine);
}
///
/// 检查Loader Motors switchon是否出错
///
///
private bool CheckSwitchOnLoaderError()
{
if (!ModuleHelper.IsInstalled(ModuleName.Transporter1))
{
return false;
}
return CommonFunction.CheckRoutineEndState(_loaderSwitchAllOnRoutine);
}
#endregion
#region SRD1
///
/// Switch on SRD1 Motors
///
///
private bool SwitchOnSRD1()
{
return _srd1SwitchOnRoutine.Start() == RState.Running;
}
///
/// 检查SRD1 Motors switchon是否完成
///
///
private bool CheckSwitchOnSRD1Complete()
{
if (!ModuleHelper.IsInstalled(ModuleName.SRD1))
{
return true;
}
return CommonFunction.CheckRoutineEndState(_srd1SwitchOnRoutine);
}
///
/// 检查SRD1 Motors switchon是否出错
///
///
private bool CheckSwitchOnSRD1Error()
{
if (!ModuleHelper.IsInstalled(ModuleName.SRD1))
{
return false;
}
return CommonFunction.CheckRoutineStopState(_srd1SwitchOnRoutine);
}
#endregion
#region SRD2
///
/// Switch on SRD2 Motors
///
///
private bool SwitchOnSRD2()
{
return _srd2SwitchOnRoutine.Start()==RState.Running;
}
///
/// 检查SRD2 Motors switchon是否完成
///
///
private bool CheckSwitchOnSRD2Complete()
{
if (!ModuleHelper.IsInstalled(ModuleName.SRD2))
{
return true;
}
return CommonFunction.CheckRoutineEndState(_srd2SwitchOnRoutine);
}
///
/// 检查SRD2 Motors switchon是否出错
///
///
private bool CheckSwitchOnSRD2Error()
{
if (!ModuleHelper.IsInstalled(ModuleName.SRD2))
{
return true;
}
return CommonFunction.CheckRoutineStopState(_srd2SwitchOnRoutine);
}
#endregion
///
/// 启动
///
///
///
public RState Start(params object[] objs)
{
_loaderTransporterSwitchOnRoutine = new TransporterSwitchOnRoutine(ModuleName.Transporter2.ToString());
_processTransporterSwitchOnRoutine = new TransporterSwitchOnRoutine(ModuleName.Transporter1.ToString());
_loaderSwitchAllOnRoutine = new LoaderSwitchAllOnRoutine(ModuleName.Loader1.ToString());
_puf1SwitchOnRoutine = new PufSwitchOnRoutine(ModuleName.PUF1);
_puf2SwitchOnRoutine = new PufSwitchOnRoutine(ModuleName.PUF2);
_srd1SwitchOnRoutine = new SRDSwitchOnRoutine(ModuleName.SRD1.ToString());
_srd2SwitchOnRoutine = new SRDSwitchOnRoutine(ModuleName.SRD2.ToString());
_device = DEVICE.GetDevice(Module);
return Runner.Start(Module, $"Safety All On");
}
}
}