Files
HC_APTBS/ViewModels/TestSectionViewModel.cs
2026-04-11 12:45:18 +02:00

174 lines
7.6 KiB
C#

using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using HC_APTBS.Models;
namespace HC_APTBS.ViewModels
{
/// <summary>
/// Represents one test type section (e.g. "F", "DFI", "SVME") containing
/// a header with metadata and a horizontal list of <see cref="PhaseCardViewModel"/> cards.
/// </summary>
public sealed partial class TestSectionViewModel : ObservableObject
{
// ── Suppress cascade guard ────────────────────────────────────────────────
private bool _suppressCascade;
// ── Identity / metadata ───────────────────────────────────────────────────
/// <summary>Test type identifier (e.g. "F", "DFI", "SVME").</summary>
[ObservableProperty] private string _testName = string.Empty;
/// <summary>Human-readable description of the test type.</summary>
[ObservableProperty] private string _description = string.Empty;
/// <summary>Conditioning time in seconds.</summary>
[ObservableProperty] private int _conditioningTimeSec;
/// <summary>Measurement time in seconds.</summary>
[ObservableProperty] private int _measurementTimeSec;
/// <summary>Measurements per second during the measurement window.</summary>
[ObservableProperty] private double _measurementsPerSecond;
// ── UI state ──────────────────────────────────────────────────────────────
/// <summary>Whether this section's expander is open.</summary>
[ObservableProperty] private bool _isExpanded = true;
/// <summary>True when any phase in this test section is currently executing.</summary>
[ObservableProperty] private bool _isActiveTest;
/// <summary>
/// Bidirectional check state for all phases. Setting this cascades down to
/// all child <see cref="PhaseCardViewModel.IsEnabled"/> properties; child changes
/// cascade back up to recalculate this value.
/// </summary>
[ObservableProperty] private bool _allPhasesChecked = true;
// ── Phases ────────────────────────────────────────────────────────────────
/// <summary>Phase cards shown in the horizontal scroll area.</summary>
public ObservableCollection<PhaseCardViewModel> Phases { get; } = new();
// ── Back-reference ────────────────────────────────────────────────────────
/// <summary>Back-reference to the source model.</summary>
internal TestDefinition? Source { get; set; }
// ── Cascade: parent → children ────────────────────────────────────────────
partial void OnAllPhasesCheckedChanged(bool value)
{
if (_suppressCascade) return;
_suppressCascade = true;
try
{
foreach (var phase in Phases)
phase.IsEnabled = value;
}
finally
{
_suppressCascade = false;
}
}
// ── Cascade: child → parent ──────────────────────────────────────────────
/// <summary>
/// Called by a child <see cref="PhaseCardViewModel"/> when its
/// <see cref="PhaseCardViewModel.IsEnabled"/> changes.
/// Recalculates <see cref="AllPhasesChecked"/> without re-cascading.
/// </summary>
internal void OnChildEnabledChanged(PhaseCardViewModel _)
{
if (_suppressCascade) return;
_suppressCascade = true;
try
{
AllPhasesChecked = Phases.All(p => p.IsEnabled);
}
finally
{
_suppressCascade = false;
}
}
// ── Factory ───────────────────────────────────────────────────────────────
/// <summary>
/// Creates a <see cref="TestSectionViewModel"/> from a <see cref="TestDefinition"/> model,
/// populating all child <see cref="PhaseCardViewModel"/> instances.
/// </summary>
/// <param name="test">Source test definition.</param>
/// <param name="showValues">Initial show-operation-values state.</param>
public static TestSectionViewModel FromDefinition(TestDefinition test, bool showValues)
{
var section = new TestSectionViewModel
{
TestName = test.Name,
Description = MapDescription(test.Name),
ConditioningTimeSec = test.ConditioningTimeSec,
MeasurementTimeSec = test.MeasurementTimeSec,
MeasurementsPerSecond = test.MeasurementsPerSecond,
Source = test
};
foreach (var phaseDef in test.Phases)
{
var card = new PhaseCardViewModel
{
Name = phaseDef.Name,
IsCritical = phaseDef.IsCritical,
IsEnabled = phaseDef.Enabled,
ResultText = phaseDef.Enabled ? "\u2013" : "disabled",
ShowOperationValues = showValues,
Source = phaseDef,
EnabledChanged = section.OnChildEnabledChanged
};
// Populate operation values from Sends.
foreach (var tp in phaseDef.Sends)
card.OperationValues.Add(new OperationValueViewModel { Name = tp.Name, Value = tp.Value });
// Populate readiness conditions from Readies.
foreach (var tp in phaseDef.Readies)
card.ReadyValues.Add(new OperationValueViewModel { Name = tp.Name, Value = tp.Value });
// Populate graphic result indicators from Receives.
foreach (var tp in phaseDef.Receives)
{
card.ResultIndicators.Add(new GraphicIndicatorViewModel
{
ParameterName = tp.Name,
ExpectedValue = tp.Value,
Tolerance = tp.Tolerance
});
}
section.Phases.Add(card);
}
section.AllPhasesChecked = section.Phases.All(p => p.IsEnabled);
return section;
}
/// <summary>
/// Maps a test type identifier to a human-readable description.
/// </summary>
private static string MapDescription(string testName) => testName switch
{
TestType.Wl => "Warm-up",
TestType.Dfi => "Adjustment",
TestType.F => "Flow",
TestType.Svme => "Servo valve",
TestType.Up => "Upstroke",
TestType.Pfp => "Pre-injection",
_ => testName
};
}
}