73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using System.Windows;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using SSPCTester.Devices.Config;
|
|
using SSPCTester.Devices.Interfaces;
|
|
using SSPCTester.Logic.Models;
|
|
using SSPCTester.Logic.Testing;
|
|
using SSPCTester.UI.Services;
|
|
|
|
namespace SSPCTester.UI.ViewModels;
|
|
|
|
/// <summary>
|
|
/// 一次测试会话:包装 TestRunner + CancellationToken + 进度上报。
|
|
/// </summary>
|
|
public sealed class TestSessionViewModel
|
|
{
|
|
private readonly IServiceProvider _sp;
|
|
private readonly Project _project;
|
|
private readonly ISspc _sspc;
|
|
private readonly IPowerSupply _power;
|
|
private readonly ILoad _load;
|
|
private readonly IDaq _daq;
|
|
private readonly UiLogSink _logSink;
|
|
private readonly CancellationTokenSource _cts = new();
|
|
|
|
public event Action<int, CurveData, CurveData>? CurveReady;
|
|
public event Action? ProtectResultsChanged;
|
|
|
|
public TestSessionViewModel(IServiceProvider sp, Project project,
|
|
ISspc sspc, IPowerSupply power, ILoad load, IDaq daq, UiLogSink logSink)
|
|
{
|
|
_sp = sp; _project = project;
|
|
_sspc = sspc; _power = power; _load = load; _daq = daq;
|
|
_logSink = logSink;
|
|
}
|
|
|
|
public CancellationToken Token => _cts.Token;
|
|
public void Cancel() => _cts.Cancel();
|
|
|
|
public async Task RunAllAsync()
|
|
{
|
|
var ctx = new TestContext(_project, _sspc, _power, _load, _daq);
|
|
var runner = _sp.GetRequiredService<TestRunner>();
|
|
var modules = new List<ITestModule>
|
|
{
|
|
_sp.GetRequiredService<BasicTest>(),
|
|
new CurveTest(_sp.GetRequiredService<IOptions<DeviceOptions>>(), (ch, r, f) => CurveReady?.Invoke(ch, r, f)),
|
|
_sp.GetRequiredService<PowerTest>(),
|
|
_sp.GetRequiredService<ProtectTest>(),
|
|
};
|
|
|
|
IProgress<TestProgress> progress = new Progress<TestProgress>(p =>
|
|
{
|
|
if (p.ModuleName == ProtectTest.ModuleName)
|
|
ProtectResultsChanged?.Invoke();
|
|
|
|
if (!string.IsNullOrEmpty(p.Message))
|
|
{
|
|
var lvl = p.StepStatus switch
|
|
{
|
|
TestStatus.Fail => UiLogLevel.Error,
|
|
TestStatus.Pass => UiLogLevel.Success,
|
|
TestStatus.Measured => UiLogLevel.Success,
|
|
_ => UiLogLevel.Info
|
|
};
|
|
_logSink.Add(lvl, $"[{p.ModuleName}] {p.Message}");
|
|
}
|
|
});
|
|
|
|
await runner.RunAllAsync(ctx, modules, progress, _cts.Token);
|
|
}
|
|
}
|