58 lines
2.4 KiB
C#
58 lines
2.4 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace HC_APTBS.Services.Impl
|
|
{
|
|
/// <summary>
|
|
/// Delegates DFI calibration operations to <see cref="IKwpService"/>,
|
|
/// resolving the K-Line port from <see cref="IConfigurationService"/>.
|
|
/// </summary>
|
|
public sealed class CalibrationService : ICalibrationService
|
|
{
|
|
private readonly IKwpService _kwp;
|
|
private readonly IConfigurationService _config;
|
|
|
|
/// <param name="kwpService">K-Line protocol service.</param>
|
|
/// <param name="configService">Provides the K-Line port setting.</param>
|
|
public CalibrationService(IKwpService kwpService, IConfigurationService configService)
|
|
{
|
|
_kwp = kwpService;
|
|
_config = configService;
|
|
}
|
|
|
|
// ── ICalibrationService ────────────────────────────────────────────────────
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<double> ReadDfiAsync(CancellationToken ct = default)
|
|
{
|
|
string raw = await _kwp.ReadDfiAsync(Port, ct);
|
|
return double.TryParse(raw,
|
|
System.Globalization.NumberStyles.Float,
|
|
System.Globalization.CultureInfo.InvariantCulture, out double v) ? v : 0;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<double> WriteDfiAsync(double dfiDegrees, CancellationToken ct = default)
|
|
{
|
|
int version = _config.Settings.PidLoopMs; // pump version stored via config TODO: expose
|
|
string raw = await _kwp.WriteDfiAsync(Port, (float)dfiDegrees, version, ct);
|
|
return double.TryParse(raw,
|
|
System.Globalization.NumberStyles.Float,
|
|
System.Globalization.CultureInfo.InvariantCulture, out double v) ? v : 0;
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<double> WriteDfiAndRestartAsync(double dfiDegrees, CancellationToken ct = default)
|
|
{
|
|
int version = _config.Settings.PidLoopMs; // pump version
|
|
string raw = await _kwp.WriteDfiAndRestartAsync(Port, (float)dfiDegrees, version, ct);
|
|
return double.TryParse(raw,
|
|
System.Globalization.NumberStyles.Float,
|
|
System.Globalization.CultureInfo.InvariantCulture, out double v) ? v : 0;
|
|
}
|
|
|
|
private string Port => _kwp.DetectKLinePort() ?? string.Empty;
|
|
}
|
|
}
|