print server with new concept
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Reflection;
|
||||
using Inspectron.Epson.PrintServer.Printers;
|
||||
|
||||
namespace Inspectron.Epson.PrintServer.PrintServices;
|
||||
|
||||
@@ -7,12 +8,14 @@ public class EpsonPrintService: IPrintService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IPrinterFactory _printerFactory;
|
||||
private readonly IWrapperPrinterFactory _wrapperPrinterFactory;
|
||||
private readonly IPrinterConfigurationSource _printerConfigurationSource;
|
||||
|
||||
public EpsonPrintService(ILogger logger, IPrinterFactory printerFactory, IPrinterConfigurationSource printerConfigurationSource)
|
||||
public EpsonPrintService(ILogger logger, IPrinterFactory printerFactory, IWrapperPrinterFactory wrapperPrinterFactory, IPrinterConfigurationSource printerConfigurationSource)
|
||||
{
|
||||
_logger = logger;
|
||||
_printerFactory = printerFactory;
|
||||
_wrapperPrinterFactory = wrapperPrinterFactory;
|
||||
_printerConfigurationSource = printerConfigurationSource;
|
||||
}
|
||||
|
||||
@@ -21,7 +24,7 @@ public class EpsonPrintService: IPrintService
|
||||
try
|
||||
{
|
||||
var configuration = _printerConfigurationSource.GetConfiguration(printerIp);
|
||||
await using var epsonPrinter = new EpsonPrinter(_logger);
|
||||
await using var epsonPrinter = _printerFactory.CreatePrinter();
|
||||
string address;
|
||||
int port = 9100;
|
||||
var printerAddress = configuration.Address;
|
||||
@@ -45,19 +48,46 @@ public class EpsonPrintService: IPrintService
|
||||
{
|
||||
throw new InvalidOperationException("Printer is offline or out of paper.");
|
||||
}
|
||||
|
||||
var printerAdapter = _printerFactory.CreatePrinterFromId(printerId.Value, epsonPrinter);
|
||||
var printerAdapter = _wrapperPrinterFactory.CreatePrinterFromId(printerId.Value, epsonPrinter);
|
||||
|
||||
await printerAdapter.InitAsync();
|
||||
|
||||
if (configuration.LogoFilename != null)
|
||||
if (!string.IsNullOrEmpty(job.Document.LogoUrl))
|
||||
{
|
||||
await printerAdapter.PrintImageAsync(configuration.LogoFilename);
|
||||
// Download logo image
|
||||
var logoFileName = Path.GetFileName(new Uri(job.Document.LogoUrl).LocalPath);
|
||||
var logoCachePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "logos");
|
||||
|
||||
// Ensure logos cache directory exists
|
||||
if (!Directory.Exists(logoCachePath))
|
||||
{
|
||||
Directory.CreateDirectory(logoCachePath);
|
||||
}
|
||||
|
||||
var cachedLogoPath = Path.Combine(logoCachePath, logoFileName);
|
||||
|
||||
// Download only if not already cached
|
||||
if (!File.Exists(cachedLogoPath))
|
||||
{
|
||||
using var httpClient = new HttpClient();
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(10);
|
||||
|
||||
var logoData = await httpClient.GetByteArrayAsync(job.Document.LogoUrl);
|
||||
await File.WriteAllBytesAsync(cachedLogoPath, logoData);
|
||||
|
||||
_logger.LogInformation("Downloaded logo {LogoFileName} from {LogoUrl}", logoFileName, job.Document.LogoUrl);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("Using cached logo {LogoFileName}", logoFileName);
|
||||
}
|
||||
|
||||
await printerAdapter.PrintImageAsync(cachedLogoPath);
|
||||
}
|
||||
|
||||
await printerAdapter.SetFontSizeAsync(configuration.FontSize);
|
||||
|
||||
await printerAdapter.PrintTextAsync(job.Document);
|
||||
await printerAdapter.PrintTextAsync(job.Document.Content);
|
||||
await Task.Delay(200);
|
||||
status = await epsonPrinter.GetPrinterStatusAsync();
|
||||
if (!status.IsOnline)
|
||||
|
||||
Reference in New Issue
Block a user