Files
HC_APTBS/ViewModels/TestSectionViewModel.cs
LucianoDev 827b811b39 feat: developer tools page, auto-test orchestrator, BIP display, tests redesign
Bundles several feature streams that have been iterating on the working tree:

- Developer Tools page (Debug-only via DEVELOPER_TOOLS symbol): hosts the
  identification card, manual KWP write + transaction log, ROM/EEPROM dump
  card with progress banner and completion message, persisted custom-commands
  library, persisted EEPROM passwords library. New service primitives:
  IKwpService.SendRawCustomAsync / ReadEepromAsync / ReadRomEepromAsync.
  Persistence mirrors the Clients XML pattern in two new files
  (custom_commands.xml, eeprom_passwords.xml).
- Auto-test orchestrator (IAutoTestOrchestrator + AutoTestState): linear
  K-Line read -> unlock -> bench-on -> test sequence with snackbar UI and
  progress dialog VM, gated on dashboard alarms.
- BIP-STATUS display: BipDisplayViewModel + BipDisplayView, RAM read at
  0x0106 via IKwpService.ReadBipStatusAsync; status definitions in
  BipStatusDefinition.
- Tests page redesign: TestSectionCard + PhaseTileView replacing the old
  TestPlanView/TestRunningView/TestDoneView/TestPreconditionsView/
  TestSectionView controls and their VMs.
- Pump command sliders: Fluent thick-track style with overhang thumb,
  click-anywhere-and-drag, mouse-wheel adjustment.
- Window startup: app.manifest declares PerMonitorV2 DPI awareness,
  MainWindow installs a WM_GETMINMAXINFO hook in OnSourceInitialized and
  maximizes there (after the hook is in place) so the app fits the work
  area exactly on any display configuration.
- Misc: PercentToPixelsConverter, seed_aliases.py one-shot pump-alias
  importer, tools/Import-BipStatus.ps1, kline_eeprom_spec.md and
  dump-functions reference docs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-07 13:59:50 +02:00

208 lines
9.4 KiB
C#

using System.Collections.ObjectModel;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HC_APTBS.Models;
using HC_APTBS.Services;
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
{
private readonly ILocalizationService _loc;
/// <summary>Initialises a new test section with a localization service.</summary>
public TestSectionViewModel(ILocalizationService loc) => _loc = loc;
// ── 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>WPF-UI SymbolIcon name to show in the card header (Fluent Tests page).</summary>
[ObservableProperty] private string _iconSymbol = "Beaker24";
/// <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;
}
}
// ── Commands ──────────────────────────────────────────────────────────────
/// <summary>Inverts <see cref="AllPhasesChecked"/>. Bound to the section card's click gesture.</summary>
[RelayCommand]
private void ToggleAllPhases() => AllPhasesChecked = !AllPhasesChecked;
// ── 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>
/// <param name="loc">Localization service for user-facing strings.</param>
public static TestSectionViewModel FromDefinition(TestDefinition test, bool showValues, ILocalizationService loc)
{
var section = new TestSectionViewModel(loc)
{
TestName = test.Name,
Description = loc.GetString(MapDescriptionKey(test.Name)),
IconSymbol = MapIconSymbol(test.Name),
ConditioningTimeSec = test.ConditioningTimeSec,
MeasurementTimeSec = test.MeasurementTimeSec,
MeasurementsPerSecond = test.MeasurementsPerSecond,
Source = test
};
foreach (var phaseDef in test.Phases)
{
var card = new PhaseCardViewModel(loc)
{
Name = phaseDef.Name,
IsCritical = phaseDef.IsCritical,
IsEnabled = phaseDef.Enabled,
ResultText = phaseDef.Enabled ? "\u2013" : loc.GetString("Common.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 localization resource key.
/// Returns the test name itself for unknown types (fail-visible).
/// </summary>
private static string MapDescriptionKey(string testName) => testName switch
{
TestType.Wl => "TestType.Warmup",
TestType.Dfi => "TestType.Adjustment",
TestType.F => "TestType.Flow",
TestType.Svme => "TestType.ServoValve",
TestType.Up => "TestType.Upstroke",
TestType.Pfp => "TestType.PreInjection",
_ => testName
};
/// <summary>
/// Maps a test type identifier to a WPF-UI SymbolIcon name used in the
/// Fluent Tests page card header.
/// </summary>
private static string MapIconSymbol(string testName) => testName switch
{
TestType.Wl => "Temperature24",
TestType.Dfi => "WrenchScrewdriver24",
TestType.F => "Drop24",
TestType.Svme => "Timeline24",
TestType.Up => "ArrowTrendingLines24",
TestType.Pfp => "Gauge24",
_ => "Beaker24"
};
}
}