EPDClient.cs 9.0 KB

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