You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
236 lines
7.5 KiB
C#
236 lines
7.5 KiB
C#
using HighWayIot.Log4net;
|
|
using HighWayIot.Repository.domain;
|
|
using HighWayIot.Repository.service.Impl;
|
|
using HighWayIot.TouchSocket.Entity;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using TouchSocket.Core;
|
|
|
|
namespace HighWayIot.TouchSocket
|
|
{
|
|
/// <summary>
|
|
/// 服务端接收报文分析
|
|
/// </summary>
|
|
public class ServerBufferAnalysis
|
|
{
|
|
private static LogHelper logHelper = LogHelper.Instance;
|
|
|
|
List<byte> tempData = new List<byte>();
|
|
|
|
/// <summary>
|
|
/// 基础数据转包
|
|
/// </summary>
|
|
public void BaseServerBufferAnalysis(byte[] message)
|
|
{
|
|
BaseMessagePack unpackEntity = new BaseMessagePack();
|
|
int index = 1;
|
|
//报文完整性验证
|
|
if (!MessageCheck(message))
|
|
{
|
|
tempData.AddRange(message.ToList());
|
|
message = tempData.ToArray();
|
|
if (!MessageCheck(message))
|
|
{
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
tempData.Clear();
|
|
}
|
|
}
|
|
|
|
//功能码
|
|
Array.Copy(message, index, unpackEntity.FunctionCode, 0, 2);
|
|
Array.Reverse(unpackEntity.FunctionCode);
|
|
index += 2;
|
|
|
|
//报文字节长度
|
|
Array.Copy(message, index, unpackEntity.Length, 0, 2);
|
|
Array.Reverse(unpackEntity.Length);
|
|
index += 2;
|
|
|
|
//版本号
|
|
unpackEntity.VersionCode = message[index];
|
|
index++;
|
|
|
|
//报文序列号
|
|
Array.Copy(message, index, unpackEntity.SerialCode, 0, 4);
|
|
Array.Reverse(unpackEntity.SerialCode);
|
|
index += 4;
|
|
|
|
//时间戳
|
|
Array.Copy(message, index, unpackEntity.TimeStamp, 0, 8);
|
|
Array.Reverse(unpackEntity.TimeStamp);
|
|
index += 8;
|
|
|
|
//数据内容
|
|
int dataLength = BitConverter.ToUInt16(unpackEntity.Length, 0) - 13;
|
|
unpackEntity.DataContent = new byte[dataLength];
|
|
Array.Copy(message, index, unpackEntity.DataContent, 0, dataLength);
|
|
index += dataLength;
|
|
|
|
//校验值
|
|
unpackEntity.CheckSum = message[index];
|
|
|
|
//核对校验值
|
|
byte[] checkBytes = new byte[message.Length - 4];
|
|
Array.Copy(message, 1, checkBytes, 0, message.Length - 4);
|
|
if (unpackEntity.CheckSum != CalculateCheckSum(checkBytes))
|
|
{
|
|
logHelper.Error("报文校验失败!");
|
|
return;
|
|
}
|
|
|
|
//到工厂分类解析
|
|
MessageFactory(unpackEntity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 报文完整性验证
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <returns>true为通过 false为失败</returns>
|
|
private bool MessageCheck(byte[] message)
|
|
{
|
|
bool result = true;
|
|
if (message[0] != 0x3A || message[message.Length - 1] != 0x0A || message[message.Length - 2] != 0x0D)
|
|
{
|
|
result = false;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 和校验
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
/// <param name="length"></param>
|
|
/// <returns></returns>
|
|
public byte CalculateCheckSum(byte[] bytes)
|
|
{
|
|
int length = bytes.Length;
|
|
int check = 0;
|
|
for (int i = 0; i < length; i++)
|
|
{
|
|
check += bytes[i];
|
|
}
|
|
byte result = (byte)(255 - (check % 255));
|
|
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("条码发送报文编号数值转换出现错误!");
|
|
// }
|
|
//}
|
|
|
|
}
|
|
}
|