SKVisionClientBase.cs 4.8 KB

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