ActiveFsm.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using Aitex.Core.RT.Fsm;
  2. using Aitex.Core.RT.Log;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. namespace MECF.Framework.Common.Fsm
  8. {
  9. public class ActiveFsm
  10. {
  11. public int FsmState
  12. {
  13. get { return _fsm.State; }
  14. }
  15. public string StringFsmStatus
  16. {
  17. get
  18. {
  19. return _fsmStateMap.ContainsKey(FsmState) ? _fsmStateMap[FsmState] : FsmState.ToString();
  20. }
  21. }
  22. Dictionary<int, string> _fsmStateMap = new Dictionary<int, string>();
  23. Dictionary<int, string> _fsmMessageMap = new Dictionary<int, string>();
  24. private Thread _thread = null;
  25. private IStateMachine _fsm = null;
  26. public ActiveFsm()
  27. {
  28. _fsm = new StateMachine();
  29. }
  30. public ActiveFsm(string name) : base()
  31. {
  32. _fsm = new StateMachine();
  33. }
  34. public void MapState(int state, string stringState)
  35. {
  36. _fsmStateMap[state] = stringState;
  37. }
  38. public void MapMessage(int msg, string stringMessage)
  39. {
  40. _fsmMessageMap[msg] = stringMessage;
  41. }
  42. public string GetStringMessage(int msg)
  43. {
  44. return _fsmMessageMap.ContainsKey(msg) ? _fsmMessageMap[msg] : msg.ToString();
  45. }
  46. protected void StartFsm(string name, int fsmInterval, int initState)
  47. {
  48. _fsm.Init(initState, fsmInterval);
  49. _fsm.Name = name;
  50. _fsm.Start();
  51. _thread = new Thread(new ThreadStart(_fsm.Loop));
  52. _thread.Name = _fsm.Name;
  53. _thread.Start();
  54. while (!_thread.IsAlive)
  55. Thread.Sleep(1);
  56. }
  57. protected void StopFsm()
  58. {
  59. if (_fsm == null)
  60. return;
  61. {
  62. _fsm.Stop();
  63. }
  64. if (_thread != null)
  65. {
  66. if (_thread.IsAlive)
  67. {
  68. Thread.Sleep(100);
  69. if (_thread.IsAlive)
  70. {
  71. try
  72. {
  73. _thread.Abort();
  74. }
  75. catch (Exception ex)
  76. {
  77. LOG.Error($" {_fsm.Name} FSM terminated, {ex.Message}");
  78. }
  79. }
  80. }
  81. }
  82. }
  83. protected void Transition<T, V>(T state, V msg, FsmFunc func, T next)
  84. {
  85. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  86. int _state = Convert.ToInt32(state);
  87. int _next = Convert.ToInt32(next);
  88. int _msg = Convert.ToInt32(msg);
  89. Transition(_state, _msg, func, _next);
  90. }
  91. protected void Transition(int state, int msg, FsmFunc func, int next)
  92. {
  93. if (_fsm != null)
  94. _fsm.Transition(state, msg, func, next);
  95. }
  96. protected void AnyStateTransition(int msg, FsmFunc func, int next)
  97. {
  98. if (_fsm != null)
  99. _fsm.AnyStateTransition(msg, func, next);
  100. }
  101. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next)
  102. {
  103. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  104. int _next = Convert.ToInt32(next);
  105. int _msg = Convert.ToInt32(msg);
  106. AnyStateTransition(_msg, func, _next);
  107. }
  108. protected void EnterExitTransition<T, V>(T state, FsmFunc enter, Nullable<V> msg, FsmFunc exit) where V : struct
  109. {
  110. Debug.Assert(typeof(T).IsEnum && ((msg == null) || typeof(V).IsEnum));
  111. int _state = Convert.ToInt32(state);
  112. int _msg = msg == null ? (int)FSM_MSG.NONE : Convert.ToInt32(msg);
  113. EnterExitTransition(_state, enter, _msg, exit);
  114. }
  115. protected void EnterExitTransition(int state, FsmFunc enter, int msg, FsmFunc exit)
  116. {
  117. if (_fsm != null)
  118. _fsm.EnterExitTransition(state, enter, msg, exit);
  119. }
  120. public void PostMsg<T>(T msg, params object[] args) where T : struct
  121. {
  122. Debug.Assert(typeof(T).IsEnum);
  123. int id = Convert.ToInt32(msg);
  124. PostMsg(id, args);
  125. }
  126. public void PostMsg(int msg, params object[] args)
  127. {
  128. if (_fsm == null)
  129. {
  130. LOG.Error($"fsm is null, post msg {msg}");
  131. return;
  132. }
  133. _fsm.PostMsgWithoutLock(msg, args);
  134. }
  135. public bool CheckToPostMessage(int msg, out string reason, params object[] args)
  136. {
  137. if (!_fsm.FindTransition(_fsm.State, msg))
  138. {
  139. reason = $"{_fsm.Name} is in {StringFsmStatus} state,can not do {GetStringMessage(msg)}";
  140. return false;
  141. }
  142. _fsm.PostMsgWithoutLock(msg, args);
  143. reason = string.Empty;
  144. return true;
  145. }
  146. }
  147. }