tbf/TBF/UiBridge/TestProgressEventArgs.cs
2021-06-18 15:35:46 +02:00

125 lines
3.4 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using Common;
namespace TBF.UiBridge
{
public class TestProgressEventArgs : EventArgs
{
public string TestName;
public int Part;
public int Repeats;
public int RepetitionNr;
public Progress Phase;
public int Progress; /// value in the range 0..100
public float OveralProgress;
#region Static and base constructor part
static int[] estimatedTime; /// Estimated time of each phase to calculate the progress [s]
static int[] cumulativeSumsOfEstimates;
static int currentPhaseStartTime;
static Progress currentPhase;
static TestProgressEventArgs()
{
int[] estTime = new int[(int)Common.Progress.Count];
for (int i = 0; i < estTime.Length; i++) estTime[i] = 10;
cumulativeSumsOfEstimates = new int[(int)Common.Progress.Count];
SetEstimatedTimes(estTime);
currentPhaseStartTime = Rig.StateMachine.Time;
currentPhase = Common.Progress.JustStarted;
}
/// <summary>
/// Set estimated time of each phase in [s] (see Progress).
/// </summary>
/// <param name="estTime">Array: estimated time of each phase in [s]</param>
public static void SetEstimatedTimes(int[] estTime)
{
if (estTime.Length != (int)Common.Progress.Count) return;
estimatedTime = estTime;
for (int i = 0; i < estTime.Length; i++)
{
cumulativeSumsOfEstimates[i] = (i > 0) ? cumulativeSumsOfEstimates[i - 1] : 0;
cumulativeSumsOfEstimates[i] += estTime[i];
}
}
/// <summary>
/// Base constructor which maintains currentPhase, currentPhaseStartTime
/// and calculates 'Progress'.
/// </summary>
/// <param name="progress"></param>
private TestProgressEventArgs(Progress phase)
{
if (phase != currentPhase)
{
currentPhase = phase;
currentPhaseStartTime = Rig.StateMachine.Time;
}
Phase = phase;
int phaseNr = (int)phase;
int currentPhaseTime = Math.Min(Rig.StateMachine.Time - currentPhaseStartTime, estimatedTime[phaseNr]);
int currentTestTime = currentPhaseTime + ((phaseNr == 0) ? 0 : cumulativeSumsOfEstimates[phaseNr - 1]);
int add10pct = cumulativeSumsOfEstimates[(int)Common.Progress.Count - 1] / 9;
Progress = 100 * (currentTestTime + add10pct)
/ (cumulativeSumsOfEstimates[(int)Common.Progress.Count - 1] + add10pct);
}
#endregion
public TestProgressEventArgs(Config.Entities.Test test, int repetitionNr, Progress progress)
: this(progress)
{
TestName = Results.Utils.GetTestName(test.Name, test.Repeats, repetitionNr);
Part = test.Part;
Repeats = test.Repeats;
RepetitionNr = repetitionNr;
}
public TestProgressEventArgs(Config.Entities.Test test, Progress progress)
: this(test, 1, progress)
{
}
/// <summary>
/// From TestRslt.
/// Used after restore of an interupted session
/// </summary>
public TestProgressEventArgs(Results.Entities.TestRslt testRslt)
{
TestName = testRslt.Name();
Part = testRslt.Part;
Repeats = testRslt.Repeats();
RepetitionNr = testRslt.RepetitionNr;
Progress = testRslt.TestDone ? 100 : 0;
}
/// <summary>
/// Used by iPearl communication dialog
/// </summary>
public TestProgressEventArgs(string testName, Progress progress)
: this(progress)
{
TestName = testName;
Part = 0;
Repeats = 1;
RepetitionNr = 1;
}
}
}