using Aitex.Core.Common;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.SubstrateTrackings;
using PunkHPX8_Core;
using PunkHPX8_RT.Devices.AXIS;
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace PunkHPX8_RT.Modules.PlatingCell
{
public class PlatingCellVerticalPositionRoutine : RoutineBase, IRoutine
{
private enum PositionStep
{
WaitMatcher,
GotoPosition,
GoToPositionCheck,
End
}
#region 内部变量
///
/// vertical axis
///
private JetAxisBase _verticalAxis;
///
/// matcher集合
///
private List _matcher = new List();
///
/// cell集合
///
private List _cellEntities=new List();
///
/// 位置
///
private string _station;
///
/// 偏移量
///
private double _offset;
///
/// 移动速度
///
private int _speed;
///
/// 加速度
///
private int _accelerate;
#endregion
///
/// 构造函数
///
///
public PlatingCellVerticalPositionRoutine(string module) : base(module)
{
}
///
/// 中止
///
public void Abort()
{
Runner.Stop("Manual Abort");
}
///
/// 监控
///
///
public RState Monitor()
{
Runner.Wait(PositionStep.WaitMatcher,CheckMatcher,_delay_2s)
.Run(PositionStep.GotoPosition, VerticalGotoPosition, 100)
.WaitWithStopCondition(PositionStep.GoToPositionCheck, CheckVerticalPositionStatus, CheckVerticalPositionRunStop)
.End(PositionStep.End, NullFun, _delay_1ms);
return Runner.Status;
}
///
/// 检验匹配
///
///
private bool CheckMatcher()
{
if (_cellEntities.Count<=1)
{
return true;
}
foreach (PlatingCellEntity item in _cellEntities)
{
if (!WaferManager.Instance.CheckHasWafer(item.Module, 0))
{
return false;
}
}
WaferInfo firstWafer = WaferManager.Instance.GetWafer(_cellEntities[0].Module, 0);
WaferInfo secondWafer = WaferManager.Instance.GetWafer(_cellEntities[1].Module, 0);
if (firstWafer == null || secondWafer == null || firstWafer.IsEmpty || secondWafer.IsEmpty)
{
return false;
}
string firstCurrentStep = _cellEntities[0].CurrentStepState;
string secondCurrentStep = _cellEntities[1].CurrentStepState;
bool hasDoubleProductionWafer = firstWafer.WaferType == secondWafer.WaferType;
if (hasDoubleProductionWafer)
{
return firstCurrentStep == secondCurrentStep;
}
else
{
//dummy reclaim/rinse/dry与生产片一致
if (secondCurrentStep.StartsWith("Reclaim") || secondCurrentStep.StartsWith("Rinse") || secondCurrentStep.StartsWith("Dry"))
{
return firstCurrentStep==secondCurrentStep;
}
else
{
return true;
}
}
}
///
/// vertical 运动到位置
///
///
private bool VerticalGotoPosition()
{
if(_verticalAxis != null )
{
if (!_verticalAxis.IsSwitchOn)
{
LOG.WriteLog(eEvent.ERR_PLATINGCELL, Module, "Vertical is not Power On");
return false;
}
else if (!_verticalAxis.IsHomed)
{
LOG.WriteLog(eEvent.ERR_PLATINGCELL, Module, "Vertical is not Home,Home vertical first");
return false;
}
return _verticalAxis.PositionStation(_station,_offset,false,_speed,_accelerate);
}
else
{
LOG.WriteLog(eEvent.ERR_PLATINGCELL, Module, "vertical axis is null");
return false;
}
}
///
/// 检验Vertical移动状态
///
///
private bool CheckVerticalPositionStatus()
{
return _verticalAxis.Status == RState.End;
}
///
/// 检验Vertical是否还在运动
///
///
private bool CheckVerticalPositionRunStop()
{
return _verticalAxis.Status == RState.Failed;
}
///
/// 启动
///
///
///
public RState Start(params object[] objs)
{
_station=objs[0].ToString();
_offset = (double)objs[1];
if(objs.Length > 2)
{
_speed = (int)objs[2];
}
if (objs.Length > 3)
{
_accelerate = (int)objs[3];
}
_matcher = ModuleMatcherManager.Instance.GetMatcherListByVertical(Module); //获取电机所在的platingcell 列表
_cellEntities.Clear();
foreach (string str in _matcher)
{
if(!Enum.TryParse(str, out ModuleName moduleName))
{
continue;
}
if (!ModuleHelper.IsInstalled(moduleName))
{
continue;
}
PlatingCellEntity cellEntity = Singleton.Instance.GetModule(str);
if (cellEntity.IsDisable)
{
continue;
}
if (cellEntity.IsError)
{
continue;
}
if (cellEntity.IsAuto)
{
_cellEntities.Add(cellEntity);
}
}
_verticalAxis = DEVICE.GetDevice($"{Module}.Vertical");
return Runner.Start(Module, "Start PlatingCell Vertical Initialize");
}
}
}