SKVisionClientBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using SKMC.Api.Common.Logger;
  2. using SKMC.Api.Common.Tcp;
  3. using SKMC.Api.Common.Vision;
  4. using System;
  5. namespace SKMC.Api.Vision.SKV0
  6. {
  7. /// <summary>
  8. /// 视觉客户端通用基类
  9. /// </summary>
  10. public class SKVisionClientBase
  11. {
  12. protected static readonly ILogger log = LogFactory.Get();
  13. public string Address { get; set; } = "127.0.0.1";
  14. public int Port { get; set; }
  15. public int TIMEOUT { get; set; } = 5000;
  16. // 测试模式(空跑模式)下, 不等待接收响应
  17. public bool TestMode { get; set; } = false;
  18. // 是否开启
  19. public bool Enabled { get; set; } = true;
  20. // 是否运行中, 结束运行可放弃等待服务端响应数据
  21. public bool Running { get; set; }
  22. private TCPClient client;
  23. public SKVisionClientBase(int port, int timeout)
  24. {
  25. //Enabled = Process_Vision_Enabled == 1;
  26. //Address = Vision_Address;
  27. TIMEOUT = timeout;
  28. Port = port;
  29. client = new TCPClient(Address, Port) { KeepAlived = true };
  30. }
  31. /// <summary>
  32. /// 保持连接, 如果连接已中断则重新连接
  33. /// </summary>
  34. /// <param name="mills">验证连接的超时时间(ms)</param>
  35. /// <returns></returns>
  36. public TCPClient Connect(int mills)
  37. {
  38. if (!Enabled) return null;
  39. if (!client.IsConnected(mills))
  40. {
  41. client.Connect();
  42. }
  43. return client;
  44. }
  45. /// <summary>
  46. /// 连接新的连接
  47. /// </summary>
  48. /// <returns></returns>
  49. public TCPClient Connect()
  50. {
  51. if (!Enabled) return null;
  52. if (client != null) client.Connect();
  53. return client;
  54. }
  55. public void Reset(int index)
  56. {
  57. if (client != null) client.Clear();
  58. Running = false;
  59. }
  60. /// <summary>
  61. /// 请求并获取结果
  62. /// 请求后阻塞获取确认消息
  63. /// </summary>
  64. /// <param name="requestMsg">请求消息</param>
  65. /// <param name="responseAction">响应结果解析</param>
  66. /// <returns></returns>
  67. public void Request(string requestMsg, Action<string> responseAction = null)
  68. {
  69. try
  70. {
  71. client = Connect(100);
  72. Reset(0);
  73. }
  74. catch (Exception e)
  75. {
  76. throw new VisionException { Type = VisionException.Conn_Fail, Source = e.Message };
  77. }
  78. try
  79. {
  80. client.Send(requestMsg);
  81. }
  82. catch (Exception e)
  83. {
  84. log.Error("send failed", e);
  85. client = Connect();
  86. client.Send(requestMsg);
  87. }
  88. string response = null;
  89. try
  90. {
  91. response = client.Recevie();
  92. }
  93. catch (Exception e)
  94. {
  95. log.Error(e.Message);
  96. throw new VisionException { Type = VisionException.Receive_Timeout, Source = e.Message };
  97. }
  98. try
  99. {
  100. responseAction?.Invoke(response);
  101. }
  102. catch (Exception e)
  103. {
  104. log.Error(e.Message);
  105. throw new VisionException { Type = VisionException.Parse_Error, Source = e.Message };
  106. }
  107. }
  108. /// <summary>
  109. /// 请求不获取结果
  110. /// 请求后不阻塞获取确认消息
  111. /// </summary>
  112. /// <param name="requestMsg">请求消息</param>
  113. /// <returns></returns>
  114. public void Request(string requestMsg, bool isOnlySend = true)
  115. {
  116. try
  117. {
  118. client = Connect(100);
  119. Reset(0);
  120. }
  121. catch (Exception e)
  122. {
  123. log.Error(e.Message);
  124. throw new VisionException { Type = VisionException.Conn_Fail, Source = e.Message };
  125. }
  126. try
  127. {
  128. client.Send(requestMsg);
  129. }
  130. catch (Exception e)
  131. {
  132. log.Error("send failed", e);
  133. client = Connect();
  134. client.Send(requestMsg);
  135. }
  136. }
  137. public string GetResult()
  138. {
  139. try
  140. {
  141. return client.Recevie();
  142. }
  143. catch (Exception e)
  144. {
  145. log.Error(e.Message);
  146. throw new VisionException { Type = VisionException.Receive_Timeout, Source = e.Message };
  147. }
  148. }
  149. }
  150. }