SSPC-Tester/SSPCTester.Logic/Testing/BasicTest.cs
2026-07-03 14:16:15 +08:00

63 lines
2.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SSPCTester.Logic.Models;
namespace SSPCTester.Logic.Testing;
/// <summary>基础测试模块24 路依次开 → 读值 → 关 → 校验归零。</summary>
public sealed class BasicTest : ITestModule
{
public string Name => "基础测试";
public async Task RunAsync(TestContext ctx, IProgress<TestProgress> progress, CancellationToken ct)
{
int n = ctx.Sspc.ChannelCount;
if (!ctx.Sspc.IsConnected)
await ctx.Sspc.ConnectAsync(ct).ConfigureAwait(false);
ctx.Project.BasicResults.Clear();
for (int ch = 1; ch <= n; ch++)
{
ct.ThrowIfCancellationRequested();
var row = new ChannelResult { Channel = ch, Status = TestStatus.Running };
ctx.Project.BasicResults.Add(row);
progress.Report(new TestProgress { ModuleName = Name, Step = ch - 1, TotalSteps = n, Message = $"测试通道 CH{ch}" });
try
{
await ctx.Sspc.TurnOnAsync(ch, ct).ConfigureAwait(false);
await Task.Delay(BasicTestCriteria.SwitchSettleDelayMs, ct).ConfigureAwait(false);
var on = await ctx.Sspc.ReadMeasurementAsync(ch, ct).ConfigureAwait(false);
row.VoltageOn = on.Voltage;
row.CurrentOn = Math.Abs(on.Current);
row.DisplayVoltage = row.VoltageOn;
row.DisplayCurrent = row.CurrentOn;
bool onOk = BasicTestCriteria.IsOnState(row.VoltageOn, row.CurrentOn);
await ctx.Sspc.TurnOffAsync(ch, ct).ConfigureAwait(false);
await Task.Delay(BasicTestCriteria.SwitchSettleDelayMs, ct).ConfigureAwait(false);
var offResult = await BasicTestCriteria
.WaitForStableOffAsync(ctx.Sspc, ch, ct)
.ConfigureAwait(false);
row.VoltageOff = offResult.Measurement.Voltage;
row.CurrentOff = Math.Abs(offResult.Measurement.Current);
row.DisplayVoltage = row.VoltageOff;
row.DisplayCurrent = row.CurrentOff;
row.Status = onOk && offResult.IsStableOff
? TestStatus.Pass : TestStatus.Fail;
}
catch (Exception ex)
{
row.Status = TestStatus.Fail;
try { await ctx.Sspc.TurnOffAsync(ch, CancellationToken.None).ConfigureAwait(false); } catch { }
progress.Report(new TestProgress
{
ModuleName = Name,
Step = ch,
TotalSteps = n,
StepStatus = row.Status,
Message = $"CH{ch} 通讯失败:{ex.Message}"
});
continue;
}
progress.Report(new TestProgress { ModuleName = Name, Step = ch, TotalSteps = n, StepStatus = row.Status, Message = $"CH{ch} {row.Status}" });
}
}
}