115 lines
4.6 KiB
C#
115 lines
4.6 KiB
C#
using System;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
namespace HC_APTBS.ViewModels
|
|
{
|
|
/// <summary>
|
|
/// Represents the vertical graphic result indicator for a single receive parameter
|
|
/// within a phase card. Displays expected value, tolerance bounds, and live/final
|
|
/// measurement result as a vertical progress bar.
|
|
/// </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 in real-time during the measurement phase.
|
|
/// Triggers recalculation of <see cref="ProgressPercent"/> and <see cref="IsWithinTolerance"/>.
|
|
/// </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>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") : "---";
|
|
|
|
// ── 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));
|
|
if (HasValue) RecalculateProgress(CurrentValue);
|
|
}
|
|
|
|
/// <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;
|
|
OnPropertyChanged(nameof(DisplayValue));
|
|
}
|
|
}
|
|
}
|