discovery of printer type
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Inspectron.Epson;
|
||||
|
||||
@@ -8,6 +9,12 @@ public class DiscoveryService : IDiscoveryService
|
||||
private const int DiscoveryPort = 3289;
|
||||
private const string DiscoveryMessage = "EPSONQ";
|
||||
private const int MessageLength = 14;
|
||||
private readonly ILogger? _logger;
|
||||
|
||||
public DiscoveryService(ILogger? logger = null)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Discovers Epson printers on the local network using UDP broadcast
|
||||
@@ -52,12 +59,16 @@ public class DiscoveryService : IDiscoveryService
|
||||
// Check if we've already discovered this printer
|
||||
if (!discoveredPrinters.Any(p => p.IPAddress.Equals(result.RemoteEndPoint.Address)))
|
||||
{
|
||||
var ipAddress = result.RemoteEndPoint.Address.ToString();
|
||||
var printer = new DiscoveredPrinter
|
||||
{
|
||||
IPAddress = result.RemoteEndPoint.Address.ToString(),
|
||||
IPAddress = ipAddress,
|
||||
DiscoveredAt = DateTime.UtcNow
|
||||
};
|
||||
|
||||
// Try to get printer model by connecting and querying
|
||||
printer.ModelName = await GetPrinterModelAsync(ipAddress);
|
||||
|
||||
discoveredPrinters.Add(printer);
|
||||
onPrinterDiscovered?.Invoke(printer);
|
||||
}
|
||||
@@ -91,6 +102,51 @@ public class DiscoveryService : IDiscoveryService
|
||||
msg[10] = 0x10;
|
||||
return msg;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Connects to a printer and retrieves its model name
|
||||
/// </summary>
|
||||
private async Task<string?> GetPrinterModelAsync(string ipAddress, int port = 9100)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger?.LogDebug("Connecting to printer at {IP} to retrieve model", ipAddress);
|
||||
|
||||
await using var epsonPrinter = new EpsonPrinter(_logger);
|
||||
await epsonPrinter.ConnectAsync(ipAddress, port, timeoutSeconds: 3);
|
||||
|
||||
var printerId = await epsonPrinter.GetPrinterIdAsync();
|
||||
if (printerId.HasValue)
|
||||
{
|
||||
var modelName = GetModelNameFromId(printerId.Value);
|
||||
_logger?.LogDebug("Printer at {IP} identified as {Model} (ID: 0x{Id:X2})",
|
||||
ipAddress, modelName, printerId.Value);
|
||||
return modelName;
|
||||
}
|
||||
|
||||
_logger?.LogDebug("Could not retrieve printer ID from {IP}", ipAddress);
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger?.LogWarning(ex, "Failed to get printer model from {IP}", ipAddress);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a printer ID byte to a model name string
|
||||
/// </summary>
|
||||
private static string GetModelNameFromId(byte printerId)
|
||||
{
|
||||
return printerId switch
|
||||
{
|
||||
0x01 => "TM-T30III",
|
||||
0x0D => "TM-U220II",
|
||||
0x13 => "TM-U220II",
|
||||
_ => $"Unknown (0x{printerId:X2})"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user