SKVisionClientBase.cs 4.7 KB

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