Add project files.
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Inspectron.Epson.PrintServer.PrintServices;
|
||||
|
||||
public class EpsonPrintService: IPrintService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly IPrinterFactory _printerFactory;
|
||||
private readonly IPrinterConfigurationSource _printerConfigurationSource;
|
||||
|
||||
public EpsonPrintService(ILogger logger, IPrinterFactory printerFactory, IPrinterConfigurationSource printerConfigurationSource)
|
||||
{
|
||||
_logger = logger;
|
||||
_printerFactory = printerFactory;
|
||||
_printerConfigurationSource = printerConfigurationSource;
|
||||
}
|
||||
|
||||
public async Task<bool> PrintAsync(string printerIp, PrintJob job)
|
||||
{
|
||||
try
|
||||
{
|
||||
var configuration = _printerConfigurationSource.GetConfiguration(printerIp);
|
||||
await using var epsonPrinter = new EpsonPrinter(_logger);
|
||||
string address;
|
||||
int port = 9100;
|
||||
var printerAddress = configuration.Address;
|
||||
if (printerAddress.Contains(":"))
|
||||
{
|
||||
var parts = printerAddress.Split(':');
|
||||
address = parts[0];
|
||||
port = int.Parse(parts[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
address = printerAddress;
|
||||
}
|
||||
|
||||
await epsonPrinter.ConnectAsync(address, port);
|
||||
|
||||
var printerId = await epsonPrinter.GetPrinterIdAsync();
|
||||
|
||||
var status = await epsonPrinter.GetPrinterStatusAsync();
|
||||
if (!status.IsOnline)
|
||||
{
|
||||
throw new InvalidOperationException("Printer is offline or out of paper.");
|
||||
}
|
||||
|
||||
var printerAdapter = _printerFactory.CreatePrinterFromId(printerId.Value, epsonPrinter);
|
||||
|
||||
await printerAdapter.InitAsync();
|
||||
|
||||
if (configuration.LogoFilename != null)
|
||||
{
|
||||
await printerAdapter.PrintImageAsync(configuration.LogoFilename);
|
||||
}
|
||||
|
||||
await printerAdapter.SetFontSizeAsync(configuration.FontSize);
|
||||
|
||||
await printerAdapter.PrintTextAsync(job.Document);
|
||||
await Task.Delay(200);
|
||||
status = await epsonPrinter.GetPrinterStatusAsync();
|
||||
if (!status.IsOnline)
|
||||
{
|
||||
throw new InvalidOperationException("Printer is offline or out of paper.");
|
||||
}
|
||||
|
||||
await printerAdapter.Cut();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogWarning( e, "Failed to print job for printer {PrinterUid}", printerIp);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user