pblbackend/DOAN.ServiceCore/DoanBackgroundService.cs

155 lines
5.8 KiB
C#
Raw Normal View History

2024-11-08 10:10:59 +08:00
using System;
2024-11-13 15:53:15 +08:00
using System.Security.Cryptography.X509Certificates;
2024-11-08 10:10:59 +08:00
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
{
2024-11-08 11:31:08 +08:00
public class DoanBackgroundService : IHostedService, IDisposable
2024-11-08 10:10:59 +08:00
{
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private Task _executingTask;
2024-11-13 15:53:15 +08:00
private PLCTool pLCTool;
2024-11-08 10:10:59 +08:00
public Task StartAsync(CancellationToken cancellationToken)
{
2024-11-13 15:53:15 +08:00
pLCTool = new PLCTool();
pLCTool.ConnectPLC();
2024-11-08 10:10:59 +08:00
// 当服务开始时,启动后台任务
_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)
{
2024-11-13 15:53:15 +08:00
try {
int index = 1;
int indexMax = await DbScoped.SugarScope.CopyNew()
.Queryable<Storagelocation>().CountAsync();
2024-11-08 10:10:59 +08:00
2024-11-13 15:53:15 +08:00
while (!stoppingToken.IsCancellationRequested)
{
List<Storagelocation> storagelocationList = await DbScoped.SugarScope.CopyNew()
.Queryable<Storagelocation>().Where(it => it.Id == index).ToListAsync();
if (storagelocationList.Count > 0)
{
2024-11-08 10:10:59 +08:00
2024-11-13 15:53:15 +08:00
foreach (var storagelocation in storagelocationList)
{
2024-11-08 10:10:59 +08:00
2024-11-13 15:53:15 +08:00
//遮挡不亮 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)
{
//缺料,需要补料
2024-11-14 18:24:10 +08:00
// 从满料到缺料 仓库库存修正
if (storagelocation.PackageNum > PackageLine)
2024-11-13 15:53:15 +08:00
{
storagelocation.PackageNum = PackageLine;
2024-11-14 18:24:10 +08:00
// 减少日志输出
/* inventorylog.Operation = 1;
2024-11-13 15:53:15 +08:00
inventorylog.PackageNum = 1;
2024-11-14 18:24:10 +08:00
inventorylog.CreatedBy = "PLC-缺料";
inventorylog.CreatedTime = DateTime.Now.ToLocalTime();*/
2024-11-13 15:53:15 +08:00
await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync();
2024-11-14 18:24:10 +08:00
//await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync();
2024-11-13 15:53:15 +08:00
}
}
else
{
2024-11-14 18:24:10 +08:00
// 从缺料到补料 仓库库存修正
2024-11-13 15:53:15 +08:00
if (storagelocation.PackageNum <= PackageLine)
{
//不需要补料 补料成功
storagelocation.PackageNum = storagelocation.MaxCapacity;
inventorylog.Operation = 2;
inventorylog.PackageNum = storagelocation.MaxCapacity;
2024-11-14 18:24:10 +08:00
inventorylog.CreatedBy = "PLC-补料成功";
2024-11-13 15:53:15 +08:00
inventorylog.CreatedTime = DateTime.Now.ToLocalTime();
await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync();
await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync();
}
}
2024-11-08 10:10:59 +08:00
2024-11-08 11:31:08 +08:00
2024-11-13 15:53:15 +08:00
}
2024-11-08 11:31:08 +08:00
2024-11-08 11:32:28 +08:00
2024-11-13 15:53:15 +08:00
// Console.WriteLine("永驻线程正在读取plc值 " + result + "地址:" + address[i]);
2024-11-08 11:31:08 +08:00
}
2024-11-13 15:53:15 +08:00
// await Task.Delay(500, stoppingToken);
2024-11-08 10:10:59 +08:00
2024-11-13 15:53:15 +08:00
index++;
if (index > indexMax)
{
index = 1;
}
2024-11-08 11:31:08 +08:00
2024-11-08 10:10:59 +08:00
}
2024-11-13 15:53:15 +08:00
} catch (Exception ex) {
Console.WriteLine("DoanBackGround线程ExecuteAsync异常:" + ex.Message);
2024-11-08 10:10:59 +08:00
}
2024-11-13 15:53:15 +08:00
2024-11-08 10:10:59 +08:00
}
public void Dispose()
{
2024-11-13 15:53:15 +08:00
try
{
pLCTool.ConnectClose();
_cancellationTokenSource.Cancel();
_executingTask.Wait();
_cancellationTokenSource.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("DoanBackGround线程Dispose异常:" + ex.Message);
}
2024-11-08 10:10:59 +08:00
}
2024-11-13 15:53:15 +08:00
2024-11-08 10:10:59 +08:00
}
}