174 lines
6.6 KiB
C#
174 lines
6.6 KiB
C#
using MediatR;
|
|
using Microsoft.Extensions.Logging;
|
|
using Seyounth.Auto.Hs.Runtime.Balances;
|
|
using Seyounth.Auto.Hs.Runtime.Handlers;
|
|
using Seyounth.Auto.Hs.Runtime.Plc;
|
|
using Seyounth.Auto.Hs.Runtime.Printer;
|
|
using Seyounth.Auto.Hs.Runtime.Scanner;
|
|
|
|
namespace Seyounth.Auto.Hs.Runtime;
|
|
|
|
public class HsAutoRuntime : IHsAutoRuntime
|
|
{
|
|
private readonly IMediator _mediator;
|
|
private readonly IBalanceService _balance;
|
|
private readonly IScannerService _scanners;
|
|
private readonly ILogger<HsAutoRuntime> _logger;
|
|
private readonly IPlcService _plcService;
|
|
private readonly IPrinterService _printers;
|
|
|
|
public HsAutoRuntime(IPlcService plcService, IMediator mediator, IBalanceService balances, IScannerService scanners,
|
|
IPrinterService printers,ILogger<HsAutoRuntime> logger)
|
|
{
|
|
_printers = printers;
|
|
_mediator = mediator;
|
|
_balance = balances;
|
|
_scanners = scanners;
|
|
_logger = logger;
|
|
_plcService = plcService;
|
|
_scanners.OnScanned += ScannersOnOnScanned;
|
|
plcService.OnWarning += PlcServiceOnOnWarning;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取到报警信息处理逻辑
|
|
/// </summary>
|
|
/// <param name="warning"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
private Task PlcServiceOnOnWarning(Tuple<short, short> warning)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private void ScannersOnOnScanned(IScanner scanner, string barcode)
|
|
{
|
|
if (scanner.Id == 1)
|
|
{
|
|
#pragma warning disable CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
|
|
HandleFilmOnScanned(barcode);
|
|
#pragma warning restore CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
|
|
}
|
|
else if (scanner.Id == 2)
|
|
{
|
|
#pragma warning disable CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
|
|
HandleBoxOnScanned(barcode);
|
|
#pragma warning restore CS4014 // 由于此调用不会等待,因此在调用完成前将继续执行当前方法
|
|
}
|
|
}
|
|
|
|
private async Task HandleFilmOnScanned(string barcode)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Film Scanner: {barcode}", barcode);
|
|
var weight = await _balance.WeighAsync(1);
|
|
_logger.LogInformation($"Film Barcode:{barcode}| Weight: {weight}");
|
|
var content = await _mediator.Send(new WeighSpindleRequest(barcode, weight));
|
|
_logger.LogInformation($"Film Barcode:{barcode}| print content: {content}");
|
|
await _printers.PrintAsync(1, content);
|
|
await _plcService.WriteFilmLabelPrintResult(1);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError($"Film Scanner: {barcode}| Exception: {ex.Message}");
|
|
await _plcService.WriteFilmLabelPrintResult(2);
|
|
}
|
|
}
|
|
|
|
private async Task HandleBoxOnScanned(string barcode)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Box Scanner: {barcode}", barcode);
|
|
int jackingFlag;
|
|
do
|
|
{
|
|
jackingFlag = await _plcService.GetJackingFlagAsync();
|
|
} while (jackingFlag == 0);
|
|
|
|
_logger.LogInformation($"Box Barcode:{barcode}| Jacking flag: {jackingFlag}");
|
|
var weight = await _balance.WeighAsync(2);
|
|
_logger.LogInformation($"Box Barcode:{barcode}| Weight: {weight}");
|
|
var content = await _mediator.Send(new WeighBoxRequest(barcode, weight));
|
|
_logger.LogInformation($"Box Barcode:{barcode}| WeighBoxResult: {content}");
|
|
await _printers.PrintAsync(2, content);
|
|
await _plcService.WriteBoxLabelPrintResult(1);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Box Scanner: {barcode}", barcode);
|
|
await _plcService.WriteBoxLabelPrintResult(2);
|
|
}
|
|
}
|
|
|
|
public Task RunAsync()
|
|
{
|
|
var tasks = new List<Task>
|
|
{
|
|
_balance.StartAsync()
|
|
.ContinueWith(t =>
|
|
{
|
|
_logger.LogInformation(t.IsCompletedSuccessfully
|
|
? "Balance connected successfully."
|
|
: $"Balance connection failed. error: {t.Exception?.Message}");
|
|
}),
|
|
_scanners.StartAsync()
|
|
.ContinueWith(t =>
|
|
{
|
|
_logger.LogInformation(t.IsCompletedSuccessfully
|
|
? "Scanner connected successfully."
|
|
: $"Scanner connection failed. error: {t.Exception?.Message}");
|
|
}),
|
|
_plcService.StartAsync()
|
|
.ContinueWith(t =>
|
|
{
|
|
_logger.LogInformation(t.IsCompletedSuccessfully
|
|
? "Plc connected successfully."
|
|
: $"Plc connection failed. error: {t.Exception?.Message}");
|
|
}),
|
|
_printers.StartAsync().ContinueWith(t =>
|
|
{
|
|
_logger.LogInformation(t.IsCompletedSuccessfully
|
|
? "Printer connected successfully."
|
|
: $"Printer connection failed. error: {t.Exception?.Message}");
|
|
})
|
|
};
|
|
return Task.WhenAll(tasks);
|
|
}
|
|
|
|
public Task StopAsync()
|
|
{
|
|
var tasks = new List<Task>
|
|
{
|
|
_balance.StopAsync()
|
|
.ContinueWith(t =>
|
|
{
|
|
_logger.LogInformation(t.IsCompletedSuccessfully
|
|
? "Balance stop successfully."
|
|
: $"Balance stop failed. error: {t.Exception?.Message}");
|
|
}),
|
|
_scanners.StopAsync()
|
|
.ContinueWith(t =>
|
|
{
|
|
_logger.LogInformation(t.IsCompletedSuccessfully
|
|
? "Scanner stop successfully."
|
|
: $"Scanner stop failed. error: {t.Exception?.Message}");
|
|
}),
|
|
_plcService.StopAsync()
|
|
.ContinueWith(t =>
|
|
{
|
|
_logger.LogInformation(t.IsCompletedSuccessfully
|
|
? "Plc stop successfully."
|
|
: $"Plc stop failed. error: {t.Exception?.Message}");
|
|
}),
|
|
_printers.StopAsync().ContinueWith(t =>
|
|
{
|
|
_logger.LogInformation(t.IsCompletedSuccessfully
|
|
? "Printer stop successfully."
|
|
: $"Printer stop failed. error: {t.Exception?.Message}");
|
|
})
|
|
};
|
|
return Task.WhenAll(tasks);
|
|
}
|
|
} |