using Aitex.Core.RT.Device;
using Aitex.Core.RT.Routine;
using MECF.Framework.Common.Routine;
using CyberX8_Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CyberX8_RT.Devices.Loader
{
public class LoaderFlowTestRoutine : RoutineBase, IRoutine
{
private enum FlowTestStep
{
FirstFlowTest,
FirstFlowTestWait,
SecondFlowTest,
SecondFlowTestWait,
End,
}
#region 内部变量
private LoaderLeakFirstFlowTestRoutine _firstFlowTestRoutine;
private LoaderReLeakFlowTestRoutine _reLeakFlowTestRoutine;
private bool _isNeedReTest;
private LoaderCommonDevice _commonDevice;
#endregion
///
/// 构造函数
///
///
public LoaderFlowTestRoutine(string module) : base(module)
{
}
///
/// 中止
///
public void Abort()
{
Runner.Stop("Manual Abort");
}
///
/// 监控
///
///
public RState Monitor()
{
Runner.Run(FlowTestStep.FirstFlowTest, StartFirstFlowTest, _delay_1ms)
.WaitWithStopCondition(FlowTestStep.FirstFlowTestWait, CheckFirstTestResult, CheckFirstTestFailedResult)
.Run(FlowTestStep.SecondFlowTest, StartSecondFlowTest, _delay_1ms)
.WaitWithStopCondition(FlowTestStep.SecondFlowTestWait, CheckSecondTestResult, CheckSecondTestFailedResult)
.End(FlowTestStep.End, NullFun, _delay_1ms);
return Runner.Status;
}
///
/// 启动第一次测试
///
///
private bool StartFirstFlowTest()
{
bool result = _firstFlowTestRoutine.Start()==RState.Running;
if(result)
{
_commonDevice.CommonData.LeakStatus = "Testing";
}
else
{
_commonDevice.CommonData.LeakStatus = "Failed";
}
return result;
}
///
/// 检验第一次测试结果
///
///
private bool CheckFirstTestResult()
{
RState rState = _firstFlowTestRoutine.Monitor();
if (rState == RState.End)
{
_commonDevice.CommonData.LeakStatus = "PASS";
return true;
}
else
{
if(rState==RState.Timeout)
{
_isNeedReTest = true;
return true;
}
}
return false;
}
///
/// 检验第一次失败测试结果
///
///
private bool CheckFirstTestFailedResult()
{
RState rState = _firstFlowTestRoutine.Monitor();
if(rState==RState.Failed)
{
_commonDevice.CommonData.LeakStatus = "Failed";
_commonDevice.LeakFlowClampOffAction();
_commonDevice.LeakVacuumOffAction();
return true;
}
return false;
}
///
/// 启动第一次测试
///
///
private bool StartSecondFlowTest()
{
if (_isNeedReTest)
{
bool result = _reLeakFlowTestRoutine.Start() == RState.Running;
if (!result)
{
_commonDevice.CommonData.LeakStatus = "Failed";
}
return result;
}
return true;
}
///
/// 检验第二次测试结果
///
///
private bool CheckSecondTestResult()
{
if (_isNeedReTest)
{
RState rState = _reLeakFlowTestRoutine.Monitor();
if (rState == RState.End)
{
_commonDevice.CommonData.LeakStatus = "PASS";
return true;
}
return false;
}
return true;
}
///
/// 检验第二次失败测试结果
///
///
private bool CheckSecondTestFailedResult()
{
if (_isNeedReTest)
{
RState rState = _reLeakFlowTestRoutine.Monitor();
if (rState == RState.Failed||rState==RState.Timeout)
{
_commonDevice.CommonData.LeakStatus = "Failed";
_commonDevice.LeakFlowClampOffAction();
_commonDevice.LeakVacuumOffAction();
return true;
}
return false;
}
return false;
}
///
/// 启动
///
///
///
public RState Start(params object[] objs)
{
_isNeedReTest = false;
_reLeakFlowTestRoutine = new LoaderReLeakFlowTestRoutine(Module);
_firstFlowTestRoutine=new LoaderLeakFirstFlowTestRoutine(Module);
_commonDevice = DEVICE.GetDevice($"{Module}.Common");
Runner.Start(Module, "Flow Test");
return RState.Running;
}
}
}