using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using SlnMesnac.Config; using SlnMesnac.Model.domain; using SlnMesnac.Model.dto; using SlnMesnac.Plc; using SlnMesnac.Repository.service; using SlnMesnac.Rfid; using System; using System.Collections.Generic; using System.Linq; using System.Text; #region << 版 本 注 释 >> /*-------------------------------------------------------------------- * 版权所有 (c) 2024 WenJY 保留所有权利。 * CLR版本:4.0.30319.42000 * 机器名称:LAPTOP-E0N2L34V * 命名空间:SlnMesnac.Business.base * 唯一标识:ed6470dd-80bf-4a9b-bfae-3c3a46af4c38 * * 创建者:WenJY * 电子邮箱:wenjy@mesnac.com * 创建时间:2024-04-08 17:18:05 * 版本:V1.0.0 * 描述: * *-------------------------------------------------------------------- * 修改人: * 时间: * 修改说明: * * 版本:V1.0.0 *--------------------------------------------------------------------*/ #endregion << 版 本 注 释 >> namespace SlnMesnac.Business.@base { /// /// 业务基类 /// public class BaseBusiness { public readonly ILogger _logger; /// /// 参数配置 /// public readonly AppConfig _appConfig; /// /// PLC工厂 /// public readonly List _plcFactories; /// /// RFID工厂 /// public readonly List _rfidFactories; /// /// 容器 /// private IServiceProvider _serviceProvider; #region 本地缓存信息 private List palletTasks; private List barCodeTasks; private List baseConfigInfos; #endregion /// /// 刷新日志信息 /// /// public delegate void PrintMessageToListBox(string msg); public event PrintMessageToListBox? PrintMessageToListBoxEvent; public BaseBusiness(ILogger logger, AppConfig appConfig, List plcFactories, List rfidFactories, IServiceProvider serviceProvider) { _logger = logger; _appConfig = appConfig; _plcFactories = plcFactories; _rfidFactories = rfidFactories; _serviceProvider = serviceProvider; using (var scope = _serviceProvider.CreateScope()) { baseConfigInfos = scope.ServiceProvider.GetRequiredService>(); palletTasks = scope.ServiceProvider.GetRequiredService>(); barCodeTasks = scope.ServiceProvider.GetRequiredService>(); } } /// /// 刷新日志 /// /// public void RefreshMessage(string msg) { _logger.LogInformation(msg); PrintMessageToListBoxEvent?.Invoke(msg); } /// /// 根据Key获取PLC连接信息 /// /// /// /// /// public PlcAbsractFactory GetPlcByKey(string key) { if (_plcFactories == null) { throw new ArgumentNullException($"根据Key获取PLC连接信息异常:PLC 连接信息为空"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("根据Key获取PLC连接信息异常:设备Key参数为空"); } try { var info = _plcFactories.Where(x => x.ConfigKey == key).FirstOrDefault(); return info; } catch (Exception ex) { throw new InvalidOperationException($"根据Key获取PLC连接信息异常:{ex.Message}"); } } /// /// 根据Key获取Rfid连接信息 /// /// /// /// /// public RfidAbsractFactory GetRfidByKey(string key) { if (_rfidFactories == null) { throw new ArgumentNullException($"根据Key获取RFID连接信息异常:PLC 连接信息为空"); } if (string.IsNullOrEmpty(key)) { throw new ArgumentNullException("根据Key获取RFID连接信息异常:设备Key参数为空"); } try { var info = _rfidFactories.Where(x => x.ConfigKey == key).FirstOrDefault(); return info; } catch (Exception ex) { throw new InvalidOperationException($"根据Key获取RFID连接信息异常:{ex.Message}"); } } /// /// 根据RFID Key读取RFID信息 /// /// /// /// public void ReadEpcStrByRfidKey(string rfidKey, out string epcStr) { try { var rfidEquip = GetRfidByKey(rfidKey); List tagInfoList = rfidEquip.TimePeriodRead(); epcStr = tagInfoList.OrderByDescending(x => x.Count).First().EPCstring; } catch (Exception ex) { throw new InvalidOperationException($"根据RFID Key读取RFID信息异常:{ex.Message}"); } } /// /// 通过托盘队列获取托盘信息 /// /// /// public string GetPalletInfoByTask() { try { if (palletTasks.Count == 0) { throw new ArgumentNullException("通过托盘队列获取托盘信息异常:托盘队列为空"); } return palletTasks.OrderBy(x=>x.RecordTime).First().PalletCode; } catch (Exception ex) { throw new InvalidOperationException($"通过托盘队列获取托盘信息异常:{ex.Message}"); } } /// /// 通过条码队列获取条码信息 /// /// /// public string GetBarCodeByTask() { try { if (barCodeTasks.Count == 0) { throw new ArgumentNullException("通过条码队列获取条码信息异常:托盘队列为空"); } return barCodeTasks.OrderBy(x => x.RecordTime).First().BarCode; } catch (Exception ex) { throw new InvalidOperationException($"通过条码队列获取条码信息异常:{ex.Message}"); } } /// /// 根据configKey获取PLC地址 /// /// /// /// public string GetPlcAddressByConfigKey(string configKey) { try { if (baseConfigInfos.Count == 0) { throw new ArgumentNullException("根据Key获取PLC地址异常:配置集合为空"); } return baseConfigInfos.Where(x => x.ConfigKey == configKey).First().ConfigValue; } catch (Exception ex) { throw new InvalidOperationException($"根据Key获取PLC地址异常:{ex.Message}"); } } } }