common/Production/ProductionUiCordonel/Model/FinalTestModel.cs
2026-04-23 17:50:07 +02:00

188 lines
6.1 KiB
C#

using System;
using System.Threading;
using ProductionUiCordonel.Data;
using Xylem.Common.Utils.ProcessExec.EventArguments;
namespace ProductionUiCordonel.Model
{
/// <summary>
/// Final test model preparing the Cordonel for shipping:
/// - Setup device with standard parameters covered by the VAKO or customer specific CSD,
/// - If radio is installed: radio test,
/// - Requiring radio key from LuDB,
/// - Requiring 7 passwords for the Cordonel access levels,
/// - Building the password file (Level 3 is the modified skeleton key),
/// - Store all configurations,
/// - Reboot the Cordonel to load all settings from FLASH to RAM,
/// - Configuration check of all parameters defined by VAKO and CSD.
/// </summary>
class FinalTestModel
{
#region Variables
/// <summary>
/// Port scan result event for message dispatcher to caller
/// </summary>
public event EventHandler<ProcessExecEventArgs> OnProcessUpdate;
// Text for caller to inform about actual process being carried out
private String _actualOperation;
// states of the state machine, the backup state locks against repeated execution
private FinalTestState _finalTestState;
private FinalTestState _finalTestBackupState;
// Process counter for caller to monitor actual progress
private Int32 _overallProcessedStepsCtr;
private Int32 _singleProcessedStepsCtr;
// maximum processing steps for calculation of progress bar
private const Int32 MaxOverallProcessSteps = 20;
private Int32 _maxSingleProcessSteps;
// processing retries
private const Int32 MaxProcessRetries = 2;
private Int32 _processRetryCtr;
#endregion
#region Events
/// <summary>
/// Process event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks date="2023-Jan-17" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public virtual void ProcessUpdate_Event(Object sender, ProcessExecEventArgs e)
{
// copy text from overall messages to actual, because this is from sub-routine
OnProcessUpdate?.Invoke(this, new ProcessExecEventArgs(GetActualStateText(),
OverallProcessPercent, _actualOperation, e.OverallProcessPercent));
}
#endregion
#region ProcessInformation
/// <summary>
/// Process counter in percent
/// </summary>
/// <remarks date="2023-Jan-17" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Double OverallProcessPercent => 100.0d *
(_overallProcessedStepsCtr + _singleProcessedStepsCtr) / MaxOverallProcessSteps;
/// <summary>
/// Single file process counter in percent
/// </summary>
/// <remarks date="2023-Jan-17" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public Double SingleFileProcessPercent => 100.0d * _singleProcessedStepsCtr / _maxSingleProcessSteps;
/// <summary>
/// Returns the final test state text information for display
/// </summary>
/// <returns></returns>
/// <remarks date="2023-Jan-17" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public String GetActualStateText()
{
// search text assigned to current state
var text = FinalTestStateInfo.GetTextFromState(_finalTestState);
if (_finalTestState == FinalTestState.Idle)
{
return text;
}
text += _processRetryCtr > 0 ? $"{_processRetryCtr}" : "";
if (_overallProcessedStepsCtr > 0)
{
text += $"{_overallProcessedStepsCtr}/{MaxOverallProcessSteps}";
}
return text;
}
#endregion
#region CtorAssignment
/// <summary>
/// Ctor
/// </summary>
/// <remarks date="2023-Jan-17" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public FinalTestModel()
{
}
/// <summary>
/// Dispose
/// </summary>
public void Dispose()
{
}
#endregion
#region StateMachine
/// <summary>
/// State machine of FinalTestModel.
/// </summary>
/// <remarks date="2023-Jan-17" author="Thomas Wiedebusch">
/// - Initial
/// </remarks>
public void StateMachine()
{
// avoid repeated execution of the state machine of identical state
if (_finalTestState == _finalTestBackupState)
{
Thread.Sleep(1);
}
else
{
// remind backup state to avoid repeated execution and side effects
_finalTestBackupState = _finalTestState;
switch (_finalTestState)
{
case FinalTestState.Idle:
break;
case FinalTestState.StartInitial:
_overallProcessedStepsCtr = 0;
_singleProcessedStepsCtr = 0;
_finalTestState = FinalTestState.Idle;
break;
case FinalTestState.StepFailed:
_finalTestState = _processRetryCtr++ < MaxProcessRetries
? FinalTestState.RepeatFinalTest
: FinalTestState.FinalTestFailed;
break;
case FinalTestState.RepeatFinalTest:
_overallProcessedStepsCtr = 0;
_singleProcessedStepsCtr = 0;
_finalTestState = FinalTestState.Idle;
break;
default:
_finalTestState = FinalTestState.Idle;
break;
}
}// lock repeated execution of identical state
#endregion
}
}
}