Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development of CUSTOM Printer Implementation with CommandValue Refactor #106

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 49 additions & 23 deletions ESCPOS_NET.ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,48 @@ namespace ESCPOS_NET.ConsoleTest
internal class Program
{
private static BasePrinter printer;
private static ICommandEmitter e;
private static ICommandEmitter emitter;

static void Main(string[] args)
{

Console.WriteLine("ESCPOS_NET Test Application...");

Console.WriteLine($"{(int)PrinterTypeOption.EPSON} {PrinterTypeOption.EPSON} Printer");
Console.WriteLine($"{(int)PrinterTypeOption.CUSTOM} {PrinterTypeOption.CUSTOM} Printer");
Console.Write("Choice: ");
var response = Console.ReadLine();
var valid = new List<string> { ((int)PrinterTypeOption.EPSON).ToString(), ((int)PrinterTypeOption.CUSTOM).ToString() };
if (!valid.Contains(response))
{
response = ((int)PrinterTypeOption.EPSON).ToString();
}
int choice = int.Parse(response);
switch((PrinterTypeOption)choice)
{
case PrinterTypeOption.EPSON:
emitter = new EPSON();
break;
case PrinterTypeOption.CUSTOM:
emitter = new CUSTOM();
break;
}

Console.WriteLine("1 ) Test Serial Port");
Console.WriteLine("2 ) Test Network Printer");
Console.Write("Choice: ");
string comPort = "";
string baudRate;
string ip;
string networkPort;
var response = Console.ReadLine();
var valid = new List<string> { "1", "2" };
response = Console.ReadLine();
valid = new List<string> { "1", "2" };
if (!valid.Contains(response))
{
response = "1";
}

int choice = int.Parse(response);
choice = int.Parse(response);

if (choice == 1)
{
Expand Down Expand Up @@ -88,7 +109,6 @@ static void Main(string[] args)
monitor = true;
}

e = new EPSON();
var testCases = new Dictionary<Option, string>()
{
{ Option.Printing, "Printing" },
Expand Down Expand Up @@ -134,61 +154,61 @@ static void Main(string[] args)
}
Setup(monitor);

printer?.Write(e.PrintLine($"== [ Start {testCases[enumChoice]} ] =="));
printer?.Write(emitter.PrintLine($"== [ Start {testCases[enumChoice]} ] =="));

switch (enumChoice)
{
case Option.Printing:
printer.Write(Tests.Printing(e));
printer.Write(Tests.Printing(emitter));
break;
case Option.LineSpacing:
printer.Write(Tests.LineSpacing(e));
printer.Write(Tests.LineSpacing(emitter));
break;
case Option.BarcodeStyles:
printer.Write(Tests.BarcodeStyles(e));
printer.Write(Tests.BarcodeStyles(emitter));
break;
case Option.BarcodeTypes:
printer.Write(Tests.BarcodeTypes(e));
printer.Write(Tests.BarcodeTypes(emitter));
break;
case Option.TwoDimensionCodes:
printer.Write(Tests.TwoDimensionCodes(e));
printer.Write(Tests.TwoDimensionCodes(emitter));
break;
case Option.TextStyles:
printer.Write(Tests.TextStyles(e));
printer.Write(Tests.TextStyles(emitter));
break;
case Option.FullReceipt:
printer.Write(Tests.Receipt(e));
printer.Write(Tests.Receipt(emitter));
break;
case Option.Images:
printer.Write(Tests.Images(e, false));
printer.Write(Tests.Images(emitter, false));
break;
case Option.LegacyImages:
printer.Write(Tests.Images(e, true));
printer.Write(Tests.Images(emitter, true));
break;
case Option.LargeByteArrays:
try
{
printer.Write(Tests.TestLargeByteArrays(e));
printer.Write(Tests.TestLargeByteArrays(emitter));
}
catch (Exception e)
{
Console.WriteLine($"Aborting print due to test failure. Exception: {e?.Message}, Stack Trace: {e?.GetBaseException()?.StackTrace}");
}
break;
case Option.CashDrawerPin2:
printer.Write(Tests.CashDrawerOpenPin2(e));
printer.Write(Tests.CashDrawerOpenPin2(emitter));
break;
case Option.CashDrawerPin5:
printer.Write(Tests.CashDrawerOpenPin5(e));
printer.Write(Tests.CashDrawerOpenPin5(emitter));
break;
default:
Console.WriteLine("Invalid entry.");
break;
}

Setup(monitor);
printer?.Write(e.PrintLine($"== [ End {testCases[enumChoice]} ] =="));
printer?.Write(e.PartialCutAfterFeed(5));
printer?.Write(emitter.PrintLine($"== [ End {testCases[enumChoice]} ] =="));
printer?.Write(emitter.PartialCutAfterFeed(5));

// TODO: write a sanitation check.
// TODO: make DPI to inch conversion function
Expand All @@ -199,6 +219,12 @@ static void Main(string[] args)
}
}

public enum PrinterTypeOption
{
EPSON = 1,
CUSTOM = 2,
}

public enum Option
{
Printing = 1,
Expand Down Expand Up @@ -234,11 +260,11 @@ private static void Setup(bool enableStatusBackMonitoring)
printer.StatusChanged += StatusChanged;
_hasEnabledStatusMonitoring = true;
}
printer?.Write(e.Initialize());
printer?.Write(e.Enable());
printer?.Write(emitter.Initialize());
printer?.Write(emitter.Enable());
if (enableStatusBackMonitoring)
{
printer.Write(e.EnableAutomaticStatusBack());
printer.Write(emitter.EnableAutomaticStatusBack());
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions ESCPOS_NET.UnitTest/GlobalTests/Barcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class Barcode
[InlineData("EPSON", TwoDimensionCodeType.QRCODE_MODEL2)]
[InlineData("EPSON", TwoDimensionCodeType.QRCODE_MICRO)]
[InlineData("EPSON", null)]
[InlineData("CUSTOM", TwoDimensionCodeType.QRCODE_MODEL1)]
[InlineData("CUSTOM", TwoDimensionCodeType.QRCODE_MODEL2)]
[InlineData("CUSTOM", TwoDimensionCodeType.QRCODE_MICRO)]
[InlineData("CUSTOM", null)]
public void PrintQRCode_Success(string emitter, TwoDimensionCodeType? codeType)
{
var type = Assembly.LoadFrom("ESCPOS_NET").GetType($"ESCPOS_NET.Emitters.{emitter}", true);
Expand All @@ -30,6 +34,7 @@ public void PrintQRCode_Success(string emitter, TwoDimensionCodeType? codeType)

[Theory]
[InlineData("EPSON", TwoDimensionCodeType.PDF417)]
[InlineData("CUSTOM", TwoDimensionCodeType.PDF417)]
public void PrintQRCode_Failure(string emitter, TwoDimensionCodeType codeType)
{
var type = Assembly.LoadFrom("ESCPOS_NET").GetType($"ESCPOS_NET.Emitters.{emitter}", true);
Expand Down
14 changes: 6 additions & 8 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/ActionCommands.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using ESCPOS_NET.Emitters.BaseCommandValues;

namespace ESCPOS_NET.Emitters
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
public abstract partial class BaseCommandEmitter<TCommandValues> : ICommandEmitter
{
/* Action Commands */
public virtual byte[] FullCut() => new byte[] { Cmd.GS, Ops.PaperCut, Functions.PaperCutFullCut };
public virtual byte[] FullCut() => new byte[] { Values.GS, Values.PaperCut, Values.PaperCutFullCut };

public virtual byte[] PartialCut() => new byte[] { Cmd.GS, Ops.PaperCut, Functions.PaperCutPartialCut };
public virtual byte[] PartialCut() => new byte[] { Values.GS, Values.PaperCut, Values.PaperCutPartialCut };

public virtual byte[] FullCutAfterFeed(int lineCount) => new byte[] { Cmd.GS, Ops.PaperCut, Functions.PaperCutFullCutWithFeed, (byte)lineCount };
public virtual byte[] FullCutAfterFeed(int lineCount) => new byte[] { Values.GS, Values.PaperCut, Values.PaperCutFullCutWithFeed, (byte)lineCount };

public virtual byte[] PartialCutAfterFeed(int lineCount) => new byte[] { Cmd.GS, Ops.PaperCut, Functions.PaperCutPartialCutWithFeed, (byte)lineCount };
public virtual byte[] PartialCutAfterFeed(int lineCount) => new byte[] { Values.GS, Values.PaperCut, Values.PaperCutPartialCutWithFeed, (byte)lineCount };
}
}
37 changes: 18 additions & 19 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/BarcodeCommands.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ESCPOS_NET.DataValidation;
using ESCPOS_NET.Emitters.BaseCommandValues;
using ESCPOS_NET.Extensions;
using System;
using System.Collections.Generic;
Expand All @@ -8,7 +7,7 @@

namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
public abstract partial class BaseCommandEmitter<TCommandValues> : ICommandEmitter
{
/* Barcode Commands */
public virtual byte[] PrintBarcode(BarcodeType type, string barcode, BarcodeCode code = BarcodeCode.CODE_B)
Expand Down Expand Up @@ -38,7 +37,7 @@ protected virtual byte[] BarcodeBytes(BarcodeType type, string barcode, BarcodeC
barcode = $"{(char)0x7B}{(char)code}" + barcode;
}

var command = new List<byte> { Cmd.GS, Barcodes.PrintBarcode, (byte)type, (byte)barcode.Length };
var command = new List<byte> { Values.GS, Values.PrintBarcode, (byte)type, (byte)barcode.Length };
command.AddRange(barcode.ToCharArray().Select(x => (byte)x));
return command.ToArray();
}
Expand All @@ -62,39 +61,39 @@ public virtual byte[] Print2DCode(TwoDimensionCodeType type, string data, Size2D
protected virtual byte[] TwoDimensionCodeBytes(TwoDimensionCodeType type, string data, Size2DCode size, CorrectionLevel2DCode correction)
{
List<byte> command = new List<byte>();
byte[] initial = { Cmd.GS, Barcodes.Set2DCode, Barcodes.PrintBarcode };
byte[] initial = { Values.GS, Values.Set2DCode, Values.PrintBarcode };
switch (type)
{
case TwoDimensionCodeType.PDF417:
command.AddRange(initial, Barcodes.SetPDF417NumberOfColumns, Barcodes.AutoEnding);
command.AddRange(initial, Barcodes.SetPDF417NumberOfRows, Barcodes.AutoEnding);
command.AddRange(initial, Barcodes.SetPDF417DotSize, (byte)size);
command.AddRange(initial, Barcodes.SetPDF417CorrectionLevel, (byte)correction);
command.AddRange(initial, Values.SetPDF417NumberOfColumns, Values.AutoEnding);
command.AddRange(initial, Values.SetPDF417NumberOfRows, Values.AutoEnding);
command.AddRange(initial, Values.SetPDF417DotSize, (byte)size);
command.AddRange(initial, Values.SetPDF417CorrectionLevel, (byte)correction);

// k = (pL + pH * 256) - 3 --> But pH is always 0.
int k = data.Length;
int l = k + 3;
command.AddRange(initial, (byte)l, Barcodes.StorePDF417Data);
command.AddRange(initial, (byte)l, Values.StorePDF417Data);
command.AddRange(data.ToCharArray().Select(x => (byte)x));

// Prints stored PDF417
command.AddRange(initial, Barcodes.PrintPDF417);
command.AddRange(initial, Values.PrintPDF417);
break;

case TwoDimensionCodeType.QRCODE_MODEL1:
case TwoDimensionCodeType.QRCODE_MODEL2:
case TwoDimensionCodeType.QRCODE_MICRO:
command.AddRange(initial, Barcodes.SelectQRCodeModel, (byte)type, Barcodes.AutoEnding);
command.AddRange(initial, Barcodes.SetQRCodeDotSize, (byte)size);
command.AddRange(initial, Barcodes.SetQRCodeCorrectionLevel, (byte)correction);
command.AddRange(initial, Values.SelectQRCodeModel, (byte)type, Values.AutoEnding);
command.AddRange(initial, Values.SetQRCodeDotSize, (byte)size);
command.AddRange(initial, Values.SetQRCodeCorrectionLevel, (byte)correction);
int num = data.Length + 3;
int pL = num % 256;
int pH = num / 256;
command.AddRange(initial, (byte)pL, (byte)pH, Barcodes.StoreQRCodeData);
command.AddRange(initial, (byte)pL, (byte)pH, Values.StoreQRCodeData);
command.AddRange(data.ToCharArray().Select(x => (byte)x));

// Prints stored QRCode
command.AddRange(initial, Barcodes.PrintQRCode);
command.AddRange(initial, Values.PrintQRCode);
break;

default:
Expand All @@ -104,12 +103,12 @@ protected virtual byte[] TwoDimensionCodeBytes(TwoDimensionCodeType type, string
return command.ToArray();
}

public virtual byte[] SetBarcodeHeightInDots(int height) => new byte[] { Cmd.GS, Barcodes.SetBarcodeHeightInDots, (byte)height };
public virtual byte[] SetBarcodeHeightInDots(int height) => new byte[] { Values.GS, Values.SetBarcodeHeightInDots, (byte)height };

public virtual byte[] SetBarWidth(BarWidth width) => new byte[] { Cmd.GS, Barcodes.SetBarWidth, (byte)width };
public virtual byte[] SetBarWidth(BarWidth width) => new byte[] { Values.GS, Values.SetBarWidth, (byte)width };

public virtual byte[] SetBarLabelPosition(BarLabelPrintPosition position) => new byte[] { Cmd.GS, Barcodes.SetBarLabelPosition, (byte)position };
public virtual byte[] SetBarLabelPosition(BarLabelPrintPosition position) => new byte[] { Values.GS, Values.SetBarLabelPosition, (byte)position };

public virtual byte[] SetBarLabelFontB(bool fontB) => new byte[] { Cmd.GS, Barcodes.SetBarLabelFont, (byte)(fontB ? 1 : 0) };
public virtual byte[] SetBarLabelFontB(bool fontB) => new byte[] { Values.GS, Values.SetBarLabelFont, (byte)(fontB ? 1 : 0) };
}
}
7 changes: 7 additions & 0 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/BaseCommandEmitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter<TCommandValues> : ICommandEmitter where TCommandValues : ICommandValues, new()
{
TCommandValues Values = new TCommandValues();
}
}
10 changes: 4 additions & 6 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/CashDrawerCommands.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using ESCPOS_NET.Emitters.BaseCommandValues;

namespace ESCPOS_NET.Emitters
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
public abstract partial class BaseCommandEmitter<TCommandValues> : ICommandEmitter
{
/* Cash Drawer Commands */
public virtual byte[] CashDrawerOpenPin2() => new byte[] { Cmd.ESC, Ops.CashDrawerPulse, 0x00 };
public virtual byte[] CashDrawerOpenPin2() => new byte[] { Values.ESC, Values.CashDrawerPulse, 0x00 };

public virtual byte[] CashDrawerOpenPin5() => new byte[] { Cmd.ESC, Ops.CashDrawerPulse, 0x01 };
public virtual byte[] CashDrawerOpenPin5() => new byte[] { Values.ESC, Values.CashDrawerPulse, 0x01 };
}
}
18 changes: 8 additions & 10 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/CharacterCommands.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
using ESCPOS_NET.Emitters.BaseCommandValues;

namespace ESCPOS_NET.Emitters
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
public abstract partial class BaseCommandEmitter<TCommandValues> : ICommandEmitter
{
/* Character Commands */
public virtual byte[] SetStyles(PrintStyle style) => new byte[] { Cmd.ESC, Chars.StyleMode, (byte)style };
public virtual byte[] SetStyles(PrintStyle style) => new byte[] { Values.ESC, Values.StyleMode, (byte)style };

public virtual byte[] LeftAlign() => new byte[] { Cmd.ESC, Chars.Alignment, (byte)Align.Left };
public virtual byte[] LeftAlign() => new byte[] { Values.ESC, Values.Alignment, (byte)Align.Left };

public virtual byte[] CenterAlign() => new byte[] { Cmd.ESC, Chars.Alignment, (byte)Align.Center };
public virtual byte[] CenterAlign() => new byte[] { Values.ESC, Values.Alignment, (byte)Align.Center };

public virtual byte[] RightAlign() => new byte[] { Cmd.ESC, Chars.Alignment, (byte)Align.Right };
public virtual byte[] RightAlign() => new byte[] { Values.ESC, Values.Alignment, (byte)Align.Right };

public virtual byte[] RightCharacterSpacing(int spaceCount) => new byte[] { Cmd.ESC, Chars.RightCharacterSpacing, (byte)spaceCount };
public virtual byte[] RightCharacterSpacing(int spaceCount) => new byte[] { Values.ESC, Values.RightCharacterSpacing, (byte)spaceCount };

public virtual byte[] CodePage(CodePage codePage) => new byte[] { Cmd.ESC, Chars.CodePage, (byte)codePage };
public virtual byte[] CodePage(CodePage codePage) => new byte[] { Values.ESC, Values.CodePage, (byte)codePage };
}
}
8 changes: 3 additions & 5 deletions ESCPOS_NET/Emitters/BaseCommandEmitter/DisplayCommands.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using ESCPOS_NET.Emitters.BaseCommandValues;

namespace ESCPOS_NET.Emitters
namespace ESCPOS_NET.Emitters
{
public abstract partial class BaseCommandEmitter : ICommandEmitter
public abstract partial class BaseCommandEmitter<TCommandValues> : ICommandEmitter
{
/* Display Commands */
public virtual byte[] Clear() => new byte[] { Display.CLR };
public virtual byte[] Clear() => new byte[] { Values.CLR };
}
}
Loading