Files
HC_APTBS/ViewModels/GraphicIndicatorViewModel.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

147 lines
6.1 KiB
C#

using System;
using CommunityToolkit.Mvvm.ComponentModel;
namespace HC_APTBS.ViewModels
{
/// <summary>
/// Vertical min/max/target gauge for a single receive parameter on a phase card.
/// Ticks continuously while the phase is active (conditioning + measurement) and
/// locks to the final pass/fail colour via <see cref="IsPhaseCompleted"/> once the
/// phase ends.
/// </summary>
public sealed partial class GraphicIndicatorViewModel : ObservableObject
{
/// <summary>CAN parameter name (e.g. "QDelivery", "QOver").</summary>
[ObservableProperty] private string _parameterName = string.Empty;
/// <summary>Target/expected measurement value.</summary>
[ObservableProperty] private double _expectedValue;
/// <summary>Acceptable deviation from the expected value.</summary>
[ObservableProperty] private double _tolerance;
/// <summary>
/// Current live measurement value. Updated every refresh tick by
/// <see cref="TestPanelViewModel.UpdateLiveIndicator"/> so the bar moves
/// through conditioning as well as measurement.
/// </summary>
[ObservableProperty] private double _currentValue;
/// <summary>
/// Vertical progress bar fill percentage (0-100), computed from <see cref="CurrentValue"/>
/// relative to the display range that includes tolerance margins.
/// </summary>
[ObservableProperty] private double _progressPercent;
/// <summary>True when <see cref="CurrentValue"/> falls within the tolerance window.</summary>
[ObservableProperty] private bool _isWithinTolerance = true;
/// <summary>True once a measurement has been recorded for this indicator.</summary>
[ObservableProperty] private bool _hasValue;
/// <summary>True after the owning phase completes; freezes the fill colour.</summary>
[ObservableProperty] private bool _isPhaseCompleted;
/// <summary>Pass/fail outcome of the owning phase. Only meaningful when <see cref="IsPhaseCompleted"/> is true.</summary>
[ObservableProperty] private bool _phasePassed;
/// <summary>Lower tolerance bound: <see cref="ExpectedValue"/> - <see cref="Tolerance"/>.</summary>
public double MinBound => ExpectedValue - Tolerance;
/// <summary>Upper tolerance bound: <see cref="ExpectedValue"/> + <see cref="Tolerance"/>.</summary>
public double MaxBound => ExpectedValue + Tolerance;
/// <summary>Formatted display string for the current value.</summary>
public string DisplayValue => HasValue ? CurrentValue.ToString("F1") : "---";
/// <summary>Top of the in-tolerance band, as a percent of the bar height (0 = top, 100 = bottom).</summary>
public double ToleranceBandTopPercent => Tolerance > 0 ? 20.0 : 50.0;
/// <summary>Height of the in-tolerance band, as a percent of the bar height.</summary>
public double ToleranceBandHeightPercent => Tolerance > 0 ? 60.0 : 0.0;
/// <summary>Position of the target line, as a percent of the bar height (symmetric around expected).</summary>
public double ExpectedMarkerPercent => 50.0;
// ── Recalculation on value change ─────────────────────────────────────────
partial void OnCurrentValueChanged(double value)
{
HasValue = true;
RecalculateProgress(value);
IsWithinTolerance = Math.Abs(value - ExpectedValue) <= Tolerance;
OnPropertyChanged(nameof(DisplayValue));
}
partial void OnExpectedValueChanged(double value)
{
OnPropertyChanged(nameof(MinBound));
OnPropertyChanged(nameof(MaxBound));
if (HasValue) RecalculateProgress(CurrentValue);
}
partial void OnToleranceChanged(double value)
{
OnPropertyChanged(nameof(MinBound));
OnPropertyChanged(nameof(MaxBound));
OnPropertyChanged(nameof(ToleranceBandTopPercent));
OnPropertyChanged(nameof(ToleranceBandHeightPercent));
if (HasValue) RecalculateProgress(CurrentValue);
}
/// <summary>
/// Applies a runtime tolerance update (e.g. after DFI auto-adjust) without
/// touching the live <see cref="CurrentValue"/>. Raises change notifications
/// for all dependent computed properties.
/// </summary>
public void ApplyTolerance(double expected, double tolerance)
{
ExpectedValue = expected;
Tolerance = tolerance;
}
/// <summary>
/// Computes the progress bar fill percentage using the same algorithm as the
/// original GraphicResultDisplay. The display range extends 20% beyond the
/// tolerance bounds on each side so that out-of-tolerance readings are still visible.
/// </summary>
private void RecalculateProgress(double value)
{
double range = 2.0 * Tolerance;
if (range <= 0)
{
ProgressPercent = 50;
return;
}
// The tolerance band occupies the middle 60% of the bar.
// Add 20% margin above and below.
double margin = ((100.0 * range / 60.0) - range) / 2.0;
double bottom = ExpectedValue - Tolerance - margin;
double top = ExpectedValue + Tolerance + margin;
double span = top - bottom;
if (span <= 0)
{
ProgressPercent = 50;
return;
}
double pct = (value - bottom) / span * 100.0;
ProgressPercent = Math.Clamp(pct, 5.0, 100.0);
}
/// <summary>Resets the indicator to its initial state for a new test run.</summary>
public void Reset()
{
CurrentValue = 0;
ProgressPercent = 0;
IsWithinTolerance = true;
HasValue = false;
IsPhaseCompleted = false;
PhasePassed = false;
OnPropertyChanged(nameof(DisplayValue));
}
}
}