diff --git a/ConfigurationPannel/Services/PrintServerHostedService.cs b/ConfigurationPannel/Services/PrintServerHostedService.cs index 7848941..e39de17 100644 --- a/ConfigurationPannel/Services/PrintServerHostedService.cs +++ b/ConfigurationPannel/Services/PrintServerHostedService.cs @@ -44,6 +44,7 @@ public class PrintServerHostedService : IHostedService _kernel.Bind().ToConstant(config); _kernel.Bind().To().InSingletonScope(); _kernel.Bind().ToConstant(config); + _kernel.Bind().To(); _kernel.Bind().ToSelf().InSingletonScope(); // Get instances diff --git a/EpsonPrintService/Program.cs b/EpsonPrintService/Program.cs index c81197b..87e8eac 100644 --- a/EpsonPrintService/Program.cs +++ b/EpsonPrintService/Program.cs @@ -66,6 +66,7 @@ kernel.Bind().ToMethod(ctx => kernel.Bind().To().InSingletonScope(); kernel.Bind().To(); kernel.Bind().To().InSingletonScope(); +kernel.Bind().To(); kernel.Bind().ToSelf().InSingletonScope(); kernel.Bind().To().InSingletonScope(); kernel.Bind().ToSelf().InSingletonScope(); diff --git a/Inspectron.Epson/Queue/IJobStatusReporter.cs b/Inspectron.Epson/Queue/IJobStatusReporter.cs new file mode 100644 index 0000000..cb44269 --- /dev/null +++ b/Inspectron.Epson/Queue/IJobStatusReporter.cs @@ -0,0 +1,6 @@ +using System.Threading.Tasks; + +public interface IJobStatusReporter +{ + Task ReportStatusAsync(PrintJob job, PrintJobStatus status); +} diff --git a/Inspectron.Epson/Queue/NullJobStatusReporter.cs b/Inspectron.Epson/Queue/NullJobStatusReporter.cs new file mode 100644 index 0000000..625b4f9 --- /dev/null +++ b/Inspectron.Epson/Queue/NullJobStatusReporter.cs @@ -0,0 +1,6 @@ +using System.Threading.Tasks; + +public class NullJobStatusReporter : IJobStatusReporter +{ + public Task ReportStatusAsync(PrintJob job, PrintJobStatus status) => Task.CompletedTask; +} diff --git a/Inspectron.Epson/Queue/PrintJobStatus.cs b/Inspectron.Epson/Queue/PrintJobStatus.cs new file mode 100644 index 0000000..ee4fa6b --- /dev/null +++ b/Inspectron.Epson/Queue/PrintJobStatus.cs @@ -0,0 +1,6 @@ +public enum PrintJobStatus +{ + Received, + Completed, + Failed +} diff --git a/Inspectron.Epson/Queue/PrintServer.cs b/Inspectron.Epson/Queue/PrintServer.cs index 1993a05..83e61a2 100644 --- a/Inspectron.Epson/Queue/PrintServer.cs +++ b/Inspectron.Epson/Queue/PrintServer.cs @@ -5,15 +5,17 @@ public class PrintServer { private readonly IPrintService _printService; private readonly ILogger _logger; + private readonly IJobStatusReporter _statusReporter; private readonly ConcurrentDictionary _printerQueues; private readonly SemaphoreSlim _printDiscoveryLock = new(1, 1); private int _readerCount = 0; private readonly object _readerCountLock = new(); - public PrintServer(IPrintService printService, ILogger logger) + public PrintServer(IPrintService printService, ILogger logger, IJobStatusReporter statusReporter) { _printService = printService; _logger = logger; + _statusReporter = statusReporter; _printerQueues = new ConcurrentDictionary(); } @@ -76,7 +78,7 @@ public class PrintServer public void RegisterPrinter(string printerIp) { - var queue = new PrinterQueue(printerIp, _printService, _logger, this); + var queue = new PrinterQueue(printerIp, _printService, _logger, this, _statusReporter); if (_printerQueues.TryAdd(printerIp, queue)) { queue.Start(); diff --git a/Inspectron.Epson/Queue/PrinterQueue.cs b/Inspectron.Epson/Queue/PrinterQueue.cs index fbe2da5..aa96653 100644 --- a/Inspectron.Epson/Queue/PrinterQueue.cs +++ b/Inspectron.Epson/Queue/PrinterQueue.cs @@ -16,11 +16,12 @@ public class PrinterQueue private readonly IPrintService _printService; private readonly ILogger _logger; private readonly PrintServer _printServer; + private readonly IJobStatusReporter _statusReporter; public bool IsProcessing { get; private set; } public int QueueLength => _queue.Count + _priorityQueue.Count; - public PrinterQueue(string printerIp, IPrintService printService, ILogger logger, PrintServer printServer) + public PrinterQueue(string printerIp, IPrintService printService, ILogger logger, PrintServer printServer, IJobStatusReporter statusReporter) { PrinterIp = printerIp; _queue = new ConcurrentQueue(); @@ -30,12 +31,14 @@ public class PrinterQueue _printService = printService; _logger = logger; _printServer = printServer; + _statusReporter = statusReporter; } public void Enqueue(PrintJob job) { _queue.Enqueue(job); _signal.Release(); // Signal that there's work to do + _statusReporter.ReportStatusAsync(job, PrintJobStatus.Received).GetAwaiter().GetResult(); _logger.LogInformation("Job queued for printer {PrinterId}. Queue length: {QueueLength}", PrinterIp, QueueLength); } @@ -83,6 +86,7 @@ public class PrinterQueue if (result.ErrorType == PrintErrorType.ConversionError) { _logger.LogWarning("Job failed due to conversion error, not re-queuing"); + await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Failed); } else { @@ -96,12 +100,13 @@ public class PrinterQueue else { _logger.LogInformation("Job completed successfully on printer {PrinterId}", PrinterIp); + await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Completed); } } catch (Exception ex) { _logger.LogError(ex, "Error processing job"); - // Handle exception (retry, log, etc.) + await _statusReporter.ReportStatusAsync(job, PrintJobStatus.Failed); } finally {