Add AllyReader and AllyCalibration support with tests: Introduce new register readers, calibration methods, and comprehensive unit tests for integration and functionality validation. Update project files accordingly.
98 lines
4.2 KiB
C#
98 lines
4.2 KiB
C#
using System;
|
|
using Common;
|
|
using TBF.Rig.RegisterReaders.AllyReader;
|
|
|
|
namespace TBFTests.Rig.RegisterReaders.AllyReader.Integration
|
|
{
|
|
internal sealed class AllyHardwareIntegrationSettings
|
|
{
|
|
public bool Enabled { get; private set; }
|
|
public int CommandPortNumber { get; private set; }
|
|
public int OpticalPortNumber { get; private set; }
|
|
public int CommandBaudRate { get; private set; }
|
|
public int OpticalBaudRate { get; private set; }
|
|
public int CommandTimeoutMs { get; private set; }
|
|
public int OpticalCaptureSeconds { get; private set; }
|
|
public int ActiveModeSettleMs { get; private set; }
|
|
public AllyMeterSize MeterSize { get; private set; }
|
|
|
|
public string CommandPortName { get { return "COM" + CommandPortNumber; } }
|
|
public string OpticalPortName { get { return "COM" + OpticalPortNumber; } }
|
|
|
|
public static AllyHardwareIntegrationSettings FromEnvironment()
|
|
{
|
|
return new AllyHardwareIntegrationSettings
|
|
{
|
|
Enabled = ParseEnabled(Environment.GetEnvironmentVariable("ALLY_HW_TESTS")),
|
|
CommandPortNumber = ParsePort("ALLY_COMMAND_PORT", "COM3"),
|
|
OpticalPortNumber = ParsePort("ALLY_OPTICAL_PORT", "COM4"),
|
|
CommandBaudRate = ParsePositiveInt("ALLY_COMMAND_BAUD", 2400),
|
|
OpticalBaudRate = ParsePositiveInt("ALLY_OPTICAL_BAUD", 9600),
|
|
CommandTimeoutMs = ParsePositiveInt("ALLY_COMMAND_TIMEOUT_MS", 5000),
|
|
OpticalCaptureSeconds = ParsePositiveInt("ALLY_OPTICAL_CAPTURE_SECONDS", 10),
|
|
ActiveModeSettleMs = ParsePositiveInt("ALLY_ACTIVE_SETTLE_MS", 1000),
|
|
MeterSize = ParseMeterSize(Environment.GetEnvironmentVariable("ALLY_METER_SIZE"))
|
|
};
|
|
}
|
|
|
|
public AllyReaderCfg CreateReaderConfig()
|
|
{
|
|
return new AllyReaderCfg(new TBF.Rig.RegisterReaders.AllyReader.Factory())
|
|
{
|
|
CommandComPortNr = CommandPortNumber,
|
|
CommandBaudRate = CommandBaudRate,
|
|
OptoComPortNr = OpticalPortNumber,
|
|
OptoBaudRate = OpticalBaudRate,
|
|
ConfiguredMeterSize = MeterSize,
|
|
MeterPulsesPerLiter = 1000D,
|
|
DebugLevel = DebugMode.Normal
|
|
};
|
|
}
|
|
|
|
private static bool ParseEnabled(string value)
|
|
{
|
|
return string.Equals(value, "1", StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) ||
|
|
string.Equals(value, "yes", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private static int ParsePort(string variableName, string defaultValue)
|
|
{
|
|
string value = Environment.GetEnvironmentVariable(variableName) ?? defaultValue;
|
|
if (value.StartsWith("COM", StringComparison.OrdinalIgnoreCase))
|
|
value = value.Substring(3);
|
|
|
|
int portNumber;
|
|
if (!int.TryParse(value, out portNumber) || portNumber <= 0)
|
|
throw new InvalidOperationException(variableName + " must contain COM<n> or a positive port number.");
|
|
return portNumber;
|
|
}
|
|
|
|
private static int ParsePositiveInt(string variableName, int defaultValue)
|
|
{
|
|
string value = Environment.GetEnvironmentVariable(variableName);
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
return defaultValue;
|
|
|
|
int parsed;
|
|
if (!int.TryParse(value, out parsed) || parsed <= 0)
|
|
throw new InvalidOperationException(variableName + " must be a positive integer.");
|
|
return parsed;
|
|
}
|
|
|
|
private static AllyMeterSize ParseMeterSize(string value)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
return AllyMeterSize.FiveEighths;
|
|
|
|
AllyMeterSize result;
|
|
if (!Enum.TryParse(value, true, out result) || result == AllyMeterSize.AutoDetect)
|
|
{
|
|
throw new InvalidOperationException(
|
|
"ALLY_METER_SIZE must be a supported explicit size; AutoDetect requires UI-2031 parsing.");
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|