using Aitex.Core.Common;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.UI.Control;
using CyberX8_Core;
using MECF.Framework.Common.Beckhoff.ModuleIO;
using MECF.Framework.Common.IOCore;
using MECF.Framework.Common.Routine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CyberX8_RT.Devices.SRD
{
public class SrdCommonFlipperRoutine : RoutineBase, IRoutine
{
#region 常量
private const string FLIPPERS_IN_100 = "FlippersIn100";
private const string FLIPPERS_IN_150 = "FlippersIn150";
private const string FLIPPERS_IN_200 = "FlippersIn200";
#endregion
private enum FlipperStep
{
Flipper,
Delay,
CheckFlipperStatus,
End
}
#region 内部变量
private bool _flipperIn;
private int _waferSize = 200;
private SrdCommonDevice _srdCommon;
private int _timeout = 3000;
#endregion
///
/// 构造函数
///
///
public SrdCommonFlipperRoutine(string module) : base(module)
{
}
public void Abort()
{
Runner.Stop("Manual Abort");
}
public RState Monitor()
{
Runner.Run(FlipperStep.Flipper, FlipperPosition, _delay_1ms)
.DelayIf(FlipperStep.Delay, !_flipperIn, 500)
.Run(FlipperStep.CheckFlipperStatus, NullFun, CheckFlipperOutEndStatus, _timeout)
.End(FlipperStep.End, NullFun, 100);
return Runner.Status;
}
public RState Start(params object[] objs)
{
if (objs.Length < 2) return RState.Failed;
_flipperIn = (bool)objs[0];
_waferSize = (int)objs[1];
_srdCommon = DEVICE.GetDevice($"{Module}.Common");
if (_flipperIn)
{
return Runner.Start(Module, "Flippers In");
}
else
{
return Runner.Start(Module, "Flippers Out");
}
}
///
/// Flipper Position
///
///
private bool FlipperPosition()
{
string variableName;
switch (_waferSize)
{
case 100:
variableName = FLIPPERS_IN_100;
break;
case 150:
variableName = FLIPPERS_IN_150;
break;
case 200:
variableName = FLIPPERS_IN_200;
break;
default:
NotifyError(eEvent.ERR_SRD, $"Wafer Size {_waferSize} is invalid", 0);
return false;
}
string ioName = BeckhoffModuleIOManager.Instance.GetIoNameByInnerModuleName($"{Module}.{variableName}");
return IOModuleManager.Instance.WriteIoValue(ioName, _flipperIn);
}
///
/// CheckFlipperOutStatus
///
///
private bool CheckFlipperOutEndStatus()
{
bool result = false;
switch (_waferSize)
{
case 100:
result = _flipperIn ? (!_srdCommon.CommonData.Flipper1Out100Status && !_srdCommon.CommonData.Flipper2Out100Status && !_srdCommon.CommonData.Flipper3Out100Status)
: (_srdCommon.CommonData.Flipper1Out100Status && _srdCommon.CommonData.Flipper2Out100Status && _srdCommon.CommonData.Flipper3Out100Status);
break;
case 150:
result = _flipperIn ? (!_srdCommon.CommonData.Flipper1Out150Status && !_srdCommon.CommonData.Flipper2Out150Status && !_srdCommon.CommonData.Flipper3Out150Status)
: (_srdCommon.CommonData.Flipper1Out150Status && _srdCommon.CommonData.Flipper2Out150Status && _srdCommon.CommonData.Flipper3Out150Status);
break;
case 200:
result = _flipperIn ? (!_srdCommon.CommonData.Flipper1Out200Status && !_srdCommon.CommonData.Flipper2Out200Status && !_srdCommon.CommonData.Flipper3Out200Status)
: (_srdCommon.CommonData.Flipper1Out200Status && _srdCommon.CommonData.Flipper2Out200Status && _srdCommon.CommonData.Flipper3Out200Status);
break;
default:
NotifyError(eEvent.WARN_SRD, $"Wafer Size {_waferSize} is invalid", 0);
result = false;
break;
}
return result;
}
}
}