using System; using System.Collections.Generic; using Microsoft.Extensions.Logging; using SlnMesnac.Config; using SlnMesnac.Rfid.Factory; #region << 版 本 注 释 >> /*-------------------------------------------------------------------- * 版权所有 (c) 2024 WenJY 保留所有权利。 * CLR版本:4.0.30319.42000 * 机器名称:LAPTOP-E0N2L34V * 命名空间:SlnMesnac.Rfid * 唯一标识:496f8d2b-70e3-4a05-ae18-a9b0fcd06b82 * * 创建者:WenJY * 电子邮箱:wenjy@mesnac.com * 创建时间:2024-03-27 21:58:35 * 版本:V1.0.0 * 描述: * *-------------------------------------------------------------------- * 修改人: * 时间: * 修改说明: * * 版本:V1.0.0 *--------------------------------------------------------------------*/ #endregion << 版 本 注 释 >> namespace SlnMesnac.Rfid { public class RfidPool { private readonly ILogger _logger; private readonly RflyFactory _rflyFactory; private readonly AppConfig _appConfig; private Dictionary keyValuePairs = new Dictionary(); public RfidPool(RflyFactory rflyFactory,ILogger logger,AppConfig appConfig) { _rflyFactory = rflyFactory; _logger = logger; _appConfig = appConfig; } /// /// 初始化 /// public void Init() { if (_appConfig.rfidConfig != null) { foreach (var item in _appConfig.rfidConfig) { if (item.isFlage) { try { RfidAbsractFactory absractFactory = _rflyFactory; absractFactory.Connect(item.equipIp, item.equipPort); _logger.LogInformation($"RFID:{item.equipIp}:{item.equipPort};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); var key = item.equipKey; if (!keyValuePairs.ContainsKey(key)) { keyValuePairs.Add(key, absractFactory); } else { keyValuePairs[key] = absractFactory; } } catch (Exception e) { _logger.LogError($"RFID设备:{item.equipKey}初始化异常:{e.Message}"); } } } } else { _logger.LogInformation("PLC配置信息为空"); } } /// /// 获取RFID设备 /// /// /// public RfidAbsractFactory GetRfidByKey(string key) { try { return keyValuePairs[key]; } catch (Exception e) { throw new ArgumentException($"根据RFID Key获取连接信息异常:{e.Message}"); ; } } /// /// 获取所有PLC信息 /// /// public Dictionary GetAll() { return keyValuePairs; } } }