EfemBase.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. using Aitex.Core.RT.Device;
  9. using Aitex.Core.RT.Log;
  10. using Aitex.Sorter.Common;
  11. using MECF.Framework.Common.Equipment;
  12. using VirgoRT.Device;
  13. using VirgoRT.Devices.YASKAWA;
  14. namespace VirgoRT.Devices.EFEM
  15. {
  16. class EfemBase : BaseDevice, IDevice
  17. {
  18. // Fields
  19. //
  20. protected volatile LinkedList<ActionBase> _actions = new LinkedList<ActionBase>();
  21. protected EfemCommunicationBase _comm;
  22. protected IMessageHandler _msgHandler;
  23. public virtual ILoadport this[ModuleName mod]
  24. {
  25. get { throw new ApplicationException(); }
  26. }
  27. // Properties
  28. //
  29. public new ModuleName Module { get; set; }
  30. public OnlineFlag OnlineFlag { get; set; }
  31. public bool CommunicationConnected { get; protected set; }
  32. public DeviceState Status { get; set; }
  33. public bool HasActions
  34. {
  35. get
  36. {
  37. lock (_lockerAction)
  38. {
  39. return _actions.Any(x => x.Status == ActionStatus.Pending);
  40. }
  41. }
  42. }
  43. public EfemCommunicationBase Comm => _comm;
  44. public IMessageHandler MsgHandler => _msgHandler;
  45. protected object _lockerAction = new object();
  46. public string GripStateBlade1
  47. {
  48. get;
  49. set;
  50. }
  51. public string GripStateBlade2
  52. {
  53. get;
  54. set;
  55. }
  56. protected EfemBase(XmlElement xmlNode = null)
  57. {
  58. }
  59. public bool Initialize()
  60. {
  61. return true;
  62. }
  63. public void Monitor()
  64. {
  65. throw new NotImplementedException();
  66. }
  67. public void Terminate()
  68. {
  69. throw new NotImplementedException();
  70. }
  71. public void Reset()
  72. {
  73. throw new NotImplementedException();
  74. }
  75. // Methods
  76. //
  77. public void ExecuteAction()
  78. {
  79. lock (_lockerAction)
  80. {
  81. if (!_actions.Any())
  82. {
  83. LOG.Write("No Action in the EFEM Queue");
  84. return;
  85. }
  86. if (_actions.All(x => x.Status != ActionStatus.Pending))
  87. {
  88. LOG.Write("NO pending Action in EFEM Queue");
  89. //System.Diagnostics.Trace.Assert(false);
  90. return;
  91. }
  92. var nextAction = _actions.First(a => a.Status == ActionStatus.Pending);
  93. if (nextAction != null)
  94. {
  95. LOG.Write($"found pending action : efem action [{nextAction.GetType().Name}] [{nextAction.ID}] 开始执行");
  96. nextAction.Execute();
  97. }
  98. }
  99. }
  100. public void ClearActions()
  101. {
  102. lock (_lockerAction)
  103. {
  104. _actions?.Clear();
  105. }
  106. }
  107. public void AddAction(ActionBase cmd)
  108. {
  109. lock (_lockerAction)
  110. {
  111. _actions.AddLast(cmd);
  112. LOG.Write($"efem action [{cmd.GetType().Name}] [{cmd.ID}] add to queue");
  113. if (cmd.IsBackground)
  114. {
  115. LOG.Write($"background, efem action [{cmd.GetType().Name}] [{cmd.ID}] 开始执行");
  116. cmd.Execute();
  117. }
  118. if (cmd is LedAction)
  119. {
  120. Thread.Sleep(50);
  121. }
  122. if (cmd is LiftAction)
  123. {
  124. Thread.Sleep(50);
  125. }
  126. }
  127. }
  128. public void UpdateStatus(ushort Id, ActionStatus st)
  129. {
  130. lock (_lockerAction)
  131. {
  132. var cur = _actions.First(x => x.ID == Id);
  133. if (null == cur)
  134. {
  135. LOG.Write($"NO {Id} action in the queue");
  136. return;
  137. }
  138. cur.Status = st;
  139. if (st == ActionStatus.Completed)
  140. {
  141. cur.OnPostWork();
  142. _actions.Remove(cur);
  143. LOG.Write($"efem action [{cur.GetType().Name}] [{cur.ID}] removed from queue");
  144. }
  145. }
  146. }
  147. public virtual void ReceiveMessage(string sRec) { throw new NotImplementedException(); }
  148. public void SetOnline(bool online)
  149. {
  150. this.OnlineFlag = online ? OnlineFlag.Online : OnlineFlag.Offline;
  151. }
  152. public virtual void SetOnline(ModuleName mod, bool online) { }
  153. public virtual void SetBusy(ModuleName mod, bool online) { }
  154. }
  155. }