现场PBL修改后代码提交

This commit is contained in:
2024-11-13 15:53:15 +08:00
parent 4fcad8a63e
commit 368941225e
14 changed files with 319 additions and 188 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using DOAN.Infrastructure.PLC;
@@ -14,10 +15,13 @@ namespace DOAN.ServiceCore
{
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private Task _executingTask;
private PLCTool pLCTool;
public Task StartAsync(CancellationToken cancellationToken)
{
PLCTool.ConnectPLC();
pLCTool = new PLCTool();
pLCTool.ConnectPLC();
// 当服务开始时,启动后台任务
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
@@ -37,79 +41,113 @@ namespace DOAN.ServiceCore
private async Task ExecuteAsync(CancellationToken stoppingToken)
{
// 获取所有传感器 地址
try {
int index = 1;
int indexMax = await DbScoped.SugarScope.CopyNew()
.Queryable<Storagelocation>().CountAsync();
int index = 1;
while (!stoppingToken.IsCancellationRequested)
{
Storagelocation storagelocation = await DbScoped.SugarScope.CopyNew()
.Queryable<Storagelocation>().Where(it => it.Id == index).FirstAsync();
string[] address = storagelocation.PlcAddress2.Split(",");
for (int i = 0; i < address.Length; i++)
while (!stoppingToken.IsCancellationRequested)
{
bool result = PLCTool.ReadBit(address[i]);
// 写补料日志
Inventorylog inventorylog = new Inventorylog();
inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString();
inventorylog.RackCode = storagelocation.RackCode;
if (result)
List<Storagelocation> storagelocationList = await DbScoped.SugarScope.CopyNew()
.Queryable<Storagelocation>().Where(it => it.Id == index).ToListAsync();
if (storagelocationList.Count > 0)
{
//缺料
storagelocation.PackageNum = 2;
inventorylog.Operation = 1;
inventorylog.PackageNum = 2;
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 <= 2)
foreach (var storagelocation in storagelocationList)
{
//补料成功
storagelocation.PackageNum = 4;
inventorylog.Operation = 2;
inventorylog.PackageNum = 4;
inventorylog.CreatedBy = "PLC";
inventorylog.CreatedTime = DateTime.Now.ToLocalTime();
await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync();
await DbScoped.SugarScope.CopyNew().Insertable(inventorylog).ExecuteCommandAsync();
//遮挡不亮 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;
}
}
await Task.Delay(5000, stoppingToken);
Console.WriteLine("永驻线程正在读取plc值 " + result);
index++;
if (index > 16)
{
index = 1;
}
} catch (Exception ex) {
Console.WriteLine("DoanBackGround线程ExecuteAsync异常:" + ex.Message);
}
}
public void Dispose()
{
_cancellationTokenSource.Cancel();
_executingTask.Wait();
_cancellationTokenSource.Dispose();
try
{
pLCTool.ConnectClose();
_cancellationTokenSource.Cancel();
_executingTask.Wait();
_cancellationTokenSource.Dispose();
}
catch (Exception ex)
{
Console.WriteLine("DoanBackGround线程Dispose异常:" + ex.Message);
}
}
}
}