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 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(Name, (int)STATE.INIT, 50); //Init sequence EnterExitTransition(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(); 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 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.Instance.PostMsg(PMEntity.MSG.Process, recipeName, recipeContent); // return true; //} //#endregion #region reset private bool fStartReset(object[] objs) { Singleton.Instance.PostMsg(DeviceEntity.MSG.RESET); Singleton.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 } }