EPDClient.cs 8.7 KB

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