2026-06-30 12:10:29 +08:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2026-07-02 21:38:25 +08:00
|
|
|
using SSPCTester.Devices.Interfaces;
|
2026-06-30 12:10:29 +08:00
|
|
|
using SSPCTester.Logic.Models;
|
|
|
|
|
using SSPCTester.UI.Services;
|
|
|
|
|
|
|
|
|
|
namespace SSPCTester.UI.ViewModels;
|
|
|
|
|
|
2026-07-02 21:38:25 +08:00
|
|
|
/// <summary>保护功能页 VM。</summary>
|
2026-06-30 12:10:29 +08:00
|
|
|
public partial class ProtectViewModel : ObservableObject
|
|
|
|
|
{
|
2026-07-02 21:38:25 +08:00
|
|
|
private readonly IPowerSupply _power;
|
2026-06-30 12:10:29 +08:00
|
|
|
private readonly UiLogSink _log;
|
2026-07-02 21:38:25 +08:00
|
|
|
private TestSessionViewModel? _session;
|
2026-06-30 12:10:29 +08:00
|
|
|
public ObservableCollection<ProtectRow> Rows { get; } = new();
|
|
|
|
|
|
|
|
|
|
[ObservableProperty] private Project? _project;
|
|
|
|
|
|
2026-07-02 21:38:25 +08:00
|
|
|
public ProtectViewModel(IPowerSupply power, UiLogSink log)
|
|
|
|
|
{
|
|
|
|
|
_power = power;
|
|
|
|
|
_log = log;
|
|
|
|
|
}
|
2026-06-30 12:10:29 +08:00
|
|
|
|
|
|
|
|
public void SetProject(Project p)
|
|
|
|
|
{
|
|
|
|
|
Project = p;
|
2026-07-02 21:38:25 +08:00
|
|
|
ProtectTestPlan.EnsureRows(p);
|
2026-07-02 17:43:32 +08:00
|
|
|
SyncRowsFromProject();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-02 21:38:25 +08:00
|
|
|
public void AttachSession(TestSessionViewModel session)
|
|
|
|
|
{
|
|
|
|
|
if (_session != null)
|
|
|
|
|
_session.ProtectResultsChanged -= SyncRowsFromProject;
|
|
|
|
|
|
|
|
|
|
_session = session;
|
|
|
|
|
_session.ProtectResultsChanged += SyncRowsFromProject;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-02 17:43:32 +08:00
|
|
|
private void SyncRowsFromProject()
|
|
|
|
|
{
|
|
|
|
|
if (Project == null) return;
|
|
|
|
|
|
2026-07-02 21:38:25 +08:00
|
|
|
ProtectTestPlan.RefreshMeasuredValues(Project, overwriteExisting: false);
|
2026-06-30 12:10:29 +08:00
|
|
|
Rows.Clear();
|
2026-07-02 17:43:32 +08:00
|
|
|
foreach (var row in Project.ProtectResults) Rows.Add(row);
|
2026-06-30 12:10:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2026-07-02 21:38:25 +08:00
|
|
|
private async Task MeasurePowerAsync(ProtectRow? row)
|
2026-06-30 12:10:29 +08:00
|
|
|
{
|
2026-07-02 21:38:25 +08:00
|
|
|
if (row == null || !row.CanMeasureFromPower) return;
|
|
|
|
|
|
|
|
|
|
try
|
2026-06-30 12:10:29 +08:00
|
|
|
{
|
2026-07-02 21:38:25 +08:00
|
|
|
var (volts, amps) = await _power.ReadAsync();
|
|
|
|
|
row.MeasuredValue = row.ProtectType switch
|
2026-07-02 17:43:32 +08:00
|
|
|
{
|
2026-07-02 21:38:25 +08:00
|
|
|
"过压" or "欠压" => $"{volts:F3} V",
|
|
|
|
|
_ => $"{amps:F3} A"
|
2026-07-02 17:43:32 +08:00
|
|
|
};
|
2026-07-03 14:16:15 +08:00
|
|
|
if (row.Status == TestStatus.Running)
|
|
|
|
|
row.Status = TestStatus.Untested;
|
2026-07-02 21:38:25 +08:00
|
|
|
_log.Add(UiLogLevel.Success, $"保护功能 {row.ProtectType} 实测值:{row.MeasuredValue}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Add(UiLogLevel.Error, $"读取电源失败:{ex.Message}");
|
2026-06-30 12:10:29 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|