59 Zeilen
1.5 KiB
C#
59 Zeilen
1.5 KiB
C#
using PrinterMonitor.Configuration;
|
|
using PrinterMonitor.Interfaces;
|
|
using PrinterMonitor.Models;
|
|
|
|
namespace PrinterMonitor.Monitors;
|
|
|
|
/// <summary>
|
|
/// IPrinterMonitor-Implementierung für CAB Squix Drucker via OPC UA.
|
|
/// Liest nur die drei überwachten Zustände.
|
|
/// </summary>
|
|
public class CabSquixMonitor : IPrinterMonitor
|
|
{
|
|
private readonly PrinterConfig _config;
|
|
private SquixOpcUaClient? _client;
|
|
|
|
public string PrinterName => _config.Name;
|
|
public bool IsConnected => _client?.IsConnected == true;
|
|
|
|
public CabSquixMonitor(PrinterConfig config)
|
|
{
|
|
_config = config;
|
|
}
|
|
|
|
public async Task ConnectAsync(CancellationToken ct = default)
|
|
{
|
|
_client = new SquixOpcUaClient(_config.Host, _config.Port);
|
|
await _client.ConnectAsync(ct);
|
|
}
|
|
|
|
public async Task DisconnectAsync()
|
|
{
|
|
if (_client != null)
|
|
{
|
|
await _client.DisposeAsync();
|
|
_client = null;
|
|
}
|
|
}
|
|
|
|
public async Task<SimplePrinterState> PollStateAsync(CancellationToken ct = default)
|
|
{
|
|
if (_client == null || !_client.IsConnected)
|
|
throw new InvalidOperationException("Nicht verbunden");
|
|
|
|
var (lts, printhead, paperLow) = await _client.ReadAllSensorsAsync(ct);
|
|
|
|
return new SimplePrinterState
|
|
{
|
|
LtsSensor = lts,
|
|
Druckerklappe = printhead,
|
|
KeineEtiketten = paperLow
|
|
};
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
await DisconnectAsync();
|
|
}
|
|
}
|