SKVisionProtocol.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Collections.Generic;
  3. namespace SKMC.Api.Vision.SKV0
  4. {
  5. /// <summary>
  6. /// SKVision通讯协议(V0)
  7. /// </summary>
  8. public class SKVisionProtocol
  9. {
  10. /// <summary>
  11. /// 协议头
  12. /// </summary>
  13. public string Protocol { get; } = "SKV0";
  14. /// <summary>
  15. /// 请求方向, 请求:T、响应:R
  16. /// </summary>
  17. public string Direction { get; set; }
  18. /// <summary>
  19. /// 模块码
  20. /// </summary>
  21. public string ModuleCode { get; set; }
  22. /// <summary>
  23. /// 功能码
  24. /// </summary>
  25. public string FuncCode { get; set; }
  26. /// <summary>
  27. /// 产品码或二维码
  28. /// </summary>
  29. public string ProductCode { get; set; }
  30. /// <summary>
  31. /// 时间戳, 格式为:yyMMddHHmmssfff
  32. /// </summary>
  33. public string Timestamp { get; set; }
  34. /// <summary>
  35. /// 请求设置码
  36. /// </summary>
  37. public string OptionCode { get; set; } = "0000";
  38. /// <summary>
  39. /// 响应结果码
  40. /// </summary>
  41. public string ResultCode { get; set; }
  42. /// <summary>
  43. /// 报文体内容字段集
  44. /// </summary>
  45. public List<string> BodyDomains { get; set; }
  46. /// <summary>
  47. /// SKVision通讯协议子对象
  48. /// </summary>
  49. public SKVisionProtocol SKVisionSubProtocol { get; set; }
  50. /// <summary>
  51. /// 创建请求报文
  52. /// </summary>
  53. /// <returns></returns>
  54. public string ToRequest()
  55. {
  56. string head = $"{Protocol},{Direction},{ModuleCode},{FuncCode},{ProductCode},{Timestamp},{OptionCode}";
  57. if (BodyDomains == null)
  58. {
  59. return $"{head},#,,$";
  60. }
  61. return $"{head},#,{string.Join(",", BodyDomains.ToArray())},$";
  62. }
  63. /// <summary>
  64. /// 解析响应报文并验证
  65. /// </summary>
  66. /// <param name="response"></param>
  67. /// <param name=""></param>
  68. /// <returns></returns>
  69. public SKVisionProtocol FromResponse(string response, bool validate = true)
  70. {
  71. response = response.Trim();
  72. int bodyStart = response.IndexOf("#,");
  73. int bodyEnd = response.IndexOf(",$");
  74. SKVisionProtocol skvpReponse = null;
  75. if (bodyStart > 0 && bodyEnd > 0)
  76. {
  77. string head = response.Substring(0, bodyStart);
  78. string body = response.Substring(bodyStart + 2, bodyEnd - bodyStart - 2);
  79. skvpReponse = ParseContent(head, body, validate);
  80. }
  81. // 判断是否有粘包
  82. int subBodyEnd = response.IndexOf(",$", bodyEnd + 1);
  83. int subBodyStart = response.IndexOf("#,", bodyEnd + 1);
  84. if (subBodyStart > 0 && subBodyEnd > 0)
  85. {
  86. string subHead = response.Substring(bodyEnd + 1, subBodyStart - bodyEnd - 2);
  87. string subBody = response.Substring(subBodyStart + 2, subBodyEnd - subBodyStart - 2);
  88. skvpReponse.SKVisionSubProtocol = ParseContent(subHead, subBody, validate);
  89. }
  90. if (skvpReponse != null) return skvpReponse;
  91. throw new VisionException { Type = VisionException.Parse_Error, Detail = $"视觉系统返回数据格式异常" };
  92. }
  93. /// <summary>
  94. /// 内容解析
  95. /// </summary>
  96. /// <param name="head"></param>
  97. /// <param name="body"></param>
  98. /// <param name="validate"></param>
  99. /// <returns></returns>
  100. private SKVisionProtocol ParseContent(string head, string body, bool validate = true)
  101. {
  102. SKVisionProtocol skvpReponse = new SKVisionProtocol
  103. {
  104. BodyDomains = new List<string>()
  105. };
  106. try
  107. {
  108. string[] heads = head.Split(',');
  109. string[] parts = body.Split(',');
  110. skvpReponse.Direction = heads[1];
  111. skvpReponse.ModuleCode = heads[2];
  112. skvpReponse.FuncCode = heads[3];
  113. skvpReponse.ProductCode = heads[4];
  114. skvpReponse.Timestamp = heads[5];
  115. skvpReponse.ResultCode = heads[6];
  116. foreach (string part in parts)
  117. {
  118. skvpReponse.BodyDomains.Add(part);
  119. }
  120. }
  121. catch (Exception e)
  122. {
  123. throw new VisionException { Type = VisionException.Parse_Error, Detail = $"视觉系统返回数据解析异常: {e.Message}" };
  124. }
  125. if (validate)
  126. {
  127. // if (!skvpReponse.Timestamp.Equals(Timestamp) || !skvpReponse.ModuleCode.Equals(ModuleCode) ||
  128. // !skvpReponse.FuncCode.Equals(FuncCode) || !skvpReponse.ProductCode.Equals(ProductCode))
  129. if (!skvpReponse.Timestamp.Equals(Timestamp) || !skvpReponse.ModuleCode.Equals(ModuleCode) ||
  130. !skvpReponse.FuncCode.Equals(FuncCode))
  131. {
  132. throw new VisionException { Type = VisionException.Result_Error, Detail = $"视觉系统返回数据验证异常" };
  133. }
  134. }
  135. if (!skvpReponse.ResultCode.Equals("0000"))
  136. {
  137. throw new VisionException { Type = VisionException.Result_Error, Detail = $"视觉系统异常, Code: {skvpReponse.ResultCode}" };
  138. }
  139. return skvpReponse;
  140. }
  141. }
  142. }