From ba5a283dad335167a2d51d10a89140bfd29cd344 Mon Sep 17 00:00:00 2001 From: wenjy Date: Fri, 12 Apr 2024 17:23:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?change=20-=20=E4=BF=AE=E5=A4=8DPLC=E3=80=81?= =?UTF-8?q?RFID=E5=A4=9A=E8=AE=BE=E5=A4=87=E8=BF=9E=E6=8E=A5=E6=9C=AA?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=97=AE=E9=A2=98=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=B8=AD=E9=97=B4=E4=BB=B6=E3=80=81Redis=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Config/AppConfig.cs | 8 +- SlnMesnac.Extensions/PlcFactorySetup.cs | 118 +++++++++++ SlnMesnac.Extensions/RfidFactorySetup.cs | 87 ++++++++ .../SlnMesnac.Extensions.csproj | 15 ++ SlnMesnac.Ioc/DependencyConfigurator.cs | 22 ++- SlnMesnac.Plc/PlcAbsractFactory.cs | 3 + SlnMesnac.Plc/PlcPool.cs | 187 ------------------ SlnMesnac.Plc/PlcSetup.cs | 44 ----- SlnMesnac.Redis/RedisHandler.cs | 92 +++++++++ SlnMesnac.Redis/SlnMesnac.Redis.csproj | 16 ++ SlnMesnac.Rfid/RfidAbsractFactory.cs | 3 + SlnMesnac.Rfid/RfidPool.cs | 112 ----------- SlnMesnac.Rfid/RfidSetup.cs | 38 ---- SlnMesnac.WPF/App.xaml.cs | 6 +- SlnMesnac.WPF/SlnMesnac.WPF.csproj | 1 + SlnMesnac.WPF/Startup.cs | 15 +- SlnMesnac.WPF/appsettings.json | 19 +- SlnMesnac.sln | 14 +- 18 files changed, 398 insertions(+), 402 deletions(-) create mode 100644 SlnMesnac.Extensions/PlcFactorySetup.cs create mode 100644 SlnMesnac.Extensions/RfidFactorySetup.cs create mode 100644 SlnMesnac.Extensions/SlnMesnac.Extensions.csproj delete mode 100644 SlnMesnac.Plc/PlcPool.cs delete mode 100644 SlnMesnac.Plc/PlcSetup.cs create mode 100644 SlnMesnac.Redis/RedisHandler.cs create mode 100644 SlnMesnac.Redis/SlnMesnac.Redis.csproj delete mode 100644 SlnMesnac.Rfid/RfidPool.cs delete mode 100644 SlnMesnac.Rfid/RfidSetup.cs diff --git a/SlnMesnac.Config/AppConfig.cs b/SlnMesnac.Config/AppConfig.cs index 64f58ea..1cfc66e 100644 --- a/SlnMesnac.Config/AppConfig.cs +++ b/SlnMesnac.Config/AppConfig.cs @@ -51,7 +51,13 @@ namespace SlnMesnac.Config /// RFID连接配置 /// public List rfidConfig { get; set; } - + + + /// + /// Redis配置 + /// + public string redisConfig { get; set; } + public AppConfig Value => this; } } diff --git a/SlnMesnac.Extensions/PlcFactorySetup.cs b/SlnMesnac.Extensions/PlcFactorySetup.cs new file mode 100644 index 0000000..383e7e1 --- /dev/null +++ b/SlnMesnac.Extensions/PlcFactorySetup.cs @@ -0,0 +1,118 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Serilog; +using SlnMesnac.Config; +using SlnMesnac.Plc; +using SlnMesnac.Plc.Factory; +using System; +using System.Collections.Generic; +using System.Text; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:SlnMesnac.Extensions +* 唯一标识:9bf604b4-3937-476a-adb0-27adc6fbea28 +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-12 15:25:47 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Extensions +{ + /// + /// PLC工厂 + /// + public static class PlcFactorySetup + { + + public static void AddPlcFactorySetup(this IServiceCollection services) + { + services.AddSingleton>(x => + { + AppConfig appConfig = x.GetService(); + + List absractFactories = new List(); + + try + { + if (appConfig.plcConfig != null) + { + foreach (var item in appConfig.plcConfig) + { + if (item.isFlage) + { + PlcAbsractFactory _plc = InitPlc(x, item.plcType); + + var connectResult = _plc.Connect(item.plcIp, item.plcPort); + if (connectResult) + { + Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); + _plc.ConfigKey = item.plcKey; + + if(absractFactories.Contains(_plc)) + { + absractFactories.Remove(_plc); + } + + absractFactories.Add(_plc); + } + else + { + Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); + } + } + } + } + } + catch (Exception e) + { + Log.Error($"PLC初始化连接异常:{e.Message}"); + } + + return absractFactories; + }); + } + + private static PlcAbsractFactory InitPlc(IServiceProvider serviceProvider,string plcType) + { + PlcAbsractFactory _plc = null; + var _inovance = serviceProvider.GetRequiredService(); + var _melsecBinary = serviceProvider.GetRequiredService(); + var _omronNj = serviceProvider.GetRequiredService(); + var _siemens = serviceProvider.GetRequiredService(); + + switch (plcType) + { + case "InovancePlc": + _plc = _inovance; + break; + case "MelsecBinaryPlc": + _plc = _melsecBinary; + break; + case "OmronNJPlc": + _plc = _omronNj; + break; + case "SiemensPlc": + _plc = _siemens; + break; + default: + break; + } + + return _plc; + } + } +} diff --git a/SlnMesnac.Extensions/RfidFactorySetup.cs b/SlnMesnac.Extensions/RfidFactorySetup.cs new file mode 100644 index 0000000..ac5e29f --- /dev/null +++ b/SlnMesnac.Extensions/RfidFactorySetup.cs @@ -0,0 +1,87 @@ +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 保留所有权利。 +* CLR版本:4.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>(x => + { + AppConfig appConfig = x.GetService(); + List absractFactories = new List(); + + try + { + if (appConfig.rfidConfig != null) + { + foreach (var item in appConfig.rfidConfig) + { + if (item.isFlage) + { + RfidAbsractFactory _rfid = x.GetService(); + 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; + }); + } + } +} diff --git a/SlnMesnac.Extensions/SlnMesnac.Extensions.csproj b/SlnMesnac.Extensions/SlnMesnac.Extensions.csproj new file mode 100644 index 0000000..cd6c9bd --- /dev/null +++ b/SlnMesnac.Extensions/SlnMesnac.Extensions.csproj @@ -0,0 +1,15 @@ + + + + netstandard2.1 + enable + + + + + + + + + + diff --git a/SlnMesnac.Ioc/DependencyConfigurator.cs b/SlnMesnac.Ioc/DependencyConfigurator.cs index 77f1be8..111428f 100644 --- a/SlnMesnac.Ioc/DependencyConfigurator.cs +++ b/SlnMesnac.Ioc/DependencyConfigurator.cs @@ -43,10 +43,10 @@ namespace SlnMesnac.Ioc RegisterImplementations(builder, Assembly.LoadFrom("SlnMesnac.Repository.dll")); //注入Plc - RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Plc.dll")); + RegisterTypeTransient(builder, Assembly.LoadFrom("SlnMesnac.Plc.dll")); //注入Rfid - RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Rfid.dll")); + RegisterTypeTransient(builder, Assembly.LoadFrom("SlnMesnac.Rfid.dll")); //注入通用类 RegisterType(builder, Assembly.LoadFrom("SlnMesnac.Common.dll")); @@ -119,5 +119,23 @@ namespace SlnMesnac.Ioc } } } + + private static void RegisterTypeTransient(ContainerBuilder builder, Assembly assembly) + { + var types = assembly.GetTypes() + .Where(t => t.IsClass && !t.IsAbstract && !t.IsGenericType) + .ToList(); + + foreach (var type in types) + { + var interfaces = type.GetInterfaces(); + var baseType = type.BaseType; + + if (!typeof(Delegate).IsAssignableFrom(type)) + { + builder.RegisterType(type); + } + } + } } } diff --git a/SlnMesnac.Plc/PlcAbsractFactory.cs b/SlnMesnac.Plc/PlcAbsractFactory.cs index d309ec6..049cf0a 100644 --- a/SlnMesnac.Plc/PlcAbsractFactory.cs +++ b/SlnMesnac.Plc/PlcAbsractFactory.cs @@ -24,6 +24,9 @@ namespace SlnMesnac.Plc { public abstract class PlcAbsractFactory { + + public string ConfigKey { get;set; } + /// /// 是否连接 /// diff --git a/SlnMesnac.Plc/PlcPool.cs b/SlnMesnac.Plc/PlcPool.cs deleted file mode 100644 index cf332c7..0000000 --- a/SlnMesnac.Plc/PlcPool.cs +++ /dev/null @@ -1,187 +0,0 @@ -using Microsoft.Extensions.Logging; -using SlnMesnac.Config; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using SlnMesnac.Plc.Factory; - -#region << 版 本 注 释 >> -/*-------------------------------------------------------------------- -* 版权所有 (c) 2024 WenJY 保留所有权利。 -* CLR版本:4.0.30319.42000 -* 机器名称:LAPTOP-E0N2L34V -* 命名空间:SlnMesnac.Plc -* 唯一标识:496f8d2b-70e3-4a05-ae18-a9b0fcd06b82 -* -* 创建者:WenJY -* 电子邮箱:wenjy@mesnac.com -* 创建时间:2024-03-27 21:58:35 -* 版本:V1.0.0 -* 描述: -* -*-------------------------------------------------------------------- -* 修改人: -* 时间: -* 修改说明: -* -* 版本:V1.0.0 -*--------------------------------------------------------------------*/ -#endregion << 版 本 注 释 >> -namespace SlnMesnac.Plc -{ - /// - /// PLC连接池 - /// - public class PlcPool - { - private ILogger _logger; - private readonly AppConfig _appConfig; - - private Dictionary keyValuePairs ; - - private readonly InovanceFactory _inovance; - private readonly MelsecBinaryFactory _melsecBinary; - private readonly OmronNJFactory _omronNj; - private readonly SiemensFactory _siemens; - - public PlcPool(ILogger logger, InovanceFactory inovance,MelsecBinaryFactory melsecBinary,OmronNJFactory omronNj,SiemensFactory siemens, AppConfig appConfig) - { - _logger = logger; - _inovance = inovance; - _melsecBinary = melsecBinary; - _omronNj = omronNj; - _siemens = siemens; - _appConfig = appConfig; - - keyValuePairs = new Dictionary(); - } - public void Init() - { - if (!HslCommunication.Authorization.SetAuthorizationCode("1839541f-8fb4-42c4-a13f-733b027fe5af")) - { - _logger.LogInformation("HslCommunication激活失败,可用时长24小时"); - } - _logger.LogInformation("HslCommunication 11.8.1.0激活成功"); - - if (_appConfig.plcConfig != null) - { - foreach (var item in _appConfig.plcConfig) - { - if (item.isFlage) - { - AddPlc(item.plcType, item.plcIp, item.plcPort, item.plcKey); - } - } - } - else - { - _logger.LogInformation("PLC配置信息为空"); - } - } - - /// - /// 添加PLC连接 - /// - /// - /// - /// - /// - public void AddPlc(string plcType, string ip, int port, string key) - { - try - { - PlcAbsractFactory _plc = null; - switch (plcType) - { - case "InovancePlc": - _plc = _inovance; - break; - case "MelsecBinaryPlc": - _plc = _melsecBinary; - break; - case "OmronNJPlc": - _plc = _omronNj; - break; - case "SiemensPlc": - _plc = _siemens; - break; - default: - break; - } - var connectResult = _plc.Connect(ip, port); - if (connectResult) - { - _logger.LogInformation($"PLC:{ip}:{port};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); - if (!keyValuePairs.ContainsKey(key)) - { - keyValuePairs.Add(key, _plc); - } - else - { - keyValuePairs[key] = _plc; - } - } - else - { - _logger.LogInformation($"PLC:{ip}:{port};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); - } - } - catch (Exception e) - { - _logger.LogError($"PLC初始化连接异常:{e.Message}"); - } - } - - /// - /// 获取PLC - /// - /// - /// - public PlcAbsractFactory GetPlcByKey(string key) - { - try - { - return keyValuePairs[key]; - } - catch (Exception e) - { - throw new ArgumentException($"根据PLC Key获取连接信息异常:{e.Message}"); ; - } - } - - /// - /// 获取所有PLC信息 - /// - /// - public Dictionary GetAll() - { - return keyValuePairs; - } - - /// - /// 关闭所有连接 - /// - /// - public bool disConnectAll() - { - bool result = false; - try - { - foreach (var kv in keyValuePairs) - { - if (kv.Value != null) - { - kv.Value.DisConnect(); - } - } - result = true; - } - catch (Exception ex) - { - _logger.LogError("关闭PLC连接异常", ex); - } - - return result; - } - } -} diff --git a/SlnMesnac.Plc/PlcSetup.cs b/SlnMesnac.Plc/PlcSetup.cs deleted file mode 100644 index 95256f5..0000000 --- a/SlnMesnac.Plc/PlcSetup.cs +++ /dev/null @@ -1,44 +0,0 @@ -using System.Linq; -using System.Reflection; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -using SlnMesnac.Plc.Factory; - -#region << 版 本 注 释 >> -/*-------------------------------------------------------------------- -* 版权所有 (c) 2024 WenJY 保留所有权利。 -* CLR版本:4.0.30319.42000 -* 机器名称:LAPTOP-E0N2L34V -* 命名空间:SlnMesnac.Plc -* 唯一标识:496f8d2b-70e3-4a05-ae18-a9b0fcd06b82 -* -* 创建者:WenJY -* 电子邮箱:wenjy@mesnac.com -* 创建时间:2024-03-27 21:58:35 -* 版本:V1.0.0 -* 描述: -* -*-------------------------------------------------------------------- -* 修改人: -* 时间: -* 修改说明: -* -* 版本:V1.0.0 -*--------------------------------------------------------------------*/ -#endregion << 版 本 注 释 >> -namespace SlnMesnac.Plc -{ - public static class PlcSetup - { - public static IApplicationBuilder UsePlcExtensions(this IApplicationBuilder app) - { - PlcPool? plcPool = app.ApplicationServices.GetService(); - if(plcPool != null) - { - plcPool.Init(); - } - return app; - } - - } -} diff --git a/SlnMesnac.Redis/RedisHandler.cs b/SlnMesnac.Redis/RedisHandler.cs new file mode 100644 index 0000000..4b0331c --- /dev/null +++ b/SlnMesnac.Redis/RedisHandler.cs @@ -0,0 +1,92 @@ +using Microsoft.Extensions.Logging; +using SlnMesnac.Config; +using StackExchange.Redis; +using System; +using System.Collections.Generic; +using System.Text; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:LAPTOP-E0N2L34V +* 命名空间:SlnMesnac.Redis +* 唯一标识:00418016-53c9-4f87-a13f-daa19d656bba +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-12 15:15:25 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Redis +{ + public class RedisHandler + { + private ILogger _logger; + + private readonly AppConfig _appConfig; + + private readonly ISubscriber _subscriber; + + private readonly ConnectionMultiplexer redis; + + public RedisHandler(AppConfig appConfig, ILogger logger) + { + _appConfig = appConfig; + + redis = ConnectionMultiplexer.Connect(_appConfig.redisConfig); + _subscriber = redis.GetSubscriber(); + _logger = logger; + } + + + + /// + /// 推送消息 + /// + /// + /// + public void PublishMessage(string channel, string message) + { + long res = _subscriber.Publish(channel, message); + + _logger.LogInformation($"向主题:{channel};推送消息:{message};结果:{res}"); + } + + /// + /// 订阅消息 + /// + /// + /// + public void SubscribeToChannel(string channel, Action onMessageReceived) + { + _subscriber.Subscribe(channel, (ch, message) => + { + onMessageReceived(ch, message); + _logger.LogInformation($"订阅主题:{channel};收到主题:{ch};推送的消息:{message}"); + }); + } + + public void CleanExpiredMessages(string channel) + { + var redis = _subscriber.Multiplexer.GetDatabase(); + redis.KeyDelete(channel); + } + + public void SetValue(string key, string message) + { + IDatabase db = redis.GetDatabase(0); + TimeSpan expiry = TimeSpan.FromSeconds(10); + db.StringSet(key, message, expiry); + } + } +} diff --git a/SlnMesnac.Redis/SlnMesnac.Redis.csproj b/SlnMesnac.Redis/SlnMesnac.Redis.csproj new file mode 100644 index 0000000..96001b0 --- /dev/null +++ b/SlnMesnac.Redis/SlnMesnac.Redis.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.1 + enable + + + + + + + + + + + diff --git a/SlnMesnac.Rfid/RfidAbsractFactory.cs b/SlnMesnac.Rfid/RfidAbsractFactory.cs index bc4e1e9..2a2c1b7 100644 --- a/SlnMesnac.Rfid/RfidAbsractFactory.cs +++ b/SlnMesnac.Rfid/RfidAbsractFactory.cs @@ -31,6 +31,9 @@ namespace SlnMesnac.Rfid /// public abstract class RfidAbsractFactory { + + public string ConfigKey { get; set; } + /// /// 建立连接 /// diff --git a/SlnMesnac.Rfid/RfidPool.cs b/SlnMesnac.Rfid/RfidPool.cs deleted file mode 100644 index b2cdabd..0000000 --- a/SlnMesnac.Rfid/RfidPool.cs +++ /dev/null @@ -1,112 +0,0 @@ -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; - } - } -} \ No newline at end of file diff --git a/SlnMesnac.Rfid/RfidSetup.cs b/SlnMesnac.Rfid/RfidSetup.cs deleted file mode 100644 index 5fe4e99..0000000 --- a/SlnMesnac.Rfid/RfidSetup.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -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 static class RfidSetup - { - public static IApplicationBuilder UseRfidExtensions(this IApplicationBuilder app) - { - var pool = app.ApplicationServices.GetService(); - pool.Init(); - return app; - } - } -} \ No newline at end of file diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index 0c0ad04..ea819ab 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -7,6 +7,9 @@ using SlnMesnac.Config; using System; using System.Windows; using Autofac.Extensions.DependencyInjection; +using SlnMesnac.Plc; +using System.Collections.Generic; +using SlnMesnac.Rfid; namespace SlnMesnac.WPF { @@ -37,11 +40,12 @@ namespace SlnMesnac.WPF ServiceProvider = host.Services; + await host.StartAsync(); + var appConfig = host.Services.GetService(); var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; Log.Information($"系统初始化完成,日志存放路径:{appConfig.logPath}"); - await host.StartAsync(); } /// diff --git a/SlnMesnac.WPF/SlnMesnac.WPF.csproj b/SlnMesnac.WPF/SlnMesnac.WPF.csproj index 731da9b..13b1f23 100644 --- a/SlnMesnac.WPF/SlnMesnac.WPF.csproj +++ b/SlnMesnac.WPF/SlnMesnac.WPF.csproj @@ -22,6 +22,7 @@ + diff --git a/SlnMesnac.WPF/Startup.cs b/SlnMesnac.WPF/Startup.cs index 047ebe8..f65ebe7 100644 --- a/SlnMesnac.WPF/Startup.cs +++ b/SlnMesnac.WPF/Startup.cs @@ -4,13 +4,14 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SlnMesnac.Config; using SlnMesnac.Repository; -using SlnMesnac.Plc; using SlnMesnac.Serilog; using System; using Autofac; using Microsoft.Extensions.Configuration; using SlnMesnac.Rfid; using SlnMesnac.Ioc; +using SlnMesnac.Plc; +using SlnMesnac.Extensions; namespace SlnMesnac.WPF { @@ -38,6 +39,12 @@ namespace SlnMesnac.WPF //注册ORM services.AddSqlSugarSetup(); + + //注册PLC工厂 + services.AddPlcFactorySetup(); + + //注册RFID工厂 + services.AddRfidFactorySetup(); } /// @@ -64,12 +71,6 @@ namespace SlnMesnac.WPF //启用Serilog中间件 app.UseSerilogExtensions(); - //初始化PLC中间件 - app.UsePlcExtensions(); - - //初始化RFID中间件 - app.UseRfidExtensions(); - app.UseRouting(); app.UseAuthorization(); diff --git a/SlnMesnac.WPF/appsettings.json b/SlnMesnac.WPF/appsettings.json index f9fd88f..9346894 100644 --- a/SlnMesnac.WPF/appsettings.json +++ b/SlnMesnac.WPF/appsettings.json @@ -25,35 +25,36 @@ { "configId": 1, "plcType": "MelsecBinaryPlc", - "plcIp": "127.0.0.1", + "plcIp": "127.0.0.1", "plcPort": 6000, "plcKey": "mcs", - "isFlage": false + "isFlage": true }, { "configId": 2, "plcType": "MelsecBinaryPlc", "plcIp": "127.0.0.1", - "plcPort": 6000, + "plcPort": 6001, "plcKey": "cwss", - "isFlage": false + "isFlage": true } ], "RfidConfig": [ { "configId": 1, "equipIp": "127.0.0.1", - "equipPort": 6000, + "equipPort": 6003, "equipKey": "test1", - "isFlage": false + "isFlage": true }, { "configId": 2, "equipIp": "127.0.0.1", - "equipPort": 6009, + "equipPort": 6004, "equipKey": "test2", - "isFlage": false + "isFlage": true } - ] + ], + "redisConfig": "175.27.215.92:6379,password=redis@2023" } } diff --git a/SlnMesnac.sln b/SlnMesnac.sln index e80dbbe..b713cfb 100644 --- a/SlnMesnac.sln +++ b/SlnMesnac.sln @@ -31,7 +31,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Rfid", "SlnMesnac EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Ioc", "SlnMesnac.Ioc\SlnMesnac.Ioc.csproj", "{30A3F86B-774E-4153-9A00-FD3173C710EB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{00FC9358-2381-4C1B-BD45-6D31DD1DB7D3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SlnMesnac.Generate", "SlnMesnac.Generate\SlnMesnac.Generate.csproj", "{00FC9358-2381-4C1B-BD45-6D31DD1DB7D3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnMesnac.Extensions", "SlnMesnac.Extensions\SlnMesnac.Extensions.csproj", "{6D929802-24AA-42A7-92C5-303C3D59A990}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SlnMesnac.Redis", "SlnMesnac.Redis\SlnMesnac.Redis.csproj", "{0E041719-E755-43CD-8A0E-DF62E0B2E463}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -99,6 +103,14 @@ Global {00FC9358-2381-4C1B-BD45-6D31DD1DB7D3}.Debug|Any CPU.Build.0 = Debug|Any CPU {00FC9358-2381-4C1B-BD45-6D31DD1DB7D3}.Release|Any CPU.ActiveCfg = Release|Any CPU {00FC9358-2381-4C1B-BD45-6D31DD1DB7D3}.Release|Any CPU.Build.0 = Release|Any CPU + {6D929802-24AA-42A7-92C5-303C3D59A990}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D929802-24AA-42A7-92C5-303C3D59A990}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D929802-24AA-42A7-92C5-303C3D59A990}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D929802-24AA-42A7-92C5-303C3D59A990}.Release|Any CPU.Build.0 = Release|Any CPU + {0E041719-E755-43CD-8A0E-DF62E0B2E463}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0E041719-E755-43CD-8A0E-DF62E0B2E463}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0E041719-E755-43CD-8A0E-DF62E0B2E463}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0E041719-E755-43CD-8A0E-DF62E0B2E463}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From ebe296acf7f3e38a70fb1aa21dd461d2b04e6884 Mon Sep 17 00:00:00 2001 From: wenjy Date: Fri, 12 Apr 2024 17:35:54 +0800 Subject: [PATCH 2/3] =?UTF-8?q?change=20-=20=E8=AE=BE=E7=BD=AEPLC=E3=80=81?= =?UTF-8?q?RFID=E5=B7=A5=E5=8E=82=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= =?UTF-8?q?=E4=B8=BA=E6=AF=8F=E6=AC=A1=E8=A7=A3=E6=9E=90=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=96=B0=E5=AE=9E=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SqlsugarSetup.cs | 10 ++++------ SlnMesnac.Ioc/DependencyConfigurator.cs | 9 +++++++-- SlnMesnac/SlnMesnac.csproj | 1 + SlnMesnac/Startup.cs | 4 +--- 4 files changed, 13 insertions(+), 11 deletions(-) rename {SlnMesnac.Repository => SlnMesnac.Extensions}/SqlsugarSetup.cs (89%) diff --git a/SlnMesnac.Repository/SqlsugarSetup.cs b/SlnMesnac.Extensions/SqlsugarSetup.cs similarity index 89% rename from SlnMesnac.Repository/SqlsugarSetup.cs rename to SlnMesnac.Extensions/SqlsugarSetup.cs index 3f3557b..8761bea 100644 --- a/SlnMesnac.Repository/SqlsugarSetup.cs +++ b/SlnMesnac.Extensions/SqlsugarSetup.cs @@ -1,7 +1,5 @@ using Microsoft.Extensions.DependencyInjection; using SlnMesnac.Config; -using SlnMesnac.Repository.service; -using SlnMesnac.Repository.service.Impl; using SqlSugar; using System; using System.Collections.Generic; @@ -12,12 +10,12 @@ using System.Text; * 版权所有 (c) 2024 WenJY 保留所有权利。 * CLR版本:4.0.30319.42000 * 机器名称:LAPTOP-E0N2L34V -* 命名空间:SlnMesnac.Repository -* 唯一标识:496f8d2b-70e3-4a05-ae18-a9b0fcd06b82 +* 命名空间:SlnMesnac.Extensions +* 唯一标识:bbb46406-e99d-4205-8046-ad954cf88315 * * 创建者:WenJY * 电子邮箱:wenjy@mesnac.com -* 创建时间:2024-03-27 21:58:35 +* 创建时间:2024-04-12 17:31:43 * 版本:V1.0.0 * 描述: * @@ -29,7 +27,7 @@ using System.Text; * 版本:V1.0.0 *--------------------------------------------------------------------*/ #endregion << 版 本 注 释 >> -namespace SlnMesnac.Repository +namespace SlnMesnac.Extensions { public static class SqlsugarSetup { diff --git a/SlnMesnac.Ioc/DependencyConfigurator.cs b/SlnMesnac.Ioc/DependencyConfigurator.cs index 111428f..6234bb9 100644 --- a/SlnMesnac.Ioc/DependencyConfigurator.cs +++ b/SlnMesnac.Ioc/DependencyConfigurator.cs @@ -90,7 +90,7 @@ namespace SlnMesnac.Ioc } /// - /// 自动注入自定义类、抽象类 + /// 自动注入自定义类、抽象类,设置为单例 /// /// /// @@ -120,6 +120,11 @@ namespace SlnMesnac.Ioc } } + /// + /// 自动注入自定义类,设置生命周期为每次解析返回新实例 + /// + /// + /// private static void RegisterTypeTransient(ContainerBuilder builder, Assembly assembly) { var types = assembly.GetTypes() @@ -133,7 +138,7 @@ namespace SlnMesnac.Ioc if (!typeof(Delegate).IsAssignableFrom(type)) { - builder.RegisterType(type); + builder.RegisterType(type).AsSelf().InstancePerDependency(); } } } diff --git a/SlnMesnac/SlnMesnac.csproj b/SlnMesnac/SlnMesnac.csproj index f623be7..191e4dc 100644 --- a/SlnMesnac/SlnMesnac.csproj +++ b/SlnMesnac/SlnMesnac.csproj @@ -13,6 +13,7 @@ + diff --git a/SlnMesnac/Startup.cs b/SlnMesnac/Startup.cs index 971cb8d..14525f4 100644 --- a/SlnMesnac/Startup.cs +++ b/SlnMesnac/Startup.cs @@ -1,10 +1,8 @@ using Microsoft.OpenApi.Models; -using SlnMesnac.Common; using SlnMesnac.Config; -using SlnMesnac.Plc; using SlnMesnac.Quartz; -using SlnMesnac.Repository; using SlnMesnac.Serilog; +using SlnMesnac.Extensions; using System.Runtime.Serialization; namespace SlnMesnac From 0164b9529d198476605a1fc257cf716b5a9dd5ad Mon Sep 17 00:00:00 2001 From: wenjy Date: Fri, 12 Apr 2024 17:43:12 +0800 Subject: [PATCH 3/3] =?UTF-8?q?change=20-=20=E6=B7=BB=E5=8A=A0=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1=E5=9F=BA=E7=B1=BB=E8=8E=B7=E5=8F=96RFID=E3=80=81PLC?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Business/Class1.cs | 9 -- SlnMesnac.Business/SlnMesnac.Business.csproj | 5 + SlnMesnac.Business/base/BaseBusiness.cs | 106 +++++++++++++++++++ 3 files changed, 111 insertions(+), 9 deletions(-) delete mode 100644 SlnMesnac.Business/Class1.cs create mode 100644 SlnMesnac.Business/base/BaseBusiness.cs diff --git a/SlnMesnac.Business/Class1.cs b/SlnMesnac.Business/Class1.cs deleted file mode 100644 index 8124245..0000000 --- a/SlnMesnac.Business/Class1.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace SlnMesnac.Business -{ - public class Class1 - { - //业务逻辑类 - } -} diff --git a/SlnMesnac.Business/SlnMesnac.Business.csproj b/SlnMesnac.Business/SlnMesnac.Business.csproj index 8ef8970..4e2cf5e 100644 --- a/SlnMesnac.Business/SlnMesnac.Business.csproj +++ b/SlnMesnac.Business/SlnMesnac.Business.csproj @@ -5,4 +5,9 @@ enable + + + + + diff --git a/SlnMesnac.Business/base/BaseBusiness.cs b/SlnMesnac.Business/base/BaseBusiness.cs new file mode 100644 index 0000000..644ff4d --- /dev/null +++ b/SlnMesnac.Business/base/BaseBusiness.cs @@ -0,0 +1,106 @@ +using SlnMesnac.Plc; +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 +* 唯一标识:b00d95c1-a164-43a3-9f34-2a5d2efb3f34 +* +* 创建者:WenJY +* 电子邮箱:wenjy@mesnac.com +* 创建时间:2024-04-12 17:36:19 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.Business.@base +{ + internal class BaseBusiness + { + private readonly List _plcFactories; + + private readonly List _rfidFactories; + + public BaseBusiness(List plcFactories, List rfidFactories) + { + _plcFactories = plcFactories; + _rfidFactories = rfidFactories; + } + + /// + /// 根据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}"); + } + } + } +}