123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- using System;
- using System.Collections.Generic;
- using Aitex.Core.RT.Fsm;
- using Aitex.Core.RT.Routine;
- using Aitex.Core.Util;
- using Aitex.Triton160.RT.Device;
- using Aitex.Triton160.RT.Module;
- using Aitex.Triton160.RT.Routine.RT;
- namespace Aitex.RT.Module
- {
- public class RouteManager : Entity, IEntity
- {
- public enum STATE
- {
- INIT,
- IDLE,
- ERROR,
- Reset,
- };
- public enum MSG
- {
- Auto,
- AutoStop,
- RESET,
- ABORT,
- ERROR,
- };
- //private device
- //routine
- private InitRoutine initRoutine = null;
- private RD_TRIG plcSync = new RD_TRIG();
- //private int state = (int)STATE.INIT;
- private Queue<IRoutine> steps = null;
- //private IRoutine curStep = null;
- public bool IsAutoMode { get; private set; }
- public string Name { get; set; }
- public string Status { get { return State2String(fsm.State); } }
-
- //bool bAuto = false;
- public RouteManager()
- {
- Name = "系统";
- //state = (int)STATE.INIT;
- fsm = new StateMachine<RouteManager>(Name, (int)STATE.INIT, 50);
- //Init sequence
- EnterExitTransition<STATE, FSM_MSG>(STATE.INIT, fStartInit, FSM_MSG.NONE, null);
- Transition(STATE.INIT, FSM_MSG.TIMER, fInit, STATE.IDLE);
- Transition(STATE.INIT, MSG.ERROR, fError, STATE.ERROR);
- //Reset
- AnyStateTransition(MSG.RESET, fStartReset, STATE.Reset);
- Transition(STATE.Reset, FSM_MSG.TIMER, fReset, STATE.IDLE);
- AnyStateTransition(MSG.ERROR, fError, STATE.ERROR);
- AnyStateTransition((int)FSM_MSG.ALARM, fError, (int)STATE.ERROR);
- //DATA.Subscribe(ModuleName.System.ToString(), ParamName.RTState, () => state);
- //DATA.Subscribe(ModuleName.System.ToString(), ParamName.RTStatus, () => Status);
- steps = new Queue<IRoutine>();
- Running = true;
- }
- public bool Check(int msg, out string reason, object[] objs)
- {
- bool bRet = true;
- reason = "";
- switch (msg)
- {
-
- case (int)MSG.Auto:
- {
- if (fsm.State != (int)STATE.IDLE)
- {
- reason = String.Format("{0}正在处理{1},不能执行{2}", Name, State2String(fsm.State), Msg2String(msg));
- return false;
- }
- }
- break;
- default:
- break;
- }
- return bRet;
- }
- private string Msg2String(int msg)
- {
- string result = "";
- switch (msg)
- {
- case (int)MSG.RESET:
- result = "复位";
- break;
- case (int)MSG.ABORT:
- result = "终止";
- break;
- }
- return result;
- }
- private string State2String(int state)
- {
- string result = "";
- switch (state)
- {
- case (int)STATE.INIT:
- case (int)STATE.Reset:
- result = "初始化";
- break;
- case (int)STATE.IDLE:
- result = "就绪";
- break;
- case (int)STATE.ERROR:
- result = "错误";
- break;
- }
- return result;
- }
- protected override bool Init()
- {
- initRoutine = new InitRoutine("System", "初始化");
- initRoutine.Initalize();
- return true;
- }
- protected override void Term()
- {
- }
- #region Init
- private bool fStartInit(object[] objs)
- {
- Running = false;
- Result ret = initRoutine.Start(objs);
- if (ret == Result.DONE)
- {
- return false;
- }
- else if (ret == Result.FAIL)
- {
- return false; //do noting
- }
- return true;
- }
- private bool fInit(object[] objs)
- {
- Result ret = initRoutine.Monitor();
- if (ret == Result.DONE)
- {
- Running = true;
- //state = (int)STATE.IDLE;
- return true;
- }
- else if (ret == Result.FAIL)
- {
- //do nothing
- //state = (int)STATE.ERROR;
- return true;
- }
- return false; ;
- }
- private bool fError(object[] objs)
- {
- // refillRoutine.Abort();
- IsAutoMode = false;
- return true;
- }
- #endregion
- //#region Auto
-
- //private bool RunRecipe(string recipeName)
- //{
- // var rManager = RecipeFileManager.Instance;
- // var recipeContent = rManager.LoadRecipe(ModuleName.System.ToString(), recipeName, true);
- // if (string.IsNullOrEmpty(recipeContent))
- // {
- // EV.PostMessage(ModuleName.System.ToString(), EventEnum.PrepareProcessErr, ModuleName.System.ToString(), string.Format("工艺程序{0}读取错误", recipeName));
- // return false; // The recipe is invalide.
- // }
- // List<string> reasons;
- // if (!rManager.CheckRecipe(ModuleName.System.ToString(), recipeContent, out reasons))
- // {
- // EV.PostMessage(ModuleName.System.ToString(), EventEnum.PrepareProcessErr, ModuleName.System.ToString(), string.Format("工艺程序{0}内容存在错误", recipeName));
- // string info = "";
- // foreach (var item in reasons)
- // info += item;
- // EV.PostPopDialogMessage(EventLevel.Alarm, String.Format("工艺程序{0} 错误", recipeName), info);
- // return false; //Recipe content check.
- // }
- // string reason = String.Empty;
- // Singleton<PMEntity>.Instance.PostMsg(PMEntity.MSG.Process, recipeName, recipeContent);
- // return true;
- //}
- //#endregion
- #region reset
- private bool fStartReset(object[] objs)
- {
- Singleton<DeviceEntity>.Instance.PostMsg(DeviceEntity.MSG.RESET);
- Singleton<PMEntity>.Instance.PostMsg(PMEntity.MSG.Reset);
- return true;
- }
-
- private bool fReset(object[] objs)
- {
- int elapsed = fsm.ElapsedTime;
- if (elapsed > 500) //wait 2s
- {
- //notify
- reset();
- if (fsm.State == (int)STATE.ERROR)
- return true;
- return false;
- }
- return false;
- }
- private void reset()
- {
- IsAutoMode = false;
- string reason = string.Empty;
- }
- #endregion
- }
- }
|