Files
Print_server/Inspectron.Epson/PrintServer/Printers/TM-T30IIITranslated.cs
2026-01-14 11:47:43 +01:00

119 lines
3.4 KiB
C#

using System.Text.Json;
using Inspectron.Epson.PrintServer.Printers.Utils;
using Inspectron.Epson.PrintServer.Printers.Utils.FinalRecipe;
using Inspectron.Epson.PrintServer.PrintServices;
using FinalReceipt = Inspectron.Epson.PrintServer.Printers.Utils.FinalRecipe.FinalReceipt;
namespace Inspectron.Epson.PrintServer.Printers;
public class TM_T30IIITranslated:IPrinter
{
private readonly EpsonPrinter _printer;
public TM_T30IIITranslated(EpsonPrinter printer)
{
_printer = printer;
}
public Task InitAsync()
{
return _printer.InitAsync();
}
public async Task PrintImageAsync(string path)
{
await _printer.SetAbsolutePrintPosition(100);
await _printer.LoadImageAsync(Path.Combine("logos", path), 384);
await Task.Delay(100);
await _printer.FeedLinesAsync(1);
await _printer.SetAbsolutePrintPosition(0);
}
public async Task SetFontSizeAsync(int fontSize)
{
//await _printer.SetFontSizeAsync(fontSize, fontSize);
}
public async Task PrintTextAsync(string text)
{
FinalReceipt? receipt;
try
{
receipt = JsonSerializer.Deserialize<FinalReceipt>(text);
}
catch
{
// Fallback to kitchen receipt
await PrintKitchen(text);
return;
}
var converter = new ReceiptConverter(lineWidth: 48, bigFontLineWidth: 24);
var printCommands = converter.ConvertToPrintCommands(receipt);
await _printer.FeedLinesAsync(1);
await _printer.SetCustomLineSpacing(22);
foreach (var command in printCommands)
{
string attributes = "";
if (command.IsBig) attributes += "[BIG]\n";
if (command.IsBold) attributes += "[BOLD]\n";
Console.WriteLine($"{attributes}{command.Text}");
if (command.IsBig)
{
await _printer.SetFontSizeAsync(2, 1);
}
else
{
await _printer.SetFontSizeAsync(1, 1);
}
await _printer.SetEmphasized(command.IsBold);
await _printer.PrintTextAsync(command.Text + "\n");
}
await _printer.SetDefaultLineSpacing();
await _printer.FeedLinesAsync(10);
await Task.Delay(200);
}
private async Task PrintKitchen(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)
{
if (command.IsBig)
{
await _printer.SetFontSizeAsync(2,2);
}
else
{
await _printer.SetFontSizeAsync(1, 1);
}
await Task.Delay(200);
await _printer.SetRedColor(command.IsRed);
await Task.Delay(200);
await _printer.SetEmphasized(command.IsBold);
await Task.Delay(200);
await _printer.PrintTextAsync(command.Text + "\n");
await Task.Delay(200);
}
await _printer.FeedLinesAsync(10);
await Task.Delay(1000);
}
public async Task Cut()
{
await _printer.CutAsync();
await Task.Delay(200);
}
}