do not discover while printing
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>1.0.4</Version>
|
||||
<Version>1.0.5</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -67,6 +67,7 @@ public class PrinterDiscoveryBackgroundTask
|
||||
|
||||
private async Task DiscoverAndNotifyAsync()
|
||||
{
|
||||
_printServer.EnterDiscoveryLock();
|
||||
try
|
||||
{
|
||||
var printers = await _discoveryService.DiscoverPrintersAsync(_discoveryTimeout);
|
||||
@@ -90,6 +91,10 @@ public class PrinterDiscoveryBackgroundTask
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to discover printers");
|
||||
}
|
||||
finally
|
||||
{
|
||||
_printServer.ExitDiscoveryLock();
|
||||
}
|
||||
}
|
||||
|
||||
private bool HasConfigurationChanged(HashSet<string> currentIps)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Inspectron.Epson.PrintServer.Printers.Utils;
|
||||
using Inspectron.Epson.PrintServer.PrintServices;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Inspectron.Epson.PrintServer.Printers;
|
||||
|
||||
@@ -37,6 +38,8 @@ public class TM_U220IITranslated : IPrinter
|
||||
{
|
||||
if (command.IsCut)
|
||||
{
|
||||
await _printer.FeedLinesAsync(10);
|
||||
await Task.Delay(200 * 5);
|
||||
await _printer.CutAsync(false);
|
||||
await Task.Delay(200);
|
||||
continue;
|
||||
|
||||
@@ -6,6 +6,7 @@ public class PrintServer
|
||||
private readonly IPrintService _printService;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ConcurrentDictionary<string, PrinterQueue> _printerQueues;
|
||||
private readonly ReaderWriterLockSlim _printDiscoveryLock = new();
|
||||
|
||||
public PrintServer(IPrintService printService, ILogger logger)
|
||||
{
|
||||
@@ -15,9 +16,30 @@ public class PrintServer
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Acquires a read lock for print operations. Multiple print operations can run concurrently.
|
||||
/// </summary>
|
||||
public void EnterPrintLock() => _printDiscoveryLock.EnterReadLock();
|
||||
|
||||
/// <summary>
|
||||
/// Releases the read lock after a print operation completes.
|
||||
/// </summary>
|
||||
public void ExitPrintLock() => _printDiscoveryLock.ExitReadLock();
|
||||
|
||||
/// <summary>
|
||||
/// Acquires a write lock for discovery operations. This blocks until all print operations complete
|
||||
/// and prevents new print operations from starting.
|
||||
/// </summary>
|
||||
public void EnterDiscoveryLock() => _printDiscoveryLock.EnterWriteLock();
|
||||
|
||||
/// <summary>
|
||||
/// Releases the write lock after discovery completes.
|
||||
/// </summary>
|
||||
public void ExitDiscoveryLock() => _printDiscoveryLock.ExitWriteLock();
|
||||
|
||||
public void RegisterPrinter(string printerIp)
|
||||
{
|
||||
var queue = new PrinterQueue(printerIp, _printService, _logger);
|
||||
var queue = new PrinterQueue(printerIp, _printService, _logger, this);
|
||||
if (_printerQueues.TryAdd(printerIp, queue))
|
||||
{
|
||||
queue.Start();
|
||||
|
||||
@@ -15,11 +15,12 @@ public class PrinterQueue
|
||||
private Task _processingTask;
|
||||
private readonly IPrintService _printService;
|
||||
private readonly ILogger _logger;
|
||||
private readonly PrintServer _printServer;
|
||||
|
||||
public bool IsProcessing { get; private set; }
|
||||
public int QueueLength => _queue.Count + _priorityQueue.Count;
|
||||
|
||||
public PrinterQueue(string printerIp, IPrintService printService, ILogger logger)
|
||||
public PrinterQueue(string printerIp, IPrintService printService, ILogger logger, PrintServer printServer)
|
||||
{
|
||||
PrinterIp = printerIp;
|
||||
_queue = new ConcurrentQueue<PrintJob>();
|
||||
@@ -28,6 +29,7 @@ public class PrinterQueue
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
_printService = printService;
|
||||
_logger = logger;
|
||||
_printServer = printServer;
|
||||
}
|
||||
|
||||
public void Enqueue(PrintJob job)
|
||||
@@ -70,6 +72,7 @@ public class PrinterQueue
|
||||
{
|
||||
_logger.LogInformation("Processing job on printer {PrinterId}", PrinterIp);
|
||||
|
||||
_printServer.EnterPrintLock();
|
||||
try
|
||||
{
|
||||
// Call the actual print function
|
||||
@@ -100,6 +103,10 @@ public class PrinterQueue
|
||||
_logger.LogError(ex, "Error processing job");
|
||||
// Handle exception (retry, log, etc.)
|
||||
}
|
||||
finally
|
||||
{
|
||||
_printServer.ExitPrintLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
|
||||
Reference in New Issue
Block a user