templates support
This commit is contained in:
16
Inspectron.Epson.Templates/Configuration/PrinterProfile.cs
Normal file
16
Inspectron.Epson.Templates/Configuration/PrinterProfile.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace Inspectron.Epson.Templates.Configuration;
|
||||
|
||||
public record PrinterProfile(
|
||||
string Id,
|
||||
string Name,
|
||||
int LineWidth,
|
||||
int BigLineWidth,
|
||||
bool SupportsRed)
|
||||
{
|
||||
public static PrinterProfile Default { get; } = new(
|
||||
Id: "default",
|
||||
Name: "Default Printer",
|
||||
LineWidth: 48,
|
||||
BigLineWidth: 24,
|
||||
SupportsRed: false);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
namespace Inspectron.Epson.Templates.Configuration;
|
||||
|
||||
public class PrinterProfileRegistry
|
||||
{
|
||||
private readonly Dictionary<byte, PrinterProfile> _profilesById = new();
|
||||
private readonly Dictionary<string, PrinterProfile> _profilesByName = new();
|
||||
|
||||
public PrinterProfileRegistry()
|
||||
{
|
||||
// Register built-in profiles
|
||||
RegisterBuiltInProfiles();
|
||||
}
|
||||
|
||||
private void RegisterBuiltInProfiles()
|
||||
{
|
||||
// TM-T30III (default thermal printer)
|
||||
var tmT30III = new PrinterProfile(
|
||||
Id: "tm-t30iii",
|
||||
Name: "TM-T30III",
|
||||
LineWidth: 48,
|
||||
BigLineWidth: 24,
|
||||
SupportsRed: false);
|
||||
Register(0x01, tmT30III);
|
||||
|
||||
// TM-U220II (impact printer with red support)
|
||||
var tmU220II = new PrinterProfile(
|
||||
Id: "tm-u220ii",
|
||||
Name: "TM-U220II",
|
||||
LineWidth: 33,
|
||||
BigLineWidth: 20,
|
||||
SupportsRed: true);
|
||||
Register(0x0D, tmU220II);
|
||||
Register(0x13, tmU220II);
|
||||
}
|
||||
|
||||
public void Register(byte printerId, PrinterProfile profile)
|
||||
{
|
||||
_profilesById[printerId] = profile;
|
||||
_profilesByName[profile.Id] = profile;
|
||||
}
|
||||
|
||||
public PrinterProfile GetProfile(byte printerId)
|
||||
{
|
||||
return _profilesById.TryGetValue(printerId, out var profile)
|
||||
? profile
|
||||
: PrinterProfile.Default;
|
||||
}
|
||||
|
||||
public PrinterProfile GetProfile(string profileId)
|
||||
{
|
||||
return _profilesByName.TryGetValue(profileId, out var profile)
|
||||
? profile
|
||||
: PrinterProfile.Default;
|
||||
}
|
||||
|
||||
public IEnumerable<PrinterProfile> GetAllProfiles()
|
||||
{
|
||||
return _profilesByName.Values.Distinct();
|
||||
}
|
||||
|
||||
public bool TryGetProfile(byte printerId, out PrinterProfile? profile)
|
||||
{
|
||||
return _profilesById.TryGetValue(printerId, out profile);
|
||||
}
|
||||
|
||||
public bool TryGetProfile(string profileId, out PrinterProfile? profile)
|
||||
{
|
||||
return _profilesByName.TryGetValue(profileId, out profile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace Inspectron.Epson.Templates.Configuration;
|
||||
|
||||
public record TemplateAssignment(
|
||||
int ReceiptType,
|
||||
string? ProfileId,
|
||||
string TemplatePath)
|
||||
{
|
||||
public bool MatchesExact(int receiptType, string profileId)
|
||||
{
|
||||
return ReceiptType == receiptType &&
|
||||
!string.IsNullOrEmpty(ProfileId) &&
|
||||
ProfileId.Equals(profileId, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public bool MatchesTypeOnly(int receiptType)
|
||||
{
|
||||
return ReceiptType == receiptType && string.IsNullOrEmpty(ProfileId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Inspectron.Epson.Templates.Configuration;
|
||||
|
||||
public class TemplateConfiguration
|
||||
{
|
||||
[JsonPropertyName("assignments")]
|
||||
public List<TemplateAssignmentJson> Assignments { get; set; } = new();
|
||||
|
||||
[JsonPropertyName("fallbackTemplate")]
|
||||
public string FallbackTemplate { get; set; } = "fallback.template";
|
||||
|
||||
public static TemplateConfiguration Load(string jsonPath)
|
||||
{
|
||||
var json = File.ReadAllText(jsonPath);
|
||||
return JsonSerializer.Deserialize<TemplateConfiguration>(json)
|
||||
?? new TemplateConfiguration();
|
||||
}
|
||||
|
||||
public static TemplateConfiguration LoadFromJson(string json)
|
||||
{
|
||||
return JsonSerializer.Deserialize<TemplateConfiguration>(json)
|
||||
?? new TemplateConfiguration();
|
||||
}
|
||||
|
||||
public IEnumerable<TemplateAssignment> GetAssignments()
|
||||
{
|
||||
return Assignments.Select(a => new TemplateAssignment(
|
||||
a.ReceiptType,
|
||||
a.ProfileId,
|
||||
a.TemplatePath));
|
||||
}
|
||||
}
|
||||
|
||||
public class TemplateAssignmentJson
|
||||
{
|
||||
[JsonPropertyName("receiptType")]
|
||||
public int ReceiptType { get; set; }
|
||||
|
||||
[JsonPropertyName("profileId")]
|
||||
public string? ProfileId { get; set; }
|
||||
|
||||
[JsonPropertyName("template")]
|
||||
public string TemplatePath { get; set; } = string.Empty;
|
||||
}
|
||||
45
Inspectron.Epson.Templates/Configuration/TemplateResolver.cs
Normal file
45
Inspectron.Epson.Templates/Configuration/TemplateResolver.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace Inspectron.Epson.Templates.Configuration;
|
||||
|
||||
public class TemplateResolver
|
||||
{
|
||||
private readonly List<TemplateAssignment> _assignments;
|
||||
private readonly string _fallbackTemplate;
|
||||
|
||||
public TemplateResolver(TemplateConfiguration configuration)
|
||||
{
|
||||
_assignments = configuration.GetAssignments().ToList();
|
||||
_fallbackTemplate = configuration.FallbackTemplate;
|
||||
}
|
||||
|
||||
public TemplateResolver(IEnumerable<TemplateAssignment> assignments, string fallbackTemplate = "fallback.template")
|
||||
{
|
||||
_assignments = assignments.ToList();
|
||||
_fallbackTemplate = fallbackTemplate;
|
||||
}
|
||||
|
||||
public string Resolve(int receiptType, string profileId)
|
||||
{
|
||||
// Priority 1: Exact match (receiptType + profileId)
|
||||
var exactMatch = _assignments.FirstOrDefault(a => a.MatchesExact(receiptType, profileId));
|
||||
if (exactMatch != null)
|
||||
{
|
||||
return exactMatch.TemplatePath;
|
||||
}
|
||||
|
||||
// Priority 2: Type-only match (receiptType without profileId)
|
||||
var typeMatch = _assignments.FirstOrDefault(a => a.MatchesTypeOnly(receiptType));
|
||||
if (typeMatch != null)
|
||||
{
|
||||
return typeMatch.TemplatePath;
|
||||
}
|
||||
|
||||
// Priority 3: Fallback
|
||||
return _fallbackTemplate;
|
||||
}
|
||||
|
||||
public string Resolve(int receiptType, byte printerId, PrinterProfileRegistry registry)
|
||||
{
|
||||
var profile = registry.GetProfile(printerId);
|
||||
return Resolve(receiptType, profile.Id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user