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.0 KiB
C#

using SlnMesnac.RfidUpload.Analysis;
using SlnMesnac.RfidUpload.Common;
using SlnMesnac.RfidUpload.Model;
using SlnMesnac.RfidUpload.TouchSocket;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SlnMesnac.RfidUpload.Business
{
/// <summary>
/// 业务逻辑处理
/// </summary>
public class HandleBusiness
{
private JsonChange _jsonChange = JsonChange.Instance;
private InstructionAdapter adapter = InstructionAdapter.Instance;
private WebApiClientApp _webApiClientApp = WebApiClientApp.Instance;
private List<InstructionInfo> instructionInfoList = new List<InstructionInfo>();
#region 单例实现
private static readonly Lazy<HandleBusiness> _lazy = new Lazy<HandleBusiness>(() => new HandleBusiness());
public static HandleBusiness Instance
{
get
{
return _lazy.Value;
}
}
#endregion
#region 委托事件
/// <summary>
/// 刷新标签解析
/// </summary>
public delegate void RefreshInstructionInfoDataGrid(InstructionInfo instructionInfo);
public event RefreshInstructionInfoDataGrid InstructionInfoDataGridEvent;
/// <summary>
/// 刷新日志内容
/// </summary>
public delegate void RefreshLogMessage(string message);
public event RefreshLogMessage RefreshLogMessageEvent;
#endregion
private HandleBusiness() { }
/// <summary>
/// 标签处理
/// </summary>
/// <param name="buffer"></param>
/// <exception cref="ArgumentException"></exception>
public void LabelHandle(byte[] buffer)
{
//功能码判断
if (buffer[4] == 0x31)
{
try
{
InstructionInfo info = adapter.ParseInstruction(buffer);
if (info != null)
{
if (FilterTags(info))
{
instructionInfoList.Add(info);
RefreshLogMessageEvent?.Invoke($"标签解析数据:{_jsonChange.ModeToJson(info)}");
InstructionInfoDataGridEvent?.Invoke(info);
}
else
{
RefreshLogMessageEvent?.Invoke($"标签:{info.data};与前次读取间隔小于1分钟不进行逻辑处理");
}
}
}
catch (Exception e)
{
RefreshLogMessageEvent?.Invoke($"标签解析异常:{e.Message}");
}
}
}
/// <summary>
/// 粘包处理
/// </summary>
/// <param name="receivedData"></param>
/// <returns></returns>
public List<byte[]> SplitPackets(byte[] receivedData)
{
List<byte[]> packets = new List<byte[]>();
int index = 0;
while (index < receivedData.Length)
{
if (receivedData[index] == 0xAA && receivedData[index + 1] == 0x55)
{
int packetLength = (receivedData[index + 2] << 8) | receivedData[index + 3];
if (packetLength <= receivedData.Length - index)
{
byte[] packet = new byte[packetLength];
Array.Copy(receivedData, index, packet, 0, packetLength);
packets.Add(packet);
index += packetLength;
}
else
{
break;
}
}
else
{
index++;
}
}
return packets;
}
public void apiTest(string url)
{
//WebApiClientApp api = new WebApiClientApp();
_webApiClientApp.Upload(url);
}
/// <summary>
/// 标签过滤同一标签1分钟内不进行逻辑处理
/// </summary>
/// <param name="instructionInfo"></param>
/// <returns></returns>
private bool FilterTags(InstructionInfo instructionInfo)
{
InstructionInfo lastInstructionInfo = instructionInfoList.Where(x => x.data == instructionInfo.data).FirstOrDefault();
if(lastInstructionInfo == null)
{
return true;
}
else
{
TimeSpan timeDifference = instructionInfo.recordtime.Subtract(lastInstructionInfo.recordtime);
double minutesDifference = timeDifference.TotalMinutes;
if (minutesDifference < 1)
{
return false;
}
else
{
instructionInfoList.Remove(lastInstructionInfo);
return true;
}
}
}
}
}