EPDClient.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System.Text;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using Venus_RT.Modules;
  5. using MECF.Framework.Common.Communications;
  6. using MECF.Framework.Common.Equipment;
  7. using Venus_Core;
  8. using Aitex.Core.RT.SCCore;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.Device;
  11. using Aitex.Core.RT.Log;
  12. using Aitex.Core.Util;
  13. using EPD.Data;
  14. using Venus_RT.Devices;
  15. namespace Venus_RT.Devices.EPD
  16. {
  17. class EPDClient : JetEPDBase
  18. {
  19. public enum EDPStatus
  20. {
  21. Idle,
  22. Running,
  23. Error,
  24. }
  25. private EPDSocketClient _socketClient;
  26. private string _ip;
  27. private int _port;
  28. private int _channel;
  29. private Stopwatch _heartBeatTimer = new Stopwatch();
  30. private bool _isHeartBeatReceived;
  31. private readonly R_TRIG _epdIdle = new R_TRIG();
  32. public bool Captured { get; private set; }
  33. public bool IsEPDConnected { get; private set; }
  34. public EDPStatus Status { get; private set; }
  35. public List<string> CFGFileList { get; private set; }
  36. public new string Module { get; set; }
  37. public new string Name { get; set; }
  38. public string EPDVersion { get; private set; }
  39. public string EPDState { get; private set; }
  40. public string SensorStatus { get; private set; }
  41. public string EPDRunStatus { get; private set; }
  42. public string EPDMode { get; private set; }
  43. public EPDClient(ModuleName mod)
  44. {
  45. Name = VenusDevice.EndPoint.ToString();
  46. Module = mod.ToString();
  47. var ipaddress= SC.GetStringValue($"{mod.ToString()}.EPD.IPAddress");
  48. var ipaddressValue = ipaddress.Split(':');
  49. if (ipaddressValue.Length == 2)
  50. {
  51. _ip = ipaddressValue[0];
  52. _port = System.Convert.ToInt32(ipaddressValue[1]);
  53. }
  54. _channel = 0;
  55. _socketClient = new EPDSocketClient();
  56. _socketClient.OnConnect += OnConnect;
  57. _socketClient.OnDisconnect += OnDisConnect;
  58. _socketClient.OnSocketSend += OnEPDSocketSend;
  59. _socketClient.OnSocketReceive += OnEPDSocketReceive;
  60. _socketClient.OnEPDReply += OnEPDReply;
  61. _socketClient.OnError += OnError;
  62. }
  63. public override bool Initialize()
  64. {
  65. _socketClient.Connect(_ip, _port);
  66. _socketClient.ConnectEPD();
  67. _socketClient.SetMode(2); // 1 local; 2 remote
  68. _socketClient.SetRunStatus(3); // 1 Monitor; 2 Save; 3:Capture; 4:Process
  69. _socketClient.QueryConfigList();
  70. return true;
  71. }
  72. public override void Monitor()
  73. {
  74. HeartBeat();
  75. }
  76. public override void Reset()
  77. { }
  78. public override void Terminate()
  79. {
  80. Status = EDPStatus.Idle;
  81. _socketClient.DisconnectEPD();
  82. _socketClient.Disconnect();
  83. }
  84. public override void RecipeStart(string recipe)
  85. {
  86. _socketClient.RecipeStart(_channel, "");
  87. Status = EDPStatus.Running;
  88. }
  89. public override void RecipeStop()
  90. {
  91. _socketClient.RecipeStop(_channel);
  92. Status = EDPStatus.Idle;
  93. }
  94. public override void StepStart(string cfgName, int Index)
  95. {
  96. _socketClient.Start((byte)_channel, cfgName);
  97. Status = EDPStatus.Running;
  98. }
  99. public override void StepStop()
  100. {
  101. _socketClient.Stop((byte)_channel);
  102. }
  103. private void HeartBeat()
  104. {
  105. _epdIdle.CLK = Status == EDPStatus.Idle;
  106. if (_epdIdle.Q)
  107. {
  108. _socketClient.SendHeartBeat();
  109. _heartBeatTimer.Restart();
  110. _isHeartBeatReceived = false;
  111. }
  112. else if(_epdIdle.M)
  113. {
  114. if(_heartBeatTimer.ElapsedMilliseconds > 30000)
  115. {
  116. if (!_isHeartBeatReceived && !SC.GetValue<bool>("System.IsSimulatorMode"))
  117. {
  118. LOG.Write(eEvent.ERR_ENDPOINT, Module, "HeartBeat Error, EndPoint Device did not response in 5 seconds");
  119. }
  120. _socketClient.SendHeartBeat();
  121. _heartBeatTimer.Restart();
  122. _isHeartBeatReceived = false;
  123. }
  124. }
  125. else
  126. {
  127. _heartBeatTimer.Stop();
  128. }
  129. }
  130. private void OnConnect()
  131. {
  132. LOG.Write(eEvent.INFO_ENDPOINT, Module, "Endpoint connected");
  133. }
  134. private void OnDisConnect()
  135. {
  136. LOG.Write(eEvent.INFO_ENDPOINT, Module, "Endpoint disconnected");
  137. }
  138. private void OnEPDSocketReceive(string type, byte[] data, int length)
  139. {
  140. var content = StringJoin(" ", data, length);
  141. LOG.Write(eEvent.INFO_ENDPOINT, Module, $"Endpoint Receive: {content}");
  142. }
  143. private void OnEPDSocketSend(string type, byte[] data)
  144. {
  145. var content = StringJoin(" ", data, data.Length);
  146. if(type != "HeartBeat")
  147. {
  148. LOG.Write(eEvent.INFO_ENDPOINT, Module, $"Endpoint Send=> Type:{type}, Data:{content}");
  149. }
  150. }
  151. private void OnError(int command, int errorCode)
  152. {
  153. var strError = errorCode.ToString();
  154. if (EPDDefine.ErrorMap.ContainsKey(errorCode))
  155. strError = EPDDefine.ErrorMap[errorCode];
  156. string ErrorInfo = $"EndPoint Command {(EPDCommand)command} Failed: {strError}";
  157. LOG.Write(eEvent.ERR_ENDPOINT, Module, ErrorInfo);
  158. }
  159. private void OnEPDReply(EPDCommand cmd, object obj)
  160. {
  161. switch (cmd)
  162. {
  163. case EPDCommand.SetWaferInfo:
  164. break;
  165. case EPDCommand.QueryCfgList:
  166. if (obj is List<string>)
  167. CFGFileList = (List<string>)obj;
  168. break;
  169. case EPDCommand.QueryState:
  170. if (obj is ushort state && EPDDefine.StateMap.ContainsKey(state))
  171. EPDState = EPDDefine.StateMap[state];
  172. break;
  173. case EPDCommand.QueryVer:
  174. EPDVersion = obj.ToString();
  175. break;
  176. case EPDCommand.Connect:
  177. IsEPDConnected = true;
  178. break;
  179. case EPDCommand.HeartBeat:
  180. _isHeartBeatReceived = true;
  181. break;
  182. case EPDCommand.QueryRunStatus:
  183. if (obj is ushort sta && EPDDefine.RunStatusMap.ContainsKey(sta))
  184. {
  185. EPDRunStatus = EPDDefine.RunStatusMap[sta];
  186. }
  187. break;
  188. case EPDCommand.QueryOperateMode:
  189. if (obj is ushort mode && EPDDefine.ModeMap.ContainsKey(mode))
  190. {
  191. EPDMode = EPDDefine.ModeMap[mode];
  192. }
  193. break;
  194. case EPDCommand.GetSensorStatus:
  195. if (obj is List<string> statusLst && statusLst.Count >= 2)
  196. {
  197. SensorStatus = statusLst[0];
  198. }
  199. break;
  200. case EPDCommand.GetRecipesList:
  201. if (obj is List<string> objs)
  202. {
  203. CFGFileList = objs.GetRange(3, objs.Count - 3);
  204. SensorStatus = objs[0];
  205. }
  206. break;
  207. case EPDCommand.Event:
  208. var lst = obj as List<object>;
  209. if (lst[0] is int type && type == 7)
  210. Captured = true;
  211. break;
  212. case EPDCommand.AsciiEvent:
  213. if (obj.ToString() == "ENDPOINT")
  214. Captured = true;
  215. break;
  216. case EPDCommand.QueryChannelCount:
  217. case EPDCommand.QueryChannalNames:
  218. case EPDCommand.RecipeStart:
  219. case EPDCommand.RecipeStop:
  220. case EPDCommand.Start:
  221. case EPDCommand.Stop:
  222. default:
  223. break;
  224. }
  225. }
  226. private string StringJoin(string separator, byte[] values, int length)
  227. {
  228. if (values == null || values.Length == 0)
  229. return string.Empty;
  230. if (separator == null)
  231. separator = string.Empty;
  232. var sb = new StringBuilder($"{values[0]:X2}");
  233. for (int i = 1; i < length; i++)
  234. sb.Append($"{separator}{values[i]:X2}");
  235. return sb.ToString();
  236. }
  237. }
  238. }