refactoring

This commit is contained in:
EugeneTes
2026-01-21 10:37:33 +01:00
parent cb231801f1
commit 5cef04bf92
11 changed files with 793 additions and 33 deletions

View File

@@ -11,17 +11,19 @@ public class EpsonPrintService: IPrintService
private readonly IWrapperPrinterFactory _wrapperPrinterFactory;
private readonly IPrinterConfigurationSource _printerConfigurationSource;
private readonly IReceiptConverterFactory _receiptConverterFactory;
private readonly HttpClient _httpClient;
public EpsonPrintService(ILogger logger, IPrinterFactory printerFactory, IWrapperPrinterFactory wrapperPrinterFactory, IPrinterConfigurationSource printerConfigurationSource, IReceiptConverterFactory receiptConverterFactory)
public EpsonPrintService(ILogger logger, IPrinterFactory printerFactory, IWrapperPrinterFactory wrapperPrinterFactory, IPrinterConfigurationSource printerConfigurationSource, IReceiptConverterFactory receiptConverterFactory, HttpClient httpClient)
{
_logger = logger;
_printerFactory = printerFactory;
_wrapperPrinterFactory = wrapperPrinterFactory;
_printerConfigurationSource = printerConfigurationSource;
_receiptConverterFactory = receiptConverterFactory;
_httpClient = httpClient;
}
public async Task<bool> PrintAsync(string printerIp, PrintJob job)
public async Task<PrintResult> PrintAsync(string printerIp, PrintJob job)
{
try
{
@@ -65,10 +67,7 @@ public class EpsonPrintService: IPrintService
// Download only if not already cached
if (!File.Exists(cachedLogoPath))
{
using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(10);
var logoData = await httpClient.GetByteArrayAsync(job.Document.LogoUrl);
var logoData = await _httpClient.GetByteArrayAsync(job.Document.LogoUrl);
await File.WriteAllBytesAsync(cachedLogoPath, logoData);
_logger.LogInformation("Downloaded logo {LogoFileName} from {LogoUrl}", logoFileName, job.Document.LogoUrl);
@@ -84,8 +83,18 @@ public class EpsonPrintService: IPrintService
await printerAdapter.SetFontSizeAsync(configuration.FontSize);
// Convert receipt content to print commands using the factory
var converter = _receiptConverterFactory.Create(job.Document.ReceiptType, printerId.Value);
var commands = converter.Convert(job.Document.Content);
List<PrintCommand> commands;
try
{
var converter = _receiptConverterFactory.Create(job.Document.ReceiptType, printerId.Value);
commands = converter.Convert(job.Document.Content);
}
catch (Exception e)
{
_logger.LogWarning(e, "Conversion error for print job on printer {PrinterUid}", printerIp);
return PrintResult.Fail(PrintErrorType.ConversionError);
}
await printerAdapter.PrintAsync(commands);
await Task.Delay(200);
status = await epsonPrinter.GetPrinterStatusAsync();
@@ -98,10 +107,10 @@ public class EpsonPrintService: IPrintService
}
catch (Exception e)
{
_logger.LogWarning( e, "Failed to print job for printer {PrinterUid}", printerIp);
return false;
_logger.LogWarning(e, "Failed to print job for printer {PrinterUid}", printerIp);
return PrintResult.Fail(PrintErrorType.Other);
}
return true;
return PrintResult.Ok();
}
}