DeviceEntityT.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.RT.Fsm;
  6. using Aitex.Core.Util;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.RT.Device;
  9. namespace Aitex.Core.RT.Device
  10. {
  11. public abstract class DeviceEntityT<T> : Entity, IEntity where T :class, IDeviceManager, new()
  12. {
  13. public enum STATE
  14. {
  15. INIT,
  16. RUNNING,
  17. WAIT_RESET,
  18. ERROR,
  19. };
  20. public enum MSG
  21. {
  22. INIT,
  23. ERROR,
  24. WAIT_RESET,
  25. RESET,
  26. };
  27. private T mgr = null;
  28. public bool IsWaitReset
  29. {
  30. get { return fsm.State == (int)STATE.WAIT_RESET; }
  31. }
  32. public DeviceEntityT()
  33. {
  34. Running = false;
  35. fsm = new StateMachine<DeviceEntityT<T>>("DeviceLayer", (int)STATE.INIT, 50);
  36. EnterExitTransition<STATE, MSG>(STATE.INIT, null, MSG.INIT, null);
  37. Transition(STATE.INIT, MSG.INIT, fInit, STATE.RUNNING);
  38. Transition(STATE.RUNNING, FSM_MSG.TIMER, fRun, STATE.RUNNING);
  39. Transition(STATE.RUNNING, MSG.RESET, fReset, STATE.RUNNING);
  40. mgr = Singleton<T>.Instance;
  41. }
  42. public bool Check(int msg, out string reason, params object[] args)
  43. {
  44. reason = "";
  45. return true;
  46. }
  47. private bool fInit(object[] objs)
  48. {
  49. return true;
  50. }
  51. private bool fRun(object[] objs)
  52. {
  53. try
  54. {
  55. mgr.Monitor();
  56. }
  57. catch (Exception ex)
  58. {
  59. Running = false;
  60. LOG.WriteExeption("Device Run exception", ex);
  61. //throw(ex);
  62. }
  63. Running = true;
  64. return true;
  65. }
  66. private bool fReset(object[] objs)
  67. {
  68. mgr.Reset();
  69. return true;
  70. }
  71. }
  72. }