This commit is contained in:
qianhao.xu
2024-11-08 11:31:08 +08:00
parent 2f3edfdb27
commit 477334ef25
2 changed files with 42 additions and 128 deletions

View File

@@ -10,7 +10,7 @@ using SqlSugar.IOC;
namespace DOAN.ServiceCore
{
public class BackgroundService : IHostedService, IDisposable
public class DoanBackgroundService : IHostedService, IDisposable
{
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private Task _executingTask;
@@ -48,24 +48,51 @@ namespace DOAN.ServiceCore
Storagelocation storagelocation = await DbScoped.SugarScope.CopyNew()
.Queryable<Storagelocation>().Where(it => it.Id == index).FirstAsync();
bool result = PLCTool.ReadBit(storagelocation.PlcAddress2);
if (result)
string[] address = storagelocation.PlcAddress2.Split(",");
for (int i = 0; i < address.Length; i++)
{
storagelocation.PackageNum = 2;
}
else
{
storagelocation.PackageNum = 4;
}
await DbScoped.SugarScope.CopyNew().Insertable(storagelocation).ExecuteCommandAsync();
bool result = PLCTool.ReadBit(address[i]);
// 写补料日志
Inventorylog inventorylog = new Inventorylog();
inventorylog.Id = SnowFlakeSingle.Instance.NextId().ToString();
inventorylog.RackCode = storagelocation.RackCode;
// 写补料日志
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;
inventorylog.CreatedBy = "PLC";
inventorylog.CreatedTime = DateTime.Now.ToLocalTime();
await DbScoped.SugarScope.CopyNew().Updateable(storagelocation).ExecuteCommandAsync();
}
else
{
if (storagelocation.PackageNum <= 2)
{
//补料成功
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 Task.Delay(5000, stoppingToken);
Console.WriteLine("永驻线程正在读取plc值 " + result);
index++;
if (index > 16)
{
@@ -77,7 +104,6 @@ namespace DOAN.ServiceCore
public void Dispose()
{
PLCTool.ConnectClose();
_cancellationTokenSource.Cancel();
_executingTask.Wait();
_cancellationTokenSource.Dispose();