|
|
|
@ -18,25 +18,29 @@ namespace HighWayIot.TouchSocket
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public class ServerBufferAnalysis
|
|
|
|
public class ServerBufferAnalysis
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private static LogHelper logHelper = LogHelper.Instance;
|
|
|
|
private static readonly Lazy<ServerBufferAnalysis> lazy = new Lazy<ServerBufferAnalysis>(() => new ServerBufferAnalysis());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static ServerBufferAnalysis Instance => lazy.Value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private LogHelper logHelper = LogHelper.Instance;
|
|
|
|
|
|
|
|
|
|
|
|
List<byte> tempData = new List<byte>();
|
|
|
|
List<byte> tempData = new List<byte>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
/// 基础数据转包
|
|
|
|
/// 基础数据转包
|
|
|
|
/// </summary>
|
|
|
|
/// </summary>
|
|
|
|
public void BaseServerBufferAnalysis(byte[] message)
|
|
|
|
public BaseMessagePack BaseUnpackServerBufferAnalysis(byte[] bytes)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
BaseMessagePack unpackEntity = new BaseMessagePack();
|
|
|
|
BaseMessagePack unpackEntity = new BaseMessagePack();
|
|
|
|
int index = 1;
|
|
|
|
int index = 1;
|
|
|
|
//报文完整性验证
|
|
|
|
//报文完整性验证
|
|
|
|
if (!MessageCheck(message))
|
|
|
|
if (!MessageCheck(bytes))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
tempData.AddRange(message.ToList());
|
|
|
|
tempData.AddRange(bytes.ToList());
|
|
|
|
message = tempData.ToArray();
|
|
|
|
bytes = tempData.ToArray();
|
|
|
|
if (!MessageCheck(message))
|
|
|
|
if (!MessageCheck(bytes))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -45,49 +49,105 @@ namespace HighWayIot.TouchSocket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//功能码
|
|
|
|
//功能码
|
|
|
|
Array.Copy(message, index, unpackEntity.FunctionCode, 0, 2);
|
|
|
|
Array.Copy(bytes, index, unpackEntity.FunctionCode, 0, 2);
|
|
|
|
Array.Reverse(unpackEntity.FunctionCode);
|
|
|
|
Array.Reverse(unpackEntity.FunctionCode);
|
|
|
|
index += 2;
|
|
|
|
index += 2;
|
|
|
|
|
|
|
|
|
|
|
|
//报文字节长度
|
|
|
|
//报文字节长度
|
|
|
|
Array.Copy(message, index, unpackEntity.Length, 0, 2);
|
|
|
|
Array.Copy(bytes, index, unpackEntity.Length, 0, 2);
|
|
|
|
Array.Reverse(unpackEntity.Length);
|
|
|
|
Array.Reverse(unpackEntity.Length);
|
|
|
|
index += 2;
|
|
|
|
index += 2;
|
|
|
|
|
|
|
|
|
|
|
|
//版本号
|
|
|
|
//版本号
|
|
|
|
unpackEntity.VersionCode = message[index];
|
|
|
|
unpackEntity.VersionCode = bytes[index];
|
|
|
|
index++;
|
|
|
|
index++;
|
|
|
|
|
|
|
|
|
|
|
|
//报文序列号
|
|
|
|
//报文序列号
|
|
|
|
Array.Copy(message, index, unpackEntity.SerialCode, 0, 4);
|
|
|
|
Array.Copy(bytes, index, unpackEntity.SerialCode, 0, 4);
|
|
|
|
Array.Reverse(unpackEntity.SerialCode);
|
|
|
|
Array.Reverse(unpackEntity.SerialCode);
|
|
|
|
index += 4;
|
|
|
|
index += 4;
|
|
|
|
|
|
|
|
|
|
|
|
//时间戳
|
|
|
|
//时间戳
|
|
|
|
Array.Copy(message, index, unpackEntity.TimeStamp, 0, 8);
|
|
|
|
Array.Copy(bytes, index, unpackEntity.TimeStamp, 0, 8);
|
|
|
|
Array.Reverse(unpackEntity.TimeStamp);
|
|
|
|
Array.Reverse(unpackEntity.TimeStamp);
|
|
|
|
index += 8;
|
|
|
|
index += 8;
|
|
|
|
|
|
|
|
|
|
|
|
//数据内容
|
|
|
|
//数据内容
|
|
|
|
int dataLength = BitConverter.ToUInt16(unpackEntity.Length, 0) - 13;
|
|
|
|
int dataLength = BitConverter.ToUInt16(unpackEntity.Length, 0) - 13;
|
|
|
|
unpackEntity.DataContent = new byte[dataLength];
|
|
|
|
unpackEntity.DataContent = new byte[dataLength];
|
|
|
|
Array.Copy(message, index, unpackEntity.DataContent, 0, dataLength);
|
|
|
|
Array.Copy(bytes, index, unpackEntity.DataContent, 0, dataLength);
|
|
|
|
index += dataLength;
|
|
|
|
index += dataLength;
|
|
|
|
|
|
|
|
|
|
|
|
//校验值
|
|
|
|
//校验值
|
|
|
|
unpackEntity.CheckSum = message[index];
|
|
|
|
unpackEntity.CheckSum = bytes[index];
|
|
|
|
|
|
|
|
|
|
|
|
//核对校验值
|
|
|
|
//核对校验值
|
|
|
|
byte[] checkBytes = new byte[message.Length - 4];
|
|
|
|
byte[] checkBytes = new byte[bytes.Length - 4];
|
|
|
|
Array.Copy(message, 1, checkBytes, 0, message.Length - 4);
|
|
|
|
Array.Copy(bytes, 1, checkBytes, 0, bytes.Length - 4);
|
|
|
|
if (unpackEntity.CheckSum != CalculateCheckSum(checkBytes))
|
|
|
|
if (unpackEntity.CheckSum != CalculateCheckSum(checkBytes))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
logHelper.Error("报文校验失败!");
|
|
|
|
logHelper.Error("报文校验失败!");
|
|
|
|
return;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//到工厂分类解析
|
|
|
|
//到工厂分类解析
|
|
|
|
MessageFactory(unpackEntity);
|
|
|
|
return unpackEntity;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 基础数据封包
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
public byte[] BasePackedServerBufferAnalysis(BaseMessagePack message)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int length = BitConverter.ToUInt16(message.Length, 0) + 8;
|
|
|
|
|
|
|
|
byte[] bytes = new byte[length];
|
|
|
|
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
//起始字符
|
|
|
|
|
|
|
|
bytes[index] = 0x3A;
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//功能码
|
|
|
|
|
|
|
|
Array.Reverse(message.FunctionCode);
|
|
|
|
|
|
|
|
Array.Copy(message.FunctionCode, 0, bytes, index, 2);
|
|
|
|
|
|
|
|
index += 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//报文字节长度
|
|
|
|
|
|
|
|
Array.Reverse(message.Length);
|
|
|
|
|
|
|
|
Array.Copy(message.Length, 0, bytes, index, 2);
|
|
|
|
|
|
|
|
index += 2;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//版本号
|
|
|
|
|
|
|
|
bytes[index] = message.VersionCode;
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//报文序列号
|
|
|
|
|
|
|
|
Array.Reverse(message.SerialCode);
|
|
|
|
|
|
|
|
Array.Copy(message.SerialCode, 0, bytes, index, 4);
|
|
|
|
|
|
|
|
index += 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//时间戳
|
|
|
|
|
|
|
|
Array.Reverse(message.TimeStamp);
|
|
|
|
|
|
|
|
Array.Copy(message.TimeStamp, 0, bytes, index, 8);
|
|
|
|
|
|
|
|
index += 8;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//数据内容
|
|
|
|
|
|
|
|
Array.Reverse(message.Length);
|
|
|
|
|
|
|
|
int dataLength = BitConverter.ToUInt16(message.Length, 0) - 13;
|
|
|
|
|
|
|
|
Array.Copy(message.DataContent, 0, bytes, index, dataLength);
|
|
|
|
|
|
|
|
index += dataLength;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//校验值
|
|
|
|
|
|
|
|
byte[] checkBytes = new byte[bytes.Length - 4];
|
|
|
|
|
|
|
|
Array.Copy(bytes, 1, checkBytes, 0, bytes.Length - 4);
|
|
|
|
|
|
|
|
bytes[index] = CalculateCheckSum(checkBytes);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//结束字符1
|
|
|
|
|
|
|
|
bytes[index] = 0x0D;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//结束字符2
|
|
|
|
|
|
|
|
bytes[index] = 0x0A;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// <summary>
|
|
|
|
@ -123,113 +183,5 @@ namespace HighWayIot.TouchSocket
|
|
|
|
return result;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
|
/// 消息分类工厂
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
private void MessageFactory(BaseMessagePack messagePack)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
int functionCode = BitConverter.ToUInt16(messagePack.FunctionCode, 0);
|
|
|
|
|
|
|
|
if (functionCode == 0) //心跳报文
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functionCode == 1) //连接注册请求报文
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functionCode == 600) //读码应答报文
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (functionCode == 610) //状态上报报文
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
|
|
|
///// 心跳报文分析
|
|
|
|
|
|
|
|
///// </summary>
|
|
|
|
|
|
|
|
///// <param name="bytes"></param>
|
|
|
|
|
|
|
|
//public static void HeartbeatSocket(byte[] bytes)
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// if (int.TryParse(Encoding.ASCII.GetString(bytes, 4, 4), out int deviceno))
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// BaseHeartbeatServiceImpl sql = new BaseHeartbeatServiceImpl();
|
|
|
|
|
|
|
|
// if (sql.UpdateHeartbeatInfo(deviceno) == 0)
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// RFIDHeartbeat heartbeat = new RFIDHeartbeat()
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// DeviceNo = deviceno,
|
|
|
|
|
|
|
|
// BeatTime = DateTime.Now,
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// sql.AddHeartbeatInfo(heartbeat);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// logHelper.Error("心跳报文编号数值转换出现错误!");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
|
|
|
///// RFID发送设备状态
|
|
|
|
|
|
|
|
///// </summary>
|
|
|
|
|
|
|
|
///// <param name="bytes"></param>
|
|
|
|
|
|
|
|
//public static void RFIDStatusSocket(byte[] bytes)
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// if (int.TryParse(Encoding.ASCII.GetString(bytes, 4, 4), out int deviceno) &&
|
|
|
|
|
|
|
|
// int.TryParse(Encoding.ASCII.GetString(bytes, 9, 1), out int state))
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// BaseStateServiceImpl sql = new BaseStateServiceImpl();
|
|
|
|
|
|
|
|
// RFIDState rFIDState = new RFIDState()
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// DeviceNo = deviceno,
|
|
|
|
|
|
|
|
// DeviceState = state == 1 ? true : false,
|
|
|
|
|
|
|
|
// LogTime = DateTime.Now,
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// sql.AddStateInfo(rFIDState);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// logHelper.Error("设备状态报文编号数值转换出现错误!");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
|
|
|
///// RFID发送条码
|
|
|
|
|
|
|
|
///// </summary>
|
|
|
|
|
|
|
|
///// <param name="bytes"></param>
|
|
|
|
|
|
|
|
//public static void RFIDCodeSocket(byte[] bytes)
|
|
|
|
|
|
|
|
//{
|
|
|
|
|
|
|
|
// string readKind = Encoding.ASCII.GetString(bytes, 4, 2);
|
|
|
|
|
|
|
|
// if (int.TryParse(Encoding.ASCII.GetString(bytes, 7, 4), out int deviceno))
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// RFIDContent rFIDContent = new RFIDContent()
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// DeviceNo = deviceno,
|
|
|
|
|
|
|
|
// ReadKind = readKind,
|
|
|
|
|
|
|
|
// LogTime = DateTime.Now,
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
// string content = "";
|
|
|
|
|
|
|
|
// if (readKind == "NB" || readKind == "GR")
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// content = Encoding.ASCII.GetString(bytes, 12, 16);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else if (readKind == "MR")
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// content = Encoding.ASCII.GetString(bytes, 12, bytes.Length - 12 - 2); // 减去条码内容之前和之后内容的长度
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// rFIDContent.Content = content;
|
|
|
|
|
|
|
|
// BaseContentServiceImpl sql = new BaseContentServiceImpl();
|
|
|
|
|
|
|
|
// sql.AddContentInfo(rFIDContent);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// else
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// logHelper.Error("条码发送报文编号数值转换出现错误!");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|