EfemDevice.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using Aitex.Core.Common;
  2. using Aitex.Core.Common.DeviceData;
  3. using Aitex.Core.RT.DataCenter;
  4. using Aitex.Core.RT.Device;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.OperationCenter;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.Util;
  10. using Aitex.Sorter.Common;
  11. using MECF.Framework.Common.Equipment;
  12. using MECF.Framework.Common.SubstrateTrackings;
  13. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Threading;
  19. using System.Xml;
  20. using CyberX8_Core;
  21. using CyberX8_RT.Devices;
  22. using CyberX8_RT.Devices.EFEM;
  23. using CyberX8_RT.Devices.YASKAWA;
  24. using CyberX8_RT.Modules;
  25. namespace CyberX8_RT.Devices
  26. {
  27. sealed class MessageHandler : IMessageHandler
  28. {
  29. // Fields
  30. //
  31. private readonly IList<EfemMessage> _msgQueue = new List<EfemMessage>();
  32. private readonly EfemBase _efem;
  33. // Properties
  34. //
  35. public bool IsCompleted
  36. {
  37. get
  38. {
  39. if (_msgQueue.Count <= 0) return false;
  40. return EfemMessage.MsgHead.INF == _msgQueue[_msgQueue.Count - 1].Head;
  41. }
  42. }
  43. public event EventHandler<EfemActionArgs> CommandUpdated;
  44. public event EventHandler<EfemEventArgs> EventUpdated;
  45. public event EventHandler<EfemErrorArgs> ErrorOccurred;
  46. // Constructor
  47. //
  48. public MessageHandler(EfemBase device)
  49. {
  50. _efem = device;
  51. }
  52. public void Send(IEfemMessage msg)
  53. {
  54. // _efem.Comm.SendTo(msg);
  55. _msgQueue.Add(msg as EfemMessage);
  56. }
  57. public void ReceiveMessage(string sRec)
  58. {
  59. string[] msgs = sRec.Split('\r');
  60. foreach (var msg in msgs)
  61. {
  62. if (string.IsNullOrWhiteSpace(msg)) continue;
  63. EfemMessage rec_msg = msg.ToMessage();
  64. switch (rec_msg.Head)
  65. {
  66. case EfemMessage.MsgHead.ACK:
  67. _msgQueue.Add(rec_msg);
  68. OnCommandUpdated(new EfemActionArgs
  69. {
  70. Module = rec_msg.Module,
  71. CommandType = rec_msg.Operation,
  72. Status = ActionStatus.RecACK
  73. });
  74. break;
  75. case EfemMessage.MsgHead.INF:
  76. OnCommandUpdated(new EfemActionArgs
  77. {
  78. Module = rec_msg.Module,
  79. CommandType = rec_msg.Operation,
  80. Status = ActionStatus.RecINF
  81. });
  82. // 收到INF之后发送ACK确认
  83. string strACK = rec_msg.RawString.Replace("INF", "ACK");
  84. // _efem.Comm.SendTo(strACK);
  85. EfemMessage ack_msg = strACK.ToMessage();
  86. ack_msg.Direct = MsgDirection.To;
  87. _msgQueue.Add(ack_msg);
  88. OnCommandUpdated(new EfemActionArgs
  89. {
  90. Module = rec_msg.Module,
  91. CommandType = rec_msg.Operation,
  92. Status = ActionStatus.Completed,
  93. Data = rec_msg.Data.Count > 0 ? rec_msg.Data.First() : string.Empty
  94. });
  95. break;
  96. case EfemMessage.MsgHead.EVT:
  97. OnEventUpdated(new EfemEventArgs
  98. {
  99. EvtStr = rec_msg.ToParamString(),
  100. Module = rec_msg.Module,
  101. CommandType = rec_msg.Operation,
  102. DataList = rec_msg.Data
  103. });
  104. break;
  105. case EfemMessage.MsgHead.NAK:
  106. OnErrorOccurred(new EfemErrorArgs
  107. {
  108. Factor = rec_msg.Factor,
  109. Description = Constant.FactorString[rec_msg.Factor],
  110. CommandType = rec_msg.Operation,
  111. Module = rec_msg.Module,
  112. });
  113. break;
  114. case EfemMessage.MsgHead.CAN:
  115. OnErrorOccurred(new EfemErrorArgs
  116. {
  117. Factor = rec_msg.Factor,
  118. Description = Constant.FactorString.ContainsKey(rec_msg.Factor) ? Constant.FactorString[rec_msg.Factor] : rec_msg.Factor,
  119. Message = rec_msg.Data[0],
  120. CommandType = rec_msg.Operation,
  121. Module = rec_msg.Module,
  122. });
  123. break;
  124. case EfemMessage.MsgHead.ABS:
  125. OnErrorOccurred(new EfemErrorArgs
  126. {
  127. Factor = rec_msg.Factor,
  128. Description = $"{rec_msg.Data[0]}, {rec_msg.Data[1]}",
  129. CommandType = rec_msg.Operation,
  130. Module = rec_msg.Module,
  131. });
  132. break;
  133. }
  134. }
  135. }
  136. private void OnCommandUpdated(EfemActionArgs args)
  137. {
  138. CommandUpdated?.Invoke(this, args);
  139. }
  140. private void OnEventUpdated(EfemEventArgs args)
  141. {
  142. EventUpdated?.Invoke(this, args);
  143. }
  144. private void OnErrorOccurred(EfemErrorArgs args)
  145. {
  146. ErrorOccurred?.Invoke(this, args);
  147. }
  148. }
  149. sealed class SignalTower
  150. {
  151. // Fields
  152. //
  153. private readonly Dictionary<LightType, LightStatus> _lights = new Dictionary<LightType, LightStatus>(5);
  154. public SignalTower()
  155. {
  156. _lights.Add(LightType.RED, LightStatus.OFF);
  157. _lights.Add(LightType.YELLOW, LightStatus.OFF);
  158. _lights.Add(LightType.BLUE, LightStatus.OFF);
  159. _lights.Add(LightType.GREEN, LightStatus.OFF);
  160. _lights.Add(LightType.WHITE, LightStatus.OFF);
  161. _lights.Add(LightType.BUZZER1, LightStatus.OFF);
  162. DATA.Subscribe($"{ModuleName.EFEM}.SignalTower", () =>
  163. {
  164. return new AITSignalTowerData
  165. {
  166. IsGreenLightOn = _lights[LightType.GREEN] == LightStatus.ON,
  167. IsRedLightOn = _lights[LightType.RED] == LightStatus.ON,
  168. IsYellowLightOn = _lights[LightType.YELLOW] == LightStatus.ON,
  169. IsWhiteLightOn = _lights[LightType.WHITE] == LightStatus.ON,
  170. IsBlueLightOn = _lights[LightType.BLUE] == LightStatus.ON,
  171. IsBuzzerOn = _lights[LightType.BUZZER1] == LightStatus.ON,
  172. };
  173. },SubscriptionAttribute.FLAG.IgnoreSaveDB);
  174. }
  175. // Methods
  176. //
  177. public void ChangeLightStatus(LightType light, LightStatus st)
  178. {
  179. if (!_lights.ContainsKey(light))
  180. LOG.Write(eEvent.WARN_EFEM_COMMON_WARN, ModuleName.EFEM, $"NO {light} configured");
  181. _lights[light] = st;
  182. }
  183. }
  184. }