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.

88 lines
3.1 KiB
C#

2 months ago
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using SlnMesnac.Config;
using SlnMesnac.Plc;
using SlnMesnac.Rfid;
using SlnMesnac.Rfid.Factory;
using System;
using System.Collections.Generic;
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 void AddRfidFactorySetup(this IServiceCollection services)
{
services.AddSingleton<List<RfidAbsractFactory>>(x =>
{
AppConfig appConfig = x.GetService<AppConfig>();
List <RfidAbsractFactory> absractFactories = new List<RfidAbsractFactory>();
try
{
if (appConfig.rfidConfig != null)
{
foreach (var item in appConfig.rfidConfig)
{
if (item.isFlage)
{
RfidAbsractFactory _rfid = x.GetService<RflyFactory>();
bool connectResult = _rfid.Connect(item.equipIp, item.equipPort);
if (connectResult)
{
Log.Information($"RFID{item.equipIp}:{item.equipPort};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
_rfid.ConfigKey = item.equipKey;
if (absractFactories.Contains(_rfid))
{
absractFactories.Remove(_rfid);
}
absractFactories.Add(_rfid);
}
else
{
Log.Information($"RFID{item.equipIp}:{item.equipPort};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
}
}
}
}
else
{
Log.Error("RFID配置信息为空");
}
}
catch (Exception e)
{
Log.Error($"RFID初始化连接异常{e.Message}");
}
return absractFactories;
});
}
}
}