diff --git a/DOAN.Admin.WebApi/Program.cs b/DOAN.Admin.WebApi/Program.cs index 67a2f26..6b29807 100644 --- a/DOAN.Admin.WebApi/Program.cs +++ b/DOAN.Admin.WebApi/Program.cs @@ -12,6 +12,7 @@ using DOAN.Common.DynamicApiSimple.Extens; using DOAN.Infrastructure.WebExtensions; using DOAN.ServiceCore.Signalr; using DOAN.ServiceCore.SqlSugar; +using DOAN.ServiceCore; var builder = WebApplication.CreateBuilder(args); // NLog: Setup NLog for Dependency injection @@ -89,6 +90,8 @@ builder.Services.AddHslCommunication(); // 添加本地化服务 builder.Services.AddLocalization(options => options.ResourcesPath = ""); +//永驻线程 +builder.Services.AddHostedService(); var app = builder.Build(); InternalApp.ServiceProvider = app.Services; diff --git a/DOAN.Model/PBL/Billofmaterials.cs b/DOAN.Model/PBL/Billofmaterials.cs index 5f88c32..071fd6b 100644 --- a/DOAN.Model/PBL/Billofmaterials.cs +++ b/DOAN.Model/PBL/Billofmaterials.cs @@ -53,7 +53,7 @@ namespace DOAN.Model.PBL /// 版本 /// [SugarColumn(ColumnName = "version")] - public int Version { get; set; } + public string Version { get; set; } /// diff --git a/DOAN.Model/PBL/Dto/LightUp.cs b/DOAN.Model/PBL/Dto/LightUp.cs index 088100d..5389988 100644 --- a/DOAN.Model/PBL/Dto/LightUp.cs +++ b/DOAN.Model/PBL/Dto/LightUp.cs @@ -29,7 +29,7 @@ namespace DOAN.Model.PBL.Dto /// /// 版本号 /// - public int Version { get; set; } + public string Version { get; set; } diff --git a/DOAN.Model/PBL/MES_Interaction.cs b/DOAN.Model/PBL/MES_Interaction.cs index cc264cb..59b0234 100644 --- a/DOAN.Model/PBL/MES_Interaction.cs +++ b/DOAN.Model/PBL/MES_Interaction.cs @@ -42,7 +42,7 @@ namespace DOAN.Model.PBL /// 版本号 /// [SugarColumn(ColumnName = "Version")] - public int Version { get; set; } + public string Version { get; set; } /// diff --git a/DOAN.Model/PBL/Storagelocation.cs b/DOAN.Model/PBL/Storagelocation.cs index 130c243..6127283 100644 --- a/DOAN.Model/PBL/Storagelocation.cs +++ b/DOAN.Model/PBL/Storagelocation.cs @@ -50,11 +50,17 @@ namespace DOAN.Model.PBL /// - /// plc address + ///PLC地址(亮灯拣货) /// [SugarColumn(ColumnName = "plc_address")] public string PlcAddress { get; set; } + /// + /// PLC地址(空箱补料) + /// + [SugarColumn(ColumnName = "plc_address_2")] + public string PlcAddress2 { get; set; } + /// /// 创建人 /// diff --git a/DOAN.Service/PBL/MESInteractionServcie.cs b/DOAN.Service/PBL/MESInteractionServcie.cs index 81cac85..a46a057 100644 --- a/DOAN.Service/PBL/MESInteractionServcie.cs +++ b/DOAN.Service/PBL/MESInteractionServcie.cs @@ -127,9 +127,7 @@ namespace DOAN.Service.PBL light_Log.IsSuccess = isSuccess; light_Log.Operationer = "PBL"; light_Log.CreatedTime = DateTime.Now; - int result = Context.Insertable(light_Log).ExecuteCommand(); - - + int result = Context.Insertable(light_Log).ExecuteCommand(); return result > 0; } diff --git a/DOAN.ServiceCore/BackgroundService.cs b/DOAN.ServiceCore/BackgroundService.cs new file mode 100644 index 0000000..0d40ea4 --- /dev/null +++ b/DOAN.ServiceCore/BackgroundService.cs @@ -0,0 +1,96 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using DOAN.Infrastructure.PLC; +using DOAN.Model.PBL; +using DOAN.Model.System; +using Microsoft.Extensions.Hosting; +using SqlSugar; +using SqlSugar.IOC; +namespace DOAN.ServiceCore +{ + + public class DoanBackgroundService : IHostedService, IDisposable + { + private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); + private Task _executingTask; + + public Task StartAsync(CancellationToken cancellationToken) + { + PLCTool.ConnectPLC(); + // 当服务开始时,启动后台任务 + _executingTask = ExecuteAsync(_cancellationTokenSource.Token); + + + + return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask; + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + // 请求取消后台任务 + _cancellationTokenSource.Cancel(); + + // 等待后台任务完成 + await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken)); + } + + private async Task ExecuteAsync(CancellationToken stoppingToken) + { + // 获取所有传感器 地址 + + + int index = 1; + + + while (!stoppingToken.IsCancellationRequested) + { + Storagelocation storagelocation = await DbScoped.SugarScope.CopyNew() + .Queryable().Where(it => it.Id == index).FirstAsync(); + + + bool result = PLCTool.ReadBit(storagelocation.PlcAddress2); + // 写补料日志 + Inventorylog inventorylog = new Inventorylog(); + inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString(); + inventorylog.RackCode = storagelocation.RackCode; + + if (result) + { + //缺料 + storagelocation.PackageNum = 2; + inventorylog.Operation = 1; + inventorylog.PackageNum = 2; + } + else + { + //补料 + storagelocation.PackageNum = 4; + inventorylog.Operation = 2; + inventorylog.PackageNum = 4; + } + inventorylog.CreatedBy = "PLC"; + inventorylog.CreatedTime = DateTime.Now.ToLocalTime(); + await DbScoped.SugarScope.CopyNew().Insertable(storagelocation).ExecuteCommandAsync(); + + + await Task.Delay(3000, stoppingToken); + + + index++; + if (index > 16) + { + index = 1; + } + + } + } + + public void Dispose() + { + _cancellationTokenSource.Cancel(); + _executingTask.Wait(); + _cancellationTokenSource.Dispose(); + } + } +} diff --git a/DOAN.ServiceCore/DoanBackgroundService.cs b/DOAN.ServiceCore/DoanBackgroundService.cs new file mode 100644 index 0000000..459739c --- /dev/null +++ b/DOAN.ServiceCore/DoanBackgroundService.cs @@ -0,0 +1,85 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using DOAN.Infrastructure.PLC; +using DOAN.Model.PBL; +using DOAN.Model.System; +using Microsoft.Extensions.Hosting; +using SqlSugar; +using SqlSugar.IOC; +namespace DOAN.ServiceCore +{ + + public class BackgroundService : IHostedService, IDisposable + { + private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource(); + private Task _executingTask; + + public Task StartAsync(CancellationToken cancellationToken) + { + PLCTool.ConnectPLC(); + // 当服务开始时,启动后台任务 + _executingTask = ExecuteAsync(_cancellationTokenSource.Token); + + + + return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask; + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + // 请求取消后台任务 + _cancellationTokenSource.Cancel(); + + // 等待后台任务完成 + await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken)); + } + + private async Task ExecuteAsync(CancellationToken stoppingToken) + { + // 获取所有传感器 地址 + + + int index = 1; + + + while (!stoppingToken.IsCancellationRequested) + { + Storagelocation storagelocation = await DbScoped.SugarScope.CopyNew() + .Queryable().Where(it => it.Id == index).FirstAsync(); + + + bool result = PLCTool.ReadBit(storagelocation.PlcAddress2); + if (result) + { + storagelocation.PackageNum = 2; + } + else + { + storagelocation.PackageNum = 4; + } + await DbScoped.SugarScope.CopyNew().Insertable(storagelocation).ExecuteCommandAsync(); + + // 写补料日志 + Inventorylog inventorylog = new Inventorylog(); + inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString(); + inventorylog.RackCode = storagelocation.RackCode; + + + index++; + if (index > 16) + { + index = 1; + } + + } + } + + public void Dispose() + { + _cancellationTokenSource.Cancel(); + _executingTask.Wait(); + _cancellationTokenSource.Dispose(); + } + } +} diff --git a/Infrastructure/PLC/PLCTool.cs b/Infrastructure/PLC/PLCTool.cs index 2282ef5..bb1394f 100644 --- a/Infrastructure/PLC/PLCTool.cs +++ b/Infrastructure/PLC/PLCTool.cs @@ -17,9 +17,9 @@ namespace DOAN.Infrastructure.PLC // private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); public static SiemensS7Net siemensTcpNet = null; - public static bool ConnectPLC() + public static bool ConnectPLC() { - siemensTcpNet= new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1") + siemensTcpNet = new SiemensS7Net(SiemensPLCS.S200Smart, "192.168.2.1") { ConnectTimeOut = 5000 }; @@ -46,13 +46,26 @@ namespace DOAN.Infrastructure.PLC /// /// /// - public static bool WriteBit(string addr,bool value) + public static bool WriteBit(string addr, bool value) { - + OperateResult write = siemensTcpNet.Write(addr, value); return write.IsSuccess; - + } + /// + /// 读取bit + /// + /// + /// + public static bool ReadBit(string addr) + { + + bool M100_7 = siemensTcpNet.ReadBool(addr).Content; + return M100_7; + } + + public static void ConnectClose() {