From e90465e2867609e836608faefa81d24356f26ee5 Mon Sep 17 00:00:00 2001 From: EugeneTes Date: Mon, 2 Feb 2026 12:50:00 +0100 Subject: [PATCH] discount --- .../PrinterDiscoveryBackgroundTask.cs | 4 ++ EpsonPrintService/Program.cs | 13 ++++- EpsonTest/Program.cs | 44 +++++++-------- EpsonTest/TestHelpers.cs | 5 +- .../PreconfiguredDiscoveryService.cs | 56 +++++++++++++++++++ .../JobSources/SignalRPrintJobSource.cs | 1 + .../Utils/BarReceipt/BarReceiptConverter.cs | 2 +- .../Printers/Utils/FinalReceipt/Models.cs | 3 +- .../Utils/FinalReceipt/ReceiptConverter.cs | 14 +++-- 9 files changed, 109 insertions(+), 33 deletions(-) create mode 100644 Inspectron.Epson/PreconfiguredDiscoveryService.cs diff --git a/EpsonPrintService/PrinterDiscoveryBackgroundTask.cs b/EpsonPrintService/PrinterDiscoveryBackgroundTask.cs index 858a2f0..18af6a1 100644 --- a/EpsonPrintService/PrinterDiscoveryBackgroundTask.cs +++ b/EpsonPrintService/PrinterDiscoveryBackgroundTask.cs @@ -44,6 +44,10 @@ public class PrinterDiscoveryBackgroundTask // Run immediately on start await DiscoverAndNotifyAsync(); + // Scan once for now, then exit + // By Reini, because we are loosing connection on frequent scans + return; + // Then run periodically while (!cancellationToken.IsCancellationRequested) { diff --git a/EpsonPrintService/Program.cs b/EpsonPrintService/Program.cs index 023d458..c81197b 100644 --- a/EpsonPrintService/Program.cs +++ b/EpsonPrintService/Program.cs @@ -34,7 +34,15 @@ var config = new EpsonPrintServiceConfiguration Console.WriteLine($"Restaurant: {config.RestaurantId}"); Console.WriteLine($"API URL: {config.ApiUrl}"); -kernel.Bind().To().InSingletonScope(); +//kernel.Bind().To().InSingletonScope(); + +var discovery = new PreconfiguredDiscoveryService(); +discovery.AddPrinter("192.168.50.176", "TM-T30III", port: 9100); +discovery.AddPrinter("192.168.50.60", "TM-T30III", port: 9100); +discovery.AddPrinter("192.168.50.61", "TM-T30III", port: 9100); +discovery.AddPrinter("192.168.50.80", "TM-U220II", port: 9100); +kernel.Bind().ToConstant(discovery); + kernel.Bind().ToConstant(new HttpClient { Timeout = TimeSpan.FromSeconds(30) }).InSingletonScope(); @@ -65,6 +73,9 @@ kernel.Bind().ToSelf().InSingletonScope(); var printLoop = kernel.Get(); var printServer = kernel.Get(); + + + var discoveryTask = kernel.Get(); var heartbeatTask = kernel.Get(); diff --git a/EpsonTest/Program.cs b/EpsonTest/Program.cs index 004e3e0..8d5cb85 100644 --- a/EpsonTest/Program.cs +++ b/EpsonTest/Program.cs @@ -10,19 +10,19 @@ using System.Text.Json; // ============================================================================ // FINAL RECEIPT - HTML PRINTER // ============================================================================ -//var receipt = TestHelpers.BuildSampleFinalReceipt(true); -//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true }); +var receipt = TestHelpers.BuildSampleFinalReceipt(true); +var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true }); -//var converter = new FinalReceiptConverter(lineWidth: 48, bigFontLineWidth: 24); -//var printCommands = converter.Convert(serializedReceipt); +var converter = new FinalReceiptConverter(lineWidth: 48, bigFontLineWidth: 24); +var printCommands = converter.Convert(serializedReceipt); -//TestHelpers.PrintCommandsToConsole(printCommands); +TestHelpers.PrintCommandsToConsole(printCommands); -//var printer = await TestHelpers.CreateHtmlPrinterAsync(); -//await TestHelpers.PrintCommandsToHtmlPrinterAsync( -// printer, -// printCommands, -// logoPath: "ristorante-klinglers.ch-logo_white_bg.png"); +var printer = await TestHelpers.CreateHtmlPrinterAsync(); +await TestHelpers.PrintCommandsToHtmlPrinterAsync( + printer, + printCommands, + logoPath: "ristorante-klinglers.ch-logo_white_bg.png"); // ============================================================================ @@ -43,20 +43,20 @@ using System.Text.Json; // ============================================================================ // INVOICE RECEIPT - HTML PRINTER // ============================================================================ -var receipt = TestHelpers.BuildSampleInvoiceReceipt(); -//var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true }); -var fileContent = File.ReadAllText("invoice.json"); -var serializedReceipt = JsonSerializer.Deserialize(fileContent); -var converter = new InvoiceReceiptConverter(lineWidth: 48, bigFontLineWidth: 24); -var printCommands = converter.Convert(serializedReceipt); +//var receipt = TestHelpers.BuildSampleInvoiceReceipt(); +////var serializedReceipt = JsonSerializer.Serialize(receipt, new JsonSerializerOptions { WriteIndented = true }); +//var fileContent = File.ReadAllText("invoice.json"); +//var serializedReceipt = JsonSerializer.Deserialize(fileContent); +//var converter = new InvoiceReceiptConverter(lineWidth: 48, bigFontLineWidth: 24); +//var printCommands = converter.Convert(serializedReceipt); -TestHelpers.PrintCommandsToConsole(printCommands); +//TestHelpers.PrintCommandsToConsole(printCommands); -var printer = await TestHelpers.CreateHtmlPrinterAsync(); -await TestHelpers.PrintCommandsToHtmlPrinterAsync( - printer, - printCommands, - logoPath: "ristorante-klinglers.ch-logo_white_bg.png"); +//var printer = await TestHelpers.CreateHtmlPrinterAsync(); +//await TestHelpers.PrintCommandsToHtmlPrinterAsync( +// printer, +// printCommands, +// logoPath: "ristorante-klinglers.ch-logo_white_bg.png"); // ============================================================================ diff --git a/EpsonTest/TestHelpers.cs b/EpsonTest/TestHelpers.cs index a425e1f..39dbc7c 100644 --- a/EpsonTest/TestHelpers.cs +++ b/EpsonTest/TestHelpers.cs @@ -112,7 +112,10 @@ public static class TestHelpers }, DiscountInfo = new DiscountInfo() { - + Amount = -50.00m, + Currency = "CHF", + Description = "Fine Dine", + } }; diff --git a/Inspectron.Epson/PreconfiguredDiscoveryService.cs b/Inspectron.Epson/PreconfiguredDiscoveryService.cs new file mode 100644 index 0000000..c53283d --- /dev/null +++ b/Inspectron.Epson/PreconfiguredDiscoveryService.cs @@ -0,0 +1,56 @@ +namespace Inspectron.Epson; + +/// +/// Discovery service that returns preconfigured printers instead of performing network discovery +/// +public class PreconfiguredDiscoveryService : IDiscoveryService +{ + private readonly List _printers = new(); + + public void AddPrinter(string ipAddress, string? modelName = null, int port = 9100) + { + _printers.Add(new DiscoveredPrinter + { + IPAddress = ipAddress, + ModelName = modelName ?? "Unknown", + Port = port, + DiscoveredAt = DateTime.UtcNow + }); + } + + public void RemovePrinter(string ipAddress) + { + _printers.RemoveAll(p => p.IPAddress == ipAddress); + } + + public void ClearPrinters() + { + _printers.Clear(); + } + + public Task> DiscoverPrintersAsync( + TimeSpan? timeout = null, + Action? onPrinterDiscovered = null) + { + var results = _printers.Select(p => new DiscoveredPrinter + { + IPAddress = p.IPAddress, + ModelName = p.ModelName, + Port = p.Port, + MACAddress = p.MACAddress, + ServiceUrl = p.ServiceUrl, + ServiceType = p.ServiceType, + Lifetime = p.Lifetime, + RawResponse = p.RawResponse, + DiscoveredAt = DateTime.UtcNow + }).ToList(); + + if (onPrinterDiscovered != null) + { + foreach (var printer in results) + onPrinterDiscovered(printer); + } + + return Task.FromResult(results); + } +} diff --git a/Inspectron.Epson/PrintServer/JobSources/SignalRPrintJobSource.cs b/Inspectron.Epson/PrintServer/JobSources/SignalRPrintJobSource.cs index af0c20d..8b9a903 100644 --- a/Inspectron.Epson/PrintServer/JobSources/SignalRPrintJobSource.cs +++ b/Inspectron.Epson/PrintServer/JobSources/SignalRPrintJobSource.cs @@ -27,6 +27,7 @@ public class SignalRPrintJobSource: IPrintJobSource // Register handlers BEFORE starting connection _connection.On("PrintJob", OnPrintJobReceived); + _connection.Reconnecting += OnReconnecting; _connection.Reconnected += OnReconnected; diff --git a/Inspectron.Epson/PrintServer/Printers/Utils/BarReceipt/BarReceiptConverter.cs b/Inspectron.Epson/PrintServer/Printers/Utils/BarReceipt/BarReceiptConverter.cs index c29b20c..e205ca0 100644 --- a/Inspectron.Epson/PrintServer/Printers/Utils/BarReceipt/BarReceiptConverter.cs +++ b/Inspectron.Epson/PrintServer/Printers/Utils/BarReceipt/BarReceiptConverter.cs @@ -98,7 +98,7 @@ public class BarReceiptConverter private static void PrintDish(Dish drink, List commands) { string drinkLine = $"{drink.Number}x {drink.Name}"; - commands.Add(new PrintCommand(drinkLine)); + commands.Add(new PrintCommand(drinkLine,isBig:true)); // Modifications if (drink.Modifications != null && (drink.Modifications.Removed.Any() || drink.Modifications.Added.Any())) { diff --git a/Inspectron.Epson/PrintServer/Printers/Utils/FinalReceipt/Models.cs b/Inspectron.Epson/PrintServer/Printers/Utils/FinalReceipt/Models.cs index 42f34c6..def6817 100644 --- a/Inspectron.Epson/PrintServer/Printers/Utils/FinalReceipt/Models.cs +++ b/Inspectron.Epson/PrintServer/Printers/Utils/FinalReceipt/Models.cs @@ -35,8 +35,7 @@ public class DiscountInfo public string Description { get; set; } public decimal Amount { get; set; } public string Currency { get; set; } - public decimal? AmountInAlternateCurrency { get; set; } - public string AlternateCurrency { get; set; } + } public class FinalReceiptItem diff --git a/Inspectron.Epson/PrintServer/Printers/Utils/FinalReceipt/ReceiptConverter.cs b/Inspectron.Epson/PrintServer/Printers/Utils/FinalReceipt/ReceiptConverter.cs index e5e0f59..ccf819b 100644 --- a/Inspectron.Epson/PrintServer/Printers/Utils/FinalReceipt/ReceiptConverter.cs +++ b/Inspectron.Epson/PrintServer/Printers/Utils/FinalReceipt/ReceiptConverter.cs @@ -64,6 +64,8 @@ public class ReceiptConverter commands.Add(new PrintCommand("---------".PadLeft(_lineWidth))); commands.Add(new PrintCommand("")); //Reini + + // Total string totalLine = $"Summe: {finalReceipt.Total:F2} {finalReceipt.Currency}"; commands.Add(new PrintCommand(Center(totalLine, true), true, true)); @@ -78,12 +80,12 @@ public class ReceiptConverter } // Discount - //if (finalReceipt.DiscountInfo != null && finalReceipt.DiscountInfo.Amount > 0) - //{ - - // commands.Add(new PrintCommand(finalReceipt.DiscountInfo.Description.PadLeft(_lineWidth/2))); - // commands.Add(new PrintCommand("")); - //} + if (finalReceipt.DiscountInfo != null) + { + + commands.Add(new PrintCommand(finalReceipt.DiscountInfo.Description.PadRight(_lineWidth / 2) + (finalReceipt.DiscountInfo.Amount.ToString("F2") + " " + finalReceipt.DiscountInfo.Currency).PadLeft(_lineWidth / 2))); + commands.Add(new PrintCommand("")); + } // Split payments if (finalReceipt.SplitPayments != null && finalReceipt.SplitPayments.Count > 0)