pblbackend/DOAN.ServiceCore/DoanBackgroundService.cs

194 lines
7.3 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
{
2025-01-16 10:13:50 +08:00
/// <summary>
/// 永驻线程
/// 功能:检测传感器信号,判断箱子数
/// </summary>
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));
}
2025-01-16 10:13:50 +08:00
private static bool GetInvertedBit(byte b, int position)
{
if (position < 0 || position > 7)
{
throw new ArgumentOutOfRangeException(nameof(position), "Position must be between 0 and 7.");
}
// 创建一个掩码其中只有要检查的那一位是1
byte mask = (byte)(1 << position);
// 使用按位与运算符(&)来检查该位是否为1
bool isSet = (b & mask) != 0;
// 返回取反后的值
return !isSet;
}
/// <summary>
/// 功能:检测传感器信号,判断箱子数并更新库存及日志
2025-01-16 10:13:50 +08:00
/// </summary>
/// <param name="stoppingToken">取消令牌</param>
2025-01-16 10:13:50 +08:00
/// <returns></returns>
2024-11-08 10:10:59 +08:00
private async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
2024-11-13 15:53:15 +08:00
while (!stoppingToken.IsCancellationRequested)
{
// 读取PLC I/O状态 12个数组
byte[] plcSensorValues = pLCTool.ReadAllValue("VB100", 12);
2024-11-08 10:10:59 +08:00
// 获取所有料架层
2025-01-16 10:13:50 +08:00
List<Storagelocation> storagelocationList = await DbScoped.SugarScope.CopyNew().Queryable<Storagelocation>().ToListAsync();
// 获取点位表
2025-01-16 10:13:50 +08:00
List<PlcAddressTable> pointPositionList = await DbScoped.SugarScope.CopyNew().Queryable<PlcAddressTable>().ToListAsync();
List<Storagelocation> updateStoragelocationList = [];
List<Inventorylog> inventoryLogs = [];
2025-01-16 10:13:50 +08:00
foreach (Storagelocation layerItem in storagelocationList)
{
// 获取这个料架层的点位表
List<PlcAddressTable> layerPoints = pointPositionList.Where(it => it.FkStorageId == layerItem.Id).ToList();
2025-01-16 10:13:50 +08:00
if (layerPoints != null && layerPoints.Count > 0)
2024-11-13 15:53:15 +08:00
{
int currentPackageCount = 1; // 默认最小值为1
2024-11-08 10:10:59 +08:00
foreach (PlcAddressTable point in layerPoints)
2024-11-13 15:53:15 +08:00
{
int row = point.ByteNum - 100;
int col = point.BitNum - 1;
2025-01-16 10:13:50 +08:00
if (plcSensorValues != null)
2025-01-16 10:13:50 +08:00
{
currentPackageCount += GetInvertedBit(plcSensorValues[row], col) ? 1 : 0;
2025-01-16 10:13:50 +08:00
}
2024-11-13 15:53:15 +08:00
}
2024-11-08 11:31:08 +08:00
// 检查箱子数量变化并记录日志
if (currentPackageCount > layerItem.PackageNum)
{
UpdateAndLog(layerItem, currentPackageCount, 2, "补料", inventoryLogs);
updateStoragelocationList.Add(layerItem);
}
else if (currentPackageCount < layerItem.PackageNum)
{
UpdateAndLog(layerItem, currentPackageCount, 1, "出料", inventoryLogs);
updateStoragelocationList.Add(layerItem);
}
2025-01-16 10:13:50 +08:00
}
2024-11-08 11:31:08 +08:00
}
// 更新库存
if (updateStoragelocationList.Count > 0)
{
await DbScoped.SugarScope.CopyNew().Updateable(updateStoragelocationList).ExecuteCommandAsync();
}
2024-11-08 11:31:08 +08:00
// 插入库存变更日志
if (inventoryLogs.Count > 0)
{
await DbScoped.SugarScope.CopyNew().Insertable(inventoryLogs).ExecuteCommandAsync();
}
2024-11-08 11:31:08 +08:00
// 添加延迟以避免频繁查询
await Task.Delay(200, stoppingToken);
2024-11-08 10:10:59 +08:00
}
}
catch (OperationCanceledException)
{
Console.WriteLine("任务已取消");
}
catch (Exception ex)
{
Console.WriteLine($"DoanBackGround线程ExecuteAsync异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
}
}
/// <summary>
/// 更新料架位置的箱子数量并记录库存日志
/// </summary>
/// <param name="storageLocation">料架位置对象</param>
/// <param name="newPackageCount">新的箱子数量</param>
/// <param name="operation">操作类型 (1-出库, 2-入库)</param>
/// <param name="operatorName">操作员名称</param>
/// <param name="inventoryLogs">库存日志列表</param>
private void UpdateAndLog(Storagelocation storageLocation, int newPackageCount, int operation, string operatorName, List<Inventorylog> inventoryLogs)
{
storageLocation.PackageNum = newPackageCount;
storageLocation.UpdatedBy = operatorName;
storageLocation.UpdatedTime = DateTime.Now;
Inventorylog inventoryLog = new Inventorylog
{
Id = SnowFlakeSingle.Instance.NextId().ToString(),
RackCode = storageLocation.RackCode,
Partnumber = storageLocation.Partnumber,
Operation = operation,
PackageNum = Math.Abs((int)(newPackageCount - storageLocation.PackageNum)),
CreatedBy = operatorName,
CreatedTime = DateTime.Now.ToLocalTime()
};
inventoryLogs.Add(inventoryLog);
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
}
}