using Aitex.Core.RT.Fsm;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.Utilities;
using MECF.Framework.Common.Equipment;
using MECF.Framework.RT.ModuleLibrary.VceModules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Venus_Core;
using Venus_RT.Devices.VCE;
namespace Venus_RT.Modules.VCE
{
#region 状态和消息
public enum VceSTATE
{
Init,//初始化
Idle,//空闲
Error,//错误
Homing,//home操作 对应HM
DoorOpenning,//开门操作 对应DC
DoorClosing,//关门操作 对应DO
Loading,//取cassette操作 包括开门关门 对应LOAD,
UnLoading,
Mapping,//扫片
ReadingMap, //对应MP 包括三种格式 hex binary 智能(只有智能模式可以判断复杂情况)
Goting,//指定槽到达窗口 对应 GC
Resetting,//重置
LoadPreparing,//准备
LoadingWithoutSMIF,//没有SMIF的load
Unknown
}
public enum VceMSG
{
Home,
Error,
DoorOpen,
DoorClose,
Map,
ReadMap,
Load,
UnLoad,
LoadPrepare,
LoadWithoutSMIF,
Reset,
Goto,
Abort
}
#endregion
public class VceEntity : Entity, IEntity, IModuleEntity
{
private ModuleName _modulename;
private readonly VceModuleBase _vce;
public bool IsIdle => fsm.State == (int)VceSTATE.Idle;
public bool IsInit => fsm.State == (int)VceSTATE.Init || fsm.State == (int)VceSTATE.Unknown;
public bool IsBusy => !IsInit && !IsError && !IsIdle;
public bool IsError => fsm.State == (int)VceSTATE.Error;
public VceEntity(ModuleName moduleName)
{
_modulename = moduleName;
_vce = new HongHuVce(25, _modulename);
InitFsmMap();
}
protected override bool Init()
{
OP.Subscribe($"{_modulename}.HOME", (cmd,args) => { PostMsg(VceMSG.Home);return true; });
OP.Subscribe($"{_modulename}.DoorOpen", (cmd,args) => { PostMsg(VceMSG.DoorOpen);return true; });
OP.Subscribe($"{_modulename}.DoorClose", (cmd,args) => { PostMsg(VceMSG.DoorClose);return true; });
OP.Subscribe($"{_modulename}.Map", (cmd,args) => { PostMsg(VceMSG.Map);return true; });
OP.Subscribe($"{_modulename}.ReadMap", (cmd,args) => { PostMsg(VceMSG.ReadMap);return true; });
OP.Subscribe($"{_modulename}.Load", (cmd,args) => { PostMsg(VceMSG.Load);return true; });
OP.Subscribe($"{_modulename}.UnLoad", (cmd,args) => { PostMsg(VceMSG.UnLoad);return true; });
OP.Subscribe($"{_modulename}.Reset", (cmd,args) => { PostMsg(VceMSG.Reset);return true; });
OP.Subscribe($"{_modulename}.Goto", (cmd,args) => { PostMsg(VceMSG.Goto);return true; });
OP.Subscribe($"{_modulename}.Abort", (cmd, args) => { PostMsg(VceMSG.Abort); return true; });
return true;
}
///
/// 定义状态机的迁移表
///
private void InitFsmMap()
{
fsm = new StateMachine(_modulename.ToString(), (int)VceSTATE.Init, 50);
fsm.EnableRepeatedMsg(true);
AnyStateTransition(VceMSG.Error, fnError, VceSTATE.Error);
AnyStateTransition(VceMSG.Abort, fnAbort, VceSTATE.Idle);
AnyStateTransition(VceMSG.Home, fnStartHome, VceSTATE.Homing);
//HOME init->homing idle->homing
Transition(VceSTATE.Init, VceMSG.Home, fnStartHome, VceSTATE.Homing);
Transition(VceSTATE.Idle, VceMSG.Home, fnStartHome, VceSTATE.Homing);
Transition(VceSTATE.Error, VceMSG.Home, fnStartHome, VceSTATE.Homing);
Transition(VceSTATE.Homing, FSM_MSG.TIMER, fnHomeTimeout, VceSTATE.Idle);
//Open Door 开门
Transition(VceSTATE.Idle, VceMSG.DoorOpen, fnStartOpenDoor, VceSTATE.DoorOpenning);
Transition(VceSTATE.DoorOpenning, FSM_MSG.TIMER, fnOpenDoorTimeout, VceSTATE.Idle);
//Close Door 关门
Transition(VceSTATE.Idle, VceMSG.DoorClose, fnStartCloseDoor, VceSTATE.DoorClosing);
Transition(VceSTATE.DoorClosing, FSM_MSG.TIMER, fnCloseDoorTimeout, VceSTATE.Idle);
//Map 扫片
Transition(VceSTATE.Idle, VceMSG.Map, fnStartMapping, VceSTATE.Mapping);
Transition(VceSTATE.Mapping, FSM_MSG.TIMER, fnMappingTimeout, VceSTATE.Idle);
//Load 取cassette
Transition(VceSTATE.Idle, VceMSG.Load, fnStartLoading, VceSTATE.Loading);
Transition(VceSTATE.Loading, FSM_MSG.TIMER, fnLoadingTimeout, VceSTATE.Idle);
//UnLoad 放cassette
Transition(VceSTATE.Idle, VceMSG.UnLoad, fnStartUnLoading, VceSTATE.UnLoading);
Transition(VceSTATE.UnLoading, FSM_MSG.TIMER, fnUnLoadingTimeout, VceSTATE.Idle);
//Goto 指定槽对准窗口
Transition(VceSTATE.Idle, VceMSG.Goto, fnStartGoto, VceSTATE.Goting);
Transition(VceSTATE.Goting, FSM_MSG.TIMER, fnGotingTimeout, VceSTATE.Idle);
//ReadMap
Transition(VceSTATE.Idle, VceMSG.ReadMap, fnStartReadingMap, VceSTATE.ReadingMap);
Transition(VceSTATE.ReadingMap, FSM_MSG.TIMER, fnReadingMapTimeout, VceSTATE.Idle);
//Load Prepare
Transition(VceSTATE.Idle, VceMSG.LoadPrepare,fnStartLoadPrepare,VceSTATE.LoadPreparing) ;
Transition(VceSTATE.LoadPreparing, VceMSG.LoadPrepare,fnLoadingPrepareTimeout,VceSTATE.Idle) ;
EnumLoop.ForEach((item) => { fsm.MapState((int)item, item.ToString()); });
EnumLoop.ForEach((item) => { fsm.MapMessage((int)item, item.ToString()); });
Running = true;
}
private bool fnLoadingPrepareTimeout(object[] param)
{
throw new NotImplementedException();
}
private bool fnStartLoadPrepare(object[] param)
{
throw new NotImplementedException();
}
private bool fnReadingMapTimeout(object[] param)
{
if (_vce.Status == RState.Timeout || _vce.Status == RState.Failed)
{
PostMsg(VceMSG.Error);
return false;
}
return _vce.Status == RState.End;
}
private bool fnStartReadingMap(object[] param)
{
return _vce.ReadMap();
}
//急停
private bool fnAbort(object[] param)
{
return true;
}
//升降到槽位
private bool fnStartGoto(object[] param)
{
return _vce.Goto(Convert.ToInt32(param[0]));
}
private bool fnGotingTimeout(object[] param)
{
if (_vce.Status == RState.Timeout || _vce.Status == RState.Failed)
{
PostMsg(VceMSG.Error);
return false;
}
return _vce.Status == RState.End;
}
private bool fnError(object[] param)
{
return true;
}
private bool fnStartHome(object[] param)
{
return _vce.HomeALL();
}
private bool fnHomeTimeout(object[] param)
{
if (_vce.Status == RState.Timeout || _vce.Status == RState.Failed)
{
PostMsg(VceMSG.Error);
return false;
}
return _vce.Status == RState.End;
}
private bool fnStartOpenDoor(object[] param)
{
return _vce.OpenDoor();
}
private bool fnOpenDoorTimeout(object[] param)
{
if (_vce.Status == RState.Timeout || _vce.Status == RState.Failed)
{
PostMsg(VceMSG.Error);
return false;
}
return _vce.Status == RState.End;
}
private bool fnStartCloseDoor(object[] param)
{
return _vce.CloseDoor();
}
private bool fnCloseDoorTimeout(object[] param)
{
if (_vce.Status == RState.Timeout || _vce.Status == RState.Failed)
{
PostMsg(VceMSG.Error);
return false;
}
return _vce.Status == RState.End;
}
private bool fnStartMapping(object[] param)
{
return _vce.Map();
}
private bool fnMappingTimeout(object[] param)
{
if(_vce.Status == RState.Timeout || _vce.Status == RState.Failed)
{
PostMsg(VceMSG.Error);
return false;
}
return _vce.Status == RState.End;
}
private bool fnStartLoading(object[] param)
{
return _vce.Load();
}
private bool fnLoadingTimeout(object[] param)
{
if (_vce.Status == RState.Timeout || _vce.Status == RState.Failed)
{
PostMsg(VceMSG.Error);
return false;
}
return _vce.Status == RState.End;
}
private bool fnStartUnLoading(object[] param)
{
return _vce.UnLoad();
}
private bool fnUnLoadingTimeout(object[] param)
{
if (_vce.Status == RState.Timeout || _vce.Status == RState.Failed)
{
PostMsg(VceMSG.Error);
return false;
}
return _vce.Status == RState.End;
}
public bool Check(int msg, out string reason, params object[] args)
{
reason = "";
return true;
}
public int Invoke(string function, params object[] args)
{
throw new NotImplementedException();
}
public bool CheckAcked(int msg)
{
throw new NotImplementedException();
}
}
}