EntityTaskBase.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Aitex.Core.RT.Routine;
  4. using MECF.Framework.Common.Equipment;
  5. using CyberX8_Core;
  6. namespace CyberX8_RT.Modules
  7. {
  8. public class EntityTaskBase
  9. {
  10. protected Queue<IRoutine> QueueRoutine
  11. {
  12. get { return _routine; }
  13. }
  14. private Queue<IRoutine> _routine = new Queue<IRoutine>();
  15. public RState Start(IRoutine routine)
  16. {
  17. QueueRoutine.Clear();
  18. QueueRoutine.Enqueue(routine);
  19. return QueueRoutine.Peek().Start();
  20. }
  21. public RState TaskStart()
  22. {
  23. if (_routine.Count == 0)
  24. return RState.End;
  25. RState ret = RState.End;
  26. var lst = _routine.ToList();
  27. for (int i = 0; i < lst.Count; i++)
  28. {
  29. ret = lst[i].Start();
  30. if (ret == RState.End)
  31. {
  32. _routine.Dequeue();
  33. continue;
  34. }
  35. else
  36. {
  37. break;
  38. }
  39. }
  40. return RState.Running;
  41. }
  42. public RState TaskMonitor()
  43. {
  44. if (_routine.Count == 0)
  45. return RState.End;
  46. IRoutine routine = _routine.Peek();
  47. RState ret = routine.Monitor();
  48. if (ret == RState.End)
  49. {
  50. _routine.Dequeue();
  51. var lst = _routine.ToList();
  52. for (int i = 0; i < lst.Count; i++)
  53. {
  54. ret = lst[i].Start();
  55. if (ret == RState.End)
  56. {
  57. _routine.Dequeue();
  58. continue;
  59. }
  60. else
  61. {
  62. break;
  63. }
  64. }
  65. }
  66. return ret;
  67. }
  68. public void Abort()
  69. {
  70. if (_routine != null)
  71. {
  72. _routine.Peek().Abort();
  73. _routine.Clear();
  74. }
  75. }
  76. }
  77. }
  78. namespace CyberX8_RT
  79. {
  80. abstract class ActionBase
  81. {
  82. private static ushort _id = 100;
  83. //---------------------------------Properties------------------------------------
  84. //
  85. public ushort ID { get; }
  86. public ModuleName Module { get; protected set; }
  87. public virtual ActionStatus Status { get; set; }
  88. public virtual bool IsReady { get; set; }
  89. public bool IsBackground { get; set; }
  90. //--------------------------------Constructor------------------------------------
  91. //
  92. protected ActionBase(ModuleName mod)
  93. {
  94. this.ID = _id++;
  95. this.Module = mod;
  96. this.Status = ActionStatus.Pending;
  97. }
  98. public virtual void Execute()
  99. {
  100. this.Status = ActionStatus.SendCmd;
  101. }
  102. protected virtual void OnPreWork() { }
  103. public virtual void OnPostWork(string data = null) { }
  104. public virtual void OnError(string data = null) { }
  105. }
  106. }