printer discovery in print service
This commit is contained in:
@@ -18,7 +18,7 @@ public class ConfigureModel : PageModel
|
|||||||
private readonly PrintServerHostedService _printServerService;
|
private readonly PrintServerHostedService _printServerService;
|
||||||
private readonly LogoService _logoService;
|
private readonly LogoService _logoService;
|
||||||
private readonly IWebHostEnvironment _env;
|
private readonly IWebHostEnvironment _env;
|
||||||
private readonly ENPCDiscoveryService _discoveryService;
|
private readonly DiscoveryService _discoveryService;
|
||||||
private readonly ILogger<ConfigureModel> _logger;
|
private readonly ILogger<ConfigureModel> _logger;
|
||||||
|
|
||||||
public ConfigureModel(
|
public ConfigureModel(
|
||||||
@@ -32,7 +32,7 @@ public class ConfigureModel : PageModel
|
|||||||
_printServerService = printServerService;
|
_printServerService = printServerService;
|
||||||
_logoService = logoService;
|
_logoService = logoService;
|
||||||
_env = env;
|
_env = env;
|
||||||
_discoveryService = new ENPCDiscoveryService();
|
_discoveryService = new DiscoveryService();
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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.PrinterAssinment;
|
||||||
using Inspectron.Epson.PrintServer.Printers;
|
using Inspectron.Epson.PrintServer.Printers;
|
||||||
using Inspectron.Epson.PrintServer.PrintServices;
|
using Inspectron.Epson.PrintServer.PrintServices;
|
||||||
|
using EpsonPrintService;
|
||||||
|
|
||||||
StandardKernel kernel = new StandardKernel();
|
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>(
|
var config = JsonSerializer.Deserialize<EpsonPrintServiceConfiguration>(
|
||||||
File.ReadAllText("config.json"));
|
File.ReadAllText("config.json"));
|
||||||
|
|
||||||
|
kernel.Bind<IDiscoveryService>().To<DiscoveryService>().InSingletonScope();
|
||||||
|
|
||||||
|
|
||||||
kernel.Bind<EpsonPrintServiceConfiguration>().ToConstant(config);
|
kernel.Bind<EpsonPrintServiceConfiguration>().ToConstant(config);
|
||||||
|
|
||||||
kernel.Bind<IPrintJobSource, SignalRPrintJobSource>().To<SignalRPrintJobSource>().InSingletonScope();
|
kernel.Bind<IPrintJobSource, SignalRPrintJobSource>().To<SignalRPrintJobSource>().InSingletonScope();
|
||||||
@@ -38,8 +38,12 @@ kernel.Bind<IAssignedPrinterRepository>().ToConstant(config);
|
|||||||
kernel.Bind<IPrinterFactory>().To<PrinterFactory>().InSingletonScope();
|
kernel.Bind<IPrinterFactory>().To<PrinterFactory>().InSingletonScope();
|
||||||
kernel.Bind<IPrinterConfigurationSource>().To<FixedConfigurationSource>();
|
kernel.Bind<IPrinterConfigurationSource>().To<FixedConfigurationSource>();
|
||||||
kernel.Bind<PrintServer>().ToSelf().InSingletonScope();
|
kernel.Bind<PrintServer>().ToSelf().InSingletonScope();
|
||||||
|
kernel.Bind<IDiscoveredPrintersReceiver>().To<ConsoleDiscoveredPrintersReceiver>().InSingletonScope();
|
||||||
|
kernel.Bind<PrinterDiscoveryBackgroundTask>().ToSelf().InSingletonScope();
|
||||||
|
|
||||||
var printLoop = kernel.Get<PrintLoop>();
|
var printLoop = kernel.Get<PrintLoop>();
|
||||||
var printServer = kernel.Get<PrintServer>();
|
var printServer = kernel.Get<PrintServer>();
|
||||||
|
var discoveryTask = kernel.Get<PrinterDiscoveryBackgroundTask>();
|
||||||
|
|
||||||
printServer.RegisterPrinter("127.0.0.1");
|
printServer.RegisterPrinter("127.0.0.1");
|
||||||
|
|
||||||
@@ -55,6 +59,9 @@ Console.CancelKeyPress += (sender, e) =>
|
|||||||
cts.Cancel();
|
cts.Cancel();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Start the printer discovery background task
|
||||||
|
_ = discoveryTask.StartAsync(cts.Token);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.Delay(Timeout.Infinite, cts.Token);
|
await Task.Delay(Timeout.Infinite, cts.Token);
|
||||||
|
|||||||
@@ -42,18 +42,18 @@ using System.Xml.Linq;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
var printer = new EpsonPrinter();
|
//var printer = new EpsonPrinter();
|
||||||
await printer.ConnectAsync("127.0.0.1",8888);
|
//await printer.ConnectAsync("127.0.0.1",8888);
|
||||||
for (int i = 0; i < 20; i++)
|
//for (int i = 0; i < 20; i++)
|
||||||
{
|
//{
|
||||||
await printer.PrintTextAsync("Hello\n");
|
// await printer.PrintTextAsync("Hello\n");
|
||||||
}
|
//}
|
||||||
|
|
||||||
await printer.FeedLinesAsync(5);
|
//await printer.FeedLinesAsync(5);
|
||||||
await printer.CutAsync();
|
//await printer.CutAsync();
|
||||||
|
|
||||||
var status = await printer.GetPrinterStatusAsync();
|
//var status = await printer.GetPrinterStatusAsync();
|
||||||
Console.ReadLine();
|
//Console.ReadLine();
|
||||||
|
|
||||||
|
|
||||||
//var receipt = new Receipt
|
//var receipt = new Receipt
|
||||||
@@ -230,21 +230,24 @@ Console.ReadLine();
|
|||||||
//await Task.Delay(200);
|
//await Task.Delay(200);
|
||||||
|
|
||||||
//KitchenReceiptPrinter krp = new KitchenReceiptPrinter(33);
|
//KitchenReceiptPrinter krp = new KitchenReceiptPrinter(33);
|
||||||
//var receipt = new KitchenReceipt(
|
var receipt = new KitchenReceipt(
|
||||||
// Location: "Warme Küche",
|
Location: "Warme Küche",
|
||||||
// Date: "23-Okt-25 12:18 Nr.:13201B166",
|
Date: "23-Okt-25 12:18 Nr.:13201B166",
|
||||||
// Owner: "Mariano Amato\n32 (VK Restaurant)",
|
Owner: "Mariano Amato\n32 (VK Restaurant)",
|
||||||
// Tisch: "22",
|
Tisch: "22",
|
||||||
// items: new List<KitchenItem>
|
items: new List<KitchenItem>
|
||||||
// {
|
{
|
||||||
// new KitchenGang("1. Gang"),
|
new KitchenGang("1. Gang"),
|
||||||
// new KitchenProduct(1, "Avocado Sashimi"),
|
new KitchenProduct(1, "Avocado Sashimi"),
|
||||||
// new KitchenProduct(1, "Baby Spinach Salad with Truffl"),
|
new KitchenProduct(1, "Baby Spinach Salad with Truffl"),
|
||||||
// new KitchenGang("2. Gang"),
|
new KitchenGang("2. Gang"),
|
||||||
// new KitchenProduct(1, "Beef Tataki"),
|
new KitchenProduct(1, "Beef Tataki"),
|
||||||
// new KitchenProduct(1, "Salmon Taco")
|
new KitchenProduct(1, "Salmon Taco")
|
||||||
// }
|
}
|
||||||
//);
|
);
|
||||||
|
|
||||||
|
var test = JsonSerializer.Serialize(receipt, new JsonSerializerOptions() { WriteIndented = true });
|
||||||
|
Console.WriteLine(test);
|
||||||
|
|
||||||
////// Create printer with 42 character line width (default)
|
////// Create printer with 42 character line width (default)
|
||||||
//var commands = krp.ConvertToCommands(receipt);
|
//var commands = krp.ConvertToCommands(receipt);
|
||||||
@@ -288,8 +291,8 @@ Console.ReadLine();
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
await printer.FeedLinesAsync(10);
|
//await printer.FeedLinesAsync(10);
|
||||||
await Task.Delay(1000);
|
//await Task.Delay(1000);
|
||||||
|
|
||||||
|
|
||||||
//await printer.SetQuadrupleMode(false);
|
//await printer.SetQuadrupleMode(false);
|
||||||
@@ -335,7 +338,7 @@ await Task.Delay(1000);
|
|||||||
//await printer.PrintTextAsync("Hello!");
|
//await printer.PrintTextAsync("Hello!");
|
||||||
//await printer.FeedLinesAsync(5);
|
//await printer.FeedLinesAsync(5);
|
||||||
|
|
||||||
await printer.CutAsync();
|
//await printer.CutAsync();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ using System.Net.Sockets;
|
|||||||
|
|
||||||
namespace Inspectron.Epson;
|
namespace Inspectron.Epson;
|
||||||
|
|
||||||
public class ENPCDiscoveryService
|
public class DiscoveryService : IDiscoveryService
|
||||||
{
|
{
|
||||||
private const int DiscoveryPort = 3289;
|
private const int DiscoveryPort = 3289;
|
||||||
private const string DiscoveryMessage = "EPSONQ";
|
private const string DiscoveryMessage = "EPSONQ";
|
||||||
17
Inspectron.Epson/IDiscoveryService.cs
Normal file
17
Inspectron.Epson/IDiscoveryService.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
namespace Inspectron.Epson;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Interface for ENPC printer discovery service
|
||||||
|
/// </summary>
|
||||||
|
public interface IDiscoveryService
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Discovers Epson printers on the local network using UDP broadcast
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="timeout">How long to wait for responses (default: 5 seconds)</param>
|
||||||
|
/// <param name="onPrinterDiscovered">Optional callback invoked when a printer is discovered</param>
|
||||||
|
/// <returns>List of discovered printer information</returns>
|
||||||
|
Task<List<DiscoveredPrinter>> DiscoverPrintersAsync(
|
||||||
|
TimeSpan? timeout = null,
|
||||||
|
Action<DiscoveredPrinter>? onPrinterDiscovered = null);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user