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.

185 lines
8.1 KiB
C#

4 weeks ago
using HslCommunication.Enthernet;
using Microsoft.Extensions.DependencyInjection;
4 months ago
using Models;
2 years ago
using Serilog;
4 weeks ago
using SlnMesnac.Common;
2 years ago
using SlnMesnac.Config;
4 weeks ago
using SlnMesnac.Model.dto;
using SlnMesnac.Model.Enum;
2 years ago
using SlnMesnac.Rfid;
4 weeks ago
using SlnMesnac.Rfid.Enum;
2 years ago
using SlnMesnac.Rfid.Factory;
4 weeks ago
using SlnMesnac.TouchSocket;
4 months ago
using SqlSugar;
2 years ago
using System;
using System.Collections.Generic;
4 weeks ago
using System.Threading.Tasks;
2 years ago
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2024 WenJY
* CLR4.0.30319.42000
* LAPTOP-E0N2L34V
* SlnMesnac.Extensions
* 007aaf92-2adf-42a1-8b64-4e02925e3d5b
*
* WenJY
* wenjy@mesnac.com
* 2024-04-12 17:08:27
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.Extensions
{
public static class RfidFactorySetup
{
public static readonly Dictionary<string, Type> _rfidFactoryTypeMap = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase)
{
{ "RFly_I160", typeof(RflyFactory) }, // 原有的Rfly工厂
{ "Fuchs", typeof(FuchsFactory) }, // 新增的HF读卡器工厂
// 可扩展更多工厂类型
};
4 weeks ago
public static TcpServer _TcpServer;
private static StringChange _StringChange;
private static List<Base_sensor_info> sensor_Infos;
2 years ago
public static void AddRfidFactorySetup(this IServiceCollection services)
{
services.AddSingleton<List<RfidAbsractFactory>>(x =>
{
4 months ago
ISqlSugarClient sqlSugarClient = x.GetRequiredService<ISqlSugarClient>();
2 years ago
AppConfig appConfig = x.GetService<AppConfig>();
4 weeks ago
_StringChange = x.GetService<StringChange>();
_TcpServer = x.GetService<TcpServer>();
4 months ago
List<Base_device_info> baseRfidInfos = sqlSugarClient.Queryable<Base_device_info>().Where(x=>x.Collectid == appConfig.StationCode).ToList();
4 weeks ago
sensor_Infos = sqlSugarClient.Queryable<Base_sensor_info>().Where(x => x.Deleteflag == 0).ToList();
4 months ago
List<RfidAbsractFactory> absractFactories = new List<RfidAbsractFactory>();
2 years ago
try
{
4 months ago
foreach (var item in baseRfidInfos)
2 years ago
{
4 months ago
if (item.Deleteflag == 0)
2 years ago
{
// 3.2 校验工厂类型
if (string.IsNullOrEmpty(item.Devicetype))
{
Log.Error($"设备{item.Deviceid}的工厂类型无效");
continue;
}
// 3.3 动态获取对应工厂实例核心替换硬编码的RflyFactory
Type factoryType = _rfidFactoryTypeMap[item.Devicetype];
RfidAbsractFactory _rfid = x.GetService(factoryType) as RfidAbsractFactory;
if (_rfid == null)
{
Log.Error($"无法解析工厂实例:{factoryType.FullName}请确认已注册到DI容器");
continue;
}
4 months ago
int colonIndex = item.Connectstr.IndexOf(":");
string IP = colonIndex != -1 ? item.Connectstr.Substring(0, colonIndex) : item.Connectstr;
string Port = colonIndex != -1 ? item.Connectstr.Substring(colonIndex + 1) : item.Connectstr;
//RfidAbsractFactory _rfid = x.GetService<RflyFactory>();
_rfid.deviceid = item.Deviceid;
4 months ago
_rfid.ip = IP;
_rfid.port = int.Parse(Port);
_rfid.ConfigKey = sensor_Infos.Find(x => x.Deviceid == item.Deviceid).Combineid;
_rfid.FilterData = sensor_Infos.Find(x => x.Deviceid == item.Deviceid).Mapid;
//bool connectResult = _rfid.Connect(IP, int.Parse(Port));
4 weeks ago
//_rfid._Action += RecvIdentifyData_Instance;
4 months ago
bool connectResult = false;
if (connectResult)
2 years ago
{
4 months ago
//Log.Information($"RFID{item.Connectstr};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
//_rfid.ConfigKey = item.equipKey;
2 years ago
4 months ago
if (absractFactories.Contains(_rfid))
{
absractFactories.Remove(_rfid);
2 years ago
}
4 months ago
absractFactories.Add(_rfid);
}
else
{
//Log.Information($"RFID{item.Connectstr};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
if (absractFactories.Contains(_rfid))
2 years ago
{
4 months ago
absractFactories.Remove(_rfid);
2 years ago
}
4 months ago
absractFactories.Add(_rfid);
2 years ago
}
}
}
4 months ago
2 years ago
}
catch (Exception e)
{
Log.Error($"RFID初始化连接异常{e.Message}");
}
return absractFactories;
});
}
4 weeks ago
public static void RecvIdentifyData_Instance(string iCombineId, List<TagInfo> tagInfos)
{
try
{
SoftMessagePack pSendMessagePackInfo = new SoftMessagePack();
//iCombineId = "3114";
byte[] dataInfo; string info = "";
byte[] array2 = new byte[0];
if (tagInfos != null && tagInfos.Count > 0)
{
dataInfo = tagInfos[0].EPC;
info = tagInfos[0].EPCstring;
pSendMessagePackInfo.iLen = (ushort)(dataInfo.Length + 4 + 1); //+4为合并编号的长度 +1为长度占的一个字节
pSendMessagePackInfo.pMessage = new byte[pSendMessagePackInfo.iLen];
Array.Copy(dataInfo, 0, pSendMessagePackInfo.pMessage, 5, dataInfo.Length);
pSendMessagePackInfo.pMessage[4] = (byte)dataInfo.Length;
}
else
{
dataInfo = new byte[0];
info = "NoData";
pSendMessagePackInfo.iLen = 0 + 4;
pSendMessagePackInfo.pMessage = new byte[pSendMessagePackInfo.iLen];
}
byte[] u32byte = new byte[4];
pSendMessagePackInfo.iStationId = GetMESCallback(int.Parse(iCombineId));
pSendMessagePackInfo.iMsta = 0XF0 + 1;
pSendMessagePackInfo.iMessageType = (byte)MessageType.AutoReport_Type;
u32byte = BitConverter.GetBytes(uint.Parse(iCombineId));
Array.Copy(u32byte, 0, pSendMessagePackInfo.pMessage, 0, 4); //合并编号
_TcpServer.SendMessage(pSendMessagePackInfo);
}
catch (Exception e)
{
Log.Error($"RecvIdentifyData_Instance异常{e.Message}");
}
}
private static ushort GetMESCallback(int id)
{
var sensorInfo = sensor_Infos.Find((x) => { return x.Combineid == id.ToString(); });
if (sensorInfo == null)
{
return 1;
}
return (ushort)_StringChange.ParseToInt(sensorInfo.Mesid);
}
2 years ago
}
}