refactoring
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,5 +2,5 @@ using System.Threading.Tasks;
|
||||
|
||||
public interface IPrintService
|
||||
{
|
||||
Task<bool> PrintAsync(string printerIp, PrintJob job);
|
||||
Task<PrintResult> PrintAsync(string printerIp, PrintJob job);
|
||||
}
|
||||
|
||||
6
Inspectron.Epson/Queue/PrintErrorType.cs
Normal file
6
Inspectron.Epson/Queue/PrintErrorType.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
public enum PrintErrorType
|
||||
{
|
||||
None,
|
||||
ConversionError,
|
||||
Other
|
||||
}
|
||||
14
Inspectron.Epson/Queue/PrintResult.cs
Normal file
14
Inspectron.Epson/Queue/PrintResult.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
public class PrintResult
|
||||
{
|
||||
public bool Success { get; }
|
||||
public PrintErrorType ErrorType { get; }
|
||||
|
||||
private PrintResult(bool success, PrintErrorType errorType)
|
||||
{
|
||||
Success = success;
|
||||
ErrorType = errorType;
|
||||
}
|
||||
|
||||
public static PrintResult Ok() => new(true, PrintErrorType.None);
|
||||
public static PrintResult Fail(PrintErrorType errorType) => new(false, errorType);
|
||||
}
|
||||
@@ -1,36 +1,35 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
public class PrintServer
|
||||
{
|
||||
private readonly IPrintService _printService;
|
||||
private readonly ILogger _logger;
|
||||
private readonly Dictionary<string, PrinterQueue> _printerQueues;
|
||||
private readonly ConcurrentDictionary<string, PrinterQueue> _printerQueues;
|
||||
|
||||
public PrintServer(IPrintService printService, ILogger logger)
|
||||
{
|
||||
_printService = printService;
|
||||
_logger = logger;
|
||||
_printerQueues = new Dictionary<string, PrinterQueue>();
|
||||
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
|
||||
|
||||
}
|
||||
|
||||
public void RegisterPrinter(string printerIp)
|
||||
{
|
||||
if (_printerQueues.ContainsKey(printerIp))
|
||||
return;
|
||||
|
||||
var queue = new PrinterQueue(printerIp, _printService,_logger);
|
||||
_printerQueues[printerIp] = queue;
|
||||
queue.Start();
|
||||
Console.WriteLine($"Printer {printerIp} registered");
|
||||
var queue = new PrinterQueue(printerIp, _printService, _logger);
|
||||
if (_printerQueues.TryAdd(printerIp, queue))
|
||||
{
|
||||
queue.Start();
|
||||
Console.WriteLine($"Printer {printerIp} registered");
|
||||
}
|
||||
}
|
||||
|
||||
public void UnregisterPrinter(string printerIp)
|
||||
{
|
||||
if (_printerQueues.TryGetValue(printerIp, out var queue))
|
||||
if (_printerQueues.TryRemove(printerIp, out var queue))
|
||||
{
|
||||
queue.StopAsync().Wait();
|
||||
_printerQueues.Remove(printerIp);
|
||||
Console.WriteLine($"Printer {printerIp} unregistered");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,15 +73,22 @@ public class PrinterQueue
|
||||
try
|
||||
{
|
||||
// Call the actual print function
|
||||
bool success = await _printService.PrintAsync(PrinterIp, job);
|
||||
var result = await _printService.PrintAsync(PrinterIp, job);
|
||||
|
||||
if (!success)
|
||||
if (!result.Success)
|
||||
{
|
||||
// Re-queue with retry logic
|
||||
job.RetryCount++;
|
||||
_logger.LogWarning("Job failed, retrying ({RetryCount}/3)", job.RetryCount);
|
||||
await Task.Delay(5000, cancellationToken); // Wait before retry
|
||||
EnqueuePriority(job);
|
||||
if (result.ErrorType == PrintErrorType.ConversionError)
|
||||
{
|
||||
_logger.LogWarning("Job failed due to conversion error, not re-queuing");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Re-queue with retry logic
|
||||
job.RetryCount++;
|
||||
_logger.LogWarning("Job failed, retrying ({RetryCount}/3)", job.RetryCount);
|
||||
await Task.Delay(5000, cancellationToken); // Wait before retry
|
||||
EnqueuePriority(job);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user