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