2025-03-21 09:07:39 +08:00

329 lines
12 KiB
C#

using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Text.Json;
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;
using Seyounth.Hyosung.Data.Services.Hyosung.Entities;
using Seyounth.Hyosung.Runtime.Models;
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 PackLineOption PackLineOption { get; private set; }
public StackStationModel Stack1 { get; private set; } = new();
public StackStationModel Stack2 { get; private set; } = new();
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)
{
logger.LogInformation($"plc request scan fixture:{fixtureId}");
try
{
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}");
}
}
catch (Exception e)
{
await hyosungPlcService.WriteScanFixtureResultAsync(fixtureId, false);
logger.LogError(e, $"scan fixture{fixtureId} fail");
}
}
/// <summary>
/// 处理PLC请求扫描纱事件
/// </summary>
/// <param name="varietyId"></param>
private async Task OnPlcRequestScanProduct(int varietyId)
{
logger.LogInformation($"plc request scan yarn qrcode");
try
{
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;
}
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)
{
await hyosungPlcService.WriteScanYarnResultAsync(false);
logger.LogError(e, $"scan yarn fail");
}
}
/// <summary>
/// 处理PLC请求下线事件
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private async Task OnPlcRequestLeavingProductionLine(PlcStackInfo info)
{
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");
}
}
/// <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)
{
logger.LogInformation($"plc request new tray code:{index} {varietyId}");
try
{
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}");
}
catch (Exception e)
{
logger.LogError(e, $"plc request new tray code fail");
}
}
/// <summary>
/// PLC码垛一次完成
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private async Task OnPlcPutCompleted(PlcStackingInfo arg)
{
logger.LogInformation($"plc put completed:{JsonSerializer.Serialize(arg)}");
try
{
if (arg.TrayCode == Stack1.TrayCode)
{
Stack1.CurrentCount += arg.YarnCode.Count;
foreach (var yarnCode in arg.YarnCode)
{
Stack1.Yarns.Add(await yarnService.GetYarnByCodeAsync(yarnCode));
}
}
else if (arg.TrayCode == Stack2.TrayCode)
{
Stack2.CurrentCount += arg.YarnCode.Count;
foreach (var yarnCode in arg.YarnCode)
{
Stack2.Yarns.Add(await yarnService.GetYarnByCodeAsync(yarnCode));
}
}
foreach (var yarnCode in arg.YarnCode)
{
await yarnService.BindTrayAsync(yarnCode, await trayService.GetIdByCode(arg.TrayCode));
logger.LogInformation($" stack yarn{yarnCode} succeed");
}
await hyosungPlcService.WriteReceivedYarnCountAsync(arg.YarnCode.Count);
logger.LogInformation($"plc put completed success");
}
catch (Exception e)
{
logger.LogError(e, $"plc put completed fail");
}
}
/// <summary>
/// 处理PLC请求打包线配置事件
/// </summary>
/// <param name="arg"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private async Task OnPlcRequestPackLineOption(string arg)
{
logger.LogInformation($"plc request pack line option");
try
{
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");
}
}
/// <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)
{
logger.LogInformation($"plc request print label:{arg1} {trayCode} {height}");
try
{
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;
int controlNo = 0;
if(tray.ControlNo is null || tray.ControlNo == 0)
{
controlNo = await hyosungWmsService.GetControlNo(variety, grade);
tray = await trayService.PrintTrayAsync(trayCode, height, controlNo, mod);
}
if (arg1 == 1)
{
await printer.PrintAsync(1, trayCode);
await hyosungPlcService.WritePrintLabelResultAsync(arg1, variety.SubLabelCount, true);
}
else
{
await printer.PrintAsync(2, trayCode);
await hyosungPlcService.WritePrintLabelResultAsync(arg1, variety.MasterLabelCount, true);
await hyosungWmsService.UpdateControlNo(variety, controlNo);
await hyosungWmsService.AddLabelResult(new LabelResult(tray, variety));
}
logger.LogInformation($"plc request print label success");
}
catch (Exception e)
{
await hyosungPlcService.WritePrintLabelResultAsync(arg1, 1, false);
logger.LogError(e, "print label fail");
}
}
}