HstConnectionBase.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System.Linq;
  2. using System.Net;
  3. using System.Text;
  4. using System.Threading;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.RT.SCCore;
  8. using Aitex.Core.Util;
  9. using MECF.Framework.Common.Communications;
  10. using MECF.Framework.Common.Communications.Tcp.Socket.Client.APM;
  11. using MECF.Framework.Common.Communications.Tcp.Socket.Client.APM.EventArgs;
  12. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot;
  13. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.OcrReaders.HST
  14. {
  15. public abstract class HstConnectionBase : IConnection
  16. {
  17. public string Address
  18. {
  19. get { return _address; }
  20. }
  21. public bool IsConnected
  22. {
  23. get { return _socket == null ? false : _socket.IsConnected; }
  24. }
  25. public bool Connect()
  26. {
  27. if (_socket == null)
  28. return true;
  29. if (_socket.IsConnecting)
  30. return true;
  31. Disconnect();
  32. if (!string.IsNullOrEmpty(_address))
  33. {
  34. _socket = new AsyncSocket(_address, "\r\n");
  35. _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataReceived);
  36. _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHappened);
  37. }
  38. _socket.Connect(_address);
  39. return true;
  40. }
  41. public bool Disconnect()
  42. {
  43. if (_socket != null)
  44. {
  45. _socket.Dispose();
  46. _socket = null;
  47. }
  48. return true;
  49. }
  50. public bool IsBusy
  51. {
  52. get { return _activeHandler != null; }
  53. }
  54. public bool IsCommunicationError { get; private set; }
  55. public string LastCommunicationError { get; private set; }
  56. protected AsyncSocket _socket;
  57. protected HandlerBase _activeHandler; //set, control,
  58. protected object _lockerActiveHandler = new object();
  59. private string _address;//ip:port
  60. private RD_TRIG _trigCommunicationError = new RD_TRIG();
  61. public bool EnableRetry { get; set; }
  62. public R_TRIG _trigRetryConnect = new R_TRIG();
  63. public int MaxRetryCount { get; set; }
  64. public HstConnectionBase(string addr)
  65. {
  66. _address = addr;
  67. if (!string.IsNullOrEmpty(addr))
  68. {
  69. _socket = new AsyncSocket(addr, "\r\n");
  70. _socket.OnDataChanged += new AsyncSocket.MessageHandler(OnDataReceived);
  71. _socket.OnErrorHappened += new AsyncSocket.ErrorHandler(OnErrorHappened);
  72. _socket.EnableLog = true;
  73. }
  74. }
  75. private void OnErrorHappened(ErrorEventArgs args)
  76. {
  77. }
  78. public void Execute(HandlerBase handler)
  79. {
  80. if (_activeHandler != null)
  81. return;
  82. if (handler == null)
  83. return;
  84. if (IsConnected)
  85. {
  86. lock (_lockerActiveHandler)
  87. {
  88. _activeHandler = handler;
  89. _activeHandler.SetState(EnumHandlerState.Sent);
  90. }
  91. if (!_socket.Write(Encoding.ASCII.GetString(handler.SendBinary)+"\r\n"))
  92. {
  93. lock (_lockerActiveHandler)
  94. {
  95. _activeHandler = null;
  96. }
  97. IsCommunicationError = true;
  98. }
  99. }
  100. }
  101. protected abstract MessageBase ParseResponse(string rawMessage);
  102. protected virtual void OnEventArrived(MessageBase msg)
  103. {
  104. }
  105. protected abstract void OnDataReceived(string oneLineMessage);
  106. public HandlerBase MonitorTimeout()
  107. {
  108. HandlerBase result = null;
  109. lock (_lockerActiveHandler)
  110. {
  111. if (_activeHandler != null && _activeHandler.CheckTimeout())
  112. {
  113. result = _activeHandler;
  114. _activeHandler = null;
  115. SetCommunicationError(true, "receive response timeout");
  116. }
  117. }
  118. return result;
  119. }
  120. /// <summary>
  121. ///
  122. /// </summary>
  123. /// <param name="retryConnectMaxCount">-1 keep retry, 0 no retry</param>
  124. public void MonitorConnection(int retryMaxCount, out bool retried)
  125. {
  126. retried = false;
  127. _trigRetryConnect.CLK = IsCommunicationError || !IsConnected;
  128. if (_trigRetryConnect.Q)
  129. {
  130. int retry = 0;
  131. while (retry < retryMaxCount || retryMaxCount < 0)
  132. {
  133. LOG.Write($"Retry connect with {_address} for the {retry + 1} time.");
  134. Connect();
  135. Thread.Sleep(retry * 1000);
  136. retried = true;
  137. lock (_lockerActiveHandler)
  138. {
  139. _activeHandler = null;
  140. }
  141. if (IsConnected)
  142. break;
  143. retry++;
  144. //break;
  145. }
  146. }
  147. SetCommunicationError(false, "retried");
  148. }
  149. /// <summary>
  150. /// receive invalid packet
  151. /// send out timeout
  152. /// receive response timeout
  153. /// </summary>
  154. /// <param name="isError"></param>
  155. /// <param name="reason"></param>
  156. public void SetCommunicationError(bool isError, string reason)
  157. {
  158. IsCommunicationError = isError;
  159. LastCommunicationError = reason;
  160. _trigCommunicationError.CLK = isError;
  161. if (_trigCommunicationError.R)
  162. {
  163. LOG.Write($"{Address} communication error, {reason}");
  164. }
  165. if (_trigCommunicationError.T)
  166. {
  167. LOG.Write($"{Address} communication error recovered, {reason}");
  168. }
  169. }
  170. public void Reset()
  171. {
  172. _trigRetryConnect.RST = true;
  173. }
  174. public void SetEnableLog(bool value)
  175. {
  176. _socket.EnableLog = value;
  177. }
  178. }
  179. }