diff --git a/.idea/.idea.SlnMesnac/.idea/encodings.xml b/.idea/.idea.SlnMesnac/.idea/encodings.xml
index df87cf9..40b2185 100644
--- a/.idea/.idea.SlnMesnac/.idea/encodings.xml
+++ b/.idea/.idea.SlnMesnac/.idea/encodings.xml
@@ -1,4 +1,6 @@
-
+
+
+
\ No newline at end of file
diff --git a/SlnMesnac.Plc/Impl/InovancePlc.cs b/SlnMesnac.Plc/Impl/InovancePlc.cs
index 9510fbf..9b8307d 100644
--- a/SlnMesnac.Plc/Impl/InovancePlc.cs
+++ b/SlnMesnac.Plc/Impl/InovancePlc.cs
@@ -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..76caf5c 100644
--- a/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs
+++ b/SlnMesnac.Plc/Impl/MelsecBinaryPlc.cs
@@ -38,7 +38,7 @@ namespace SlnMesnac.Plc.Impl
///
public bool Connect(string IP, int port)
{
- PrintLogInfo("三菱Q系列PLC连接开始");
+ PrintLogInfo($"三菱Q系列PLC连接开始,IP:{IP};Port:{port}");
melsec_net.IpAddress = IP;
melsec_net.Port = port;
try
@@ -53,7 +53,7 @@ namespace SlnMesnac.Plc.Impl
else
{
this.IsConnected = false;
- PrintLogInfo("三菱Q系列PLC建立连接失败!!!");
+ PrintLogInfo($"三菱Q系列PLC建立连接失败:{connect.Message}");
return false;
}
}
@@ -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..01edbff 100644
--- a/SlnMesnac.Plc/Impl/OmronNJPlc.cs
+++ b/SlnMesnac.Plc/Impl/OmronNJPlc.cs
@@ -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..f385f2c 100644
--- a/SlnMesnac.Plc/Impl/SiemensPlc.cs
+++ b/SlnMesnac.Plc/Impl/SiemensPlc.cs
@@ -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..e134495 100644
--- a/SlnMesnac.Plc/PlcPool.cs
+++ b/SlnMesnac.Plc/PlcPool.cs
@@ -4,6 +4,8 @@ using SlnMesnac.Plc.Impl;
using System;
using System.Collections;
using System.Collections.Generic;
+using System.Threading.Tasks;
+using SlnMesnac.Config;
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)
diff --git a/SlnMesnac.Plc/PlcSetup.cs b/SlnMesnac.Plc/PlcSetup.cs
index 53afcc5..0b86054 100644
--- a/SlnMesnac.Plc/PlcSetup.cs
+++ b/SlnMesnac.Plc/PlcSetup.cs
@@ -4,6 +4,7 @@ using SlnMesnac.Plc.Impl;
using System;
using System.Collections.Generic;
using System.Text;
+using Microsoft.AspNetCore.Builder;
namespace SlnMesnac.Plc
{
@@ -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/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
{
diff --git a/SlnMesnac/Controllers/IngCheckController.cs b/SlnMesnac/Controllers/IngCheckController.cs
index 49ea9ad..f0124cb 100644
--- a/SlnMesnac/Controllers/IngCheckController.cs
+++ b/SlnMesnac/Controllers/IngCheckController.cs
@@ -2,6 +2,7 @@ using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using SlnMesnac.Config;
using SlnMesnac.Model.dto;
+using SlnMesnac.Plc;
using SlnMesnac.Repository.service;
namespace SlnMesnac.Controllers;
@@ -21,7 +22,9 @@ public class IngCheckController
///
///
///
- public IngCheckController(ILogger logger,IMcsBinToMaterService mcsBinToMaterService,AppConfig appConfig)
+ public IngCheckController(ILogger logger,
+ IMcsBinToMaterService mcsBinToMaterService,
+ AppConfig appConfig)
{
_logger = logger;
_mcsBinToMaterService = mcsBinToMaterService;
diff --git a/SlnMesnac/Startup.cs b/SlnMesnac/Startup.cs
index fede45b..ab669c8 100644
--- a/SlnMesnac/Startup.cs
+++ b/SlnMesnac/Startup.cs
@@ -119,6 +119,9 @@ namespace SlnMesnac
//启用Serilog中间件
app.UseSerilogExtensions();
+ //初始化PLC中间件
+ app.UsePlcExtensions();
+
//app.UseHttpsRedirection();
app.UseRouting();