55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
using System.Diagnostics;
|
|
using SSPCTester.Devices.Interfaces;
|
|
|
|
namespace SSPCTester.Logic.Testing;
|
|
|
|
/// <summary>基础通断测试的统一判定规则。</summary>
|
|
public static class BasicTestCriteria
|
|
{
|
|
public const double OnVoltageThreshold = 1.0;
|
|
public const double OffVoltageThreshold = 0.5;
|
|
public const int SwitchSettleDelayMs = 1_000;
|
|
public const int OffSettleTimeoutMs = 2_000;
|
|
public const int OffInitialDelayMs = 100;
|
|
public const int OffSampleIntervalMs = 100;
|
|
public const int RequiredStableOffSamples = 2;
|
|
|
|
public static bool IsOn(double voltage) => voltage > OnVoltageThreshold;
|
|
|
|
public static bool IsOff(double voltage) => Math.Abs(voltage) < OffVoltageThreshold;
|
|
|
|
/// <summary>
|
|
/// 等待关断电压稳定。连续多次低于阈值才确认关断,避免旧帧和瞬时噪声造成误判。
|
|
/// </summary>
|
|
public static async Task<OffStateResult> WaitForStableOffAsync(
|
|
ISspc sspc,
|
|
int channel,
|
|
CancellationToken ct = default)
|
|
{
|
|
await Task.Delay(OffInitialDelayMs, ct).ConfigureAwait(false);
|
|
|
|
var stopwatch = Stopwatch.StartNew();
|
|
int stableSamples = 0;
|
|
ChannelMeasurement? latest = null;
|
|
|
|
while (stopwatch.ElapsedMilliseconds < OffSettleTimeoutMs)
|
|
{
|
|
latest = await sspc.ReadMeasurementAsync(channel, ct).ConfigureAwait(false);
|
|
stableSamples = IsOff(latest.Voltage) ? stableSamples + 1 : 0;
|
|
|
|
if (stableSamples >= RequiredStableOffSamples)
|
|
return new OffStateResult(true, latest, stopwatch.Elapsed);
|
|
|
|
await Task.Delay(OffSampleIntervalMs, ct).ConfigureAwait(false);
|
|
}
|
|
|
|
latest ??= await sspc.ReadMeasurementAsync(channel, ct).ConfigureAwait(false);
|
|
return new OffStateResult(false, latest, stopwatch.Elapsed);
|
|
}
|
|
}
|
|
|
|
public sealed record OffStateResult(
|
|
bool IsStableOff,
|
|
ChannelMeasurement Measurement,
|
|
TimeSpan Elapsed);
|