using System; using System.Security.Cryptography.X509Certificates; 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; private PLCTool pLCTool; public Task StartAsync(CancellationToken cancellationToken) { pLCTool = new PLCTool(); 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) { try { int index = 1; int indexMax = await DbScoped.SugarScope.CopyNew() .Queryable().CountAsync(); while (!stoppingToken.IsCancellationRequested) { List storagelocationList = await DbScoped.SugarScope.CopyNew() .Queryable().Where(it => it.Id == index).ToListAsync(); if (storagelocationList.Count > 0) { foreach (var storagelocation in storagelocationList) { //遮挡不亮 false bool result = pLCTool.ReadBit(storagelocation.PlcAddress2); // 写补料日志 Inventorylog inventorylog = new Inventorylog(); inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString(); inventorylog.RackCode = storagelocation.RackCode; // 合并货架的id int[] ids = {1,2,3,4,19,20,21,22 }; int PackageLine = 2; //if (ids.Contains(storagelocation.Id)) //{ // PackageLine = 1; //} if (result) { //缺料,需要补料 // 从满料到缺料 仓库库存修正 if (storagelocation.PackageNum > PackageLine) { storagelocation.PackageNum = PackageLine; // 减少日志输出 /* inventorylog.Operation = 1; inventorylog.PackageNum = 1; inventorylog.CreatedBy = "PLC-缺料"; inventorylog.CreatedTime = DateTime.Now.ToLocalTime();*/ await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync(); //await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync(); } } else { // 从缺料到补料 仓库库存修正 if (storagelocation.PackageNum <= PackageLine) { //不需要补料 补料成功 storagelocation.PackageNum = storagelocation.MaxCapacity; inventorylog.Operation = 2; inventorylog.PackageNum = storagelocation.MaxCapacity; inventorylog.CreatedBy = "PLC-补料成功"; inventorylog.CreatedTime = DateTime.Now.ToLocalTime(); await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync(); await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync(); } } } // Console.WriteLine("永驻线程,正在读取plc值 " + result + "地址:" + address[i]); } // await Task.Delay(500, stoppingToken); index++; if (index > indexMax) { index = 1; } } } catch (Exception ex) { Console.WriteLine("DoanBackGround线程ExecuteAsync异常:" + ex.Message); } } public void Dispose() { try { pLCTool.ConnectClose(); _cancellationTokenSource.Cancel(); _executingTask.Wait(); _cancellationTokenSource.Dispose(); } catch (Exception ex) { Console.WriteLine("DoanBackGround线程Dispose异常:" + ex.Message); } } } }