RouteManager.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using System;
  2. using System.Collections.Generic;
  3. using Aitex.Core.RT.Fsm;
  4. using Aitex.Core.RT.Routine;
  5. using Aitex.Core.Util;
  6. using Aitex.Triton160.RT.Device;
  7. using Aitex.Triton160.RT.Module;
  8. using Aitex.Triton160.RT.Routine.RT;
  9. namespace Aitex.RT.Module
  10. {
  11. public class RouteManager : Entity, IEntity
  12. {
  13. public enum STATE
  14. {
  15. INIT,
  16. IDLE,
  17. ERROR,
  18. Reset,
  19. };
  20. public enum MSG
  21. {
  22. Auto,
  23. AutoStop,
  24. RESET,
  25. ABORT,
  26. ERROR,
  27. };
  28. //private device
  29. //routine
  30. private InitRoutine initRoutine = null;
  31. private RD_TRIG plcSync = new RD_TRIG();
  32. //private int state = (int)STATE.INIT;
  33. private Queue<IRoutine> steps = null;
  34. //private IRoutine curStep = null;
  35. public bool IsAutoMode { get; private set; }
  36. public string Name { get; set; }
  37. public string Status { get { return State2String(fsm.State); } }
  38. //bool bAuto = false;
  39. public RouteManager()
  40. {
  41. Name = "系统";
  42. //state = (int)STATE.INIT;
  43. fsm = new StateMachine<RouteManager>(Name, (int)STATE.INIT, 50);
  44. //Init sequence
  45. EnterExitTransition<STATE, FSM_MSG>(STATE.INIT, fStartInit, FSM_MSG.NONE, null);
  46. Transition(STATE.INIT, FSM_MSG.TIMER, fInit, STATE.IDLE);
  47. Transition(STATE.INIT, MSG.ERROR, fError, STATE.ERROR);
  48. //Reset
  49. AnyStateTransition(MSG.RESET, fStartReset, STATE.Reset);
  50. Transition(STATE.Reset, FSM_MSG.TIMER, fReset, STATE.IDLE);
  51. AnyStateTransition(MSG.ERROR, fError, STATE.ERROR);
  52. AnyStateTransition((int)FSM_MSG.ALARM, fError, (int)STATE.ERROR);
  53. //DATA.Subscribe(ModuleName.System.ToString(), ParamName.RTState, () => state);
  54. //DATA.Subscribe(ModuleName.System.ToString(), ParamName.RTStatus, () => Status);
  55. steps = new Queue<IRoutine>();
  56. Running = true;
  57. }
  58. public bool Check(int msg, out string reason, object[] objs)
  59. {
  60. bool bRet = true;
  61. reason = "";
  62. switch (msg)
  63. {
  64. case (int)MSG.Auto:
  65. {
  66. if (fsm.State != (int)STATE.IDLE)
  67. {
  68. reason = String.Format("{0}正在处理{1},不能执行{2}", Name, State2String(fsm.State), Msg2String(msg));
  69. return false;
  70. }
  71. }
  72. break;
  73. default:
  74. break;
  75. }
  76. return bRet;
  77. }
  78. private string Msg2String(int msg)
  79. {
  80. string result = "";
  81. switch (msg)
  82. {
  83. case (int)MSG.RESET:
  84. result = "复位";
  85. break;
  86. case (int)MSG.ABORT:
  87. result = "终止";
  88. break;
  89. }
  90. return result;
  91. }
  92. private string State2String(int state)
  93. {
  94. string result = "";
  95. switch (state)
  96. {
  97. case (int)STATE.INIT:
  98. case (int)STATE.Reset:
  99. result = "初始化";
  100. break;
  101. case (int)STATE.IDLE:
  102. result = "就绪";
  103. break;
  104. case (int)STATE.ERROR:
  105. result = "错误";
  106. break;
  107. }
  108. return result;
  109. }
  110. protected override bool Init()
  111. {
  112. initRoutine = new InitRoutine("System", "初始化");
  113. initRoutine.Initalize();
  114. return true;
  115. }
  116. protected override void Term()
  117. {
  118. }
  119. #region Init
  120. private bool fStartInit(object[] objs)
  121. {
  122. Running = false;
  123. Result ret = initRoutine.Start(objs);
  124. if (ret == Result.DONE)
  125. {
  126. return false;
  127. }
  128. else if (ret == Result.FAIL)
  129. {
  130. return false; //do noting
  131. }
  132. return true;
  133. }
  134. private bool fInit(object[] objs)
  135. {
  136. Result ret = initRoutine.Monitor();
  137. if (ret == Result.DONE)
  138. {
  139. Running = true;
  140. //state = (int)STATE.IDLE;
  141. return true;
  142. }
  143. else if (ret == Result.FAIL)
  144. {
  145. //do nothing
  146. //state = (int)STATE.ERROR;
  147. return true;
  148. }
  149. return false; ;
  150. }
  151. private bool fError(object[] objs)
  152. {
  153. // refillRoutine.Abort();
  154. IsAutoMode = false;
  155. return true;
  156. }
  157. #endregion
  158. //#region Auto
  159. //private bool RunRecipe(string recipeName)
  160. //{
  161. // var rManager = RecipeFileManager.Instance;
  162. // var recipeContent = rManager.LoadRecipe(ModuleName.System.ToString(), recipeName, true);
  163. // if (string.IsNullOrEmpty(recipeContent))
  164. // {
  165. // EV.PostMessage(ModuleName.System.ToString(), EventEnum.PrepareProcessErr, ModuleName.System.ToString(), string.Format("工艺程序{0}读取错误", recipeName));
  166. // return false; // The recipe is invalide.
  167. // }
  168. // List<string> reasons;
  169. // if (!rManager.CheckRecipe(ModuleName.System.ToString(), recipeContent, out reasons))
  170. // {
  171. // EV.PostMessage(ModuleName.System.ToString(), EventEnum.PrepareProcessErr, ModuleName.System.ToString(), string.Format("工艺程序{0}内容存在错误", recipeName));
  172. // string info = "";
  173. // foreach (var item in reasons)
  174. // info += item;
  175. // EV.PostPopDialogMessage(EventLevel.Alarm, String.Format("工艺程序{0} 错误", recipeName), info);
  176. // return false; //Recipe content check.
  177. // }
  178. // string reason = String.Empty;
  179. // Singleton<PMEntity>.Instance.PostMsg(PMEntity.MSG.Process, recipeName, recipeContent);
  180. // return true;
  181. //}
  182. //#endregion
  183. #region reset
  184. private bool fStartReset(object[] objs)
  185. {
  186. Singleton<DeviceEntity>.Instance.PostMsg(DeviceEntity.MSG.RESET);
  187. Singleton<PMEntity>.Instance.PostMsg(PMEntity.MSG.Reset);
  188. return true;
  189. }
  190. private bool fReset(object[] objs)
  191. {
  192. int elapsed = fsm.ElapsedTime;
  193. if (elapsed > 500) //wait 2s
  194. {
  195. //notify
  196. reset();
  197. if (fsm.State == (int)STATE.ERROR)
  198. return true;
  199. return false;
  200. }
  201. return false;
  202. }
  203. private void reset()
  204. {
  205. IsAutoMode = false;
  206. string reason = string.Empty;
  207. }
  208. #endregion
  209. }
  210. }