123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Aitex.Core.RT.DataCenter;
- using Aitex.Core.RT.Device;
- using Aitex.Core.RT.Fsm;
- using Aitex.Core.RT.OperationCenter;
- using Aitex.Core.RT.Routine;
- using Aitex.Core.Utilities;
- using Aitex.Sorter.Common;
- using MECF.Framework.Common.Equipment;
- using MECF.Framework.Common.Schedulers;
- using MECF.Framework.RT.ModuleLibrary.BufferModules;
- namespace JetEfemLib.Buffers
- {
- public class BufferModule : BufferModuleBase
- {
- public enum STATE
- {
- NotInstall,
- Init,
- Idle,
- Homing,
- Cooling,
- Error,
- }
- public enum MSG
- {
- Home,
- Reset,
- Abort,
- Error,
- Cooling,
- SetOnline,
- SetOffline,
- ToInit,
- };
- public override bool IsReady
- {
- get { return FsmState == (int)STATE.Idle && CheckAllMessageProcessed(); }
- }
- public override bool IsError
- {
- get { return FsmState == (int)STATE.Error; }
- }
- public override bool IsInit
- {
- get { return FsmState == (int)STATE.Init; }
- }
- public bool IsBusy
- {
- get { return !IsInit && !IsError && !IsReady; }
- }
- public bool IsIdle
- {
- get { return FsmState == (int)STATE.Idle && CheckAllMessageProcessed(); }
- }
- public int CurrentCoolingTime
- {
- get
- {
- if (FsmState == (int)STATE.Cooling)
- return _coolingRoutine.ElapsedTime;
- return 0;
- }
- }
- public int TotalCoolingTime
- {
- get
- {
- if (FsmState == (int)STATE.Cooling)
- return (int)_coolingRoutine._coolingValue;
- return 0;
- }
- }
- public bool CoolingTypeIsTime
- {
- get
- {
- if (FsmState == (int)STATE.Cooling)
- return _coolingRoutine._coolingTypeIsTime;
- return false;
- }
- }
- public event Action<string> OnEnterError;
- private bool _isInit;
- private BufferHomeRoutine _homeRoutine;
- private BufferCoolingRoutine _coolingRoutine;
- public BufferModule(ModuleName module) : base(3)
- {
- Module = module.ToString();
- Name = module.ToString();
- IsOnline = true;
- EnumLoop<STATE>.ForEach((item) =>
- {
- MapState((int)item, item.ToString());
- });
- EnumLoop<MSG>.ForEach((item) =>
- {
- MapMessage((int)item, item.ToString());
- });
- EnableFsm(50, IsInstalled ? STATE.Init : STATE.NotInstall);
- }
- public override bool Initialize()
- {
- InitRoutine();
- InitDevice();
- InitFsm();
- InitOp();
- InitData();
- return base.Initialize();
- }
- private void InitRoutine()
- {
- ModuleName module = ModuleHelper.Converter(Module);
- _homeRoutine = new BufferHomeRoutine(module);
- _coolingRoutine = new BufferCoolingRoutine(module);
- }
- private void InitDevice()
- {
- }
- private void InitFsm()
- {
- //Error
- AnyStateTransition(MSG.Error, FsmOnError, STATE.Error);
- AnyStateTransition(FSM_MSG.ALARM, FsmOnError, STATE.Error);
- Transition(STATE.Error, MSG.Reset, FsmReset, STATE.Idle);
- EnterExitTransition<STATE, FSM_MSG>(STATE.Error, FsmEnterError, FSM_MSG.NONE, FsmExitError);
- //Home
- Transition(STATE.Init, MSG.Home, FsmStartHome, STATE.Homing);
- Transition(STATE.Error, MSG.Home, FsmStartHome, STATE.Homing);
- Transition(STATE.Idle, MSG.Home, FsmStartHome, STATE.Homing);
- Transition(STATE.Homing, FSM_MSG.TIMER, FsmMonitorHomeTask, STATE.Idle);
- Transition(STATE.Homing, MSG.Error, null, STATE.Init);
- Transition(STATE.Homing, MSG.Abort, FsmAbortTask, STATE.Init);
- EnterExitTransition((int)STATE.Homing, FsmEnterIdle, (int)FSM_MSG.NONE, FsmExitIdle);
- AnyStateTransition(MSG.ToInit, FsmToInit, STATE.Init);
- //Online
- Transition(STATE.Idle, MSG.SetOnline, FsmStartSetOnline, STATE.Idle);
- Transition(STATE.Idle, MSG.SetOffline, FsmStartSetOffline, STATE.Idle);
- //cooling
- Transition(STATE.Idle, MSG.Cooling, FsmStartCooling, STATE.Cooling);
- Transition(STATE.Cooling, FSM_MSG.TIMER, FsmMonitorTask, STATE.Idle);
- Transition(STATE.Cooling, MSG.Abort, FsmAbortTask, STATE.Idle);
-
- }
- private void InitOp()
- {
- OP.Subscribe($"{Name}.Home", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Home));
- OP.Subscribe($"{Name}.Reset", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Reset));
- OP.Subscribe($"{Name}.Abort", (string cmd, object[] args) => CheckToPostMessage((int)MSG.Abort));
- OP.Subscribe($"{Module}.SetOnline", (string cmd, object[] args) => CheckToPostMessage((int)MSG.SetOnline));
- OP.Subscribe($"{Module}.SetOffline", (string cmd, object[] args) => CheckToPostMessage((int)MSG.SetOffline));
- OP.Subscribe($"{Name}.Cooling", (string cmd, object[] args) => { return CheckToPostMessage((int)MSG.Cooling, args); });
-
- }
- private void InitData()
- {
- DATA.Subscribe($"{Name}.Status", () => StringFsmStatus);
- DATA.Subscribe($"{Name}.IsOnline", () => IsOnline);
- DATA.Subscribe($"{Name}.IsBusy", () => IsBusy);
- DATA.Subscribe($"{Name}.ElapseCoolingTime", () => CurrentCoolingTime);
- DATA.Subscribe($"{Name}.TotalCoolingTime", () => TotalCoolingTime);
- DATA.Subscribe($"{Name}.CoolingTypeIsTime", () => CoolingTypeIsTime);
- }
- private bool FsmOnError(object[] param)
- {
- IsOnline = false;
- if (FsmState == (int)STATE.Error)
- {
- return false;
- }
- if (FsmState == (int)STATE.Init)
- return false;
- return true;
- }
- private bool FsmReset(object[] param)
- {
- if (!_isInit)
- {
- PostMsg(MSG.ToInit);
- return false;
- }
- return true;
- }
- private bool FsmExitError(object[] param)
- {
- return true;
- }
- private bool FsmEnterError(object[] param)
- {
- if (OnEnterError != null)
- OnEnterError(Module);
- return true;
- }
- private bool FsmStartHome(object[] param)
- {
- RState ret = StartRoutine(_homeRoutine);
- if (ret == RState.Failed || ret == RState.End)
- return false;
- _isInit = false;
- return ret == RState.Running;
- }
- private bool FsmMonitorHomeTask(object[] param)
- {
- RState ret = MonitorRoutine();
- if (ret == RState.Failed)
- {
- PostMsg(MSG.Error);
- return false;
- }
- if (ret == RState.End)
- {
- _isInit = true;
- return true;
- }
- return false;
- }
- private bool FsmAbortTask(object[] param)
- {
- AbortRoutine();
- return true;
- }
- private bool FsmExitIdle(object[] param)
- {
- return true;
- }
- private bool FsmEnterIdle(object[] param)
- {
- return true;
- }
- private bool FsmToInit(object[] param)
- {
- return true;
- }
- private bool FsmStartSetOffline(object[] param)
- {
- IsOnline = false;
- return true;
- }
- private bool FsmStartSetOnline(object[] param)
- {
- IsOnline = true;
- return true;
- }
- private bool FsmStartCooling(object[] param)
- {
- _coolingRoutine.Init((bool)param[0], (int)param[1]);
- RState ret = StartRoutine(_coolingRoutine);
- if (ret == RState.Failed || ret == RState.End)
- return false;
- return ret == RState.Running;
- }
- private bool FsmStartWarmUp(object[] param)
- {
- return true;
- }
- private bool FsmMonitorTask(object[] param)
- {
- RState ret = MonitorRoutine();
- if (ret == RState.Failed)
- {
- PostMsg(MSG.Error);
- return false;
- }
- return ret == RState.End;
- }
- public override bool Home(out string reason)
- {
- CheckToPostMessage((int)MSG.Home);
- reason = string.Empty;
- return true;
- }
- public bool CheckAcked(int entityTaskToken)
- {
- return FsmState == (int)STATE.Idle && CheckAllMessageProcessed();
- //return true;
- }
- public override void NoteTransferStart(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType)
- {
- //CheckToPostMessage(MSG.InTransfer);
- }
- public override void NoteTransferStop(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType)
- {
- //if (FsmState == (int)STATE.InTransfer)
- // CheckToPostMessage(MSG.TransferComplete);
- }
- public override bool PrepareTransfer(ModuleName robot, Hand blade, int[] targetSlot, EnumTransferType transferType, out string reason)
- {
- reason = string.Empty;
- return true;
- }
- public override bool TransferHandoff(ModuleName robot, Hand blade, int[] targetSlot, EnumTransferType transferType, out string reason)
- {
- reason = string.Empty;
- return true;
- }
- public override bool PostTransfer(ModuleName robot, Hand blade, int[] targetSlot, EnumTransferType transferType, out string reason)
- {
- reason = string.Empty;
- return true;
- }
- public override bool CheckReadyForTransfer(ModuleName robot, Hand blade, int targetSlot, EnumTransferType transferType,
- out string reason)
- {
- reason = string.Empty;
- return true;
- }
- public void InvokeOffline()
- {
- PostMsg((int)MSG.SetOffline);
- }
- public void InvokeOnline()
- {
- PostMsg((int)MSG.SetOnline);
- }
- public int InvokeCooling(bool coolingTypeIsTime, int time)
- {
- if (CheckToPostMessage((int)MSG.Cooling, coolingTypeIsTime, time))
- return (int)MSG.Cooling;
- return (int)FSM_MSG.NONE;
- }
- }
- }
|