using SKMC.Api.Common.Vision;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SKMC.Api.Device.Adapter.SKVision
{
///
/// SKVision通讯协议(V0)
///
public class SKVisionProtocol
{
///
/// 协议头
///
public string Protocol { get; } = "SKV0";
///
/// 请求方向, 请求:T、响应:R
///
public string Direction { get; set; }
///
/// 模块码
///
public string ModuleCode { get; set; }
///
/// 功能码
///
public string FuncCode { get; set; }
///
/// 产品码或二维码
///
public string ProductCode { get; set; }
///
/// 时间戳, 格式为:yyMMddHHmmssfff
///
public string Timestamp { get; set; }
///
/// 请求设置码
///
public string OptionCode { get; set; } = "0000";
///
/// 响应结果码
///
public string ResultCode { get; set; }
///
/// 报文体内容字段集
///
public List BodyDomains { get; set; }
///
/// SKVision通讯协议子对象
///
public SKVisionProtocol SKVisionSubProtocol { get; set; }
///
/// 创建请求报文
///
///
public string ToRequest()
{
string head = $"{Protocol},{Direction},{ModuleCode},{FuncCode},{ProductCode},{Timestamp},{OptionCode}";
if (BodyDomains == null)
{
return $"{head},#,,$";
}
return $"{head},#,{string.Join(",", BodyDomains.ToArray())},$";
}
///
/// 解析响应报文并验证
///
///
///
///
public SKVisionProtocol FromResponse(string response, bool validate = true)
{
response = response.Trim();
int bodyStart = response.IndexOf("#,");
int bodyEnd = response.IndexOf(",$");
SKVisionProtocol skvpReponse = null;
if (bodyStart > 0 && bodyEnd > 0)
{
string head = response.Substring(0, bodyStart);
string body = response.Substring(bodyStart + 2, bodyEnd - bodyStart - 2);
skvpReponse = ParseContent(head, body, validate);
}
// 判断是否有粘包
int subBodyEnd = response.IndexOf(",$", bodyEnd + 1);
int subBodyStart = response.IndexOf("#,", bodyEnd + 1);
if (subBodyStart > 0 && subBodyEnd > 0)
{
string subHead = response.Substring(bodyEnd + 1, subBodyStart - bodyEnd - 2);
string subBody = response.Substring(subBodyStart + 2, subBodyEnd - subBodyStart - 2);
skvpReponse.SKVisionSubProtocol = ParseContent(subHead, subBody, validate);
}
if (skvpReponse != null) return skvpReponse;
throw new VisionException { Type = VisionException.Parse_Error, Detail = $"视觉系统返回数据格式异常" };
}
///
/// 内容解析
///
///
///
///
///
private SKVisionProtocol ParseContent(string head, string body, bool validate = true)
{
SKVisionProtocol skvpReponse = new SKVisionProtocol
{
BodyDomains = new List()
};
try
{
string[] heads = head.Split(',');
string[] parts = body.Split(',');
skvpReponse.Direction = heads[1];
skvpReponse.ModuleCode = heads[2];
skvpReponse.FuncCode = heads[3];
skvpReponse.ProductCode = heads[4];
skvpReponse.Timestamp = heads[5];
skvpReponse.ResultCode = heads[6];
foreach (string part in parts)
{
skvpReponse.BodyDomains.Add(part);
}
}
catch (Exception e)
{
throw new VisionException { Type = VisionException.Parse_Error, Detail = $"视觉系统返回数据解析异常: {e.Message}" };
}
if (validate)
{
// if (!skvpReponse.Timestamp.Equals(Timestamp) || !skvpReponse.ModuleCode.Equals(ModuleCode) ||
// !skvpReponse.FuncCode.Equals(FuncCode) || !skvpReponse.ProductCode.Equals(ProductCode))
if (!skvpReponse.Timestamp.Equals(Timestamp) || !skvpReponse.ModuleCode.Equals(ModuleCode) ||
!skvpReponse.FuncCode.Equals(FuncCode))
{
throw new VisionException { Type = VisionException.Result_Error, Detail = $"视觉系统返回数据验证异常" };
}
}
if (!skvpReponse.ResultCode.Equals("0000"))
{
throw new VisionException { Type = VisionException.Result_Error, Detail = $"视觉系统异常, Code: {skvpReponse.ResultCode}" };
}
return skvpReponse;
}
}
}