2024-11-08 10:10:59 +08:00
|
|
|
|
using System;
|
2025-01-24 11:44:27 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2025-03-28 09:33:30 +08:00
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2025-01-24 11:44:27 +08:00
|
|
|
|
using System.Text.Json;
|
2024-11-08 10:10:59 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using DOAN.Infrastructure.PLC;
|
|
|
|
|
|
using DOAN.Model.PBL;
|
2025-01-24 11:44:27 +08:00
|
|
|
|
using DOAN.ServiceCore.Signalr;
|
2025-03-28 09:33:30 +08:00
|
|
|
|
using JinianNet.JNTemplate;
|
2025-01-24 11:44:27 +08:00
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
2025-03-28 09:33:30 +08:00
|
|
|
|
using Microsoft.Data.SqlClient;
|
2024-11-08 10:10:59 +08:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
using SqlSugar.IOC;
|
2025-03-28 09:33:30 +08:00
|
|
|
|
using static System.Formats.Asn1.AsnWriter;
|
2025-01-24 11:44:27 +08:00
|
|
|
|
|
2024-11-08 10:10:59 +08:00
|
|
|
|
namespace DOAN.ServiceCore
|
|
|
|
|
|
{
|
2025-01-16 10:13:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 永驻线程
|
|
|
|
|
|
/// 功能:检测传感器信号,判断箱子数
|
|
|
|
|
|
/// </summary>
|
2025-08-19 09:30:44 +08:00
|
|
|
|
public class DoanBackgroundService : BaseService<Storagelocation>, IHostedService, IDisposable
|
2024-11-08 10:10:59 +08:00
|
|
|
|
{
|
2025-01-24 11:44:27 +08:00
|
|
|
|
private readonly IHubContext<PBLhub> notificationHubContext;
|
2025-08-19 09:30:44 +08:00
|
|
|
|
private readonly CancellationTokenSource _cancellationTokenSource =
|
|
|
|
|
|
new CancellationTokenSource();
|
2024-11-08 10:10:59 +08:00
|
|
|
|
private Task _executingTask;
|
2024-11-13 15:53:15 +08:00
|
|
|
|
private PLCTool pLCTool;
|
2025-01-24 11:44:27 +08:00
|
|
|
|
private List<Storagelocation> storagelocationList = new List<Storagelocation>();
|
|
|
|
|
|
private List<PlcAddressTable> pointPositionList = new List<PlcAddressTable>();
|
2025-03-28 09:33:30 +08:00
|
|
|
|
private List<PlcButton> buttonPositionList = new List<PlcButton>();
|
2025-01-24 11:44:27 +08:00
|
|
|
|
private Timer refreshTimer;
|
|
|
|
|
|
|
|
|
|
|
|
public DoanBackgroundService(IHubContext<PBLhub> hubContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
notificationHubContext = hubContext;
|
|
|
|
|
|
}
|
2024-11-08 10:10:59 +08:00
|
|
|
|
|
|
|
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2025-03-28 09:33:30 +08:00
|
|
|
|
Console.WriteLine($"PCL定时任务开启!");
|
2024-11-13 15:53:15 +08:00
|
|
|
|
pLCTool = new PLCTool();
|
|
|
|
|
|
pLCTool.ConnectPLC();
|
2025-08-19 09:30:44 +08:00
|
|
|
|
// sql标准
|
|
|
|
|
|
Context.Queryable<Storagelocation>();
|
|
|
|
|
|
// 初始化料架层和点位表数据
|
|
|
|
|
|
RefreshData(null);
|
2025-01-24 11:44:27 +08:00
|
|
|
|
// 启动后台任务
|
|
|
|
|
|
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
|
2024-11-08 10:10:59 +08:00
|
|
|
|
|
2025-03-28 09:33:30 +08:00
|
|
|
|
// 设置定时器每10分钟刷新一次料架层和点位表 .FromMinutes(1)
|
|
|
|
|
|
refreshTimer = new Timer(RefreshData, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
|
2024-11-08 10:10:59 +08:00
|
|
|
|
|
|
|
|
|
|
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 请求取消后台任务
|
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
|
|
|
|
|
|
2025-01-24 11:44:27 +08:00
|
|
|
|
// 停止定时器
|
|
|
|
|
|
refreshTimer?.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
|
refreshTimer?.Dispose();
|
|
|
|
|
|
|
2024-11-08 10:10:59 +08:00
|
|
|
|
// 等待后台任务完成
|
|
|
|
|
|
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-24 11:44:27 +08:00
|
|
|
|
private static bool GetInvertedBit(byte b, int position)
|
2025-01-16 10:13:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (position < 0 || position > 7)
|
|
|
|
|
|
{
|
2025-08-19 09:30:44 +08:00
|
|
|
|
throw new ArgumentOutOfRangeException(
|
|
|
|
|
|
nameof(position),
|
|
|
|
|
|
"Position must be between 0 and 7" + position
|
|
|
|
|
|
);
|
2025-01-16 10:13:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
byte mask = (byte)(1 << position);
|
|
|
|
|
|
bool isSet = (b & mask) != 0;
|
2025-01-24 11:44:27 +08:00
|
|
|
|
return !isSet; // 返回取反后的值
|
2025-01-16 10:13:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-01-20 15:55:34 +08:00
|
|
|
|
/// 功能:检测传感器信号,判断箱子数并更新库存及日志
|
2025-01-16 10:13:50 +08:00
|
|
|
|
/// </summary>
|
2025-01-20 15:55:34 +08:00
|
|
|
|
/// <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)
|
|
|
|
|
|
{
|
2025-03-28 09:33:30 +08:00
|
|
|
|
Console.WriteLine($"PCL定时任务ExecuteAsync!");
|
2025-01-20 15:55:34 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-11-13 15:53:15 +08:00
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
|
|
|
|
{
|
2025-01-24 11:44:27 +08:00
|
|
|
|
// 读取PLC I/O状态(12个字节)
|
|
|
|
|
|
byte[] plcSensorValues;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-03-28 09:33:30 +08:00
|
|
|
|
// 读取1开头100,101。。。等全部地址数据
|
|
|
|
|
|
|
|
|
|
|
|
plcSensorValues = pLCTool.ReadAllValue("VB100", 13);
|
2025-01-24 11:44:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"读取PLC数据异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2025-03-28 09:33:30 +08:00
|
|
|
|
// XX 按钮处理
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 读取按钮PLC I/O状态
|
|
|
|
|
|
foreach (PlcButton button in buttonPositionList)
|
|
|
|
|
|
{
|
|
|
|
|
|
int row = button.Address - 100;
|
|
|
|
|
|
int col = button.Index;
|
|
|
|
|
|
|
2025-08-19 09:30:44 +08:00
|
|
|
|
if (
|
|
|
|
|
|
plcSensorValues != null
|
|
|
|
|
|
&& row >= 0
|
|
|
|
|
|
&& row < plcSensorValues.Length
|
|
|
|
|
|
&& !GetInvertedBit(plcSensorValues[row], col)
|
|
|
|
|
|
)
|
2025-03-28 09:33:30 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 按钮按下灭灯
|
|
|
|
|
|
if (button.Code == "灭灯按钮")
|
|
|
|
|
|
{
|
2025-08-19 09:30:44 +08:00
|
|
|
|
Storagelocation offLightstoragelocation = storagelocationList
|
|
|
|
|
|
.Where(it => it.Id == button.StoragelocationId)
|
|
|
|
|
|
.First();
|
|
|
|
|
|
bool isSuccess = pLCTool.WriteBit(
|
|
|
|
|
|
offLightstoragelocation.PlcAddress,
|
|
|
|
|
|
false
|
|
|
|
|
|
);
|
2025-03-28 09:33:30 +08:00
|
|
|
|
if (isSuccess)
|
|
|
|
|
|
{
|
|
|
|
|
|
offLightstoragelocation.IsLight = 0;
|
|
|
|
|
|
using (var scope = DbScoped.SugarScope.CopyNew())
|
|
|
|
|
|
{
|
2025-08-19 09:30:44 +08:00
|
|
|
|
await scope
|
|
|
|
|
|
.Updateable(offLightstoragelocation)
|
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-03-28 09:33:30 +08:00
|
|
|
|
//灭灯日志
|
|
|
|
|
|
Light_Log light_Log = new Light_Log
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = SnowFlakeSingle.Instance.NextId().ToString(),
|
|
|
|
|
|
LightOperation = 2,
|
|
|
|
|
|
LayerNum = offLightstoragelocation.LayerNum,
|
|
|
|
|
|
ShelfCode = offLightstoragelocation.RackCode,
|
|
|
|
|
|
IsSuccess = isSuccess,
|
|
|
|
|
|
Operationer = "按钮手动灭灯",
|
|
|
|
|
|
CreatedTime = DateTime.Now
|
|
|
|
|
|
};
|
|
|
|
|
|
await scope.Insertable(light_Log).ExecuteCommandAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-19 09:30:44 +08:00
|
|
|
|
await notificationHubContext.Clients.All.SendAsync(
|
|
|
|
|
|
"PBL_storagelocation_change",
|
|
|
|
|
|
"手动灭灯"
|
|
|
|
|
|
);
|
2025-03-28 09:33:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 镜体补料按钮
|
|
|
|
|
|
if (button.Code == "镜体补料按钮")
|
|
|
|
|
|
{
|
|
|
|
|
|
bool lightStatus = pLCTool.ReadBit("V208.4");
|
|
|
|
|
|
// 原本灭灯,亮灯补料
|
|
|
|
|
|
if (!lightStatus)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (var scope = DbScoped.SugarScope.CopyNew())
|
|
|
|
|
|
{
|
|
|
|
|
|
AlarmLog alarmLog = new AlarmLog
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = SnowFlakeSingle.Instance.NextId().ToString(),
|
|
|
|
|
|
Name = "镜体需要补料",
|
|
|
|
|
|
Code = "镜体补料",
|
|
|
|
|
|
StoragelocationId = 0,
|
|
|
|
|
|
Type = 2,
|
|
|
|
|
|
Status = 1,
|
|
|
|
|
|
ActionTime = DateTime.Now,
|
|
|
|
|
|
EndTime = DateTime.Now
|
|
|
|
|
|
};
|
|
|
|
|
|
scope.Insertable(alarmLog).ExecuteCommand();
|
|
|
|
|
|
var alarmData = new
|
|
|
|
|
|
{
|
|
|
|
|
|
RackCode = "镜体需要补料",
|
|
|
|
|
|
LayerNum = 1,
|
|
|
|
|
|
CurrentPackageCount = 0,
|
|
|
|
|
|
AlarmThreshold = 1,
|
2025-08-19 09:30:44 +08:00
|
|
|
|
ActionTime = DateTime
|
|
|
|
|
|
.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
|
|
|
|
.ToString(),
|
2025-03-28 09:33:30 +08:00
|
|
|
|
};
|
2025-08-19 09:30:44 +08:00
|
|
|
|
string alarmMessage =
|
|
|
|
|
|
System.Text.Json.JsonSerializer.Serialize(alarmData);
|
|
|
|
|
|
await notificationHubContext.Clients.All.SendAsync(
|
|
|
|
|
|
"PBL_lack_alarm",
|
|
|
|
|
|
alarmMessage
|
|
|
|
|
|
);
|
2025-03-28 09:33:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 原本亮灯,按一下灭灯
|
|
|
|
|
|
pLCTool.WriteBit("V208.4", false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-24 11:44:27 +08:00
|
|
|
|
var updateStoragelocationList = new List<Storagelocation>();
|
|
|
|
|
|
var inventoryLogs = new List<Inventorylog>();
|
2025-01-16 10:13:50 +08:00
|
|
|
|
|
2025-01-24 11:44:27 +08:00
|
|
|
|
foreach (var layerItem in storagelocationList)
|
2025-01-20 15:55:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 获取这个料架层的点位表
|
2025-08-19 09:30:44 +08:00
|
|
|
|
var layerPoints = pointPositionList
|
|
|
|
|
|
.Where(it => it.FkStorageId == layerItem.Id)
|
|
|
|
|
|
.ToList();
|
2025-01-16 10:13:50 +08:00
|
|
|
|
|
2025-01-24 11:44:27 +08:00
|
|
|
|
if (layerPoints.Any())
|
2024-11-13 15:53:15 +08:00
|
|
|
|
{
|
2025-01-20 16:15:40 +08:00
|
|
|
|
int currentPackageCount = 0; // 默认最小值为0
|
2024-11-08 10:10:59 +08:00
|
|
|
|
|
2025-01-24 11:44:27 +08:00
|
|
|
|
foreach (var point in layerPoints)
|
2024-11-13 15:53:15 +08:00
|
|
|
|
{
|
2025-01-20 15:55:34 +08:00
|
|
|
|
int row = point.ByteNum - 100;
|
|
|
|
|
|
int col = point.BitNum - 1;
|
2025-01-16 10:13:50 +08:00
|
|
|
|
|
2025-08-19 09:30:44 +08:00
|
|
|
|
if (
|
|
|
|
|
|
plcSensorValues != null
|
|
|
|
|
|
&& row >= 0
|
|
|
|
|
|
&& row < plcSensorValues.Length
|
|
|
|
|
|
)
|
2025-01-16 10:13:50 +08:00
|
|
|
|
{
|
2025-08-19 09:30:44 +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
|
|
|
|
|
2025-01-20 15:55:34 +08:00
|
|
|
|
// 检查箱子数量变化并记录日志
|
2025-01-24 11:44:27 +08:00
|
|
|
|
int previousPackageCount = layerItem.PackageNum.GetValueOrDefault();
|
|
|
|
|
|
if (currentPackageCount > previousPackageCount)
|
2025-01-20 15:55:34 +08:00
|
|
|
|
{
|
2025-08-19 09:30:44 +08:00
|
|
|
|
UpdateAndLog(
|
|
|
|
|
|
layerItem,
|
|
|
|
|
|
currentPackageCount,
|
|
|
|
|
|
2,
|
|
|
|
|
|
"出库",
|
|
|
|
|
|
inventoryLogs
|
|
|
|
|
|
);
|
2025-01-20 15:55:34 +08:00
|
|
|
|
updateStoragelocationList.Add(layerItem);
|
|
|
|
|
|
}
|
2025-01-24 11:44:27 +08:00
|
|
|
|
else if (currentPackageCount < previousPackageCount)
|
2025-01-20 15:55:34 +08:00
|
|
|
|
{
|
2025-08-19 09:30:44 +08:00
|
|
|
|
UpdateAndLog(
|
|
|
|
|
|
layerItem,
|
|
|
|
|
|
currentPackageCount,
|
|
|
|
|
|
1,
|
|
|
|
|
|
"入库",
|
|
|
|
|
|
inventoryLogs
|
|
|
|
|
|
);
|
2025-01-20 15:55:34 +08:00
|
|
|
|
updateStoragelocationList.Add(layerItem);
|
|
|
|
|
|
}
|
2025-01-24 11:44:27 +08:00
|
|
|
|
|
|
|
|
|
|
// 补料报警触发
|
2025-08-19 09:30:44 +08:00
|
|
|
|
if (
|
|
|
|
|
|
layerItem.IsLackAlarm == 1
|
|
|
|
|
|
&& currentPackageCount < layerItem.AlarmNum
|
|
|
|
|
|
)
|
2025-01-24 11:44:27 +08:00
|
|
|
|
{
|
2025-03-28 09:33:30 +08:00
|
|
|
|
using (var scope = DbScoped.SugarScope.CopyNew())
|
2025-01-24 11:44:27 +08:00
|
|
|
|
{
|
2025-03-28 09:33:30 +08:00
|
|
|
|
// 是否已经报警过,并且
|
2025-08-19 09:30:44 +08:00
|
|
|
|
bool hasLastAlarm = await scope
|
|
|
|
|
|
.Queryable<AlarmLog>()
|
2025-03-28 09:33:30 +08:00
|
|
|
|
.Where(it => it.StoragelocationId == layerItem.Id)
|
|
|
|
|
|
.Where(it => it.Type == 1)
|
|
|
|
|
|
.Where(it => it.Status == 1)
|
|
|
|
|
|
.Where(it => DateTime.Now <= it.EndTime)
|
|
|
|
|
|
.OrderBy(it => it.ActionTime, OrderByType.Desc)
|
|
|
|
|
|
.AnyAsync();
|
|
|
|
|
|
if (!hasLastAlarm)
|
|
|
|
|
|
{
|
|
|
|
|
|
string layerName = layerItem.LayerNum == 1 ? "上层" : "中层";
|
|
|
|
|
|
AlarmLog alarmLog = new AlarmLog
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = SnowFlakeSingle.Instance.NextId().ToString(),
|
2025-08-19 09:30:44 +08:00
|
|
|
|
Name =
|
|
|
|
|
|
$"镜壳需补料:{layerItem.RackCode} {layerName} 阈值{layerItem.AlarmNum}",
|
2025-03-28 09:33:30 +08:00
|
|
|
|
Code = "镜壳补料",
|
|
|
|
|
|
StoragelocationId = layerItem.Id,
|
|
|
|
|
|
Type = 1,
|
|
|
|
|
|
Status = 1,
|
|
|
|
|
|
ActionTime = DateTime.Now,
|
|
|
|
|
|
// 过X分钟超时jishi
|
|
|
|
|
|
EndTime = DateTime.Now.AddMinutes(2)
|
|
|
|
|
|
};
|
|
|
|
|
|
await scope.Insertable(alarmLog).ExecuteCommandAsync();
|
|
|
|
|
|
var alarmData = new
|
|
|
|
|
|
{
|
|
|
|
|
|
RackCode = layerItem.RackCode,
|
|
|
|
|
|
LayerNum = layerItem.LayerNum,
|
|
|
|
|
|
CurrentPackageCount = currentPackageCount,
|
|
|
|
|
|
AlarmThreshold = layerItem.AlarmNum,
|
2025-08-19 09:30:44 +08:00
|
|
|
|
ActionTime = DateTime
|
|
|
|
|
|
.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
|
|
|
|
.ToString(),
|
2025-03-28 09:33:30 +08:00
|
|
|
|
};
|
2025-08-19 09:30:44 +08:00
|
|
|
|
string alarmMessage =
|
|
|
|
|
|
System.Text.Json.JsonSerializer.Serialize(alarmData);
|
|
|
|
|
|
await notificationHubContext.Clients.All.SendAsync(
|
|
|
|
|
|
"PBL_lack_alarm",
|
|
|
|
|
|
alarmMessage
|
|
|
|
|
|
);
|
2025-03-28 09:33:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-24 11:44:27 +08:00
|
|
|
|
}
|
2025-01-16 10:13:50 +08:00
|
|
|
|
}
|
2024-11-08 11:31:08 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-20 15:55:34 +08:00
|
|
|
|
// 更新库存
|
2025-01-24 11:44:27 +08:00
|
|
|
|
if (updateStoragelocationList.Any())
|
2025-01-20 15:55:34 +08:00
|
|
|
|
{
|
2025-03-28 09:33:30 +08:00
|
|
|
|
using (var scope = DbScoped.SugarScope.CopyNew())
|
|
|
|
|
|
{
|
2025-08-19 09:30:44 +08:00
|
|
|
|
await scope
|
|
|
|
|
|
.Updateable(updateStoragelocationList)
|
|
|
|
|
|
.IgnoreColumns(it => new
|
|
|
|
|
|
{
|
|
|
|
|
|
it.IsLight,
|
|
|
|
|
|
it.IsLackAlarm,
|
|
|
|
|
|
it.AlarmNum,
|
|
|
|
|
|
it.MaxCapacity
|
|
|
|
|
|
})
|
2025-03-28 09:33:30 +08:00
|
|
|
|
.ExecuteCommandAsync();
|
|
|
|
|
|
}
|
2025-01-24 13:09:04 +08:00
|
|
|
|
// 发送库存变更Socket通知
|
|
|
|
|
|
string changeMessage = "库存变动";
|
2025-08-19 09:30:44 +08:00
|
|
|
|
await notificationHubContext.Clients.All.SendAsync(
|
|
|
|
|
|
"PBL_storagelocation_change",
|
|
|
|
|
|
changeMessage
|
|
|
|
|
|
);
|
2025-01-20 15:55:34 +08:00
|
|
|
|
}
|
2024-11-08 11:31:08 +08:00
|
|
|
|
|
2025-01-20 15:55:34 +08:00
|
|
|
|
// 插入库存变更日志
|
2025-01-24 11:44:27 +08:00
|
|
|
|
if (inventoryLogs.Any())
|
2025-01-20 15:55:34 +08:00
|
|
|
|
{
|
2025-08-19 09:30:44 +08:00
|
|
|
|
await DbScoped
|
|
|
|
|
|
.SugarScope.CopyNew()
|
|
|
|
|
|
.Insertable(inventoryLogs)
|
|
|
|
|
|
.ExecuteCommandAsync();
|
2025-01-20 15:55:34 +08:00
|
|
|
|
}
|
2025-03-28 09:33:30 +08:00
|
|
|
|
// 添加延迟以避免频繁查询(暂定3秒防误触)
|
|
|
|
|
|
await Task.Delay(2000, stoppingToken);
|
2024-11-08 10:10:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-20 15:55:34 +08:00
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("任务已取消");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-08-19 09:30:44 +08:00
|
|
|
|
Console.WriteLine(
|
|
|
|
|
|
$"DoanBackGround线程ExecuteAsync异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}"
|
|
|
|
|
|
);
|
2025-01-20 15:55:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-24 11:44:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 刷新料架层和点位表数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="state"></param>
|
2025-03-28 09:33:30 +08:00
|
|
|
|
private async void RefreshData(object state)
|
2025-01-24 11:44:27 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-03-28 09:33:30 +08:00
|
|
|
|
Console.WriteLine($"刷新料架点位和按钮");
|
2025-01-24 11:44:27 +08:00
|
|
|
|
using (var scope = DbScoped.SugarScope.CopyNew())
|
|
|
|
|
|
{
|
2025-03-28 09:33:30 +08:00
|
|
|
|
storagelocationList = await scope.Queryable<Storagelocation>().ToListAsync();
|
|
|
|
|
|
pointPositionList = await scope.Queryable<PlcAddressTable>().ToListAsync();
|
|
|
|
|
|
buttonPositionList = await scope.Queryable<PlcButton>().ToListAsync();
|
2025-01-24 11:44:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"刷新数据异常: {ex.Message}\n堆栈跟踪: {ex.StackTrace}");
|
|
|
|
|
|
// 如果刷新失败,保持现有数据不变
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-20 15:55:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新料架位置的箱子数量并记录库存日志
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="storageLocation">料架位置对象</param>
|
|
|
|
|
|
/// <param name="newPackageCount">新的箱子数量</param>
|
|
|
|
|
|
/// <param name="operation">操作类型 (1-出库, 2-入库)</param>
|
|
|
|
|
|
/// <param name="operatorName">操作员名称</param>
|
|
|
|
|
|
/// <param name="inventoryLogs">库存日志列表</param>
|
2025-01-24 11:44:27 +08:00
|
|
|
|
private void UpdateAndLog(
|
|
|
|
|
|
Storagelocation storageLocation,
|
|
|
|
|
|
int newPackageCount,
|
|
|
|
|
|
int operation,
|
|
|
|
|
|
string operatorName,
|
2025-08-19 09:30:44 +08:00
|
|
|
|
List<Inventorylog> inventoryLogs
|
|
|
|
|
|
)
|
2025-01-20 15:55:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
storageLocation.PackageNum = newPackageCount;
|
|
|
|
|
|
storageLocation.UpdatedBy = operatorName;
|
|
|
|
|
|
storageLocation.UpdatedTime = DateTime.Now;
|
|
|
|
|
|
|
2025-01-24 11:44:27 +08:00
|
|
|
|
var inventoryLog = new Inventorylog
|
2025-01-20 15:55:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
Id = SnowFlakeSingle.Instance.NextId().ToString(),
|
|
|
|
|
|
RackCode = storageLocation.RackCode,
|
|
|
|
|
|
Partnumber = storageLocation.Partnumber,
|
|
|
|
|
|
Operation = operation,
|
2025-08-19 09:30:44 +08:00
|
|
|
|
PackageNum = Math.Abs(
|
|
|
|
|
|
newPackageCount - storageLocation.PackageNum.GetValueOrDefault()
|
|
|
|
|
|
),
|
2025-01-20 15:55:34 +08:00
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
_cancellationTokenSource.Cancel();
|
2025-03-28 09:33:30 +08:00
|
|
|
|
_executingTask?.GetAwaiter().GetResult(); // 确保任务完成
|
2024-11-13 15:53:15 +08:00
|
|
|
|
_cancellationTokenSource.Dispose();
|
2025-01-24 11:44:27 +08:00
|
|
|
|
refreshTimer?.Change(Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
|
refreshTimer?.Dispose();
|
2025-03-28 09:33:30 +08:00
|
|
|
|
pLCTool.ConnectClose();
|
2024-11-13 15:53:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("DoanBackGround线程Dispose异常:" + ex.Message);
|
|
|
|
|
|
}
|
2024-11-08 10:10:59 +08:00
|
|
|
|
}
|
2025-01-24 11:44:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|