do not discover while printing
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<Version>1.0.4</Version>
|
<Version>1.0.5</Version>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -67,10 +67,11 @@ public class PrinterDiscoveryBackgroundTask
|
|||||||
|
|
||||||
private async Task DiscoverAndNotifyAsync()
|
private async Task DiscoverAndNotifyAsync()
|
||||||
{
|
{
|
||||||
|
_printServer.EnterDiscoveryLock();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var printers = await _discoveryService.DiscoverPrintersAsync(_discoveryTimeout);
|
var printers = await _discoveryService.DiscoverPrintersAsync(_discoveryTimeout);
|
||||||
|
|
||||||
var currentIps = printers.Select(p => p.IPAddress).ToHashSet();
|
var currentIps = printers.Select(p => p.IPAddress).ToHashSet();
|
||||||
|
|
||||||
foreach (string currentIp in currentIps)
|
foreach (string currentIp in currentIps)
|
||||||
@@ -90,6 +91,10 @@ public class PrinterDiscoveryBackgroundTask
|
|||||||
{
|
{
|
||||||
_logger.LogWarning(ex, "Failed to discover printers");
|
_logger.LogWarning(ex, "Failed to discover printers");
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_printServer.ExitDiscoveryLock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool HasConfigurationChanged(HashSet<string> currentIps)
|
private bool HasConfigurationChanged(HashSet<string> currentIps)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Inspectron.Epson.PrintServer.Printers.Utils;
|
using Inspectron.Epson.PrintServer.Printers.Utils;
|
||||||
using Inspectron.Epson.PrintServer.PrintServices;
|
using Inspectron.Epson.PrintServer.PrintServices;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace Inspectron.Epson.PrintServer.Printers;
|
namespace Inspectron.Epson.PrintServer.Printers;
|
||||||
|
|
||||||
@@ -37,6 +38,8 @@ public class TM_U220IITranslated : IPrinter
|
|||||||
{
|
{
|
||||||
if (command.IsCut)
|
if (command.IsCut)
|
||||||
{
|
{
|
||||||
|
await _printer.FeedLinesAsync(10);
|
||||||
|
await Task.Delay(200 * 5);
|
||||||
await _printer.CutAsync(false);
|
await _printer.CutAsync(false);
|
||||||
await Task.Delay(200);
|
await Task.Delay(200);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -6,18 +6,40 @@ public class PrintServer
|
|||||||
private readonly IPrintService _printService;
|
private readonly IPrintService _printService;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
private readonly ConcurrentDictionary<string, PrinterQueue> _printerQueues;
|
private readonly ConcurrentDictionary<string, PrinterQueue> _printerQueues;
|
||||||
|
private readonly ReaderWriterLockSlim _printDiscoveryLock = new();
|
||||||
|
|
||||||
public PrintServer(IPrintService printService, ILogger logger)
|
public PrintServer(IPrintService printService, ILogger logger)
|
||||||
{
|
{
|
||||||
_printService = printService;
|
_printService = printService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
|
_printerQueues = new ConcurrentDictionary<string, PrinterQueue>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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)
|
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))
|
if (_printerQueues.TryAdd(printerIp, queue))
|
||||||
{
|
{
|
||||||
queue.Start();
|
queue.Start();
|
||||||
|
|||||||
@@ -15,11 +15,12 @@ public class PrinterQueue
|
|||||||
private Task _processingTask;
|
private Task _processingTask;
|
||||||
private readonly IPrintService _printService;
|
private readonly IPrintService _printService;
|
||||||
private readonly ILogger _logger;
|
private readonly ILogger _logger;
|
||||||
|
private readonly PrintServer _printServer;
|
||||||
|
|
||||||
public bool IsProcessing { get; private set; }
|
public bool IsProcessing { get; private set; }
|
||||||
public int QueueLength => _queue.Count + _priorityQueue.Count;
|
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;
|
PrinterIp = printerIp;
|
||||||
_queue = new ConcurrentQueue<PrintJob>();
|
_queue = new ConcurrentQueue<PrintJob>();
|
||||||
@@ -28,6 +29,7 @@ public class PrinterQueue
|
|||||||
_cancellationTokenSource = new CancellationTokenSource();
|
_cancellationTokenSource = new CancellationTokenSource();
|
||||||
_printService = printService;
|
_printService = printService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_printServer = printServer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Enqueue(PrintJob job)
|
public void Enqueue(PrintJob job)
|
||||||
@@ -70,6 +72,7 @@ public class PrinterQueue
|
|||||||
{
|
{
|
||||||
_logger.LogInformation("Processing job on printer {PrinterId}", PrinterIp);
|
_logger.LogInformation("Processing job on printer {PrinterId}", PrinterIp);
|
||||||
|
|
||||||
|
_printServer.EnterPrintLock();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Call the actual print function
|
// Call the actual print function
|
||||||
@@ -100,6 +103,10 @@ public class PrinterQueue
|
|||||||
_logger.LogError(ex, "Error processing job");
|
_logger.LogError(ex, "Error processing job");
|
||||||
// Handle exception (retry, log, etc.)
|
// Handle exception (retry, log, etc.)
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_printServer.ExitPrintLock();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
|
|||||||
Reference in New Issue
Block a user