2025-03-16 03:17:36 +08:00
|
|
|
using System.Collections.Concurrent;
|
2025-03-17 02:37:39 +08:00
|
|
|
using System.Collections.ObjectModel;
|
2025-03-18 18:32:10 +08:00
|
|
|
using System.Text.Json;
|
2025-03-16 03:17:36 +08:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Seyounth.Hyosung.Core.Agv;
|
|
|
|
using Seyounth.Hyosung.Core.Plc;
|
|
|
|
using Seyounth.Hyosung.Core.Printer;
|
|
|
|
using Seyounth.Hyosung.Core.Scanner;
|
|
|
|
using Seyounth.Hyosung.Data.Models;
|
|
|
|
using Seyounth.Hyosung.Data.Models.Plc;
|
|
|
|
using Seyounth.Hyosung.Data.Services;
|
|
|
|
using Seyounth.Hyosung.Data.Services.Hyosung;
|
2025-03-17 02:37:39 +08:00
|
|
|
using Seyounth.Hyosung.Runtime.Models;
|
2025-03-16 03:17:36 +08:00
|
|
|
|
|
|
|
namespace Seyounth.Hyosung.Runtime;
|
|
|
|
|
|
|
|
public class HyosungRuntime(
|
|
|
|
ILogger<HyosungRuntime> logger,
|
|
|
|
IHyosungPlcService hyosungPlcService,
|
|
|
|
IHyosungScannerService hyosungScannerService,
|
|
|
|
IHyosungPrinter printer,
|
|
|
|
IYarnService yarnService,
|
|
|
|
ITrayService trayService,
|
|
|
|
IVarietyService varietyService,
|
|
|
|
IHyosungAgvService hyosungAgvService,
|
|
|
|
IHyosungWmsService hyosungWmsService) : IHyosungRuntime
|
|
|
|
{
|
2025-03-17 03:26:29 +08:00
|
|
|
public PackLineOption PackLineOption { get; private set; }
|
2025-03-17 02:37:39 +08:00
|
|
|
public StackStationModel Stack1 { get; private set; } = new();
|
|
|
|
public StackStationModel Stack2 { get; private set; } = new();
|
|
|
|
|
2025-03-16 03:17:36 +08:00
|
|
|
public async Task StartAsync(CancellationToken token)
|
|
|
|
{
|
|
|
|
//启动扫码服务
|
|
|
|
await hyosungScannerService.StartAsync(token);
|
2025-03-17 22:17:28 +08:00
|
|
|
await printer.StartAsync(token);
|
2025-03-16 03:17:36 +08:00
|
|
|
//最后启动PLC服务
|
|
|
|
hyosungPlcService.OnPlcRequestScanProduct += OnPlcRequestScanProduct;
|
|
|
|
hyosungPlcService.OnPlcRequestScanFixture += OnPlcRequestScanFixture;
|
|
|
|
hyosungPlcService.OnPlcRequestLeavingProductionLine += OnPlcRequestLeavingProductionLine;
|
|
|
|
hyosungPlcService.OnPlcNeedNewTrayCode += OnPlcNeedNewTrayCode;
|
|
|
|
hyosungPlcService.OnPlcPutCompleted += OnPlcPutCompleted;
|
|
|
|
hyosungPlcService.OnPlcRequestPackLineOption += OnPlcRequestPackLineOption;
|
|
|
|
hyosungPlcService.OnRequestPrintLabel += OnPlcRequestPrintLabel;
|
|
|
|
await hyosungPlcService.StartAsync(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task StopAsync(CancellationToken token)
|
|
|
|
{
|
|
|
|
//先停止扫码服务
|
2025-03-17 22:17:28 +08:00
|
|
|
await hyosungScannerService.StopAsync(token);
|
2025-03-16 03:17:36 +08:00
|
|
|
await printer.StopAsync(token);
|
|
|
|
//先停止PLC服务
|
|
|
|
//解绑相关事件
|
|
|
|
hyosungPlcService.OnPlcRequestScanProduct -= OnPlcRequestScanProduct;
|
|
|
|
hyosungPlcService.OnPlcRequestScanFixture -= OnPlcRequestScanFixture;
|
|
|
|
hyosungPlcService.OnPlcRequestLeavingProductionLine -= OnPlcRequestLeavingProductionLine;
|
|
|
|
hyosungPlcService.OnPlcNeedNewTrayCode -= OnPlcNeedNewTrayCode;
|
|
|
|
hyosungPlcService.OnPlcPutCompleted -= OnPlcPutCompleted;
|
|
|
|
await hyosungPlcService.StopAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SendVarietyToPlcAsync(Variety variety)
|
|
|
|
{
|
|
|
|
await hyosungPlcService.WriteVarietyAsync(variety);
|
|
|
|
logger.LogInformation($"send variety to plc success: {variety.Id}");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 处理PLC请求扫描治具事件
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="fixtureId">治具ID</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
private async Task OnPlcRequestScanFixture(int fixtureId)
|
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogInformation($"plc request scan fixture:{fixtureId}");
|
|
|
|
try
|
2025-03-16 03:17:36 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
var fix = await hyosungScannerService.ScanFixtureAsync(fixtureId);
|
|
|
|
if (string.IsNullOrEmpty(fix))
|
|
|
|
{
|
|
|
|
await hyosungPlcService.WriteScanFixtureResultAsync(fixtureId, false);
|
|
|
|
logger.LogInformation($"scan fixture{fixtureId} fail");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await hyosungPlcService.WriteScanFixtureResultAsync(fixtureId, true, (short)Convert.ToInt32(fix));
|
|
|
|
logger.LogInformation($"scan fixture{fixtureId} success: {fix}");
|
|
|
|
}
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
2025-03-18 18:32:10 +08:00
|
|
|
catch (Exception e)
|
2025-03-16 03:17:36 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
await hyosungPlcService.WriteScanFixtureResultAsync(fixtureId, false);
|
|
|
|
logger.LogError(e, $"scan fixture{fixtureId} fail");
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-18 18:32:10 +08:00
|
|
|
|
2025-03-16 03:17:36 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 处理PLC请求扫描纱事件
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="varietyId"></param>
|
|
|
|
private async Task OnPlcRequestScanProduct(int varietyId)
|
|
|
|
{
|
|
|
|
logger.LogInformation($"plc request scan yarn qrcode");
|
2025-03-18 18:32:10 +08:00
|
|
|
try
|
2025-03-16 03:17:36 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
var yarn = await hyosungScannerService.ScanYarnAsync(varietyId);
|
|
|
|
if (yarn is null)
|
|
|
|
{
|
|
|
|
await hyosungPlcService.WriteScanYarnResultAsync(false);
|
|
|
|
logger.LogInformation($"scan yarn fail");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var variety = await varietyService.GetById(varietyId);
|
|
|
|
if (yarn.Lot != variety.Lot)
|
|
|
|
{
|
|
|
|
await hyosungPlcService.WriteScanYarnResultAsync(false);
|
|
|
|
logger.LogInformation($"scan yarn fail,lot not equal");
|
|
|
|
return;
|
|
|
|
}
|
2025-03-16 03:17:36 +08:00
|
|
|
|
2025-03-18 18:32:10 +08:00
|
|
|
await yarnService.AddYarnAsync(yarn);
|
|
|
|
await hyosungPlcService.WriteScanYarnResultAsync(true, (short)varietyId, yarn.ScanCode);
|
|
|
|
logger.LogInformation($"scan yarn {yarn.ScanCode} success: qrcode[{yarn.QrCode}]");
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
2025-03-18 05:24:15 +08:00
|
|
|
{
|
|
|
|
await hyosungPlcService.WriteScanYarnResultAsync(false);
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogError(e, $"scan yarn fail");
|
2025-03-18 05:24:15 +08:00
|
|
|
}
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 处理PLC请求下线事件
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
private async Task OnPlcRequestLeavingProductionLine(PlcStackInfo info)
|
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogInformation($"plc request leaving production line");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await trayService.StorageAsync(info.TrayCode);
|
|
|
|
await hyosungAgvService.StorageAsync(info.TrayCode);
|
|
|
|
//标志下线已完成
|
|
|
|
await hyosungPlcService.LeaveCompletedAsync();
|
|
|
|
logger.LogInformation($"plc leaving production line success");
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
logger.LogError(e, $"plc leaving production line fail");
|
|
|
|
}
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 处理Plc需要新的托盘号事件
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="index"></param>
|
|
|
|
/// <param name="varietyId"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
private async Task OnPlcNeedNewTrayCode(int index, int varietyId)
|
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogInformation($"plc request new tray code:{index} {varietyId}");
|
|
|
|
try
|
2025-03-17 02:37:39 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
var tray = await trayService.GeneraNewTray(varietyId);
|
|
|
|
var variety = await varietyService.GetById(varietyId);
|
|
|
|
var stackModel = new StackStationModel();
|
|
|
|
stackModel.TrayCode = tray.TrayCode;
|
|
|
|
stackModel.VarietyCode = variety.Code;
|
|
|
|
stackModel.Layers = variety.StackingLayers;
|
|
|
|
stackModel.TotalCount = variety.TotalCount;
|
|
|
|
stackModel.CurrentCount = 0;
|
|
|
|
stackModel.Yarns = new ObservableCollection<Yarn>();
|
|
|
|
if (index == 1)
|
|
|
|
{
|
|
|
|
Stack1 = stackModel;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Stack2 = stackModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
await hyosungPlcService.WriteTrayCodeAsync(index, tray.TrayCode);
|
|
|
|
logger.LogInformation($"plc request new tray code success: {tray.TrayCode}");
|
2025-03-17 02:37:39 +08:00
|
|
|
}
|
2025-03-18 18:32:10 +08:00
|
|
|
catch (Exception e)
|
2025-03-17 02:37:39 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogError(e, $"plc request new tray code fail");
|
2025-03-17 02:37:39 +08:00
|
|
|
}
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// PLC码垛一次完成
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="arg"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
|
|
private async Task OnPlcPutCompleted(PlcStackingInfo arg)
|
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogInformation($"plc put completed:{JsonSerializer.Serialize(arg)}");
|
|
|
|
try
|
2025-03-17 02:37:39 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
if (arg.TrayCode == Stack1.TrayCode)
|
2025-03-17 02:37:39 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
Stack1.CurrentCount += arg.YarnCode.Count;
|
|
|
|
foreach (var yarnCode in arg.YarnCode)
|
|
|
|
{
|
|
|
|
Stack1.Yarns.Add(await yarnService.GetYarnByCodeAsync(yarnCode));
|
|
|
|
}
|
2025-03-17 02:37:39 +08:00
|
|
|
}
|
2025-03-18 18:32:10 +08:00
|
|
|
else if (arg.TrayCode == Stack2.TrayCode)
|
|
|
|
{
|
|
|
|
Stack2.CurrentCount += arg.YarnCode.Count;
|
|
|
|
foreach (var yarnCode in arg.YarnCode)
|
|
|
|
{
|
|
|
|
Stack2.Yarns.Add(await yarnService.GetYarnByCodeAsync(yarnCode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-17 02:37:39 +08:00
|
|
|
foreach (var yarnCode in arg.YarnCode)
|
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
await yarnService.BindTrayAsync(yarnCode, await trayService.GetIdByCode(arg.TrayCode));
|
|
|
|
logger.LogInformation($" stack yarn{yarnCode} succeed");
|
2025-03-17 02:37:39 +08:00
|
|
|
}
|
|
|
|
|
2025-03-18 18:32:10 +08:00
|
|
|
await hyosungPlcService.WriteReceivedYarnCountAsync(arg.YarnCode.Count);
|
|
|
|
logger.LogInformation($"plc put completed success");
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
2025-03-16 03:17:36 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogError(e, $"plc put completed fail");
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 处理PLC请求打包线配置事件
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="arg"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
2025-03-20 19:32:49 +08:00
|
|
|
private async Task OnPlcRequestPackLineOption(string arg)
|
2025-03-16 03:17:36 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogInformation($"plc request pack line option");
|
|
|
|
try
|
2025-03-16 03:17:36 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
var tray = await trayService.GetByCode(arg);
|
|
|
|
var variety = await varietyService.GetById(tray.VarietyId);
|
|
|
|
PackLineOption = new PackLineOption()
|
|
|
|
{
|
|
|
|
HeadCount = variety.StackHeadCount ?? 0,
|
|
|
|
HasBox = variety.HasBox,
|
|
|
|
IsTop = variety.NeedTopBoard is NeedType.Need or NeedType.NotNeed
|
|
|
|
? variety.NeedTopBoard == NeedType.Need
|
|
|
|
: tray.IsEven && variety.NeedTopBoard == NeedType.EvenNeed,
|
|
|
|
IsPack = variety.NeedPackStrap is NeedType.Need or NeedType.NotNeed
|
|
|
|
? variety.NeedPackStrap == NeedType.Need
|
|
|
|
: tray.IsEven && variety.NeedPackStrap == NeedType.EvenNeed,
|
|
|
|
IsFilm = variety.NeedFilmWrapping is NeedType.Need or NeedType.NotNeed
|
|
|
|
? variety.NeedFilmWrapping == NeedType.Need
|
|
|
|
: tray.IsEven && variety.NeedFilmWrapping == NeedType.EvenNeed,
|
|
|
|
IsLam = variety.NeedFilmCoating is NeedType.Need or NeedType.NotNeed
|
|
|
|
? variety.NeedFilmCoating == NeedType.Need
|
|
|
|
: tray.IsEven && variety.NeedFilmCoating == NeedType.EvenNeed,
|
|
|
|
TrayCode = arg
|
|
|
|
};
|
|
|
|
await hyosungPlcService.WritePackLineOptionAsync(PackLineOption);
|
|
|
|
logger.LogInformation($"plc request pack line option success");
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
logger.LogError(e, $"plc request pack line option fail");
|
|
|
|
}
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 处理贴标站请求贴标事件
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="arg1"></param>
|
|
|
|
/// <param name="arg2"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
/// <exception cref="NotImplementedException"></exception>
|
2025-03-20 19:32:49 +08:00
|
|
|
private async Task OnPlcRequestPrintLabel(int arg1, string trayCode, int height)
|
2025-03-16 03:17:36 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogInformation($"plc request print label:{arg1} {trayCode} {height}");
|
2025-03-16 03:17:36 +08:00
|
|
|
try
|
|
|
|
{
|
2025-03-20 19:32:49 +08:00
|
|
|
var tray = await trayService.GetByCode(trayCode);
|
|
|
|
var variety = await varietyService.GetById(tray.VarietyId);
|
|
|
|
var mod = await hyosungWmsService.GetItemInfoByItemCode(variety.Code);
|
|
|
|
var grade = "1";
|
|
|
|
if (mod.GRADE != "AA") grade = mod.GRADE;
|
|
|
|
var controlNo = await hyosungWmsService.GetControlNo(variety, grade);
|
2025-03-19 02:42:56 +08:00
|
|
|
await trayService.PrintTrayAsync(trayCode, height, controlNo, mod);
|
|
|
|
if (arg1 == 1)
|
|
|
|
{
|
2025-03-20 19:32:49 +08:00
|
|
|
await printer.PrintAsync(1, trayCode);
|
|
|
|
await hyosungPlcService.WritePrintLabelResultAsync(arg1, variety.SubLabelCount, true);
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
|
|
|
else
|
2025-03-20 19:32:49 +08:00
|
|
|
{
|
2025-03-18 18:32:10 +08:00
|
|
|
await printer.PrintAsync(2, trayCode);
|
2025-03-20 19:32:49 +08:00
|
|
|
await hyosungPlcService.WritePrintLabelResultAsync(arg1, variety.MasterLabelCount, true);
|
|
|
|
}
|
|
|
|
|
2025-03-16 03:17:36 +08:00
|
|
|
|
2025-03-19 02:42:56 +08:00
|
|
|
await hyosungWmsService.UpdateControlNo(variety, controlNo);
|
2025-03-18 18:32:10 +08:00
|
|
|
logger.LogInformation($"plc request print label success");
|
2025-03-16 03:17:36 +08:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2025-03-20 19:32:49 +08:00
|
|
|
await hyosungPlcService.WritePrintLabelResultAsync(arg1, 1,false);
|
2025-03-16 03:17:36 +08:00
|
|
|
logger.LogError(e, "print label fail");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|