| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using SKMC.Api.Common.Logger;
- using SKMC.Api.Common.Tcp;
- using SKMC.Api.Common.Vision;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SKMC.Api.Vision.SKV0
- {
- /// <summary>
- /// 视觉客户端通用基类
- /// </summary>
- public class SKVisionClientBase
- {
- protected static readonly ILogger log = LogFactory.Get();
- public string Address { get; set; } = "127.0.0.1";
- public int Port { get; set; }
- public int TIMEOUT { get; set; } = 5000;
- // 测试模式(空跑模式)下, 不等待接收响应
- public bool TestMode { get; set; } = false;
- // 是否开启
- public bool Enabled { get; set; } = true;
- // 是否运行中, 结束运行可放弃等待服务端响应数据
- public bool Running { get; set; }
- private TCPClient client;
- public SKVisionClientBase(int port, int timeout)
- {
- //Enabled = Process_Vision_Enabled == 1;
- //Address = Vision_Address;
- TIMEOUT = timeout;
- Port = port;
- client = new TCPClient(Address, Port) { KeepAlived = true };
- }
- /// <summary>
- /// 保持连接, 如果连接已中断则重新连接
- /// </summary>
- /// <param name="mills">验证连接的超时时间(ms)</param>
- /// <returns></returns>
- public TCPClient Connect(int mills)
- {
- if (!Enabled) return null;
- if (!client.IsConnected(mills))
- {
- client.Connect();
- }
- return client;
- }
- /// <summary>
- /// 连接新的连接
- /// </summary>
- /// <returns></returns>
- public TCPClient Connect()
- {
- if (!Enabled) return null;
- if (client != null) client.Connect();
- return client;
- }
- public void Reset(int index)
- {
- if (client != null) client.Clear();
- Running = false;
- }
- /// <summary>
- /// 请求并获取结果
- /// 请求后阻塞获取确认消息
- /// </summary>
- /// <param name="requestMsg">请求消息</param>
- /// <param name="responseAction">响应结果解析</param>
- /// <returns></returns>
- public void Request(string requestMsg, Action<string> responseAction = null)
- {
- try
- {
- client = Connect(100);
- Reset(0);
- }
- catch (Exception e)
- {
- throw new VisionException { Type = VisionException.Conn_Fail, Source = e.Message };
- }
- try
- {
- client.Send(requestMsg);
- }
- catch (Exception e)
- {
- log.Error("send failed", e);
- client = Connect();
- client.Send(requestMsg);
- }
- string response = null;
- try
- {
- response = client.Recevie();
- }
- catch (Exception e)
- {
- log.Error(e.Message);
- throw new VisionException { Type = VisionException.Receive_Timeout, Source = e.Message };
- }
- try
- {
- responseAction?.Invoke(response);
- }
- catch (Exception e)
- {
- log.Error(e.Message);
- throw new VisionException { Type = VisionException.Parse_Error, Source = e.Message };
- }
- }
- /// <summary>
- /// 请求不获取结果
- /// 请求后不阻塞获取确认消息
- /// </summary>
- /// <param name="requestMsg">请求消息</param>
- /// <returns></returns>
- public void Request(string requestMsg, bool isOnlySend = true)
- {
- try
- {
- client = Connect(100);
- Reset(0);
- }
- catch (Exception e)
- {
- log.Error(e.Message);
- throw new VisionException { Type = VisionException.Conn_Fail, Source = e.Message };
- }
- try
- {
- client.Send(requestMsg);
- }
- catch (Exception e)
- {
- log.Error("send failed", e);
- client = Connect();
- client.Send(requestMsg);
- }
- }
- public string GetResult()
- {
- try
- {
- return client.Recevie();
- }
- catch (Exception e)
- {
- log.Error(e.Message);
- throw new VisionException { Type = VisionException.Receive_Timeout, Source = e.Message };
- }
- }
- }
- }
|