SSPC-Tester/SSPCTester.UI/ViewModels/ProtectViewModel.cs
2026-07-02 17:43:32 +08:00

88 lines
3.1 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 System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using SSPCTester.Logic.Models;
using SSPCTester.UI.Services;
namespace SSPCTester.UI.ViewModels;
/// <summary>保护功能页 VM框架占位。</summary>
public partial class ProtectViewModel : ObservableObject
{
private readonly UiLogSink _log;
public ObservableCollection<ProtectRow> Rows { get; } = new();
[ObservableProperty] private Project? _project;
[ObservableProperty] private bool _enableTrip = true;
[ObservableProperty] private bool _enableShort = true;
[ObservableProperty] private bool _enableOverTemp = true;
[ObservableProperty] private bool _enableOverVolt = true;
[ObservableProperty] private bool _enableUnderVolt = true;
[ObservableProperty] private bool _enableLimit = true;
[ObservableProperty] private double _overVoltThreshold = 32;
[ObservableProperty] private double _underVoltThreshold = 22;
[ObservableProperty] private double _overCurrentThreshold = 1.2;
[ObservableProperty] private double _overTempThreshold = 85;
public ProtectViewModel(UiLogSink log) { _log = log; }
public void SetProject(Project p)
{
Project = p;
SyncRowsFromProject();
}
private void SyncRowsFromProject()
{
if (Project == null) return;
var latestPower = Project.PowerResults.LastOrDefault();
for (int i = 0; i < Project.ProtectResults.Count; i++)
{
var row = Project.ProtectResults[i];
row.Index = i + 1;
row.MeasuredValue = ProtectRow.BuildMeasuredValue(row.ProtectType, latestPower?.Vin, latestPower?.Iin);
if (string.IsNullOrWhiteSpace(row.JudgeMode))
row.JudgeMode = "人工判别";
}
Rows.Clear();
foreach (var row in Project.ProtectResults) Rows.Add(row);
}
[RelayCommand]
private void RunDemo()
{
if (Project == null) return;
Project.ProtectResults.Clear();
Rows.Clear();
var latestPower = Project.PowerResults.LastOrDefault();
var items = new (string, string)[]
{
("跳闸", "1.5x额定电流"),
("短路", "输出短路"),
("过温", $">={OverTempThreshold} ℃"),
("过压", $">={OverVoltThreshold} V"),
("欠压", $"<={UnderVoltThreshold} V"),
("限流", $">={OverCurrentThreshold} A"),
};
for (int i = 0; i < items.Length; i++)
{
var r = new ProtectRow
{
Index = i + 1,
ProtectType = items[i].Item1,
Trigger = items[i].Item2,
MeasuredValue = ProtectRow.BuildMeasuredValue(items[i].Item1, latestPower?.Vin, latestPower?.Iin),
JudgeMode = "人工判别",
TripTimeMs = 2 + i * 0.3,
Status = TestStatus.Pass
};
Project.ProtectResults.Add(r);
Rows.Add(r);
}
_log.Add(UiLogLevel.Warning, "保护功能模块仅生成框架演示数据。");
}
}