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.

125 lines
5.5 KiB
C#

using Dm.filter.log;
using Microsoft.Extensions.DependencyInjection;
2 months ago
using Models;
2 years ago
using Serilog;
using SlnMesnac.Config;
using SlnMesnac.Model.domain;
2 years ago
using SlnMesnac.Plc;
using SlnMesnac.Rfid;
using SlnMesnac.Rfid.Factory;
2 months ago
using SqlSugar;
2 years ago
using System;
using System.Collections.Generic;
using System.Reflection;
2 years ago
using System.Text;
#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读卡器工厂
// 可扩展更多工厂类型
};
2 years ago
public static void AddRfidFactorySetup(this IServiceCollection services)
{
services.AddSingleton<List<RfidAbsractFactory>>(x =>
{
2 months ago
ISqlSugarClient sqlSugarClient = x.GetRequiredService<ISqlSugarClient>();
2 years ago
AppConfig appConfig = x.GetService<AppConfig>();
2 months ago
List<Base_device_info> baseRfidInfos = sqlSugarClient.Queryable<Base_device_info>().Where(x=>x.Collectid == appConfig.StationCode).ToList();
List<Base_sensor_info> sensor_Infos = sqlSugarClient.Queryable<Base_sensor_info>().Where(x => x.Deleteflag == 0).ToList();
List<RfidAbsractFactory> absractFactories = new List<RfidAbsractFactory>();
2 years ago
try
{
2 months ago
foreach (var item in baseRfidInfos)
2 years ago
{
2 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;
}
2 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;
2 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));
bool connectResult = false;
if (connectResult)
2 years ago
{
2 months ago
//Log.Information($"RFID{item.Connectstr};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
//_rfid.ConfigKey = item.equipKey;
2 years ago
2 months ago
if (absractFactories.Contains(_rfid))
{
absractFactories.Remove(_rfid);
2 years ago
}
2 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
{
2 months ago
absractFactories.Remove(_rfid);
2 years ago
}
2 months ago
absractFactories.Add(_rfid);
2 years ago
}
}
}
2 months ago
2 years ago
}
catch (Exception e)
{
Log.Error($"RFID初始化连接异常{e.Message}");
}
return absractFactories;
});
}
2 years ago
}
}