EPDClient.cs 8.9 KB

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