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.

274 lines
12 KiB
C#

#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2025 WenJY
* CLR4.0.30319.42000
* Mr.Wen's MacBook Pro
* Sln.Rfid.Business
* 70110000-AA48-4CDD-B5F9-215E08E2BE8E
*
* WenJY
*
* 2025-11-25 14:28:17
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Sln.Rfid.Common;
using Sln.Rfid.Model.dao;
using Sln.Rfid.Repository.service;
using Sln.Rfid.Serilog;
using TouchSocket.Core;
using TouchSocket.Sockets;
namespace Sln.Rfid.Business
{
public class TagBusiness
{
private readonly StringChange _stringChange;
private readonly SerilogHelper _serilogHelper;
private readonly Dictionary<BaseDeviceInfo, TcpClient> _dics;
private readonly IRecordTagInfoService _recordTagInfoService;
private readonly WebSocketBusiness _webSocket;
public TagBusiness(Dictionary<BaseDeviceInfo, TcpClient> dics, StringChange stringChange, SerilogHelper serilogHelper, IRecordTagInfoService recordTagInfoService, WebSocketBusiness webSocket)
{
_dics = dics;
_stringChange = stringChange;
_serilogHelper = serilogHelper;
_recordTagInfoService = recordTagInfoService;
_webSocket = webSocket;
}
public async void Handle()
{
if (_dics != null)
{
foreach (var (item, tcpClient) in _dics)
{
ReadEpcAsync(item, tcpClient);
}
}
}
public async Task ReadEpcAsync(BaseDeviceInfo deviceInfo,TcpClient tcpClient)
{
tcpClient.Received = (client, e) =>
{
//从服务器收到信息。但是一般byteBlock和requestInfo会根据适配器呈现不同的值。
var byteBlock = e.ByteBlock;
int pos = byteBlock.Pos;
var bytes = byteBlock.ToArray();
_serilogHelper.Rfid($"收到 {deviceInfo.deviceName} 指令信息:{_stringChange.bytesToHexStr(bytes, bytes.Length)}");
try
{
if (byteBlock.CanReadLen < 7)
{
_serilogHelper.Rfid($"{deviceInfo.deviceName} 指令标签长度不足");
this.SaveEpc(deviceInfo, string.Empty, true,"指令标签长度不足");
}
else
{
byteBlock.Read(out byte[] header, 2);
byteBlock.Read(out byte[] length, 1);
string hexString = BitConverter.ToString(length).Replace("-", "");
int bodyLength = Convert.ToInt32(hexString, 16);
byteBlock.Read(out byte[] code, 1);
byteBlock.Read(out byte[] status, 1);
if (status[0] == 0x40)
{
_serilogHelper.Rfid($"{deviceInfo.deviceName} 未获取到标签信息");
this.SaveEpc(deviceInfo, string.Empty, true,"未获取到标签信息");
}
else
{
byteBlock.Read(out byte[] count, 1);
byteBlock.Read(out byte[] rssi, 1);
byteBlock.Read(out byte[] ant, 1);
byteBlock.Read(out byte[] pc, 2);
byteBlock.Read(out byte[] epc, bodyLength - 5);
byteBlock.Read(out byte[] xor, 1);
byteBlock.Read(out byte[] end, 1);
string epcStr = Encoding.ASCII.GetString(epc).ToUpper();
_serilogHelper.Rfid(
$"{deviceInfo.deviceName} EPC指令{_stringChange.bytesToHexStr(epc, epc.Length)}; EPC信息: {epcStr}");
this.SaveEpc(deviceInfo, epcStr, false,string.Empty);
}
}
}
catch (Exception ex)
{
_serilogHelper.Rfid($"{deviceInfo.deviceName} 指令标签解析失败: " + ex.Message);
byteBlock.Pos = pos;
this.SaveEpc(deviceInfo, string.Empty, true,"指令标签解析异常");
}
return EasyTask.CompletedTask;
};
// while (true)
// {
// using (var receiver = tcpClient.CreateReceiver())
// {
// tcpClient.SendAsync(new byte[]
// {
// 0xAA, 0x55, 0x02, 0x01, 0x00, 0x64, 0x67, 0x0D
// }).GetAwaiter();
//
// try
// {
// _serilogHelper.Rfid($"开始读取 {deviceInfo.deviceName} 标签数据");
// using (var cts = new CancellationTokenSource(1000 * 10)) // timeout
// {
// using (var receiverResult = await receiver.ReadAsync(cts.Token))
// {
// // var bytes = receiverResult.Memory.Span.ToArray();
// var byteBlock = receiverResult.ByteBlock;
// int pos = byteBlock.Pos;
//
// var bytes = byteBlock.ToArray();
//
// _serilogHelper.Rfid(
// $"收到 {deviceInfo.deviceName} 指令信息:{_stringChange.bytesToHexStr(bytes, bytes.Length)}");
//
// try
// {
// if (byteBlock.CanReadLen < 7)
// {
// _serilogHelper.Rfid($"{deviceInfo.deviceName} 指令标签长度不足");
//
// this.SaveEpc(deviceInfo, string.Empty, true,"指令标签长度不足");
//
// Thread.Sleep(1000*1);
//
// continue;
// }
//
// byteBlock.Read(out byte[] header, 2);
// byteBlock.Read(out byte[] length, 1);
//
// string hexString = BitConverter.ToString(length).Replace("-", "");
// int bodyLength = Convert.ToInt32(hexString, 16);
//
// byteBlock.Read(out byte[] code, 1);
// byteBlock.Read(out byte[] status, 1);
//
// if (status[0] == 0x40)
// {
// _serilogHelper.Rfid($"{deviceInfo.deviceName} 未获取到标签信息");
//
// this.SaveEpc(deviceInfo, string.Empty, true,"未获取到标签信息");
//
// Thread.Sleep(1000*1);
//
// continue;
// }
//
//
// byteBlock.Read(out byte[] count, 1);
// byteBlock.Read(out byte[] rssi, 1);
// byteBlock.Read(out byte[] ant, 1);
// byteBlock.Read(out byte[] pc, 2);
// byteBlock.Read(out byte[] epc, bodyLength - 5);
// byteBlock.Read(out byte[] xor, 1);
// byteBlock.Read(out byte[] end, 1);
//
// string epcStr = Encoding.ASCII.GetString(epc).ToUpper();
//
// _serilogHelper.Rfid(
// $"{deviceInfo.deviceName} EPC指令{_stringChange.bytesToHexStr(epc, epc.Length)}; EPC信息: {epcStr}");
// this.SaveEpc(deviceInfo, epcStr, false,string.Empty);
// }
// catch (Exception e)
// {
// _serilogHelper.Rfid($"{deviceInfo.deviceName} 指令标签解析失败: " + e.Message);
// byteBlock.Pos = pos;
// this.SaveEpc(deviceInfo, string.Empty, true,"指令标签解析异常");
// }
// }
// }
// }
// catch (Exception e)
// {
// _serilogHelper.Rfid($"等待 {deviceInfo.deviceName} 指令超时,设备超时未返回");
// }
// }
//
// Thread.Sleep(1000*deviceInfo.readFrequency);
// }
}
/// <summary>
/// 保存 EPC 条码信息
/// </summary>
/// <param name="deviceInfo"></param>
/// <param name="epcStr"></param>
/// <param name="isAlarm"></param>
/// <param name="alarmStr"></param>
public void SaveEpc(BaseDeviceInfo deviceInfo, string epcStr,bool isAlarm,string alarmStr)
{
try
{
RecordTagInfo recordTagInfo = new RecordTagInfo();
recordTagInfo.deviceId = deviceInfo.objid;
recordTagInfo.readStatus = isAlarm ? '0' : '1';
recordTagInfo.epcStr = epcStr;
recordTagInfo.alarmFlag = isAlarm ? '1' : '0';
recordTagInfo.alarmAction = alarmStr;
recordTagInfo.recordTime = DateTime.Now;
bool isRes = _recordTagInfoService.SplitInsert(new List<RecordTagInfo> { recordTagInfo },
out List<long> insertIds);
if (isRes)
{
_serilogHelper.Info(
$"{deviceInfo.deviceName} 标签数据保存成功Id:{JsonConvert.SerializeObject(insertIds)}");
}
else
{
_serilogHelper.Info($"{deviceInfo.deviceName} 标签数据保存失败");
}
_webSocket.PushMsg(JsonConvert.SerializeObject(recordTagInfo));
}
catch (Exception e)
{
_serilogHelper.Info($"{deviceInfo.deviceName} 标签数据保存异常:{e.Message}");
}
}
/// <summary>
/// 推送设备读取信息
/// </summary>
/// <param name="deviceInfo"></param>
/// <param name="isSuc"></param>
/// <param name="epcStr"></param>
/// <param name="msg"></param>
public void PushEpc(BaseDeviceInfo deviceInfo,bool isSuc, string epcStr,string msg)
{
}
}
}