change - 参数配置、Sql、Plc初始化配置修改

master
wenjy 10 months ago
parent 052661b951
commit 4a07ba4c87

@ -1,5 +1,6 @@
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
namespace SlnMesnac.Config
{
@ -14,14 +15,24 @@ namespace SlnMesnac.Config
public string logPath { get; set; }
/// <summary>
/// MES 数据库连接字符串
/// MCS 开仓地址
/// </summary>
public string mesConnStr { get; set; }
public string mcsOpenAddr { get; set; }
/// <summary>
/// MCS 数据库连接字符串
/// CWSS 开仓地址
/// </summary>
public string mcsConnStr { get; set; }
public string cwssOpenAddr { get; set; }
/// <summary>
/// Sql连接配置
/// </summary>
public List<SqlConfig> sqlConfig { get; set; }
/// <summary>
/// PLC连接配置
/// </summary>
public List<PlcConfig> plcConfig { get; set; }
public AppConfig Value => this;
}

@ -0,0 +1,33 @@
namespace SlnMesnac.Config
{
/// <summary>
/// PLC配置通过AppConfig进行获取
/// </summary>
public class PlcConfig
{
/// <summary>
/// PLC配置ID
/// </summary>
public int configId { get; set; }
/// <summary>
/// PIC类型
/// </summary>
public string plcType { get; set; }
/// <summary>
/// PLC IP
/// </summary>
public string plcIp { get; set; }
/// <summary>
/// PLC Port
/// </summary>
public int plcPort { get; set; }
/// <summary>
/// PLC Key
/// </summary>
public string plcKey { get; set; }
}
}

@ -0,0 +1,23 @@
namespace SlnMesnac.Config
{
/// <summary>
/// Sql连接配置
/// </summary>
public class SqlConfig
{
/// <summary>
/// Sql 配置ID实体通过该ID关联数据源
/// </summary>
public string configId { get; set; }
/// <summary>
/// 数据库类型MySql-0;SqlServer-1;Sqlite-2;Oracle-3
/// </summary>
public int dbType { get; set; }
/// <summary>
/// 连接字符串
/// </summary>
public string connStr { get; set; }
}
}

@ -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);
}
}
}

@ -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);
}
}
}
}

@ -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);
}
}
}
}

@ -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);
}
}
}
}

@ -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<PlcPool> logger, InovancePlc inovancePlc,MelsecBinaryPlc melsecBinaryPlc,OmronNJPlc omronNJPlc,SiemensPlc siemensPlc)
public PlcPool(ILogger<PlcPool> 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}");
}
}
/// <summary>
/// 初始化Plc
/// 添加PLC连接
/// </summary>
/// <param name="plcType"></param>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <param name="key"></param>
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)
@ -85,9 +116,16 @@ namespace SlnMesnac.Plc
/// <param name="key"></param>
/// <returns></returns>
public IPlc GetPlcByKey(string key)
{
try
{
return keyValuePairs[key];
}
catch (Exception e)
{
throw new ArgumentException($"根据PLC Key获取连接信息异常:{e.Message}"); ;
}
}
/// <summary>
/// 获取所有PLC信息

@ -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<SiemensPlc>();
services.AddSingleton<PlcPool>();
}
public static IApplicationBuilder UsePlcExtensions(this IApplicationBuilder app)
{
var plcPool = app.ApplicationServices.GetService<PlcPool>();
plcPool.Init();
return app;
}
}
}

@ -7,11 +7,13 @@
<ItemGroup>
<PackageReference Include="HslCommunication" Version="11.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SlnMesnac.Common\SlnMesnac.Common.csproj" />
<ProjectReference Include="..\SlnMesnac.Config\SlnMesnac.Config.csproj" />
</ItemGroup>
</Project>

@ -20,26 +20,24 @@ namespace SlnMesnac.Repository
services.AddSingleton<ISqlSugarClient>(x =>
{
var appConfig = x.GetService<AppConfig>();
SqlSugarScope Db = new SqlSugarScope(new List<ConnectionConfig>()
var connectConfigList = new List<ConnectionConfig>();
if (appConfig.sqlConfig != null)
{
new ConnectionConfig()
foreach (var item in appConfig.sqlConfig)
{
ConfigId = "mes",
DbType = DbType.SqlServer,
ConnectionString = appConfig.mesConnStr,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
},
new ConnectionConfig()
var config = new ConnectionConfig()
{
ConfigId = "mcs",
DbType = DbType.Oracle,
ConnectionString = appConfig.mcsConnStr,
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) => { };
});

@ -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();

@ -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"
}
]
}
}

@ -17,18 +17,15 @@ namespace SlnMesnac.Controllers
private readonly IBaseUserService _service;
private readonly PlcPool _plcPool;
/// <summary>
///
/// </summary>
/// <param name="logger"></param>
/// <param name="service"></param>
public BaseUserController(ILogger<BaseUserController> logger, IBaseUserService service, PlcPool plcPool)
public BaseUserController(ILogger<BaseUserController> logger, IBaseUserService service)
{
_logger = logger;
_service = service;
_plcPool = plcPool;
}
/// <summary>
@ -38,7 +35,6 @@ namespace SlnMesnac.Controllers
[HttpGet]
public IEnumerable<BaseUser> Get()
{
_plcPool.InitPlc("SiemensPlc", "127.0.0.1", 102, "SiemensPlc");
IEnumerable<BaseUser> users = null;
try
{

Loading…
Cancel
Save