190 lines
8.4 KiB
C#
190 lines
8.4 KiB
C#
///
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using log4net;
|
|
using TBF.Rig.GenericDevices;
|
|
|
|
namespace TBF.Rig.ControlBoard.Uni
|
|
{
|
|
public class FlyingStartStopTestWithDivOp : IOperation
|
|
{
|
|
private static readonly ILog log = LogManager.GetLogger(typeof(FlyingStartStopTestWithDivOp));
|
|
public override string ToString() { return string.Format("FlyingStartStopTestWithDivOp()"); }
|
|
|
|
/// Arguments of the constructor
|
|
readonly UniCB uniCB;
|
|
readonly TBF.Rig.Uni.RegValve.RegValve regV;
|
|
readonly TBF.Rig.Uni.FlowMeter.FlowMeter flowMeter;
|
|
readonly TBF.Rig.Uni.Diverter.Diverter div;
|
|
readonly double qFrom;
|
|
readonly double qTo;
|
|
readonly int pulsesCount; /// Number of reference pulses for a complete test
|
|
readonly int massPulsesCount; /// Number of reference pulses for mass collection
|
|
readonly bool readDivTransition;
|
|
readonly bool isProlongedTest;
|
|
|
|
/// <summary>
|
|
/// Operation for a Flying start-stop test with a diverter (constructor)
|
|
///
|
|
/// Events: OpArgumentError ...... invalid arguments, test cannot be started
|
|
/// StartingTest ......... starting a test
|
|
/// TestInProgress ....... test was successfully started, test is running
|
|
/// TestPart2InProgress .. first part when water is diverted to a tank was successfully completed, part2 in progress,
|
|
/// prolonged method only: (massPulsesCount GT 0) and (massPulsesCount LT pulsesCount)
|
|
/// 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>
|
|
/// <param name="massPulsesCount">Test part 1 duration - number of reference flow meter pulses<</param>
|
|
public FlyingStartStopTestWithDivOp(UniCB cb, OutputPath devices, double qFrom, double qTo,
|
|
int pulsesCount, int massPulsesCount, bool readDivTransition)
|
|
{
|
|
this.uniCB = cb;
|
|
|
|
flowMeter = devices.FlowMeter as TBF.Rig.Uni.FlowMeter.FlowMeter;
|
|
if (flowMeter == null) throw new ArgumentNullException("Invalid flow meter");
|
|
|
|
regV = devices.RegValve as TBF.Rig.Uni.RegValve.RegValve;
|
|
if (regV == null) throw new ArgumentNullException("Invalid regulation valve");
|
|
|
|
div = devices.Diverter as TBF.Rig.Uni.Diverter.Diverter;
|
|
if (div == null) throw new ArgumentNullException("Invalid diverter");
|
|
|
|
this.qFrom = qFrom;
|
|
this.qTo = qTo;
|
|
this.pulsesCount = pulsesCount;
|
|
this.massPulsesCount = massPulsesCount;
|
|
this.readDivTransition = readDivTransition;
|
|
this.isProlongedTest = (0 < massPulsesCount) && (massPulsesCount < pulsesCount);
|
|
|
|
log.Debug(this.ToString());
|
|
}
|
|
|
|
/// Internal states of this operation
|
|
enum OpState
|
|
{
|
|
StartingTest,
|
|
ReadingDivTransition,
|
|
TestInProgress,
|
|
TestPart1InProgress, /// prolonged test only
|
|
TestPart2InProgress, /// prolonged test only
|
|
TestCompleted,
|
|
}
|
|
|
|
OpState opState;
|
|
|
|
/// <summary>
|
|
/// Start this operation
|
|
/// </summary>
|
|
public void Start()
|
|
{
|
|
log.InfoFormat("Op.Start() RV#={0} Et#={1} Div#={2} Qfrom={3} Qto={4}", regV.Idx1, flowMeter.Idx1, div.DiverterNr, qFrom, qTo);
|
|
|
|
/// Start the test (and the flow measurement)
|
|
uniCB.StartTest(false, flowMeter.Idx1, div.DiverterNr, div.StartThreshold, true, true, false, isProlongedTest, pulsesCount, massPulsesCount);
|
|
|
|
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.TestPart1InProgress .. first part when water is diverted to a tank was successfully completed, part2 in progress,
|
|
/// Event.TestPart2InProgress .. first part when water is diverted to a tank was successfully completed, part2 in progress,
|
|
/// prolonged method only: (massPulsesCount GT 0) and (massPulsesCount LT pulsesCount)
|
|
/// Event.TestCompleted ........ test was successfully completed
|
|
/// Event.Error ................ unspecified error while running a test
|
|
/// </returns>
|
|
public Event Run()
|
|
{
|
|
log.DebugFormat("Op.Run() opState={0}", opState);
|
|
|
|
switch (opState)
|
|
{
|
|
case OpState.StartingTest:
|
|
|
|
if (qFrom < 0 || qFrom >= qTo || qTo > 1.25 * flowMeter.NominalFlow)
|
|
{
|
|
return Event.OpArgumentError; /// Invalid qFrom, qTo
|
|
}
|
|
|
|
if (!flowMeter.MsrmntAvailable || /// Flow measurement not started yet
|
|
(uniCB.State & (ulong)StatusP.ReadingCounters) == 0 || /// Test was not started yet
|
|
uniCB.RefFrequency == 0) /// Measured flow is 0
|
|
{
|
|
return Event.StartingTest; /// Flow regulation cannot be started
|
|
}
|
|
|
|
/// Start flow regulation
|
|
double reqFlowAve = (qFrom + qTo) / 2;
|
|
double reqFlowLo = (0.7 * qFrom) + (0.3 * reqFlowAve); /// Move the lower limit 15% of the range up
|
|
double reqFlowHi = (0.7 * qTo) + (0.3 * reqFlowAve); /// Move the upper limit 15% of the range down
|
|
///
|
|
uniCB.SetFlow(false, regV.Idx1, 2000 * reqFlowLo / flowMeter.NominalFlow,
|
|
2000 * reqFlowHi / flowMeter.NominalFlow);
|
|
|
|
opState = readDivTransition ? OpState.ReadingDivTransition
|
|
: (isProlongedTest ? OpState.TestPart1InProgress : OpState.TestInProgress);
|
|
return isProlongedTest ? Event.TestPart1InProgress : Event.TestInProgress;
|
|
|
|
case OpState.ReadingDivTransition:
|
|
|
|
/// Read diverter transition data
|
|
uniCB.ReadDiverterTransition(false, div.DiverterNr);
|
|
opState = isProlongedTest ? OpState.TestPart1InProgress : OpState.TestInProgress;
|
|
return isProlongedTest ? Event.TestPart1InProgress : Event.TestInProgress;
|
|
|
|
case OpState.TestInProgress:
|
|
|
|
if ((uniCB.State & (ulong)StatusP.TestCompleted) == 0)
|
|
{
|
|
return Event.TestInProgress; /// Test was not completed yet
|
|
}
|
|
opState = OpState.TestCompleted;
|
|
return Event.TestCompleted;
|
|
|
|
case OpState.TestPart1InProgress:
|
|
|
|
if ((uniCB.State & (ulong)StatusP.DirectionOfDiverter) != 0)
|
|
{
|
|
return Event.TestPart1InProgress; /// Test was not completed yet
|
|
}
|
|
opState = OpState.TestPart2InProgress;
|
|
return Event.TestPart2InProgress;
|
|
|
|
case OpState.TestPart2InProgress:
|
|
|
|
if ((uniCB.State & (ulong)StatusP.TestCompleted) == 0)
|
|
{
|
|
/// Test was not completed yet
|
|
return Event.TestPart2InProgress;
|
|
}
|
|
opState = OpState.TestCompleted;
|
|
return Event.TestCompleted;
|
|
|
|
case OpState.TestCompleted:
|
|
default:
|
|
return Event.TestCompleted;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stop this operation
|
|
/// </summary>
|
|
public void Stop()
|
|
{
|
|
log.Info("Op.Stop()");
|
|
uniCB.Stop(false);
|
|
}
|
|
}
|
|
}
|