You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
4.1 KiB
102 lines
4.1 KiB
using System.Collections.Generic;
|
|
using GPSBusiness.BBGPSStandard;
|
|
using System.Linq;
|
|
using GPSBusiness.Helper;
|
|
using System;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace GPSBusiness.GPSStandard
|
|
{
|
|
/// <summary>
|
|
/// gps协议帮帮助类
|
|
/// </summary>
|
|
public static class GpsStandardHelper
|
|
{
|
|
public static IStandardToCarGPSHandler BBStandardToCarGPSHandler;
|
|
private static bool gege = true;
|
|
|
|
/// <summary>
|
|
/// 处理粘包问题
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
/// <param name="conid"></param>
|
|
/// <param name="mdtStr"></param>
|
|
/// <returns></returns>
|
|
public static List<GPSResponseData> GetGpsStandardData(byte[] bytes, IntPtr conid, string mdtStr)
|
|
{
|
|
|
|
try
|
|
{
|
|
var gpsResponseDatas = new List<GPSResponseData>();
|
|
string packet = ByteConvert.ByteArrayToHexStr(bytes);
|
|
//"GetGpsStandardData".Log().DebugFormat(" > [部标协议tcp请求]: {0}", packet);
|
|
//部标协议
|
|
//以7E开头,7E结尾,中间没有其他&7E(表示没有粘包)
|
|
if (bytes[0] == 0x7E && bytes[bytes.Length - 1] == 0x7E && bytes.Count(u => u == 0x7E) == 2 && gege)
|
|
{
|
|
if (bytes.Length >= 15)
|
|
{
|
|
var responseData = BbGpsStandardService.GetGpsResponseDataForEqu(bytes,
|
|
BBStandardToCarGPSHandler, conid, mdtStr);
|
|
gpsResponseDatas.Add(responseData);
|
|
}
|
|
}
|
|
//部标粘包处理
|
|
else if (bytes.Count(u => u == 0x7E) >= 2 && gege)
|
|
{
|
|
var list = GetSlxkValue(packet, "7E", "7E");
|
|
foreach (var item in list)
|
|
{
|
|
//"GetGpsStandardData".Log().DebugFormat(" > [部标粘包处理item]: {0}", item);
|
|
byte[] by = ByteConvert.HexStringToByte(item);
|
|
gpsResponseDatas.Add(BbGpsStandardService.GetGpsResponseDataForEqu(by, BBStandardToCarGPSHandler, conid, mdtStr));
|
|
}
|
|
}
|
|
//无法解释的协议,暂时不处理
|
|
else
|
|
{
|
|
if (packet == "1B5B317E") gege = true;
|
|
if (packet == "1B5B347E") gege = false;
|
|
"GetGpsStandardData".Log().DebugFormat(" > [未知协议 tcp 请求]: " + packet + gege.ToString());
|
|
}
|
|
return gpsResponseDatas;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
//拿出符合要求的数据包
|
|
public static List<string> GetSlxkValue(string str, string s, string e)
|
|
{
|
|
Regex rg = new Regex("(?<=(" + s + "))((?!" + s + "|" + e + ").)*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
|
|
var list = rg.Matches(str);
|
|
var result = new List<string>();
|
|
foreach (var item in list)
|
|
{
|
|
string packet = item.ToString();
|
|
if (packet.Length > 0)
|
|
{
|
|
result.Add(string.Format("{0}{1}{2}", s, item, e));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
//拿出符合要求的数据包---这是旧的
|
|
public static List<string> GetValue(string str, string s, string e)
|
|
{
|
|
Regex rg = new Regex("(?<=(" + s + "))((?!" + s + "|" + e + ").)*?(?=(" + e + "))", RegexOptions.Multiline | RegexOptions.Singleline);
|
|
var list = rg.Matches(str);
|
|
var result = new List<string>();
|
|
foreach (var item in list)
|
|
{
|
|
string packet = item.ToString();
|
|
if (packet.Length >= 78 && packet.Substring(packet.Length - 2) == "7E")
|
|
{
|
|
result.Add(string.Format("{0}{1}", s, item));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|