diff --git a/SlnMesnac.Config/AppConfig.cs b/SlnMesnac.Config/AppConfig.cs index 19030b3..47e949a 100644 --- a/SlnMesnac.Config/AppConfig.cs +++ b/SlnMesnac.Config/AppConfig.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Options; using System; +using System.Collections.Generic; namespace SlnMesnac.Config { @@ -12,17 +13,27 @@ namespace SlnMesnac.Config /// 日志文件路径 /// public string logPath { get; set; } - + /// - /// MES 数据库连接字符串 + /// MCS 开仓地址 /// - public string mesConnStr { get; set; } - + public string mcsOpenAddr { get; set; } + /// - /// MCS 数据库连接字符串 + /// CWSS 开仓地址 /// - public string mcsConnStr { get; set; } - + public string cwssOpenAddr { get; set; } + + /// + /// Sql连接配置 + /// + public List sqlConfig { get; set; } + + /// + /// PLC连接配置 + /// + public List plcConfig { get; set; } + public AppConfig Value => this; } } diff --git a/SlnMesnac.Config/PlcConfig.cs b/SlnMesnac.Config/PlcConfig.cs new file mode 100644 index 0000000..9bb8d31 --- /dev/null +++ b/SlnMesnac.Config/PlcConfig.cs @@ -0,0 +1,33 @@ +namespace SlnMesnac.Config +{ + /// + /// PLC配置,通过AppConfig进行获取 + /// + public class PlcConfig + { + /// + /// PLC配置ID + /// + public int configId { get; set; } + + /// + /// PIC类型 + /// + public string plcType { get; set; } + + /// + /// PLC IP + /// + public string plcIp { get; set; } + + /// + /// PLC Port + /// + public int plcPort { get; set; } + + /// + /// PLC Key + /// + public string plcKey { get; set; } + } +} \ No newline at end of file diff --git a/SlnMesnac.Config/SqlConfig.cs b/SlnMesnac.Config/SqlConfig.cs new file mode 100644 index 0000000..7adeab7 --- /dev/null +++ b/SlnMesnac.Config/SqlConfig.cs @@ -0,0 +1,23 @@ +namespace SlnMesnac.Config +{ + /// + /// Sql连接配置 + /// + public class SqlConfig + { + /// + /// Sql 配置ID,实体通过该ID关联数据源 + /// + public string configId { get; set; } + + /// + /// 数据库类型,MySql-0;SqlServer-1;Sqlite-2;Oracle-3 + /// + public int dbType { get; set; } + + /// + /// 连接字符串 + /// + public string connStr { get; set; } + } +} \ No newline at end of file diff --git a/SlnMesnac.Plc/Impl/InovancePlc.cs b/SlnMesnac.Plc/Impl/InovancePlc.cs index 9510fbf..dcd5c69 100644 --- a/SlnMesnac.Plc/Impl/InovancePlc.cs +++ b/SlnMesnac.Plc/Impl/InovancePlc.cs @@ -56,7 +56,7 @@ namespace SlnMesnac.Plc.Impl else { this.IsConnected = false; - PrintLogInfo("汇川PLC建立连接失败!!!"); + PrintLogInfo($"汇川PLC建立连接失败:{connect.Message}"); return false; } } @@ -485,7 +485,14 @@ namespace SlnMesnac.Plc.Impl private void PrintLogInfo(string message, Exception ex = null) { - + if (ex != null) + { + _logger.LogError($"{message}:{ex.Message}"); + } + else + { + _logger.LogInformation(message); + } } } diff --git a/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs b/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs index 5533efa..ef427c2 100644 --- a/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs +++ b/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs @@ -47,7 +47,7 @@ namespace SlnMesnac.Plc.Impl if (connect.IsSuccess) { this.IsConnected = true; - PrintLogInfo("三菱Q系列PLC建立连接成功!!!"); + PrintLogInfo($"三菱Q系列PLC建立连接失败:{connect.Message}"); return true; } else @@ -437,7 +437,14 @@ namespace SlnMesnac.Plc.Impl private void PrintLogInfo(string message, Exception ex = null) { - + if (ex != null) + { + _logger.LogError($"{message}:{ex.Message}"); + } + else + { + _logger.LogInformation(message); + } } } } diff --git a/SlnMesnac.Plc/Impl/OmronNJPlc.cs b/SlnMesnac.Plc/Impl/OmronNJPlc.cs index ee47ece..be21228 100644 --- a/SlnMesnac.Plc/Impl/OmronNJPlc.cs +++ b/SlnMesnac.Plc/Impl/OmronNJPlc.cs @@ -58,7 +58,7 @@ namespace SlnMesnac.Plc.Impl else { this.IsConnected = false; - PrintLogInfo("欧姆龙NJ系列PLC建立连接失败!!!"); + PrintLogInfo($"欧姆龙NJ系列PLC建立连接失败:{connect.Message}"); return false; } } @@ -442,7 +442,14 @@ namespace SlnMesnac.Plc.Impl private void PrintLogInfo(string message, Exception ex = null) { - + if (ex != null) + { + _logger.LogError($"{message}:{ex.Message}"); + } + else + { + _logger.LogInformation(message); + } } } } diff --git a/SlnMesnac.Plc/Impl/SiemensPlc.cs b/SlnMesnac.Plc/Impl/SiemensPlc.cs index 13c9b41..b7e0423 100644 --- a/SlnMesnac.Plc/Impl/SiemensPlc.cs +++ b/SlnMesnac.Plc/Impl/SiemensPlc.cs @@ -51,7 +51,7 @@ namespace SlnMesnac.Plc.Impl else { this.IsConnected = false; - PrintLogInfo("西门子S7系列PLC建立连接失败!!!"); + PrintLogInfo($"西门子S7系列PLC建立连接失败:{connect.Message}"); return false; } } @@ -409,7 +409,14 @@ namespace SlnMesnac.Plc.Impl private void PrintLogInfo(string message, Exception ex = null) { - + if (ex != null) + { + _logger.LogError($"{message}:{ex.Message}"); + } + else + { + _logger.LogInformation(message); + } } } } \ No newline at end of file diff --git a/SlnMesnac.Plc/PlcPool.cs b/SlnMesnac.Plc/PlcPool.cs index 66446e6..2e7d517 100644 --- a/SlnMesnac.Plc/PlcPool.cs +++ b/SlnMesnac.Plc/PlcPool.cs @@ -1,9 +1,11 @@ using Microsoft.Extensions.Logging; using Newtonsoft.Json.Linq; +using SlnMesnac.Config; using SlnMesnac.Plc.Impl; using System; using System.Collections; using System.Collections.Generic; +using System.Threading.Tasks; namespace SlnMesnac.Plc { @@ -21,30 +23,59 @@ namespace SlnMesnac.Plc private readonly MelsecBinaryPlc _melsecBinaryPlc; private readonly OmronNJPlc _omronNJPlc; private readonly SiemensPlc _siemensPlc; + private readonly AppConfig _appConfig; - public PlcPool(ILogger logger, InovancePlc inovancePlc,MelsecBinaryPlc melsecBinaryPlc,OmronNJPlc omronNJPlc,SiemensPlc siemensPlc) + public PlcPool(ILogger logger, InovancePlc inovancePlc, MelsecBinaryPlc melsecBinaryPlc, OmronNJPlc omronNJPlc, SiemensPlc siemensPlc, AppConfig appConfig) { _logger = logger; _inovancePlc = inovancePlc; _melsecBinaryPlc = melsecBinaryPlc; _omronNJPlc = omronNJPlc; _siemensPlc = siemensPlc; + _appConfig = appConfig; + //this.Init(); + } + + public void Init() + { if (!HslCommunication.Authorization.SetAuthorizationCode("ed1415f8-e06a-43ad-95f7-c04f7ae93b41")) { _logger.LogInformation("HslCommunication激活失败,可用时长24小时"); } _logger.LogInformation("HslCommunication 11.0.6.0激活成功"); + + try + { + if (_appConfig.plcConfig != null) + { + Task.Run(() => + { + foreach (var item in _appConfig.plcConfig) + { + AddPlc(item.plcType, item.plcIp, item.plcPort, item.plcKey); + } + }); + } + else + { + _logger.LogInformation("PLC配置信息为空"); + } + } + catch (Exception e) + { + _logger.LogError($"PLC初始化连接异常:{e.Message}"); + } } /// - /// 初始化Plc + /// 添加PLC连接 /// /// /// /// /// - public void InitPlc(string plcType, string ip, int port, string key) + public void AddPlc(string plcType, string ip, int port, string key) { IPlc _plc = null; switch (plcType) @@ -86,7 +117,14 @@ namespace SlnMesnac.Plc /// public IPlc GetPlcByKey(string key) { - return keyValuePairs[key]; + try + { + return keyValuePairs[key]; + } + catch (Exception e) + { + throw new ArgumentException($"根据PLC Key获取连接信息异常:{e.Message}"); ; + } } /// diff --git a/SlnMesnac.Plc/PlcSetup.cs b/SlnMesnac.Plc/PlcSetup.cs index 53afcc5..9fc9439 100644 --- a/SlnMesnac.Plc/PlcSetup.cs +++ b/SlnMesnac.Plc/PlcSetup.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; using SlnMesnac.Common; using SlnMesnac.Plc.Impl; using System; @@ -17,5 +18,12 @@ namespace SlnMesnac.Plc services.AddSingleton(); services.AddSingleton(); } + + public static IApplicationBuilder UsePlcExtensions(this IApplicationBuilder app) + { + var plcPool = app.ApplicationServices.GetService(); + plcPool.Init(); + return app; + } } } diff --git a/SlnMesnac.Plc/SlnMesnac.Plc.csproj b/SlnMesnac.Plc/SlnMesnac.Plc.csproj index d2d19be..9f89a4f 100644 --- a/SlnMesnac.Plc/SlnMesnac.Plc.csproj +++ b/SlnMesnac.Plc/SlnMesnac.Plc.csproj @@ -7,11 +7,13 @@ + + diff --git a/SlnMesnac.Repository/SqlsugarSetup.cs b/SlnMesnac.Repository/SqlsugarSetup.cs index 3b2addb..90a808e 100644 --- a/SlnMesnac.Repository/SqlsugarSetup.cs +++ b/SlnMesnac.Repository/SqlsugarSetup.cs @@ -20,26 +20,24 @@ namespace SlnMesnac.Repository services.AddSingleton(x => { var appConfig = x.GetService(); - SqlSugarScope Db = new SqlSugarScope(new List() + + var connectConfigList = new List(); + if (appConfig.sqlConfig != null) { - new ConnectionConfig() - { - ConfigId = "mes", - DbType = DbType.SqlServer, - ConnectionString = appConfig.mesConnStr, - InitKeyType = InitKeyType.Attribute, - IsAutoCloseConnection = true, - }, - new ConnectionConfig() + foreach (var item in appConfig.sqlConfig) { - ConfigId = "mcs", - DbType = DbType.Oracle, - ConnectionString = appConfig.mcsConnStr, - InitKeyType = InitKeyType.Attribute, - IsAutoCloseConnection = true, + var config = new ConnectionConfig() + { + ConfigId = item.configId, + DbType = (DbType)item.dbType, + ConnectionString = item.connStr, + InitKeyType = InitKeyType.Attribute, + IsAutoCloseConnection = true, + }; + connectConfigList.Add(config); } - }, - db => + } + SqlSugarScope Db = new SqlSugarScope(connectConfigList, db => { db.Aop.OnLogExecuting = (sql, pars) => { }; }); diff --git a/SlnMesnac.WPF/Startup.cs b/SlnMesnac.WPF/Startup.cs index 82f4adc..bbbf884 100644 --- a/SlnMesnac.WPF/Startup.cs +++ b/SlnMesnac.WPF/Startup.cs @@ -51,7 +51,7 @@ namespace SlnMesnac.WPF services.AddServices(); //注册Quartz - services.AddQuartzSetUp(); + //services.AddQuartzSetUp(); //注册PLC services.AddPlcSetup(); @@ -78,6 +78,9 @@ namespace SlnMesnac.WPF //启用Serilog中间件 app.UseSerilogExtensions(); + //初始化PLC中间件 + app.UsePlcExtensions(); + app.UseRouting(); app.UseAuthorization(); diff --git a/SlnMesnac.WPF/appsettings.json b/SlnMesnac.WPF/appsettings.json index d05c71e..4235900 100644 --- a/SlnMesnac.WPF/appsettings.json +++ b/SlnMesnac.WPF/appsettings.json @@ -8,8 +8,34 @@ }, "AllowedHosts": "*", "AppConfig": { - "logPath": "E:\\桌面\\日常代码\\SlnMesnac\\SlnMesnac\\bin\\Debug\\net6.0", - "mesConnStr": "server=.;uid=sa;pwd=123456;database=JiangYinMENS", - "mcsConnStr": "Data Source=175.27.215.92/helowin;User ID=aucma_scada;Password=aucma" + "logPath": "E:\\桌面\\SlnMesnac\\SlnMesnac.WPF\\bin\\Debug\\net6.0-windows", + "SqlConfig": [ + { + "configId": "mes", + "dbType": 1, + "connStr": "server=.;uid=sa;pwd=123456;database=JiangYinMENS" + }, + { + "configId": "mcs", + "dbType": 3, + "connStr": "Data Source=175.27.215.92/helowin;User ID=aucma_scada;Password=aucma" + } + ], + "PlcConfig": [ + { + "configId": 1, + "plcType": "MelsecBinaryPlc", + "plcIp": "127.0.0.1", //192.168.1.71 + "plcPort": 6000, + "plcKey": "mcs" + }, + { + "configId": 2, + "plcType": "MelsecBinaryPlc", + "plcIp": "127.0.0.1", + "plcPort": 6000, + "plcKey": "cwss" + } + ] } } diff --git a/SlnMesnac/Controllers/BaseUserController.cs b/SlnMesnac/Controllers/BaseUserController.cs index 3cd1395..3cdd7e0 100644 --- a/SlnMesnac/Controllers/BaseUserController.cs +++ b/SlnMesnac/Controllers/BaseUserController.cs @@ -17,18 +17,15 @@ namespace SlnMesnac.Controllers private readonly IBaseUserService _service; - private readonly PlcPool _plcPool; - /// /// /// /// /// - public BaseUserController(ILogger logger, IBaseUserService service, PlcPool plcPool) + public BaseUserController(ILogger logger, IBaseUserService service) { _logger = logger; _service = service; - _plcPool = plcPool; } /// @@ -38,7 +35,6 @@ namespace SlnMesnac.Controllers [HttpGet] public IEnumerable Get() { - _plcPool.InitPlc("SiemensPlc", "127.0.0.1", 102, "SiemensPlc"); IEnumerable users = null; try {