discovery fix

This commit is contained in:
EugeneTes
2026-01-28 14:40:32 +01:00
parent 8be53cec4e
commit 83998f6fd1
4 changed files with 72 additions and 18 deletions

View File

@@ -67,34 +67,45 @@ public class PrinterDiscoveryBackgroundTask
private async Task DiscoverAndNotifyAsync()
{
_printServer.EnterDiscoveryLock();
List<DiscoveredPrinter> printers;
HashSet<string> currentIps;
bool configChanged;
await _printServer.EnterDiscoveryLockAsync();
try
{
var printers = await _discoveryService.DiscoverPrintersAsync(_discoveryTimeout);
printers = await _discoveryService.DiscoverPrintersAsync(_discoveryTimeout);
currentIps = printers.Select(p => p.IPAddress).ToHashSet();
configChanged = HasConfigurationChanged(currentIps);
var currentIps = printers.Select(p => p.IPAddress).ToHashSet();
foreach (string currentIp in currentIps)
if (configChanged)
{
_printServer.RegisterPrinter(currentIp);
}
if (HasConfigurationChanged(currentIps))
{
_logger.LogInformation("Discovered printer configuration changed: {Count} printer(s)", printers.Count);
_lastDiscoveredPrinterIps = currentIps;
_lastDiscoveredPrinters = printers;
await _receiver.OnPrintersDiscoveredAsync(printers.AsReadOnly());
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to discover printers");
return; // Exit early on error
}
finally
{
_printServer.ExitDiscoveryLock();
}
// Register printers AFTER releasing the lock
foreach (string currentIp in currentIps)
{
_logger.LogDebug("Registering discovered printer with IP: {IP}", currentIp);
_printServer.RegisterPrinter(currentIp);
}
if (configChanged)
{
_logger.LogInformation("Discovered printer configuration changed: {Count} printer(s)", printers.Count);
await _receiver.OnPrintersDiscoveredAsync(printers.AsReadOnly());
}
}
private bool HasConfigurationChanged(HashSet<string> currentIps)

View File

@@ -1 +1 @@
aHR0cDovL2xvY2FsaG9zdDo1MDAyOzY1YzM2MDYyZDM2ZTRjZmVlNmVjZjIyODtvWTVpcjViQi9pMXlSeFdpQ2FRTE1QdkFsS0dBaVVjNy8yK0IyczhXcUJzPQ==
aHR0cHM6Ly9hcGkuZGV2Lmdhc3Ryb2phbWVzLmNoOzY4ODc1MjY2MTcxMjgxN2RlMjcxNTlmMjtGTEVEL1phQ2F5a0NrdEpCOHZzN1lqVFNyaStleGdKVWIva21WWk1lUHBNPQ==

6
EpsonTest/invoice.json Normal file
View File

@@ -0,0 +1,6 @@
{
"PrinterIp": "10.0.20.12",
"LogoUrl": "https://james-dev-public-images.s3.eu-central-1.amazonaws.com/01-21-26-15:26:16.jpeg",
"ReceiptType": 4,
"Content": "{\"CompanyName\":\"Gaumenfreuden\",\"Address1\":\"Orellistrasse 21\",\"Address2\":\"8044 Zurich\",\"Phone\":\"\\u002B380504371463\",\"ReceiptNumber\":null,\"DateTime\":\"2026-01-28T12:47:47.0874074Z\",\"Guests\":null,\"Items\":[{\"Quantity\":1,\"Description\":\"Beer light\",\"UnitPrice\":40,\"TotalPrice\":40,\"TaxCategory\":\"A\",\"SubItems\":null},{\"Quantity\":1,\"Description\":\"Premium Juice\",\"UnitPrice\":1.2,\"TotalPrice\":1.2,\"TaxCategory\":\"B\",\"SubItems\":null},{\"Quantity\":1,\"Description\":\"Beer light from Catalog\",\"UnitPrice\":20,\"TotalPrice\":20,\"TaxCategory\":\"A\",\"SubItems\":null},{\"Quantity\":1,\"Description\":\"Beer light from Catalog\",\"UnitPrice\":20,\"TotalPrice\":20,\"TaxCategory\":\"A\",\"SubItems\":null}],\"Total\":81.2,\"Currency\":\"CHF\",\"ThankYouMessage\":\"Thanks\",\"TableNumber\":\"HappyHoursDrinks\"}"
}

View File

@@ -6,7 +6,9 @@ public class PrintServer
private readonly IPrintService _printService;
private readonly ILogger _logger;
private readonly ConcurrentDictionary<string, PrinterQueue> _printerQueues;
private readonly ReaderWriterLockSlim _printDiscoveryLock = new();
private readonly SemaphoreSlim _printDiscoveryLock = new(1, 1);
private int _readerCount = 0;
private readonly object _readerCountLock = new();
public PrintServer(IPrintService printService, ILogger logger)
{
@@ -19,23 +21,58 @@ public class PrintServer
/// <summary>
/// Acquires a read lock for print operations. Multiple print operations can run concurrently.
/// </summary>
public void EnterPrintLock() => _printDiscoveryLock.EnterReadLock();
public void EnterPrintLock()
{
_logger.LogDebug("Entering print lock");
lock (_readerCountLock)
{
_readerCount++;
if (_readerCount == 1)
{
_printDiscoveryLock.Wait();
}
}
_logger.LogDebug("Entered print lock");
}
/// <summary>
/// Releases the read lock after a print operation completes.
/// </summary>
public void ExitPrintLock() => _printDiscoveryLock.ExitReadLock();
public void ExitPrintLock()
{
_logger.LogDebug("Exiting print lock");
lock (_readerCountLock)
{
_readerCount--;
if (_readerCount == 0)
{
_printDiscoveryLock.Release();
}
}
_logger.LogDebug("Exited print lock");
}
/// <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();
public async Task EnterDiscoveryLockAsync()
{
_logger.LogDebug("Entering discovery lock");
await _printDiscoveryLock.WaitAsync();
_logger.LogDebug("Entered discovery lock");
}
/// <summary>
/// Releases the write lock after discovery completes.
/// </summary>
public void ExitDiscoveryLock() => _printDiscoveryLock.ExitWriteLock();
public void ExitDiscoveryLock()
{
_logger.LogDebug("Exiting discovery lock");
_printDiscoveryLock.Release();
_logger.LogDebug("Exited discovery lock");
}
public void RegisterPrinter(string printerIp)
{