EPDClient.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. using System.Text.RegularExpressions;
  16. namespace Venus_RT.Devices.EPD
  17. {
  18. public class EPDClient : JetEPDBase
  19. {
  20. private EPDSocketClient _socketClient;
  21. private string _ip;
  22. private int _port;
  23. private int _channel;
  24. private Stopwatch _heartBeatTimer = new Stopwatch();
  25. private bool _isHeartBeatReceived=true;
  26. private readonly R_TRIG _epdIdle = new R_TRIG();
  27. private bool _captured;
  28. public override bool Captured { get { return _captured; }}
  29. private bool _connected = false;
  30. public override bool IsEPDConnected { get { return _connected; } }
  31. private EDPStatus _status;
  32. public override EDPStatus Status { get { return _status; } }
  33. private List<string> _cfgFileList;
  34. public override List<string> CFGFileList { get { return _cfgFileList; } }
  35. public string EPDVersion { get; private set; }
  36. public string EPDState { get; private set; }
  37. public string SensorStatus { get; private set; }
  38. public string EPDRunStatus { get; private set; }
  39. public string EPDMode { get; private set; }
  40. public EPDClient(ModuleName mod)
  41. {
  42. Name = VenusDevice.EndPoint.ToString();
  43. Module = mod.ToString();
  44. var ipaddress= SC.GetStringValue($"{mod.ToString()}.EPD.IPAddress");
  45. var ipaddressValue = ipaddress.Split(':');
  46. if (ipaddressValue.Length == 2)
  47. {
  48. _ip = ipaddressValue[0];
  49. _port = System.Convert.ToInt32(ipaddressValue[1]);
  50. }
  51. _channel = SC.GetValue<int>($"{Module}.EPD.ChannelNumber");
  52. _socketClient = new EPDSocketClient();
  53. _socketClient.OnConnect += OnConnect;
  54. _socketClient.OnDisconnect += OnDisConnect;
  55. _socketClient.OnSocketSend += OnEPDSocketSend;
  56. _socketClient.OnSocketReceive += OnEPDSocketReceive;
  57. _socketClient.OnEPDReply += OnEPDReply;
  58. _socketClient.OnError += OnError;
  59. }
  60. public override bool Initialize()
  61. {
  62. _socketClient.Connect(_ip, _port);
  63. _socketClient.ConnectEPD();
  64. _socketClient.SetMode(2); // 1 local; 2 remote
  65. _socketClient.SetRunStatus(4); // 1 Monitor; 2 Save; 3:Capture; 4:Process
  66. _socketClient.QueryConfigList();
  67. return true;
  68. }
  69. public override void Monitor()
  70. {
  71. HeartBeat();
  72. }
  73. public override void Reset()
  74. { }
  75. public override void Terminate()
  76. {
  77. _status = EDPStatus.Idle;
  78. _socketClient.DisconnectEPD();
  79. _socketClient.Disconnect();
  80. }
  81. public override void RecipeStart(string recipe)
  82. {
  83. _captured = false;
  84. _socketClient.RecipeStart(_channel, recipe);
  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. //_socketClient.SendHeartBeat();
  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. _connected = 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. //LOG.Write(0,"test11111111111111111111111XX"+lst[0].ToString());
  209. if (lst[0] is int type && type == 7)
  210. _captured = true;
  211. break;
  212. case EPDCommand.AsciiEvent:
  213. if (obj.ToString() == "ENDPOINT")
  214. _captured = true;
  215. break;
  216. case EPDCommand.QueryChannelCount:
  217. case EPDCommand.QueryChannalNames:
  218. case EPDCommand.RecipeStart:
  219. case EPDCommand.RecipeStop:
  220. case EPDCommand.Start:
  221. case EPDCommand.Stop:
  222. default:
  223. break;
  224. }
  225. }
  226. private string StringJoin(string separator, byte[] values, int length)
  227. {
  228. if (values == null || values.Length == 0)
  229. return string.Empty;
  230. if (separator == null)
  231. separator = string.Empty;
  232. var sb = new StringBuilder($"{values[0]:X2}");
  233. for (int i = 1; i < length; i++)
  234. sb.Append($"{separator}{values[i]:X2}");
  235. return sb.ToString();
  236. }
  237. public override void QueryConfigList()
  238. {
  239. _socketClient.QueryConfigList();
  240. }
  241. }
  242. }