printer discovery in print service
This commit is contained in:
26
EpsonPrintService/ConsoleDiscoveredPrintersReceiver.cs
Normal file
26
EpsonPrintService/ConsoleDiscoveredPrintersReceiver.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using Inspectron.Epson;
|
||||
|
||||
namespace EpsonPrintService;
|
||||
|
||||
/// <summary>
|
||||
/// Simple console implementation that logs discovered printers to the console
|
||||
/// </summary>
|
||||
public class ConsoleDiscoveredPrintersReceiver : IDiscoveredPrintersReceiver
|
||||
{
|
||||
public Task OnPrintersDiscoveredAsync(IReadOnlyList<DiscoveredPrinter> printers)
|
||||
{
|
||||
Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] Discovered printers changed. Found {printers.Count} printer(s):");
|
||||
|
||||
foreach (var printer in printers)
|
||||
{
|
||||
Console.WriteLine($" - {printer.IPAddress}");
|
||||
}
|
||||
|
||||
if (printers.Count == 0)
|
||||
{
|
||||
Console.WriteLine(" (no printers found)");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
15
EpsonPrintService/IDiscoveredPrintersReceiver.cs
Normal file
15
EpsonPrintService/IDiscoveredPrintersReceiver.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Inspectron.Epson;
|
||||
|
||||
namespace EpsonPrintService;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for receiving discovered printer updates
|
||||
/// </summary>
|
||||
public interface IDiscoveredPrintersReceiver
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when the list of discovered printers has changed
|
||||
/// </summary>
|
||||
/// <param name="printers">The current list of discovered printers</param>
|
||||
Task OnPrintersDiscoveredAsync(IReadOnlyList<DiscoveredPrinter> printers);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
using System.Threading.Channels;
|
||||
|
||||
namespace EpsonPrintService;
|
||||
|
||||
public interface IPrintJobSource
|
||||
{
|
||||
Task<PrintJob> GetNextJobAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
94
EpsonPrintService/PrinterDiscoveryBackgroundTask.cs
Normal file
94
EpsonPrintService/PrinterDiscoveryBackgroundTask.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using Inspectron.Epson;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace EpsonPrintService;
|
||||
|
||||
/// <summary>
|
||||
/// Background task that periodically discovers printers and notifies receivers when changes occur
|
||||
/// </summary>
|
||||
public class PrinterDiscoveryBackgroundTask
|
||||
{
|
||||
private readonly IDiscoveryService _discoveryService;
|
||||
private readonly IDiscoveredPrintersReceiver _receiver;
|
||||
private readonly ILogger _logger;
|
||||
private readonly TimeSpan _interval;
|
||||
private readonly TimeSpan _discoveryTimeout;
|
||||
|
||||
private HashSet<string> _lastDiscoveredPrinterIps = new();
|
||||
private List<DiscoveredPrinter> _lastDiscoveredPrinters = new();
|
||||
|
||||
public PrinterDiscoveryBackgroundTask(
|
||||
IDiscoveryService discoveryService,
|
||||
IDiscoveredPrintersReceiver receiver,
|
||||
ILogger logger,
|
||||
TimeSpan? interval = null,
|
||||
TimeSpan? discoveryTimeout = null)
|
||||
{
|
||||
_discoveryService = discoveryService;
|
||||
_receiver = receiver;
|
||||
_logger = logger;
|
||||
_interval = interval ?? TimeSpan.FromSeconds(30);
|
||||
_discoveryTimeout = discoveryTimeout ?? TimeSpan.FromSeconds(3);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the background discovery task
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Token to cancel the background task</param>
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogInformation("Starting printer discovery background task (interval: {Interval}s)", _interval.TotalSeconds);
|
||||
|
||||
// Run immediately on start
|
||||
await DiscoverAndNotifyAsync();
|
||||
|
||||
// Then run periodically
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(_interval, cancellationToken);
|
||||
await DiscoverAndNotifyAsync();
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error during printer discovery");
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Printer discovery background task stopped");
|
||||
}
|
||||
|
||||
private async Task DiscoverAndNotifyAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var printers = await _discoveryService.DiscoverPrintersAsync(_discoveryTimeout);
|
||||
var currentIps = printers.Select(p => p.IPAddress).ToHashSet();
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
private bool HasConfigurationChanged(HashSet<string> currentIps)
|
||||
{
|
||||
if (currentIps.Count != _lastDiscoveredPrinterIps.Count)
|
||||
return true;
|
||||
|
||||
return !currentIps.SetEquals(_lastDiscoveredPrinterIps);
|
||||
}
|
||||
}
|
||||
@@ -11,17 +11,17 @@ using Inspectron.Epson.PrintServer.JobSources;
|
||||
using Inspectron.Epson.PrintServer.PrinterAssinment;
|
||||
using Inspectron.Epson.PrintServer.Printers;
|
||||
using Inspectron.Epson.PrintServer.PrintServices;
|
||||
using EpsonPrintService;
|
||||
|
||||
StandardKernel kernel = new StandardKernel();
|
||||
|
||||
//var config = new EpsonPrintServiceConfiguration()
|
||||
//{
|
||||
// GroupId = "67e534f8e86e816689323023",
|
||||
// RestaurantId = "63e37295bd6f26dbd36164c0"
|
||||
//};
|
||||
//File.WriteAllText("config.json",JsonSerializer.Serialize(config,new JsonSerializerOptions(){WriteIndented = true}));
|
||||
|
||||
var config = JsonSerializer.Deserialize<EpsonPrintServiceConfiguration>(
|
||||
File.ReadAllText("config.json"));
|
||||
|
||||
kernel.Bind<IDiscoveryService>().To<DiscoveryService>().InSingletonScope();
|
||||
|
||||
|
||||
kernel.Bind<EpsonPrintServiceConfiguration>().ToConstant(config);
|
||||
|
||||
kernel.Bind<IPrintJobSource, SignalRPrintJobSource>().To<SignalRPrintJobSource>().InSingletonScope();
|
||||
@@ -38,8 +38,12 @@ kernel.Bind<IAssignedPrinterRepository>().ToConstant(config);
|
||||
kernel.Bind<IPrinterFactory>().To<PrinterFactory>().InSingletonScope();
|
||||
kernel.Bind<IPrinterConfigurationSource>().To<FixedConfigurationSource>();
|
||||
kernel.Bind<PrintServer>().ToSelf().InSingletonScope();
|
||||
kernel.Bind<IDiscoveredPrintersReceiver>().To<ConsoleDiscoveredPrintersReceiver>().InSingletonScope();
|
||||
kernel.Bind<PrinterDiscoveryBackgroundTask>().ToSelf().InSingletonScope();
|
||||
|
||||
var printLoop = kernel.Get<PrintLoop>();
|
||||
var printServer = kernel.Get<PrintServer>();
|
||||
var discoveryTask = kernel.Get<PrinterDiscoveryBackgroundTask>();
|
||||
|
||||
printServer.RegisterPrinter("127.0.0.1");
|
||||
|
||||
@@ -55,6 +59,9 @@ Console.CancelKeyPress += (sender, e) =>
|
||||
cts.Cancel();
|
||||
};
|
||||
|
||||
// Start the printer discovery background task
|
||||
_ = discoveryTask.StartAsync(cts.Token);
|
||||
|
||||
try
|
||||
{
|
||||
await Task.Delay(Timeout.Infinite, cts.Token);
|
||||
|
||||
Reference in New Issue
Block a user