using SlnMesnac.RfidUpload.Analysis; using SlnMesnac.RfidUpload.Common; using SlnMesnac.RfidUpload.Model; using SlnMesnac.RfidUpload.Model.config; using SlnMesnac.RfidUpload.TouchSocket; using System; using System.Collections.Generic; using System.Linq; namespace SlnMesnac.RfidUpload.Business { /// /// 业务逻辑处理 /// public class HandleBusiness { private JsonChange _jsonChange = JsonChange.Instance; private InstructionAdapter adapter = InstructionAdapter.Instance; private WebApiClientApp _webApiClientApp = WebApiClientApp.Instance; private readonly AppConfig appConfig = AppConfig.Instance; private List instructionInfoList = new List(); #region 单例实现 private static readonly Lazy _lazy = new Lazy(() => new HandleBusiness()); public static HandleBusiness Instance { get { return _lazy.Value; } } #endregion #region 委托事件 /// /// 刷新标签解析 /// public delegate void RefreshInstructionInfoDataGrid(InstructionInfo instructionInfo); public event RefreshInstructionInfoDataGrid InstructionInfoDataGridEvent; /// /// 刷新日志内容 /// public delegate void RefreshLogMessage(string message); public event RefreshLogMessage RefreshLogMessageEvent; #endregion private HandleBusiness() { } /// /// 标签处理 /// /// /// 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}"); } } } /// /// 粘包处理 /// /// /// public List SplitPackets(byte[] receivedData) { List packets = new List(); 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); } /// /// 标签过滤:同一标签1分钟内不进行逻辑处理 /// /// /// 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 (string.IsNullOrEmpty(appConfig.filterInterval)) { appConfig.filterInterval = "1"; } int filterInterval = Convert.ToInt32(appConfig.filterInterval); if (minutesDifference < filterInterval) { return false; } else { instructionInfoList.Remove(lastInstructionInfo); return true; } } } } }