print server with new concept

This commit is contained in:
EugeneTes
2026-01-16 12:00:24 +01:00
parent 147265358f
commit 5229cac987
30 changed files with 694 additions and 217 deletions

View File

@@ -0,0 +1,18 @@
using Microsoft.Extensions.Logging;
namespace Inspectron.Epson.PrintServer.Printers;
public class EpsonPrinterFactory:IPrinterFactory
{
private readonly ILogger _logger;
public EpsonPrinterFactory(ILogger logger)
{
_logger = logger;
}
public IEpsonPrinter CreatePrinter()
{
return new EpsonPrinter(_logger);
}
}

View File

@@ -0,0 +1,12 @@
using Microsoft.Extensions.Logging;
namespace Inspectron.Epson.PrintServer.Printers;
public class HtmlPrinterFactory(string filename,int paperWidth=384, byte type=0x1, ILogger logger=null) : IPrinterFactory
{
public IEpsonPrinter CreatePrinter()
{
return new HtmlPrinter(filename,paperWidth: paperWidth, id:type);
}
}

View File

@@ -0,0 +1,6 @@
namespace Inspectron.Epson.PrintServer.Printers;
public interface IPrinterFactory
{
IEpsonPrinter CreatePrinter();
}

View File

@@ -2,9 +2,9 @@
namespace Inspectron.Epson.PrintServer.Printers;
public class PrinterFactory:IPrinterFactory
public class PrinterWrapperFactory:IWrapperPrinterFactory
{
public IPrinter CreatePrinterFromId(byte printerId, EpsonPrinter epsonPrinter)
public IPrinter CreatePrinterFromId(byte printerId, IEpsonPrinter epsonPrinter)
{
switch (printerId)
{

View File

@@ -8,9 +8,9 @@ namespace Inspectron.Epson.PrintServer.Printers;
public class TM_T30IIITranslated:IPrinter
{
private readonly EpsonPrinter _printer;
private readonly IEpsonPrinter _printer;
public TM_T30IIITranslated(EpsonPrinter printer)
public TM_T30IIITranslated(IEpsonPrinter printer)
{
_printer = printer;
}

View File

@@ -1,14 +1,16 @@
using Inspectron.Epson.PrintServer.Printers.Utils;
using Inspectron.Epson.PrintServer.Printers.Utils.KitchenReceipt;
using Inspectron.Epson.PrintServer.PrintServices;
using System.Reflection;
using System.Text.Json;
namespace Inspectron.Epson.PrintServer.Printers;
public class TM_U220IITranslated:IPrinter
{
private readonly EpsonPrinter _printer;
private readonly IEpsonPrinter _printer;
public TM_U220IITranslated(EpsonPrinter printer)
public TM_U220IITranslated(IEpsonPrinter printer)
{
_printer = printer;
}
@@ -34,11 +36,10 @@ public class TM_U220IITranslated:IPrinter
public async Task PrintTextAsync(string text)
{
ReceiptTranslator translator = new ReceiptTranslator();
var ast = translator.ParseReceipt(text);
KitchenReceiptPrinter printer = new KitchenReceiptPrinter(lineWidth:33,21);
var commands = printer.ConvertToCommands((TranslatedKitchenReceipt)ast);
foreach (KitchenPrintCommand command in commands)
var deserializedReceipt = JsonSerializer.Deserialize<KitchenReceipt>(text);
KitchenReceiptConverter translator = new KitchenReceiptConverter(bigLineWidth: 20, lineWidth: 33);
var commands = translator.ConvertToPrintCommands(deserializedReceipt);
foreach (var command in commands)
{
await _printer.SetBiggerFontTM220(command.IsBig);
await Task.Delay(200);
@@ -49,12 +50,7 @@ public class TM_U220IITranslated:IPrinter
await _printer.PrintTextAsync(command.Text + "\n");
await Task.Delay(200);
}
//await _printer.PrintTextAsync(text);
//var lines = text.Split('\n').Length;
//for (int i = 0; i < lines; i++)
//{
// await Task.Delay(200);
//}
await _printer.FeedLinesAsync(10);
await Task.Delay(1000);
}

View File

@@ -24,6 +24,7 @@ public class FinalReceipt
public string ThankYouMessage { get; set; }
public string GoodbyeMessageLine1 { get; set; }
public string GoodbyeMessageLine2 { get; set; }
public PaymentTerminalReceipt TerminalReceipt { get; set; }
}
public class ReceiptItem
@@ -43,4 +44,23 @@ public class TaxInfo
public decimal Net { get; set; }
public decimal TaxAmount { get; set; }
public string Currency { get; set; }
}
public class PaymentTerminalReceipt
{
public string ReceiptType { get; set; } // e.g., "*** Kundenbeleg ***"
public string BookingType { get; set; } // e.g., "Buchung"
public string PaymentSystem { get; set; } // e.g., "TWINT"
public string TransactionNumber { get; set; }
public DateTime TransactionDateTime { get; set; }
public string TerminalId { get; set; }
public string AID { get; set; }
public string TransactionSeqCount { get; set; }
public string TransactionRefNo { get; set; }
public string AuthCode { get; set; }
public string AcquirerId { get; set; }
public decimal EftAmount { get; set; }
public decimal TipAmount { get; set; }
public decimal TotalEftAmount { get; set; }
public string Currency { get; set; }
}

View File

@@ -83,9 +83,6 @@ public class ReceiptConverter
commands.Add(new PrintCommand(""));
// Footer info
//commands.Add(new PrintCommand(Center($"Bedient von: {finalReceipt.WaiterName}", false)));
//commands.Add(new PrintCommand(Center($"Terminal: {finalReceipt.Terminal}", false)));
//commands.Add(new PrintCommand(Center($"Tisch: {finalReceipt.TableNumber}", false)));
commands.Add(new PrintCommand($"Bedient von:".PadLeft(_lineWidth / 2) + $" {finalReceipt.WaiterName}"));
commands.Add(new PrintCommand($"Terminal:".PadLeft(_lineWidth / 2) + $" {finalReceipt.Terminal}"));
commands.Add(new PrintCommand($"Tisch:".PadLeft(_lineWidth / 2)+$" {finalReceipt.TableNumber}"));
@@ -94,6 +91,33 @@ public class ReceiptConverter
// VAT number
commands.Add(new PrintCommand(Center(finalReceipt.VatNumber, false)));
commands.Add(new PrintCommand(""));
// Payment Terminal Receipt
if (finalReceipt.TerminalReceipt != null)
{
var terminal = finalReceipt.TerminalReceipt;
commands.Add(new PrintCommand(Center(terminal.ReceiptType, false)));
commands.Add(new PrintCommand(Center(terminal.BookingType, false)));
commands.Add(new PrintCommand(Center(terminal.PaymentSystem, false)));
commands.Add(new PrintCommand(terminal.TransactionNumber));
commands.Add(new PrintCommand($"{terminal.TransactionDateTime:dd.MM.yyyy}".PadRight(_lineWidth/2) + $"{terminal.TransactionDateTime:HH:mm:ss}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"Trm-Id:".PadRight(_lineWidth/2) + $"{terminal.TerminalId}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"AID:".PadRight(_lineWidth/2) + $"{terminal.AID}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"Trx. Seq-Cnt:".PadRight(_lineWidth/2) + $"{terminal.TransactionSeqCount}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"Trx. Ref-No:".PadRight(_lineWidth/2) + $"{terminal.TransactionRefNo}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"Auth. Code:".PadRight(_lineWidth/2) + $"{terminal.AuthCode}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"Acq-Id:".PadRight(_lineWidth/2) + $"{terminal.AcquirerId}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"EFT {terminal.Currency}:".PadRight(_lineWidth/2) + $"{terminal.EftAmount:F2}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"Trinkgeld {terminal.Currency}:".PadRight(_lineWidth/2) + $"{terminal.TipAmount:F2}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand($"Total-EFT {terminal.Currency}:".PadRight(_lineWidth/2) + $"{terminal.TotalEftAmount:F2}".PadLeft(_lineWidth/2)));
commands.Add(new PrintCommand(new string('-', _lineWidth)));
commands.Add(new PrintCommand(""));
}
// Thank you message
commands.Add(new PrintCommand(Center(finalReceipt.ThankYouMessage, false)));

View File

@@ -40,15 +40,11 @@
public List<Dish> Dishes { get; set; } = new List<Dish>();
/// <summary>
/// Additional items or notes (e.g., "Amuse Bouche")
/// </summary>
public List<string> AdditionalInfo { get; set; }
public KitchenReceipt()
{
Gangs = new List<Gang>();
AdditionalInfo = new List<string>();
}
}

View File

@@ -0,0 +1,27 @@
namespace Inspectron.Epson.PrintServer.Printers.Utils.OrderItems;
public class OrderItemsReceipt
{
public string OrderNumber { get; set; }
public string QRInfo { get; set; }
public DateTime DateTime { get; set; }
public string ClientNotes { get; set; }
public List<OrderItem> Items { get; set; } = new List<OrderItem>();
}
public class OrderItem
{
public int Number { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public string Size { get; set; }
public List<string> SubItems { get; set; } = new List<string>();
public ItemModifications Modifications { get; set; }
public string Comment { get; set; }
}
public class ItemModifications
{
public List<string> Removed { get; set; } = new List<string>();
public List<string> Added { get; set; } = new List<string>();
}

View File

@@ -0,0 +1,183 @@
namespace Inspectron.Epson.PrintServer.Printers.Utils.OrderItems;
public class ReceiptConverter
{
private readonly int _lineWidth;
private readonly int _bigFontLineWidth;
public ReceiptConverter(int lineWidth = 42, int bigFontLineWidth = 21)
{
_lineWidth = lineWidth;
_bigFontLineWidth = bigFontLineWidth;
}
public List<PrintCommand> ConvertToPrintCommands(OrderItemsReceipt receipt)
{
var commands = new List<PrintCommand>();
// Header box
AddHeaderBox(commands, receipt);
// Client Notes
if (!string.IsNullOrEmpty(receipt.ClientNotes))
{
commands.Add(new PrintCommand($"|Client Notes: {receipt.ClientNotes}"));
}
// Separator line
commands.Add(new PrintCommand(new string('-', _lineWidth)));
// Items
foreach (var item in receipt.Items)
{
commands.Add(new PrintCommand(""));
AddOrderItem(commands, item);
}
return commands;
}
private void AddHeaderBox(List<PrintCommand> commands, OrderItemsReceipt receipt)
{
// Column widths (fixed proportions)
int col1Width = 8; // Order column
int col3Width = 12; // Date/Time column
int col2Width = _lineWidth - col1Width - col3Width - 4; // QR column (4 for borders |)
// Prepare column content with labels on first line, values on subsequent lines
var col1Lines = new List<string> { "Order:" };
col1Lines.AddRange(WrapText($"#{receipt.OrderNumber}", col1Width));
var col2Lines = new List<string> { "QR:" };
col2Lines.AddRange(WrapText(receipt.QRInfo, col2Width));
var col3Lines = new List<string>
{
$"{receipt.DateTime:dd.MM.yyyy}",
$"{receipt.DateTime:HH:mm:ss}"
};
// Determine max rows needed
int maxRows = Math.Max(Math.Max(col1Lines.Count, col2Lines.Count), col3Lines.Count);
// Pad columns to have equal rows
while (col1Lines.Count < maxRows) col1Lines.Add("");
while (col2Lines.Count < maxRows) col2Lines.Add("");
while (col3Lines.Count < maxRows) col3Lines.Add("");
// Top border
commands.Add(new PrintCommand("+" + new string('-', col1Width) + "+" + new string('-', col2Width) + "+" + new string('-', col3Width) + "+"));
// Content rows
for (int i = 0; i < maxRows; i++)
{
string row = "|" +
CenterInWidth(col1Lines[i], col1Width) + "|" +
CenterInWidth(col2Lines[i], col2Width) + "|" +
CenterInWidth(col3Lines[i], col3Width) + "|";
commands.Add(new PrintCommand(row, isBold: i == 0));
}
// Bottom border
commands.Add(new PrintCommand("+" + new string('-', col1Width) + "+" + new string('-', col2Width) + "+" + new string('-', col3Width) + "+"));
}
private List<string> WrapText(string text, int maxWidth)
{
var lines = new List<string>();
if (string.IsNullOrEmpty(text))
{
lines.Add("");
return lines;
}
// Trim to fit within column
while (text.Length > 0)
{
if (text.Length <= maxWidth)
{
lines.Add(text);
break;
}
// Find a good break point (prefer space)
int breakPoint = text.LastIndexOf(' ', Math.Min(maxWidth, text.Length - 1));
if (breakPoint <= 0 || breakPoint > maxWidth)
{
breakPoint = maxWidth;
}
lines.Add(text.Substring(0, breakPoint).TrimEnd());
text = text.Substring(breakPoint).TrimStart();
}
return lines;
}
private void AddOrderItem(List<PrintCommand> commands, OrderItem item)
{
// Main item line: "1 Item Name x1" or "3 Item Name Size: L x2"
string numberPart = $"{item.Number}";
string namePart = item.Name;
string quantityPart = $"x{item.Quantity}";
string sizePart = "";
if (!string.IsNullOrEmpty(item.Size))
{
sizePart = $"Size: {item.Size} ";
}
string rightPart = sizePart + quantityPart;
int spacesNeeded = _lineWidth - numberPart.Length - 1 - namePart.Length - rightPart.Length;
if (spacesNeeded < 1) spacesNeeded = 1;
string mainLine = numberPart + " " + namePart + new string(' ', spacesNeeded) + rightPart;
commands.Add(new PrintCommand(mainLine));
// Sub-items
foreach (var subItem in item.SubItems)
{
commands.Add(new PrintCommand($" - {subItem}"));
}
// Modifications
if (item.Modifications != null && (item.Modifications.Removed.Any() || item.Modifications.Added.Any()))
{
commands.Add(new PrintCommand(" Change of"));
commands.Add(new PrintCommand(" Ingredients:"));
foreach (var removed in item.Modifications.Removed)
{
commands.Add(new PrintCommand($" - {removed}"));
}
foreach (var added in item.Modifications.Added)
{
commands.Add(new PrintCommand($" + {added}"));
}
}
// Comment
if (!string.IsNullOrEmpty(item.Comment))
{
commands.Add(new PrintCommand($" Comment: {item.Comment}"));
}
}
private string CenterInWidth(string text, int width)
{
if (string.IsNullOrEmpty(text))
return new string(' ', width);
if (text.Length >= width)
return text.Substring(0, width);
int totalPadding = width - text.Length;
int leftPadding = totalPadding / 2;
int rightPadding = totalPadding - leftPadding;
return new string(' ', leftPadding) + text + new string(' ', rightPadding);
}
}