tbf/TBF/Rig/ControlBoard/Uni/StandingStartStopTestOp.cs

132 lines
4.6 KiB
C#

///
/// Copyright (c) 2021 Sensus Slovensko a.s.
///
using System;
using log4net;
using TBF.Rig.GenericDevices;
namespace TBF.Rig.ControlBoard.Uni
{
public class StandingStartStopTestOp : IOperation
{
private static readonly ILog log = LogManager.GetLogger(typeof(StandingStartStopTestOp));
public override string ToString() { return string.Format("StandingStartStopTestOp()"); }
///
/// Set by the constructor
///
readonly UniCB uniCB;
readonly TBF.Rig.Uni.FlowMeter.FlowMeter flowMeter;
readonly TBF.Rig.Uni.Diverter.Diverter diverter;
readonly int pulsesCount; /// Number of reference pulses for a complete test
readonly bool withDiverter; /// Test with diverter (and scale)
///
/// Internal state of this operation
///
enum OpState
{
StartingTest,
TestInProgress,
TestCompleted,
}
OpState opState;
/// <summary>
/// Operation for a Standing start-stop test (constructor)
///
/// Events: OpArgumentError .... invalid arguments, test cannot be started
/// StartingTest ....... starting a test
/// TestInProgress ..... test was successfully started, test is running
/// TestCompleted ...... test was successfully completed
/// Error .............. unspecified error while running a test
/// </summary>
/// <param name="cb">Control board component</param>
/// <param name="devices">Components used by the test</param>
/// <param name="qFrom">Flow lower limit</param>
/// <param name="qTo">Flow upper limit</param>
/// <param name="pulsesCount">Test duration - number of reference flow meter pulses</param>
public StandingStartStopTestOp(UniCB cb, OutputPath devices, int pulsesCount, bool withDiverter)
{
this.uniCB = cb;
flowMeter = devices.FlowMeter as TBF.Rig.Uni.FlowMeter.FlowMeter;
if (flowMeter == null) throw new ArgumentNullException("Invalid flow meter");
this.pulsesCount = pulsesCount;
this.withDiverter = withDiverter;
if (withDiverter)
{
diverter = devices.Diverter as TBF.Rig.Uni.Diverter.Diverter;
if (diverter == null) throw new ArgumentNullException("Invalid diverter");
}
log.Debug(this.ToString());
}
/// <summary>
/// Start this operation
/// </summary>
public void Start()
{
log.InfoFormat("Start() Et#={0} pulses={1} withDiverter={2}", flowMeter.Idx1, pulsesCount, withDiverter);
/// Start the test (and the flow measurement)
if (withDiverter) { uniCB.DivResolution = diverter.Resolution; }
uniCB.SetActivity(Activity.StandingStartStopTest);
uniCB.StartTest(false, flowMeter.Idx1, (withDiverter ? diverter.DiverterNr : 0), 0,
false, withDiverter, false, true, false, pulsesCount);
opState = OpState.StartingTest;
}
/// <summary>
/// Run this operation
/// </summary>
/// <returns>
/// Event.OpArgumentError .... invalid arguments, test cannot be started
/// Event.StartingTest ....... starting a test
/// Event.TestInProgress ..... test was successfully started, test is running
/// Event.TestCompleted ...... test was successfully completed
/// Event.Error .............. unspecified error while running a test
/// </returns>
public Event Run()
{
log.DebugFormat("Run() opState={0}", opState);
switch (opState)
{
case OpState.StartingTest:
opState = OpState.TestInProgress;
return Event.TestInProgress;
case OpState.TestInProgress:
if (uniCB.RefPulses < pulsesCount)
{
return Event.TestInProgress; /// Test was not completed yet
}
opState = OpState.TestCompleted;
return Event.TestCompleted;
case OpState.TestCompleted:
default:
return Event.TestCompleted;
}
}
/// <summary>
/// Stop this operation
/// </summary>
public void Stop()
{
log.Info("Stop()");
uniCB.SetActivity((opState == OpState.TestCompleted) ? Activity.TestCompleted : Activity.Idle);
uniCB.StopAll(false);
}
}
}