212 lines
8.2 KiB
C#
212 lines
8.2 KiB
C#
![]() |
using System.Collections.Concurrent;
|
||
|
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;
|
||
|
|
||
|
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
|
||
|
{
|
||
|
public async Task StartAsync(CancellationToken token)
|
||
|
{
|
||
|
//启动扫码服务
|
||
|
await hyosungScannerService.StartAsync(token);
|
||
|
await printer.StartAsync(token);
|
||
|
//最后启动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)
|
||
|
{
|
||
|
//先停止扫码服务
|
||
|
// await hyosungScannerService.StopAsync(token);
|
||
|
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)
|
||
|
{
|
||
|
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}");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 处理PLC请求扫描纱事件
|
||
|
/// </summary>
|
||
|
/// <param name="varietyId"></param>
|
||
|
private async Task OnPlcRequestScanProduct(int varietyId)
|
||
|
{
|
||
|
logger.LogInformation($"plc request scan yarn qrcode");
|
||
|
var yarn = await hyosungScannerService.ScanYarnAsync(varietyId);
|
||
|
if (yarn is null)
|
||
|
{
|
||
|
await hyosungPlcService.WriteScanYarnResultAsync(false);
|
||
|
logger.LogInformation($"scan yarn fail");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
await yarnService.AddYarnAsync(yarn);
|
||
|
await hyosungPlcService.WriteScanYarnResultAsync(true, (short)varietyId, yarn.ScanCode);
|
||
|
logger.LogInformation($"scan yarn {yarn.ScanCode} success: qrcode[{yarn.QrCode}]");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 处理PLC请求下线事件
|
||
|
/// </summary>
|
||
|
/// <returns></returns>
|
||
|
/// <exception cref="NotImplementedException"></exception>
|
||
|
private async Task OnPlcRequestLeavingProductionLine(PlcStackInfo info)
|
||
|
{
|
||
|
//使托盘完成
|
||
|
await trayService.StorageAsync(info.TrayCode);
|
||
|
await hyosungAgvService.StorageAsync(info.TrayCode);
|
||
|
//标志下线已完成
|
||
|
await hyosungPlcService.LeaveCompletedAsync();
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <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)
|
||
|
{
|
||
|
var tray = await trayService.GeneraNewTray(varietyId);
|
||
|
await hyosungPlcService.WriteTrayCodeAsync(index, tray.TrayCode);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// PLC码垛一次完成
|
||
|
/// </summary>
|
||
|
/// <param name="arg"></param>
|
||
|
/// <returns></returns>
|
||
|
/// <exception cref="NotImplementedException"></exception>
|
||
|
private async Task OnPlcPutCompleted(PlcStackingInfo arg)
|
||
|
{
|
||
|
foreach (var yarnCode in arg.YarnCode)
|
||
|
{
|
||
|
await yarnService.BindTrayAsync(yarnCode, await trayService.GetIdByCode(arg.TrayCode));
|
||
|
}
|
||
|
|
||
|
await hyosungPlcService.WriteReceivedYarnCountAsync(arg.YarnCode.Count);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 处理PLC请求打包线配置事件
|
||
|
/// </summary>
|
||
|
/// <param name="arg"></param>
|
||
|
/// <returns></returns>
|
||
|
/// <exception cref="NotImplementedException"></exception>
|
||
|
private async Task OnPlcRequestPackLineOption(string arg)
|
||
|
{
|
||
|
var tray = await trayService.GetByCode(arg);
|
||
|
var variety = await varietyService.GetById(tray.VarietyId);
|
||
|
var option = 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,
|
||
|
};
|
||
|
await hyosungPlcService.WritePackLineOptionAsync(option);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 处理贴标站请求贴标事件
|
||
|
/// </summary>
|
||
|
/// <param name="arg1"></param>
|
||
|
/// <param name="arg2"></param>
|
||
|
/// <returns></returns>
|
||
|
/// <exception cref="NotImplementedException"></exception>
|
||
|
private async Task OnPlcRequestPrintLabel(int arg1, string trayCode, int height)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (arg1 == 1)
|
||
|
{
|
||
|
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);
|
||
|
await trayService.PrintTrayAsync(trayCode, height, controlNo, mod);
|
||
|
await printer.PrintAsync(1, trayCode);
|
||
|
await hyosungWmsService.UpdateControlNo(variety, controlNo);
|
||
|
}
|
||
|
else
|
||
|
await printer.PrintAsync(1, trayCode);
|
||
|
|
||
|
await hyosungPlcService.WritePrintLabelResultAsync(arg1, true);
|
||
|
}
|
||
|
catch (Exception e)
|
||
|
{
|
||
|
await hyosungPlcService.WritePrintLabelResultAsync(arg1, false);
|
||
|
logger.LogError(e, "print label fail");
|
||
|
}
|
||
|
}
|
||
|
}
|