using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Fsm;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.RT.RecipeCenter;
using Aitex.Core.Util;
using Aitex.Core.Utilities;
using CyberX12_RT.Modules.VpwCell;
using MECF.Framework.Common.Alarm;
using MECF.Framework.Common.CommonData;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.Persistent.Temperature;
using MECF.Framework.Common.Persistent.VpwCell;
using MECF.Framework.Common.Persistent.VpwMain;
using MECF.Framework.Common.RecipeCenter;
using MECF.Framework.Common.Routine;
using MECF.Framework.Common.SubstrateTrackings;
using MECF.Framework.Common.ToolLayout;
using PunkHPX8_Core;
using PunkHPX8_RT.Devices.LinMot;
using PunkHPX8_RT.Devices.VpwCell;
using PunkHPX8_RT.Modules.VpwCell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PunkHPX8_RT.Modules.VpwMain
{
public class VpwCellEntity : Entity, IEntity, IModuleEntity
{
public enum MSG
{
Home
}
#region 常量
private const string STRATUS = "Stratus";
private const string AUTO = "Auto";
private const string MANUAL = "Manual";
private const string DISABLED = "Disabled";
private const string ENGINEERING = "Engineering";
private const string PRODUCTION = "Production";
#endregion
#region 内部变量
///
/// 持久化数值
///
private VpwCellPersistentValue _persistentValue;
///
/// VPW cell集合
///
private List _vpwCellDevices = new List();
///
/// Home Routine
///
private VPWHomeRoutine _homeRoutine;
///
/// Prepare
///
private VpwPrepareRoutine _prepareRoutine;
///
/// recipe routine
///
private VpwRecipeRoutine _recipeRoutine;
///
/// 手动recipe routine
///
private VpwManualRecipeRoutine _manualRecipeRoutine;
///
/// 循环routine
///
private VpwCycleManualProcessRecipeRoutine _cycleManualProcessRoutine;
///
/// recipe完成次数
///
private int _achievedCycle;
///
/// Cycle次数
///
private int _cycle = 0;
///
/// 是否Retry
///
private bool _isRetry = false;
///
/// recipe时长
///
private int _recipeTime;
///
/// 当前Recipe
///
private VpwRecipe _currentRecipe;
///
/// run recipe start time
///
private DateTime _runRecipeStartTime;
///
/// run recipe complete time
///
private DateTime _runRecipeCompleteTime;
///
/// 工艺当前执行小步骤
///
private string _currentStateMachine = "Init";
///
/// 工艺当前执行大步骤
///
private string _currentStatus = "Init";
#endregion
#region 属性
public ModuleName Module { get; private set; }
///
/// 是否Init
///
public bool IsInit
{
get { return fsm.State == (int)VPWCellState.Init; }
}
///
/// 是否Idle
///
public bool IsIdle
{
get
{
return fsm.State == (int)VPWCellState.Idle;
}
}
///
/// 是否错误
///
public bool IsError
{
get { return fsm.State == (int)VPWCellState.Error; }
}
///
/// 正在忙碌
///
public bool IsBusy
{
get { return fsm.State == (int)VPWCellState.Initializing; }
}
///
/// 是否禁用
///
public bool IsDisable { get { return _persistentValue == null || _persistentValue.OperatingMode == DISABLED; } }
///
/// 自动模式
///
public bool IsAuto { get { return _persistentValue != null && _persistentValue.OperatingMode == AUTO; } }
///
/// 自动模式
///
public bool IsManual { get { return _persistentValue != null && _persistentValue.OperatingMode == MANUAL; } }
///
/// 是否为工程模式
///
public bool IsEngineering { get { return _persistentValue != null && _persistentValue.RecipeOperatingMode == ENGINEERING; } }
///
/// 是否为产品模式
///
public bool IsProduction { get { return _persistentValue != null && _persistentValue.RecipeOperatingMode == PRODUCTION; } }
///
/// recipe时长
///
public int RecipeTime { get { return _recipeTime; } }
#endregion
///
/// 构造函数
///
///
public VpwCellEntity(ModuleName module)
{
this.Module = module;
}
///
/// 初始化
///
///
protected override bool Init()
{
InitialFsm();
InitializeParameter();
InitializeRoutine();
InitializeDATA();
InitializeOperation();
return true;
}
///
/// 初始化参数
///
private void InitializeParameter()
{
_persistentValue = VpwCellPersistentManager.Instance.GetPersistentValue(Module.ToString());
if (_persistentValue == null)
{
LOG.WriteLog(eEvent.ERR_VPW, Module.ToString(), "Persistent Value Object is not exist");
}
_vpwCellDevices.Clear();
VpwMainItem vpwMainItem = VpwMainItemManager.Instance.GetItem(ModuleName.VPWMain1.ToString());
if (vpwMainItem == null || vpwMainItem.VpwCells == null)
{
return;
}
foreach (var item in vpwMainItem.VpwCells)
{
VpwCellDevice cellDevice = DEVICE.GetDevice(item.ModuleName);
_vpwCellDevices.Add(cellDevice);
}
}
///
/// 初始化状态机
///
private void InitialFsm()
{
fsm = new StateMachine(Module.ToString(), (int)VPWCellState.Init, 100);
fsm.EnableRepeatedMsg(true);
AnyStateTransition(VpwCellMsg.Error, NullFunc, VPWCellState.Error);
//Initialized
Transition(VPWCellState.Error, VpwCellMsg.Initialize, InitializeAll, VPWCellState.Initializing);
Transition(VPWCellState.Init, VpwCellMsg.Initialize, InitializeAll, VPWCellState.Initializing);
Transition(VPWCellState.Idle, VpwCellMsg.Initialize, InitializeAll, VPWCellState.Initializing);
Transition(VPWCellState.Initializing, FSM_MSG.TIMER, InitializeAllMonitor, VPWCellState.Idle);
Transition(VPWCellState.Error, VpwCellMsg.EnterIdle, NullFunc, VPWCellState.Idle);
Transition(VPWCellState.Init, VpwCellMsg.EnterIdle, NullFunc, VPWCellState.Idle);
Transition(VPWCellState.Idle, VpwCellMsg.EnterIdle, NullFunc, VPWCellState.Idle);
//Enter Init
Transition(VPWCellState.Idle, VpwCellMsg.Init, NullFunc, VPWCellState.Init);
//Manual Recipe
Transition(VPWCellState.Idle, VpwCellMsg.ManualRecipe, ManualRunRecipe, VPWCellState.ManualReciping);
Transition(VPWCellState.ManualReciping, FSM_MSG.TIMER, ManualRunRecipeMonitor, VPWCellState.Idle);
//Cycle Manual Process
Transition(VPWCellState.Idle, VpwCellMsg.CycleProcessRecipe, CycleManualProcess, VPWCellState.CycleManualProcessing);
Transition(VPWCellState.CycleManualProcessing, FSM_MSG.TIMER, CycleManualMonitor, VPWCellState.Idle);
//Prepare
Transition(VPWCellState.Idle, VpwCellMsg.Prepare, Prepare, VPWCellState.Preparing);
Transition(VPWCellState.Preparing, FSM_MSG.TIMER, PrepareMonitor, VPWCellState.WaitForRunRecipe);
Transition(VPWCellState.WaitForRunRecipe, VpwCellMsg.RunRecipe, RunRecipe, VPWCellState.RunReciping);
Transition(VPWCellState.RunReciping, FSM_MSG.TIMER, RunRecipeMonitor, VPWCellState.Idle);
//Retry
Transition(VPWCellState.Error, VpwCellMsg.Retry, NullFunc, VPWCellState.Retrying);
Transition(VPWCellState.Retrying, FSM_MSG.TIMER, VpwCellRetry, VPWCellState.Retrying);
Transition(VPWCellState.Retrying, VpwCellMsg.Prepare, RetryPrepare, VPWCellState.Preparing);
Transition(VPWCellState.Retrying, VpwCellMsg.RunRecipe, RetryRunRecipe, VPWCellState.RunReciping);
}
///
/// 初始化数据
///
private void InitializeDATA()
{
InitializeSVID();
DATA.Subscribe($"{Module}.FsmState", () => ((VPWCellState)fsm.State).ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
DATA.Subscribe($"{Module}.IsIdle", () => IsIdle, SubscriptionAttribute.FLAG.IgnoreSaveDB);
DATA.Subscribe($"{Module}.IsInit", () => IsInit, SubscriptionAttribute.FLAG.IgnoreSaveDB);
DATA.Subscribe($"{Module}.IsDisable", () => IsDisable, SubscriptionAttribute.FLAG.IgnoreSaveDB);
DATA.Subscribe($"{Module}.IsBusy", () => IsBusy, SubscriptionAttribute.FLAG.IgnoreSaveDB);
DATA.Subscribe($"{Module}.IsError", () => IsError, SubscriptionAttribute.FLAG.IgnoreSaveDB);
DATA.Subscribe($"{Module}.AchievedCycle", () => _achievedCycle, SubscriptionAttribute.FLAG.IgnoreSaveDB);
}
///
/// 初始化SVID
///
private void InitializeSVID()
{
DATA.Subscribe($"{Module}.OperatingMode", () => _persistentValue != null ? _persistentValue.OperatingMode : "None", SubscriptionAttribute.FLAG.IgnoreSaveDB);
}
///
/// 初始化Routine
///
private void InitializeRoutine()
{
_homeRoutine = new VPWHomeRoutine(Module.ToString());
_prepareRoutine=new VpwPrepareRoutine(Module.ToString());
_recipeRoutine=new VpwRecipeRoutine(Module.ToString());
_manualRecipeRoutine=new VpwManualRecipeRoutine(Module.ToString());
_cycleManualProcessRoutine = new VpwCycleManualProcessRecipeRoutine(Module.ToString());
}
///
/// 初始化操作
///
private void InitializeOperation()
{
OP.Subscribe($"{Module}.InitializeAll", (cmd, args) => { return CheckToPostMessage(eEvent.ERR_VPW, Module.ToString(), (int)VpwCellMsg.Initialize); });
OP.Subscribe($"{Module}.Prepare", (cmd, args) => { return CheckToPostMessage(eEvent.ERR_VPW, Module.ToString(), (int)VpwCellMsg.Prepare); });
OP.Subscribe($"{Module}.CycleManualProcessRecipe", (cmd, args) =>
{
VpwRecipe recipe = RecipeFileManager.Instance.LoadGenericityRecipe(args[0].ToString());
if (recipe == null)
{
LOG.WriteLog(eEvent.ERR_VPW, Module.ToString(), $"{args[0]} recipe is null");
return false;
}
object[] objects = new object[args.Length];
objects[0] = recipe;
for (int i = 1; i < args.Length; i++)
{
objects[i] = args[i];
}
return CheckToPostMessage(eEvent.ERR_PREWET, Module.ToString(), (int)VpwCellMsg.CycleProcessRecipe, objects);
});
}
#region InitializeAll
///
/// Initialize
///
///
///
private bool InitializeAll(object[] param)
{
if (_vpwCellDevices == null || _vpwCellDevices.Count == 0)
{
LOG.WriteLog(eEvent.ERR_VPW, Module.ToString(), "cell device is empty");
return false;
}
foreach (var device in _vpwCellDevices)
{
VpwCellEntity vpwCellEntity = Singleton.Instance.GetModule(device.Module);
if (vpwCellEntity.IsBusy)
{
LOG.WriteLog(eEvent.ERR_VPW, Module.ToString(), $"cell device {device.Module} is busy,cannot initialize");
return false;
}
}
return _homeRoutine.Start(_vpwCellDevices) == RState.Running;
}
///
/// Initialize 监控
///
///
///
private bool InitializeAllMonitor(object[] param)
{
RState ret = _homeRoutine.Monitor();
if (ret == RState.Failed || ret == RState.Timeout)
{
PostMsg(VpwCellMsg.Error);
return false;
}
return ret == RState.End;
}
#endregion
#region Prepare
///
/// Prepare
///
///
///
private bool Prepare(object[] param)
{
VpwRecipe recipe = param[0] as VpwRecipe;
return _prepareRoutine.Start(recipe) == RState.Running;
}
///
/// Prepare 监控
///
///
///
private bool PrepareMonitor(object[] param)
{
RState ret = _prepareRoutine.Monitor();
if (ret == RState.Failed || ret == RState.Timeout)
{
AlarmList alarmList = new AlarmList(Module.ToString(), ((VPWCellState)fsm.State).ToString(), (int)VpwCellMsg.Prepare,
_prepareRoutine.ErrorMsg, _prepareRoutine.ErrorStep, (int)AlarmType.Error);
AlarmListManager.Instance.AddAlarm(alarmList);
PostMsg(VpwCellMsg.Error);
return false;
}
return ret == RState.End;
}
///
/// Retry Prepare
///
///
///
private bool RetryPrepare(object[] param)
{
int stepIndex = (int)param[0];
bool result = _prepareRoutine.Retry(stepIndex) == RState.Running;
return result;
}
#endregion
#region Run Recipe
///
/// run recipe
///
///
///
private bool RunRecipe(object[] param)
{
VpwRecipe recipe = param[0] as VpwRecipe;
return _recipeRoutine.Start(recipe) == RState.Running;
}
///
/// Prepare 监控
///
///
///
private bool RunRecipeMonitor(object[] param)
{
RState ret = _recipeRoutine.Monitor();
if (ret == RState.Failed || ret == RState.Timeout)
{
AlarmList alarmList = new AlarmList(Module.ToString(), ((VPWCellState)fsm.State).ToString(), (int)VpwCellMsg.RunRecipe,
_recipeRoutine.ErrorMsg, _recipeRoutine.ErrorStep, (int)AlarmType.Error);
AlarmListManager.Instance.AddAlarm(alarmList);
PostMsg(VpwCellMsg.Error);
return false;
}
return ret == RState.End;
}
///
/// Retry RunRecipe
///
///
///
private bool RetryRunRecipe(object[] param)
{
int stepIndex = (int)param[0];
bool result = _recipeRoutine.Retry(stepIndex) == RState.Running;
return result;
}
#endregion
#region Manual Run Recipe
///
/// run recipe
///
///
///
private bool ManualRunRecipe(object[] param)
{
VpwRecipe recipe = param[0] as VpwRecipe;
return _manualRecipeRoutine.Start(recipe) == RState.Running;
}
///
/// Prepare 监控
///
///
///
private bool ManualRunRecipeMonitor(object[] param)
{
RState ret = _manualRecipeRoutine.Monitor();
if (ret == RState.Failed || ret == RState.Timeout)
{
PostMsg(VpwCellMsg.Error);
return false;
}
return ret == RState.End;
}
#endregion
#region cycle manual process
private bool CycleManualProcess(object[] param)
{
VpwRecipe recipe = param[0] as VpwRecipe;
_cycle = (int)param[1];
bool result = _cycleManualProcessRoutine.Start(param) == RState.Running;
if (result)
{
_isRetry = false;
if (CellItemRecipeTimeManager.Instance.ContainRecipe(recipe.Ppid))
{
_recipeTime = _cycle * CellItemRecipeTimeManager.Instance.GetRecipeTotalTime(recipe.Ppid);
}
else
{
_recipeTime = 0;
}
_currentRecipe = recipe;
_runRecipeStartTime = DateTime.Now;
}
return result;
}
private bool CycleManualMonitor(object[] param)
{
RState state = _cycleManualProcessRoutine.Monitor();
_currentStatus = _cycleManualProcessRoutine.CurrentStatus;
_currentStateMachine = _cycleManualProcessRoutine.CurrentStateMachine;
if (state == RState.Failed || state == RState.Timeout)
{
PostMsg(VpwCellMsg.Error);
_currentStateMachine = "Error";
_currentStatus = "Error";
_runRecipeCompleteTime = DateTime.Now;
_cycleManualProcessRoutine.VpwLotTrackHeaderDatas.ProcessTime = (_runRecipeCompleteTime - _runRecipeStartTime).TotalSeconds.ToString("F2");
//导出lotTrack数据
VpwLotTrackUtil.ExportVpwLotTrack(Module.ToString(), _cycleManualProcessRoutine.VpwLotTrackDatas,
_cycleManualProcessRoutine.VpwLotTrackHeaderDatas, IsAuto, _isRetry);
return false;
}
_achievedCycle = _cycleManualProcessRoutine.GetAchievedCycle();
bool result = state == RState.End;
if (result)
{
double elapsedMilliseconds = _cycleManualProcessRoutine.ElapsedMilliseconds;
int recipeTime = (int)Math.Floor(elapsedMilliseconds / _cycle / 1000);
CellItemRecipeTimeManager.Instance.UpdateRecipeTime(_currentRecipe.Ppid, recipeTime);
_runRecipeCompleteTime = DateTime.Now;
_cycleManualProcessRoutine.VpwLotTrackHeaderDatas.ProcessTime = (_runRecipeCompleteTime - _runRecipeStartTime).TotalSeconds.ToString("F2");
//导出lotTrack数据
VpwLotTrackUtil.ExportVpwLotTrack(Module.ToString(), _cycleManualProcessRoutine.VpwLotTrackDatas,
_cycleManualProcessRoutine.VpwLotTrackHeaderDatas, IsAuto, _isRetry);
}
return result;
}
#endregion
#region VpwCell Retry
///
/// VpwCell
///
///
///
private bool VpwCellRetry(object[] param)
{
AlarmList alarmList = AlarmListManager.Instance.GetAlarmListByModule(Module.ToString());
if (alarmList != null)
{
CheckToPostMessage(eEvent.WARN_VPW, Module.ToString(), alarmList.ModuleCmd,
alarmList.ModuleStep);
}
return false;
}
#endregion
public bool Check(int msg, out string reason, params object[] args)
{
reason = "";
return false;
}
public bool CheckAcked(int msg)
{
return false;
}
///
/// EnterInit
///
public void EnterInit()
{
}
public int Invoke(string function, params object[] args)
{
switch (function)
{
case "HomeAll":
return (int)MSG.Home;
}
return (int)FSM_MSG.NONE;
}
}
}