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.Util;
using Aitex.Core.Utilities;
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.SubstrateTrackings;
using MECF.Framework.Common.ToolLayout;
using PunkHPX8_Core;
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;
#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; } }
#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);
}
///
/// 初始化数据
///
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);
}
///
/// 初始化SVID
///
private void InitializeSVID()
{
DATA.Subscribe($"{Module}.State", () => ((VPWCellState)fsm.State).ToString(), SubscriptionAttribute.FLAG.IgnoreSaveDB);
DATA.Subscribe($"{Module}.OperatingMode", () => _persistentValue != null ? _persistentValue.OperatingMode : "None", SubscriptionAttribute.FLAG.IgnoreSaveDB);
}
///
/// 初始化Routine
///
private void InitializeRoutine()
{
_homeRoutine = new VPWHomeRoutine(Module.ToString());
}
///
/// 初始化操作
///
private void InitializeOperation()
{
OP.Subscribe($"{Module}.InitializeAll", (cmd, args) => { return CheckToPostMessage(eEvent.ERR_VPWMAIN, Module.ToString(), (int)VpwCellMsg.Initialize); });
}
#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
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;
}
}
}