From 068a66e70aa58be3949445fd76d67966b1ce637d Mon Sep 17 00:00:00 2001 From: wenjy Date: Mon, 13 May 2024 13:48:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?change=20-=20=E6=BF=80=E6=B4=BB=E7=A0=81?= =?UTF-8?q?=E6=BF=80=E6=B4=BBHsl=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Extensions/PlcFactorySetup.cs | 49 ++++++++++++++++--------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/SlnMesnac.Extensions/PlcFactorySetup.cs b/SlnMesnac.Extensions/PlcFactorySetup.cs index 383e7e1..3298593 100644 --- a/SlnMesnac.Extensions/PlcFactorySetup.cs +++ b/SlnMesnac.Extensions/PlcFactorySetup.cs @@ -48,34 +48,47 @@ namespace SlnMesnac.Extensions try { - if (appConfig.plcConfig != null) + do { - foreach (var item in appConfig.plcConfig) + if (!HslCommunication.Authorization.SetAuthorizationCode("1839541f-8fb4-42c4-a13f-733b027fe5af")) { - if (item.isFlage) - { - PlcAbsractFactory _plc = InitPlc(x, item.plcType); + Log.Information("HslCommunication激活失败,可用时长为24小时"); + break; + } + else + { + Log.Information("HslCommunication激活成功"); + } - var connectResult = _plc.Connect(item.plcIp, item.plcPort); - if (connectResult) + if (appConfig.plcConfig != null) + { + foreach (var item in appConfig.plcConfig) + { + if (item.isFlage) { - Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); - _plc.ConfigKey = item.plcKey; + PlcAbsractFactory _plc = InitPlc(x, item.plcType); - if(absractFactories.Contains(_plc)) + var connectResult = _plc.Connect(item.plcIp, item.plcPort); + if (connectResult) { - absractFactories.Remove(_plc); - } + Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接成功,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); + _plc.ConfigKey = item.plcKey; - absractFactories.Add(_plc); - } - else - { - Log.Information($"PLC:{item.plcIp}:{item.plcPort};连接失败,时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); + 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")}"); + } } } } - } + }while(false); } catch (Exception e) { -- 2.38.5 From c389b436894a565002c8bc704487102719ccff37 Mon Sep 17 00:00:00 2001 From: wenjy Date: Wed, 31 Jul 2024 17:29:54 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=8E=A8=E9=80=81=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.WPF/App.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SlnMesnac.WPF/App.xaml.cs b/SlnMesnac.WPF/App.xaml.cs index ea819ab..ec3de3a 100644 --- a/SlnMesnac.WPF/App.xaml.cs +++ b/SlnMesnac.WPF/App.xaml.cs @@ -43,7 +43,7 @@ namespace SlnMesnac.WPF await host.StartAsync(); var appConfig = host.Services.GetService(); - var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; + var logPath = $"{appConfig.logPath}/Logs/{DateTime.UtcNow:yyyy-MM-dd}/"; Log.Information($"系统初始化完成,日志存放路径:{appConfig.logPath}"); } -- 2.38.5 From 83499c572d687588683c3d8a7717651440913ab2 Mon Sep 17 00:00:00 2001 From: wenjy Date: Wed, 4 Sep 2024 11:09:06 +0800 Subject: [PATCH 3/3] =?UTF-8?q?add=20-=20webapi=E6=9C=8D=E5=8A=A1=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E5=AD=98?= =?UTF-8?q?=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SlnMesnac.Serilog/SerilogExtensions.cs | 6 +- SlnMesnac.TouchSocket/ApiServer.cs | 109 +++++++++++++++++ .../SlnMesnac.TouchSocket.csproj | 4 +- SlnMesnac.TouchSocket/TouchSocketSetup.cs | 3 + SlnMesnac.TouchSocket/WebApiServer.cs | 112 ++++++++++++++++++ SlnMesnac.WPF/Startup.cs | 3 + 6 files changed, 233 insertions(+), 4 deletions(-) create mode 100644 SlnMesnac.TouchSocket/ApiServer.cs create mode 100644 SlnMesnac.TouchSocket/WebApiServer.cs diff --git a/SlnMesnac.Serilog/SerilogExtensions.cs b/SlnMesnac.Serilog/SerilogExtensions.cs index d65e0b6..6f9b303 100644 --- a/SlnMesnac.Serilog/SerilogExtensions.cs +++ b/SlnMesnac.Serilog/SerilogExtensions.cs @@ -46,9 +46,9 @@ namespace SlnMesnac.Serilog #endregion Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.Console() - .WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information, fileSizeLimitBytes: 5 * 1024) - .WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error, fileSizeLimitBytes: 5 * 1024) - .WriteTo.File(Path.Combine(logPath, "Warn.log"), LogEventLevel.Warning, fileSizeLimitBytes: 5 * 1024) + .WriteTo.File(Path.Combine(logPath, "Info.log"), LogEventLevel.Information) + .WriteTo.File(Path.Combine(logPath, "Error.log"), LogEventLevel.Error) + .WriteTo.File(Path.Combine(logPath, "Warn.log"), LogEventLevel.Warning) //.WriteTo.File(Path.Combine(logPath, "Debug.log"), LogEventLevel.Debug, fileSizeLimitBytes: 5 * 1024) .CreateLogger(); app.UseSerilogRequestLogging(); diff --git a/SlnMesnac.TouchSocket/ApiServer.cs b/SlnMesnac.TouchSocket/ApiServer.cs new file mode 100644 index 0000000..d704959 --- /dev/null +++ b/SlnMesnac.TouchSocket/ApiServer.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using System.Text; +using TouchSocket.Rpc; +using TouchSocket.WebApi; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:SlnMesnac.TouchSocket +* 唯一标识:649766cc-308e-4bf3-8d69-dea48ec40642 +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2024-09-04 10:51:54 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.TouchSocket +{ + public class ApiServer: RpcServer + { + + public delegate void RefreshScanInfoInCenterStart(); + /// + /// 入库开始事件刷新 + /// + public event RefreshScanInfoInCenterStart RefreshScanInfoInCenterStartEvent; + + public delegate void RefreshScanInfoInCenterStop(); + /// + /// 入库结束事件刷新 + /// + public event RefreshScanInfoInCenterStop RefreshScanInfoInCenterStopEvent; + + public delegate void RefreshScanInfoOutCenterStart(); + /// + /// 出库开始事件刷新 + /// + public event RefreshScanInfoOutCenterStart RefreshScanInfoOutCenterStartEvent; + + public delegate void RefreshScanInfoOutCenterStop(); + /// + /// 出库结束事件刷新 + /// + public event RefreshScanInfoOutCenterStop RefreshScanInfoOutCenterStopEvent; + + /// + /// 入库开始 + /// + /// + /// + [EnableCors("cors")] + [WebApi(HttpMethodType.POST)] + public object getScanInfoInCenterStart(string messageHeader) + { + RefreshScanInfoInCenterStartEvent?.Invoke(); + return true; + } + + /// + /// 入库结束 + /// + /// + /// + [EnableCors("cors")] + [WebApi(HttpMethodType.POST)] + public object getScanInfoInCenterStop(string messageHeader) + { + RefreshScanInfoInCenterStopEvent?.Invoke(); + return true; + } + + /// + /// 出库开始 + /// + /// + /// + [WebApi(HttpMethodType.POST)] + public object getScanInfoOutCenterStart(string messageHeader) + { + RefreshScanInfoOutCenterStartEvent?.Invoke(); + return true; + } + + /// + /// 出库结束 + /// + /// + /// + [WebApi(HttpMethodType.POST)] + public object getScanInfoOutCenterStop(string messageHeader) + { + RefreshScanInfoOutCenterStopEvent?.Invoke(); + return true; + } + + } +} diff --git a/SlnMesnac.TouchSocket/SlnMesnac.TouchSocket.csproj b/SlnMesnac.TouchSocket/SlnMesnac.TouchSocket.csproj index 790191c..94f69ee 100644 --- a/SlnMesnac.TouchSocket/SlnMesnac.TouchSocket.csproj +++ b/SlnMesnac.TouchSocket/SlnMesnac.TouchSocket.csproj @@ -6,7 +6,9 @@ - + + + diff --git a/SlnMesnac.TouchSocket/TouchSocketSetup.cs b/SlnMesnac.TouchSocket/TouchSocketSetup.cs index 31ca7db..637d77f 100644 --- a/SlnMesnac.TouchSocket/TouchSocketSetup.cs +++ b/SlnMesnac.TouchSocket/TouchSocketSetup.cs @@ -39,6 +39,9 @@ namespace SlnMesnac.TouchSocket { var _server = app.ApplicationServices.GetService(); _server.Init(20108); + + var _apiServer = app.ApplicationServices.GetService(); + _apiServer.Init(); return app; } } diff --git a/SlnMesnac.TouchSocket/WebApiServer.cs b/SlnMesnac.TouchSocket/WebApiServer.cs new file mode 100644 index 0000000..7c85740 --- /dev/null +++ b/SlnMesnac.TouchSocket/WebApiServer.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using TouchSocket.Core; +using TouchSocket.Http; +using TouchSocket.Rpc; +using TouchSocket.Sockets; +using TouchSocket.WebApi.Swagger; + +#region << 版 本 注 释 >> +/*-------------------------------------------------------------------- +* 版权所有 (c) 2024 WenJY 保留所有权利。 +* CLR版本:4.0.30319.42000 +* 机器名称:T14-GEN3-7895 +* 命名空间:SlnMesnac.TouchSocket +* 唯一标识:4e47989b-9d43-426e-b67a-529de3b1b0e8 +* +* 创建者:WenJY +* 电子邮箱: +* 创建时间:2024-09-04 10:51:29 +* 版本:V1.0.0 +* 描述: +* +*-------------------------------------------------------------------- +* 修改人: +* 时间: +* 修改说明: +* +* 版本:V1.0.0 +*--------------------------------------------------------------------*/ +#endregion << 版 本 注 释 >> +namespace SlnMesnac.TouchSocket +{ + public class WebApiServer + { + private ApiServer _apiServer; + + public WebApiServer(ApiServer apiServer) + { + _apiServer = apiServer; + } + + public void Init() + { + try + { + var service = new HttpService(); + service.Setup(new TouchSocketConfig() + .SetListenIPHosts(7789) + .ConfigureContainer(a => + { + a.AddRpcStore(store => + { + + store.RegisterServer(_apiServer);//注册服务 + }); + + a.AddCors(corsOption => + { + corsOption.Add("cors", corsBuilder => + { + corsBuilder.AllowAnyMethod() + .AllowAnyOrigin(); + }); + }); + + a.AddLogger(logger => + { + logger.AddConsoleLogger(); + logger.AddFileLogger(); + }); + }) + .ConfigurePlugins(a => + { + a.UseCheckClear(); + + a.Add(); + + a.UseWebApi() + .ConfigureConverter(converter => + { + converter.AddJsonSerializerFormatter(new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None }); + }); + + a.UseSwagger();//使用Swagger页面 + //.UseLaunchBrowser(); + + a.UseDefaultHttpServicePlugin(); + })); + service.Start(); + + Console.WriteLine("以下连接用于测试webApi"); + Console.WriteLine($"使用:http://127.0.0.1:7789/swagger/index.html"); + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + + //Console.ReadLine(); + } + } + + internal class AuthenticationPlugin : PluginBase, IHttpPlugin + { + public async Task OnHttpRequest(IHttpSocketClient client, HttpContextEventArgs e) + { + await e.InvokeNext(); + } + } +} diff --git a/SlnMesnac.WPF/Startup.cs b/SlnMesnac.WPF/Startup.cs index f65ebe7..e4251dc 100644 --- a/SlnMesnac.WPF/Startup.cs +++ b/SlnMesnac.WPF/Startup.cs @@ -12,6 +12,7 @@ using SlnMesnac.Rfid; using SlnMesnac.Ioc; using SlnMesnac.Plc; using SlnMesnac.Extensions; +using SlnMesnac.TouchSocket; namespace SlnMesnac.WPF { @@ -71,6 +72,8 @@ namespace SlnMesnac.WPF //启用Serilog中间件 app.UseSerilogExtensions(); + app.UseTouchSocketExtensions(); + app.UseRouting(); app.UseAuthorization(); -- 2.38.5