213 lines
8.7 KiB
C#
213 lines
8.7 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using HC_APTBS.Models;
|
|
|
|
namespace HC_APTBS.ViewModels
|
|
{
|
|
/// <summary>
|
|
/// Root ViewModel for the test panel that displays all tests for the selected pump.
|
|
///
|
|
/// <para>
|
|
/// Replaces the former <c>TestDisplayViewModel</c>. Holds one
|
|
/// <see cref="TestSectionViewModel"/> per <see cref="TestDefinition"/> and provides
|
|
/// the public API that <see cref="MainViewModel"/> calls in response to
|
|
/// <see cref="Services.IBenchService"/> events.
|
|
/// </para>
|
|
/// </summary>
|
|
public sealed partial class TestPanelViewModel : ObservableObject
|
|
{
|
|
// ── Cached active phase for fast live-indicator lookup ─────────────────────
|
|
|
|
private PhaseCardViewModel? _activePhaseCard;
|
|
|
|
// ── Global toggles ────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Controls visibility of operation values (RPM, ME, FBKW) on all phase cards.
|
|
/// Cascades to every <see cref="PhaseCardViewModel.ShowOperationValues"/>.
|
|
/// </summary>
|
|
[ObservableProperty] private bool _showOperationValues;
|
|
|
|
// ── Status ────────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>Current verbose status message from the bench service.</summary>
|
|
[ObservableProperty] private string _statusText = string.Empty;
|
|
|
|
/// <summary>True while a test sequence is in progress.</summary>
|
|
[ObservableProperty] private bool _isRunning;
|
|
|
|
/// <summary>Estimated remaining time for the entire test sequence (seconds).</summary>
|
|
[ObservableProperty] private int _remainingSeconds;
|
|
|
|
// ── Test sections ─────────────────────────────────────────────────────────
|
|
|
|
/// <summary>All test sections for the currently loaded pump.</summary>
|
|
public ObservableCollection<TestSectionViewModel> Tests { get; } = new();
|
|
|
|
// ── Show values cascade ───────────────────────────────────────────────────
|
|
|
|
partial void OnShowOperationValuesChanged(bool value)
|
|
{
|
|
foreach (var section in Tests)
|
|
foreach (var phase in section.Phases)
|
|
phase.ShowOperationValues = value;
|
|
}
|
|
|
|
// ── Commands ──────────────────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Toggles enable/disable for every phase across all test sections.
|
|
/// If any phase is currently disabled, enables all; otherwise disables all.
|
|
/// </summary>
|
|
[RelayCommand]
|
|
private void ToggleCheckAll()
|
|
{
|
|
bool anyDisabled = Tests.Any(s => s.Phases.Any(p => !p.IsEnabled));
|
|
bool newState = anyDisabled;
|
|
|
|
foreach (var section in Tests)
|
|
{
|
|
// Bypass per-section cascade guard by setting AllPhasesChecked directly,
|
|
// which will cascade down to children.
|
|
section.AllPhasesChecked = newState;
|
|
}
|
|
}
|
|
|
|
// ── Public API: loading ───────────────────────────────────────────────────
|
|
|
|
/// <summary>
|
|
/// Populates the test panel with all tests from the given pump definition.
|
|
/// Call when the selected pump changes.
|
|
/// </summary>
|
|
/// <param name="pump">The pump whose tests to display.</param>
|
|
public void LoadAllTests(PumpDefinition pump)
|
|
{
|
|
Tests.Clear();
|
|
_activePhaseCard = null;
|
|
StatusText = string.Empty;
|
|
RemainingSeconds = 0;
|
|
|
|
foreach (var testDef in pump.Tests)
|
|
{
|
|
var section = TestSectionViewModel.FromDefinition(testDef, ShowOperationValues);
|
|
Tests.Add(section);
|
|
}
|
|
|
|
// Compute initial remaining seconds estimate.
|
|
RemainingSeconds = pump.Tests.Sum(t => t.EstimatedTotalSeconds());
|
|
}
|
|
|
|
// ── Public API: real-time updates from BenchService events ─────────────────
|
|
|
|
/// <summary>
|
|
/// Marks the named phase as actively executing and clears any previous active state.
|
|
/// Caches the active phase card for fast live-indicator updates.
|
|
/// </summary>
|
|
/// <param name="phaseName">Name of the phase that is now running.</param>
|
|
public void SetActivePhase(string phaseName)
|
|
{
|
|
StatusText = phaseName;
|
|
_activePhaseCard = null;
|
|
|
|
foreach (var section in Tests)
|
|
{
|
|
bool sectionActive = false;
|
|
foreach (var phase in section.Phases)
|
|
{
|
|
if (phase.Name == phaseName && !phase.IsPassed && !phase.IsFailed)
|
|
{
|
|
phase.IsActive = true;
|
|
_activePhaseCard = phase;
|
|
sectionActive = true;
|
|
}
|
|
else
|
|
{
|
|
phase.IsActive = false;
|
|
}
|
|
}
|
|
section.IsActiveTest = sectionActive;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Marks a phase as completed with the given pass/fail result.
|
|
/// </summary>
|
|
/// <param name="phaseName">Name of the completed phase.</param>
|
|
/// <param name="passed">True if the phase passed all criteria.</param>
|
|
public void SetPhaseResult(string phaseName, bool passed)
|
|
{
|
|
foreach (var section in Tests)
|
|
{
|
|
foreach (var phase in section.Phases)
|
|
{
|
|
if (phase.Name != phaseName || (!phase.IsActive && !phase.IsPassed && !phase.IsFailed))
|
|
continue;
|
|
|
|
// Only update if this is the active phase (avoid overwriting already-completed phases
|
|
// with the same name in different tests).
|
|
if (!phase.IsActive) continue;
|
|
|
|
phase.IsActive = false;
|
|
phase.IsPassed = passed;
|
|
phase.IsFailed = !passed;
|
|
phase.ResultText = passed ? "PASS" : "FAIL";
|
|
break;
|
|
}
|
|
|
|
// Recalculate section active state.
|
|
section.IsActiveTest = section.Phases.Any(p => p.IsActive);
|
|
}
|
|
|
|
if (_activePhaseCard?.Name == phaseName)
|
|
_activePhaseCard = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates the live measurement value on the graphic indicator for the currently
|
|
/// active phase that has a matching receive parameter.
|
|
/// </summary>
|
|
/// <param name="paramName">CAN parameter name (e.g. "QDelivery").</param>
|
|
/// <param name="value">Current measured value.</param>
|
|
public void UpdateLiveIndicator(string paramName, double value)
|
|
{
|
|
if (_activePhaseCard == null) return;
|
|
|
|
foreach (var indicator in _activePhaseCard.ResultIndicators)
|
|
{
|
|
if (indicator.ParameterName == paramName)
|
|
{
|
|
indicator.CurrentValue = value;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets all phase execution states and graphic indicators for a fresh test run.
|
|
/// </summary>
|
|
public void ResetResults()
|
|
{
|
|
_activePhaseCard = null;
|
|
StatusText = string.Empty;
|
|
|
|
foreach (var section in Tests)
|
|
{
|
|
section.IsActiveTest = false;
|
|
foreach (var phase in section.Phases)
|
|
phase.Reset();
|
|
}
|
|
|
|
// Recalculate remaining seconds.
|
|
int total = 0;
|
|
foreach (var section in Tests)
|
|
{
|
|
if (section.Source != null)
|
|
total += section.Source.EstimatedTotalSeconds();
|
|
}
|
|
RemainingSeconds = total;
|
|
}
|
|
}
|
|
}
|