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.
163 lines
5.2 KiB
C#
163 lines
5.2 KiB
C#
using HighWayIot.TouchSocket.Entity;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace HighWayIot.TouchSocket
|
|
{
|
|
/// <summary>
|
|
/// 消息工厂
|
|
/// </summary>
|
|
public class MessageFactory
|
|
{
|
|
private static readonly Lazy<MessageFactory> lazy = new Lazy<MessageFactory>(() => new MessageFactory());
|
|
|
|
public static MessageFactory Instance => lazy.Value;
|
|
|
|
private ServerBufferAnalysis serverBufferAnalysis = ServerBufferAnalysis.Instance;
|
|
|
|
private TcpServer tcpServer = TcpServer.Instance;
|
|
|
|
/// <summary>
|
|
/// 消息分类工厂
|
|
/// </summary>
|
|
public void Factory(BaseMessagePack messagePack, string id)
|
|
{
|
|
if (messagePack == null)
|
|
{
|
|
return;
|
|
}
|
|
int functionCode = BitConverter.ToUInt16(messagePack.FunctionCode, 0);
|
|
if (functionCode == 0) //心跳报文
|
|
{
|
|
HeartbeatProcess(messagePack, id);
|
|
}
|
|
if (functionCode == 1) //连接注册请求报文
|
|
{
|
|
RegisterProcess(messagePack, id);
|
|
}
|
|
if (functionCode == 600) //读码报文接收
|
|
{
|
|
ReadCodeReportProcess(messagePack);
|
|
}
|
|
if (functionCode == 610) //状态上报报文
|
|
{
|
|
StateReportProcess(messagePack, id);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 心跳报文处理
|
|
/// </summary>
|
|
private void HeartbeatProcess(BaseMessagePack messagePack, string id)
|
|
{
|
|
//直接反馈
|
|
tcpServer.SendMessage(
|
|
serverBufferAnalysis.BasePackedServerBufferAnalysis(
|
|
HeartbeatResponse(messagePack)
|
|
),
|
|
id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册报文处理
|
|
/// </summary>
|
|
private void RegisterProcess(BaseMessagePack messagePack, string id)
|
|
{
|
|
//直接反馈
|
|
tcpServer.SendMessage(
|
|
serverBufferAnalysis.BasePackedServerBufferAnalysis(
|
|
StandardResponse(messagePack)
|
|
),
|
|
id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读码应答报文处理
|
|
/// </summary>
|
|
private void ReadCodeReportProcess(BaseMessagePack messagePack)
|
|
{
|
|
//存数据库,无需反馈
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 状态上报报文处理
|
|
/// </summary>
|
|
private void StateReportProcess(BaseMessagePack messagePack, string id)
|
|
{
|
|
//存数据库,需反馈
|
|
|
|
tcpServer.SendMessage(
|
|
serverBufferAnalysis.BasePackedServerBufferAnalysis(
|
|
StandardResponse(messagePack)
|
|
),
|
|
id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 包封装(心跳响应)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public BaseMessagePack HeartbeatResponse(BaseMessagePack messagePack)
|
|
{
|
|
PackedData(ref messagePack, 0, new byte[0]);
|
|
|
|
return messagePack;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 包封装(注册和状态上报响应)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public BaseMessagePack StandardResponse(BaseMessagePack messagePack)
|
|
{
|
|
//解析来时报文
|
|
//看了一眼协议好像不太用解析
|
|
|
|
//封包应答报文
|
|
byte[] data = new byte[6];
|
|
int index = 0;
|
|
//来时报文序列号
|
|
Array.Reverse(messagePack.SerialCode);
|
|
Array.Copy(messagePack.SerialCode, 0, data, index, 4);
|
|
Array.Reverse(messagePack.SerialCode);
|
|
index += 4;
|
|
|
|
//来时报文功能码
|
|
Array.Reverse(messagePack.FunctionCode);
|
|
Array.Copy(messagePack.FunctionCode, 0, data, index, 2);
|
|
Array.Reverse(messagePack.FunctionCode);
|
|
index += 2;
|
|
|
|
PackedData(ref messagePack, 10, data);
|
|
return messagePack;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 封装数据部分(版本号沿用,序列号自动加一)
|
|
/// </summary>
|
|
/// <param name="messagePack">源数据包</param>
|
|
/// <param name="functionCode">新的功能码</param>
|
|
/// <param name="dataContent">新的DataContent</param>
|
|
public void PackedData(ref BaseMessagePack messagePack, ushort functionCode, byte[] dataContent)
|
|
{
|
|
if (dataContent == null)
|
|
{
|
|
return;
|
|
}
|
|
messagePack.Length = BitConverter.GetBytes(functionCode);
|
|
uint code = BitConverter.ToUInt32(messagePack.SerialCode, 0);
|
|
code++;
|
|
messagePack.SerialCode = BitConverter.GetBytes(code);
|
|
messagePack.TimeStamp = BitConverter.GetBytes(Convert.ToUInt64(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()));
|
|
messagePack.DataContent = dataContent;
|
|
messagePack.Length = BitConverter.GetBytes(Convert.ToUInt16(messagePack.DataContent.Length + 13u));
|
|
}
|
|
|
|
}
|
|
}
|