@ -1,14 +1,20 @@
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.Text ;
using System.Threading ;
using Microsoft.AspNetCore.Http ;
using Microsoft.AspNetCore.Http ;
using Microsoft.Extensions.Logging ;
using Models ;
using SlnMesnac.Common ;
using SlnMesnac.Model.domain ;
using SlnMesnac.Model.dto ;
using SlnMesnac.Repository.service ;
using SlnMesnac.Rfid.Dto ;
using SlnMesnac.Rfid.Enum ;
using SlnMesnac.TouchSocket ;
using System ;
using System.Collections ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading ;
using System.Threading.Tasks ;
using TouchSocket.Core ;
using TouchSocket.Sockets ;
@ -41,11 +47,15 @@ namespace SlnMesnac.Rfid.Factory
private ILogger < RflyFactory > _logger ;
private readonly TcpClient _tcpClient = new TcpClient ( ) ;
private readonly StringChange _stringChange ;
private string m_strIP ;
private string m_deviceID ;
private MeshttpClient _meshttpClient ;
public RflyFactory ( ILogger < RflyFactory > logger , StringChange stringChange )
public RflyFactory ( ILogger < RflyFactory > logger , StringChange stringChange , MeshttpClient meshttpClient )
{
_logger = logger ;
_stringChange = stringChange ;
_meshttpClient = meshttpClient ;
}
/// <summary>
@ -65,10 +75,62 @@ namespace SlnMesnac.Rfid.Factory
}
catch ( Exception e )
{
throw new InvalidOperationException ( $"设备连接异常:{e.Message}" ) ;
return false ;
//throw new InvalidOperationException($"设备连接异常:{e.Message}");
}
}
/// <summary>
/// 异步建立连接
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
public override async Task < bool > ConnectAsync ( string ip , int port , string deviceid )
{
try
{
m_deviceID = deviceid ;
m_strIP = ip ;
_tcpClient . Setup ( new TouchSocketConfig ( ) . SetRemoteIPHost ( $"{ip}:{port}" ) ) ;
await _tcpClient . ConnectAsync ( 1500 ) ;
_tcpClient . Received = ( client , e ) = >
{
//从客户端收到信息
var mes = Encoding . UTF8 . GetString ( e . ByteBlock . Buffer , 0 , e . ByteBlock . Len ) ; //注意: 数据长度是byteBlock.Len
byte [ ] receivedBuffer = new byte [ e . ByteBlock . Len ] ;
Array . Copy ( e . ByteBlock . Buffer , 0 , receivedBuffer , 0 , e . ByteBlock . Len ) ;
byte [ ] resultBuffer = PareReceiveBufferData ( receivedBuffer , receivedBuffer . Length ) ;
List < TagInfo > tagInfoList = Device_DealTagInfoList ( resultBuffer ) ;
string info = "" ;
if ( tagInfoList ! = null & & tagInfoList . Count > 0 )
{
tagInfoList = tagInfoList . Where ( ( x , i ) = > tagInfoList . FindIndex ( z = > z . EPCstring = = x . EPCstring ) = = i ) . ToList ( ) ;
info = string . Join ( "," , tagInfoList . Select ( item = > item . EPCstring ) ) ;
}
else
{
info = "Nodata" ;
}
MesParaData mesParaData = new MesParaData ( )
{
EQUID = deviceid ,
EPCID = info ,
READSTATUS = info = = "NoData" ? "1" : "0" ,
READTIME = DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" )
} ;
_meshttpClient . autoread ( mesParaData ) ;
//ReceivedClientBufferEvent?.Invoke(receivedBuffer);
return EasyTask . CompletedTask ;
} ;
return true ;
}
catch ( Exception e )
{
return false ;
}
}
/// <summary>
/// 按时间段盘点
/// </summary>
@ -119,6 +181,301 @@ namespace SlnMesnac.Rfid.Factory
throw new InvalidOperationException ( $"按时间段盘点异常:{e.Message}" ) ;
}
}
/// <summary>
/// 异步按时间段盘点
/// </summary>
/// <param name="timeout"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override async Task < List < TagInfo > > GetRFIDAsync ( int timeout = 1500 )
{
List < TagInfo > tagInfoList = new List < TagInfo > ( ) ;
byte [ ] u16byte = new byte [ 2 ] ;
byte [ ] bCRC = new byte [ 4 ] ;
try
{
#region 指令封装
MessagePack pMessagePack = new MessagePack ( ) ;
pMessagePack . m_pData = new byte [ 8 ] ;
pMessagePack . m_pData [ 0 ] = 0xAA ;
pMessagePack . m_pData [ 1 ] = 0x55 ;
pMessagePack . m_pData [ 2 ] = 0x02 ;
pMessagePack . m_pData [ 3 ] = 0x02 ;
//1s
pMessagePack . m_pData [ 4 ] = 0x03 ;
pMessagePack . m_pData [ 5 ] = 0xE8 ;
//2s
//pMessagePack.m_pData[4] = 0x07;
//pMessagePack.m_pData[5] = 0xD0;
//3s
//pMessagePack.m_pData[4] = 0x0B;
//pMessagePack.m_pData[5] = 0xA0;
Array . Copy ( pMessagePack . m_pData , 2 , bCRC , 0 , 4 ) ;
pMessagePack . m_pData [ 6 ] = _stringChange . CalculateVerify ( bCRC , bCRC . Length ) ;
pMessagePack . m_pData [ 7 ] = 0x0D ;
#endregion 指令封装
var waitClient = _tcpClient . CreateWaitingClient ( new WaitingOptions ( )
{
FilterFunc = response = >
{
// 检查响应数据是否符合预期
if ( response . Data ! = null & & response . Data . Length > 0 )
{
// 可以根据实际情况添加更多的检查逻辑
return true ;
}
return false ;
}
} ) ;
byte [ ] reciveBuffer = await waitClient . SendThenReturnAsync ( pMessagePack . m_pData , timeout ) ;
_logger . LogInformation ( $"{m_deviceID}发送读取指令{_stringChange.bytesToHexStr(pMessagePack.m_pData,pMessagePack.m_pData.Length)}" ) ;
byte [ ] resultBuffer = PareReceiveBufferData ( reciveBuffer , reciveBuffer . Length ) ;
_logger . LogInformation ( $"{m_deviceID}接收读取指令{_stringChange.bytesToHexStr(resultBuffer,resultBuffer.Length)}" ) ;
tagInfoList = Device_DealTagInfoList ( resultBuffer ) ;
return tagInfoList ;
}
catch ( Exception e )
{
return tagInfoList ;
//throw new InvalidOperationException($"{m_strIP}按时间段盘点异常:{e.Message}");
}
}
/// <summary>
/// 设置GPIO
/// </summary>
/// <param name="port"></param>
/// <param name="Timedout"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public override async Task < bool > WriteAlarmLight ( int port , UInt16 Timedout )
{
byte [ ] u16byte = new byte [ 2 ] ;
byte [ ] bCRC = new byte [ 6 ] ;
try
{
#region 指令封装
MessagePack pMessagePack = new MessagePack ( ) ;
pMessagePack . m_pData = new byte [ 10 ] ;
pMessagePack . m_pData [ 0 ] = 0xAA ;
pMessagePack . m_pData [ 1 ] = 0x55 ;
pMessagePack . m_pData [ 2 ] = 0x04 ;
pMessagePack . m_pData [ 3 ] = 0x51 ;
pMessagePack . m_pData [ 4 ] = ( byte ) port ;
pMessagePack . m_pData [ 5 ] = 0x00 ;
u16byte = BitConverter . GetBytes ( Timedout ) ; //超时时间
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 6 , 2 ) ;
Array . Copy ( pMessagePack . m_pData , 2 , bCRC , 0 , 6 ) ;
pMessagePack . m_pData [ 8 ] = _stringChange . CalculateVerify ( bCRC , bCRC . Length ) ;
pMessagePack . m_pData [ 9 ] = 0x0D ;
#endregion 指令封装
var waitClient = _tcpClient . CreateWaitingClient ( new WaitingOptions ( )
{
FilterFunc = response = >
{
// 检查响应数据是否符合预期
if ( response . Data ! = null & & response . Data . Length > 0 )
{
// 可以根据实际情况添加更多的检查逻辑
return true ;
}
return false ;
}
} ) ;
byte [ ] reciveBuffer = await waitClient . SendThenReturnAsync ( pMessagePack . m_pData , 2000 ) ;
//byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length);
if ( reciveBuffer [ 3 ] = = 0x51 )
{
return true ;
}
return false ;
}
catch ( Exception e )
{
throw new InvalidOperationException ( $"{m_strIP}设置GPIO异常: {e.Message}" ) ;
}
}
/// <summary>
/// 设置功率
/// </summary>
/// <param name="iDbi"></param>
/// <returns></returns>
public override async Task < bool > Set_Rf ( int iDbi )
{
byte [ ] u16byte = new byte [ 2 ] ;
byte [ ] bCRC = new byte [ 22 ] ;
try
{
#region 指令封装
iDbi = iDbi * 100 ;
MessagePack pMessagePack = new MessagePack ( ) ;
pMessagePack . m_pData = new byte [ 26 ] ;
pMessagePack . m_pData [ 0 ] = 0xAA ;
pMessagePack . m_pData [ 1 ] = 0x55 ;
pMessagePack . m_pData [ 2 ] = 0x14 ;
pMessagePack . m_pData [ 3 ] = 0x42 ;
pMessagePack . m_pData [ 4 ] = 0x01 ;
u16byte = BitConverter . GetBytes ( iDbi ) ; //功率
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 5 , 2 ) ;
u16byte = BitConverter . GetBytes ( iDbi ) ; //功率
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 7 , 2 ) ;
pMessagePack . m_pData [ 9 ] = 0x02 ;
u16byte = BitConverter . GetBytes ( iDbi ) ; //功率
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 10 , 2 ) ;
u16byte = BitConverter . GetBytes ( iDbi ) ; //功率
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 12 , 2 ) ;
pMessagePack . m_pData [ 14 ] = 0x03 ;
u16byte = BitConverter . GetBytes ( iDbi ) ; //功率
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 15 , 2 ) ;
u16byte = BitConverter . GetBytes ( iDbi ) ; //功率
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 17 , 2 ) ;
pMessagePack . m_pData [ 19 ] = 0x04 ;
u16byte = BitConverter . GetBytes ( iDbi ) ; //功率
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 20 , 2 ) ;
u16byte = BitConverter . GetBytes ( iDbi ) ; //功率
u16byte = _stringChange . Swap16Bytes ( u16byte ) ; //协议里为大端在前
Array . Copy ( u16byte , 0 , pMessagePack . m_pData , 22 , 2 ) ;
Array . Copy ( pMessagePack . m_pData , 2 , bCRC , 0 , 22 ) ;
pMessagePack . m_pData [ 24 ] = _stringChange . CalculateVerify ( bCRC , bCRC . Length ) ;
pMessagePack . m_pData [ 25 ] = 0x0D ;
#endregion 指令封装
var waitClient = _tcpClient . CreateWaitingClient ( new WaitingOptions ( )
{
FilterFunc = response = >
{
// 检查响应数据是否符合预期
if ( response . Data ! = null & & response . Data . Length > 0 )
{
// 可以根据实际情况添加更多的检查逻辑
return true ;
}
return false ;
}
} ) ;
_logger . LogInformation ( $"{m_deviceID}发送修改功率指令{_stringChange.bytesToHexStr(pMessagePack.m_pData, pMessagePack.m_pData.Length)}" ) ;
byte [ ] reciveBuffer = await waitClient . SendThenReturnAsync ( pMessagePack . m_pData , 2000 ) ;
_logger . LogInformation ( $"{m_deviceID}接收修改功率指令{_stringChange.bytesToHexStr(reciveBuffer, reciveBuffer.Length)}" ) ;
//byte[] resultBuffer = PareReceiveBufferData(reciveBuffer, reciveBuffer.Length);
if ( reciveBuffer [ 3 ] = = 0x42 )
{
return true ;
}
return false ;
}
catch ( Exception e )
{
return false ;
//throw new InvalidOperationException($"{m_strIP}设置功率异常:{e.Message}");
}
}
public override async Task < int > Get_Rf ( )
{
int DB = 0 ;
byte [ ] u16byte = new byte [ 2 ] ;
byte [ ] bCRC = new byte [ 2 ] ;
try
{
#region 指令封装
MessagePack pMessagePack = new MessagePack ( ) ;
pMessagePack . m_pData = new byte [ 6 ] ;
pMessagePack . m_pData [ 0 ] = 0xAA ;
pMessagePack . m_pData [ 1 ] = 0x55 ;
pMessagePack . m_pData [ 2 ] = 0x00 ;
pMessagePack . m_pData [ 3 ] = 0x72 ;
Array . Copy ( pMessagePack . m_pData , 2 , bCRC , 0 , 2 ) ;
pMessagePack . m_pData [ 4 ] = _stringChange . CalculateVerify ( bCRC , bCRC . Length ) ;
pMessagePack . m_pData [ 5 ] = 0x0D ;
#endregion 指令封装
var waitClient = _tcpClient . CreateWaitingClient ( new WaitingOptions ( )
{
FilterFunc = response = >
{
// 检查响应数据是否符合预期
if ( response . Data ! = null & & response . Data . Length > 0 )
{
// 可以根据实际情况添加更多的检查逻辑
return true ;
}
return false ;
}
} ) ;
byte [ ] reciveBuffer = await waitClient . SendThenReturnAsync ( pMessagePack . m_pData , 2000 ) ;
if ( reciveBuffer [ 3 ] = = 0x72 )
{
byte [ ] bDB = new byte [ 2 ] ;
bDB [ 0 ] = reciveBuffer [ 6 ] ;
bDB [ 1 ] = reciveBuffer [ 7 ] ;
DB = Convert . ToInt32 ( _stringChange . bytesToHexStr ( bDB , 2 ) , 16 ) / 100 ;
return DB ;
}
return DB ;
}
catch ( Exception e )
{
return DB ;
//throw new InvalidOperationException($"{m_strIP}设置功率异常:{e.Message}");
}
}
public override bool GetOnlineStatus ( )
{
try
{
return _tcpClient . Online ;
}
catch ( Exception e )
{
return false ;
}
}
public override void Close ( )
{
try
{
_tcpClient . SafeDispose ( ) ;
}
catch ( Exception ex )
{
throw ;
}
}
#region 标签解析
@ -188,6 +545,18 @@ namespace SlnMesnac.Rfid.Factory
m_iPosition + + ;
enumRecvState = RecvState . WaitingForStus_State ;
}
else if ( buffer [ i ] = = 0x42 )
{
m_szFullMessage [ m_iPosition ] = buffer [ i ] ;
m_iPosition + + ;
enumRecvState = RecvState . WaitingForStus_State ;
}
else if ( buffer [ i ] = = 0x72 )
{
m_szFullMessage [ m_iPosition ] = buffer [ i ] ;
m_iPosition + + ;
enumRecvState = RecvState . WaitingForStus_State ;
}
else if ( buffer [ i ] = = 0x90 ) // 如果是心跳BB DD 01 90 00 1F 8E 0D
{
m_szFullMessage [ m_iPosition ] = buffer [ i ] ;
@ -333,7 +702,7 @@ namespace SlnMesnac.Rfid.Factory
}
}
private Mutex mutauto = new Mutex ( ) ;
//private Mutex mutauto = new Mutex() ;
/// <summary>
/// 解析函数
/// </summary>
@ -348,7 +717,7 @@ namespace SlnMesnac.Rfid.Factory
int m_readEPCDataLen = 0 ;
try
{
mutauto . WaitOne ( ) ;
//mutauto.WaitOne() ;
int iFirstCountPos = 6 ; //第一次读取标签次数位置
int iFirstRSSIPos = 7 ; //第一次读取标签强度位置
int iFirstAnt = 8 ;
@ -358,7 +727,7 @@ namespace SlnMesnac.Rfid.Factory
int tempDataRSSI = 0 ;
UInt16 tempDataANT = 0 ;
int iBarcodeGroupCount = Convert . ToInt32 ( AutoDealReportData [ 5 ] . ToString ( ) ) ; //标签组数
int iBarcodeLength = 1 6 ; //标签长度
int iBarcodeLength = 1 2 ; //标签长度
int iCommonSecondFlag = 0 ;
for ( int j = 0 ; j < iBarcodeGroupCount ; j + + )
{
@ -367,8 +736,11 @@ namespace SlnMesnac.Rfid.Factory
Array . Clear ( tempPCByte , 0 , 2 ) ;
Array . Copy ( AutoDealReportData , iFirstPC , tempPCByte , 0 , 2 ) ;
int pc = Convert . ToInt32 ( tempPCByte [ 0 ] . ToString ( "X" ) ) ;
int epcLength = EPCLengthByPC ( pc ) ;
//int pc = Convert.ToInt32(tempPCByte[0].ToString("X"));
//PC转二进制取前五位转十进制
int epcLength = Convert . ToInt32 ( Convert . ToString ( Convert . ToInt64 ( tempPCByte [ 0 ] . ToString ( "X" ) , 16 ) , 2 ) . PadLeft ( 8 , '0' ) . Substring ( 0 , 5 ) , 2 ) * 2 ;
//int epcLength = EPCLengthByPC(pc);
iBarcodeLength = epcLength ;
byte [ ] tempDataByte = new byte [ epcLength ] ;
@ -396,15 +768,7 @@ namespace SlnMesnac.Rfid.Factory
tag . Count = tempDataCount ;
tag . RSSI = tempDataRSSI ;
tag . EPC = tempDataByte ;
if ( pc = = 24 )
{
tag . EPCstring = _stringChange . bytesToHexStr ( tempDataByte , tempDataByte . Length ) . Substring ( 0 , 7 ) ;
}
else
{
tag . EPCstring = Encoding . ASCII . GetString ( tempDataByte ) ;
}
tag . EPCstring = _stringChange . bytesToHexStr ( tempDataByte , tempDataByte . Length ) . Substring ( 0 , 8 ) ;
tag . PC = tempPCByte ;
tag . Antana = tempDataANT ;
@ -417,12 +781,12 @@ namespace SlnMesnac.Rfid.Factory
iFirstPC = iFirstAnt + 1 ;
iFirstLeftBarcketPos = iFirstLeftBarcketPos + iBarcodeLength + 5 ;
_logger . LogInformation ( "----函数调用:Device_DealTagInfoList 第[" + ( iCommonSecondFlag + 1 ) + "]次数据解析为:" + tag . EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]" ) ;
_logger . LogInformation ( $ "----{m_deviceID} 函数调用:Device_DealTagInfoList 第[" + ( iCommonSecondFlag + 1 ) + "]次数据解析为:" + tag . EPCstring + ",读取标签次数:[" + tempDataCount + "],标签信号强度:[" + tempDataRSSI + "],天线号:[" + tempDataANT + "]" ) ;
iCommonSecondFlag + + ;
if ( iCommonSecondFlag = = iBarcodeGroupCount )
{
mutauto . ReleaseMutex ( ) ;
_logger . LogInformation ( "《《《返回标签数据!" ) ;
//mutauto.ReleaseMutex() ;
//_logger.LogInformation("《《《返回标签数据!") ;
return tagInfoList ;
}
}
@ -430,7 +794,7 @@ namespace SlnMesnac.Rfid.Factory
}
catch ( Exception ex )
{
mutauto . ReleaseMutex ( ) ;
//mutauto.ReleaseMutex() ;
throw new InvalidOperationException ( $"Device_AutoDealContent 自动处理函数异常:{ex.Message}" ) ;
}
}
@ -478,6 +842,7 @@ namespace SlnMesnac.Rfid.Factory
return epcLength ;
}
# endregion
}
}