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.
262 lines
8.5 KiB
C#
262 lines
8.5 KiB
C#
using HighWayIot.Repository.domain;
|
|
using HighWayIot.Repository.service.Impl;
|
|
using HighWayIot.TouchSocket.Entity;
|
|
using Org.BouncyCastle.Crypto;
|
|
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;
|
|
|
|
public Action<byte[], string> MessegeSend;
|
|
|
|
/// <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);
|
|
FirstHeartbeat(id);
|
|
}
|
|
if (functionCode == 600) //读码报文接收
|
|
{
|
|
ReadCodeReportProcess(messagePack, id);
|
|
}
|
|
if (functionCode == 610) //状态上报报文
|
|
{
|
|
StateReportProcess(messagePack, id);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 心跳报文处理
|
|
/// </summary>
|
|
private void HeartbeatProcess(BaseMessagePack messagePack, string id)
|
|
{
|
|
//数据库操作
|
|
|
|
if (BaseHeartbeatServiceImpl.Instance.UpdateHeartbeatInfo(id) == 0)
|
|
{
|
|
RFIDHeartbeat heartbeat = new RFIDHeartbeat()
|
|
{
|
|
LineNo = id,
|
|
BeatTime = DateTime.Now,
|
|
};
|
|
BaseHeartbeatServiceImpl.Instance.AddHeartbeatInfo(heartbeat);
|
|
}
|
|
|
|
//反馈
|
|
MessegeSend.Invoke(
|
|
serverBufferAnalysis.BasePackedServerBufferAnalysis(
|
|
HeartbeatResponse(messagePack)
|
|
),
|
|
id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册报文处理
|
|
/// </summary>
|
|
private void RegisterProcess(BaseMessagePack messagePack, string id)
|
|
{
|
|
//直接反馈
|
|
MessegeSend.Invoke(
|
|
serverBufferAnalysis.BasePackedServerBufferAnalysis(
|
|
StandardResponse(messagePack)
|
|
),
|
|
id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读码应答报文处理
|
|
/// </summary>
|
|
private void ReadCodeReportProcess(BaseMessagePack messagePack, string id)
|
|
{
|
|
//存数据库,无需反馈
|
|
//解析数据内容
|
|
RFIDContent rfidContent = new RFIDContent();
|
|
int index = 0;
|
|
index += 4;
|
|
|
|
byte[] temp = new byte[2];
|
|
Array.Copy(messagePack.DataContent, index, temp, 0, 2);
|
|
Array.Reverse(temp);
|
|
rfidContent.DeviceNo = BitConverter.ToUInt16(temp, 0);
|
|
index += 2;
|
|
|
|
rfidContent.ReadKind = ASCIIEncoding.UTF8.GetString(messagePack.DataContent, index, 2);
|
|
index += 2;
|
|
|
|
int length = messagePack.DataContent[index];
|
|
index++;
|
|
|
|
rfidContent.Content = ASCIIEncoding.UTF8.GetString(messagePack.DataContent, index, length);
|
|
|
|
rfidContent.LineNo = id;
|
|
|
|
rfidContent.LogTime = DateTime.Now;
|
|
|
|
BaseContentServiceImpl.Instance.AddContentInfo(rfidContent);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 状态上报报文处理
|
|
/// </summary>
|
|
private void StateReportProcess(BaseMessagePack messagePack, string id)
|
|
{
|
|
//存数据库,需反馈
|
|
byte[] temp = new byte[2];
|
|
Array.Copy(messagePack.DataContent, 0, temp, 0, 2);
|
|
Array.Reverse(temp);
|
|
int deviceno = BitConverter.ToUInt16(temp, 0);
|
|
|
|
RFIDState rFIDState = new RFIDState()
|
|
{
|
|
DeviceNo = deviceno,
|
|
DeviceState = messagePack.DataContent[2] == 0 ? true : false,
|
|
LogTime = DateTime.Now,
|
|
LineNo = id
|
|
};
|
|
BaseStateServiceImpl.Instance.AddStateInfo(rFIDState);
|
|
|
|
MessegeSend.Invoke(
|
|
serverBufferAnalysis.BasePackedServerBufferAnalysis(
|
|
StandardResponse(messagePack)
|
|
),
|
|
id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读码请求报文处理
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
public void ReadCodeRequestProcess(string id)
|
|
{
|
|
MessegeSend.Invoke(
|
|
serverBufferAnalysis.BasePackedServerBufferAnalysis(
|
|
ReadRequest()
|
|
),
|
|
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);
|
|
index += 2;
|
|
|
|
PackedData(ref messagePack, 10, data);
|
|
return messagePack;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 读码请求报文(主动)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public BaseMessagePack ReadRequest()
|
|
{
|
|
BaseMessagePack messagePack = new BaseMessagePack();
|
|
|
|
messagePack.SerialCode = BitConverter.GetBytes((uint)0);
|
|
messagePack.VersionCode = 0x01;
|
|
|
|
var datacontent = BitConverter.GetBytes((ushort)481);
|
|
Array.Reverse(datacontent);
|
|
|
|
PackedData(ref messagePack, 500, datacontent);
|
|
|
|
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.FunctionCode = 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));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初次心跳发送
|
|
/// </summary>
|
|
public void FirstHeartbeat(string id)
|
|
{
|
|
BaseMessagePack messagePack = new BaseMessagePack();
|
|
messagePack.FunctionCode = BitConverter.GetBytes((ushort)0);
|
|
messagePack.VersionCode = 0x01;
|
|
messagePack.SerialCode = BitConverter.GetBytes((uint)0);
|
|
//反馈
|
|
MessegeSend.Invoke(
|
|
serverBufferAnalysis.BasePackedServerBufferAnalysis(
|
|
HeartbeatResponse(messagePack)
|
|
),
|
|
id);
|
|
}
|
|
}
|
|
}
|