/// /// Copyright (c) 2021 Sensus Slovensko a.s. /// using System; using log4net; using TBF.Rig.GenericDevices; namespace TBF.Rig.ControlBoard.Uni { public class FlyingStartStopTestOp : IOperation { private static readonly ILog log = LogManager.GetLogger(typeof(FlyingStartStopTestOp)); public override string ToString() { return string.Format("FlyingStartStopTestOp()"); } /// Arguments of the constructor readonly UniCB uniCB; readonly TBF.Rig.Uni.RegValve.RegValve regV; readonly TBF.Rig.Uni.FlowMeter.FlowMeter flowMeter; readonly double qFrom; readonly double qTo; readonly int pulsesCount; /// Number of reference pulses for a complete test /// /// Operation for a Flying 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 /// /// Control board component /// Components used by the test /// Flow lower limit /// Flow upper limit /// Test duration - number of reference flow meter pulses public FlyingStartStopTestOp(UniCB cb, OutputPath devices, double qFrom, double qTo, int pulsesCount) { 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"); this.qFrom = qFrom; this.qTo = qTo; this.pulsesCount = pulsesCount; log.Debug(this.ToString()); } /// Internal state of this operation enum OpState { StartingTest, TestInProgress, TestCompleted, } OpState opState; /// /// Start this operation /// public void Start() { log.InfoFormat("Op.Start() RV#={0} Et#={1} Qfrom={2} Qto={3}", regV.Idx1, flowMeter.Idx1, qFrom, qTo); /// Start the test (and the flow measurement) uniCB.StartTest(false, flowMeter.Idx1, 0, 40, true, false, false, false, pulsesCount, 0); opState = OpState.StartingTest; } /// /// Run this operation /// /// /// 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 /// 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 = OpState.TestInProgress; return 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.TestCompleted: default: return Event.TestCompleted; } } /// /// Stop this operation /// public void Stop() { log.Info("Op.Stop()"); uniCB.Stop(false); } } }