EPDClient.cs 8.4 KB

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