/// /// Copyright (c) 2013-2015 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Text; using log4net; using TBF.BenchControl; using TBF.Boxes; using TBF.Resources; using TBF.UiBridge; namespace TBF.BenchControl.TestMethods.LeakTest { public class LeakTestSeq : Sequences.SequenceBase { private static readonly ILog log = LogManager.GetLogger(typeof(LeakTestSeq)); /// /// Flying start mass collection method sequence /// /// Test entity /// /// Event.Done . . . . . . . OK /// Event.UiCmdStop . . . . Stopped by the user using the on-screen button STOP /// Event.OpArgumentError . Target flow is out of range /// Event.Error . . . . . . Unspecified error /// public IList Execute(Entities.Test test, LeakTestParams testParams) { Elde.ControlBoardDev cBrd = StateMachine.ControlBoard; IList e = new List(); /// Events from currently running operations Event retVal = Event.Done; checkUiOp = new Operations.CheckUIOp(true); /// Runs in more then one state processDataLoggingOp = new TBF.BenchControl.Operations.ProcessDataLoggingOp(processDataLogger, this); // Transition or SetRoute - Start switch (Transition(transitionStart, TransitionContext.TestStart)) { case Event.Error: { retVal = Event.Error; goto stopTest; } case Event.UiCmdStop: { retVal = Event.UiCmdStop; goto stopTest; } } /// Start the test, initialize test results Bridge.OnTestSelected(this, new TestSelectedEventArgs(test, 1, inPath, benchPath, outPath, sensPath, 0)); Entities.TestResult tstRslt = new Entities.TestResult(test, 1, Entities.MetersKind.Single); //------------------------------------------------ Bridge.OnActivity(this, Strings.Starting_the_pump); //------------------------------------------------ if (inPath.Pump is GenericDevices.IPumpFM) (inPath.Pump as GenericDevices.IPumpFM).TurnOn(test.PumpPower); State.Create("LeakTest : Starting the pump") .AddOperation(checkUiOp) .AddOperation(cBrd.SetValvesOp(inPath.Pump, null)) .AddOperation(benchPath.TempIn.ReadTempOp(ref tempIn)) .AddOperation(benchPath.TempOut.ReadTempOp(ref tempOut)) .AddOperation(benchPath.PressIn.ReadPressureOp(ref pressIn)) .AddOperation(benchPath.PressOut.ReadPressureOp(ref pressOut)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.UiCmdStop)) { retVal = Event.UiCmdStop; goto stopTest; } } while (e.Contains(Event.ValvesBusy)); //------------------------------------------------ Bridge.OnActivity(this, Strings.Setting_the_water_pressure); //------------------------------------------------ float pumpPower = test.PumpPower; int startTime = StateMachine.Time; while (true) { if (inPath.Pump is GenericDevices.IPumpFM) (inPath.Pump as GenericDevices.IPumpFM).TurnOn(pumpPower); State.Create("LeakTest : Setting the water pressure") .AddOperation(checkUiOp) .AddOperation(benchPath.TempIn.ReadTempOp(ref tempIn)) .AddOperation(benchPath.TempOut.ReadTempOp(ref tempOut)) .AddOperation(benchPath.PressIn.ReadPressureOp(ref pressIn)) .AddOperation(benchPath.PressOut.ReadPressureOp(ref pressOut)) .AddOperation(new Operations.TimerOp(5)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.UiCmdStop)) { retVal = Event.UiCmdStop; goto stopTest; } if (e.Contains(Event.Next)) goto pressure_set; } while (e.Contains(Event.TimerBusy)); UiBridge.Bridge.OnLog(this, string.Format("time = {0}, pump_power = {1}%, pressure_up = {2}kPa, pressure_down = {3}kPa\r\n", StateMachine.Time - startTime, pumpPower.ToString("F0"), pressIn.Val.ToString("F1"), pressIn.Val.ToString("F1"))); float step = (pumpPower < 40.0f) ? 5.0f : ((pumpPower < 60.0f) ? 3.0f : ((pumpPower < 80.0f) ? 2.0f : 1.0f)); if (pressOut.Val < testParams.PressureLo) { if (pumpPower == 100.0f) break; /// Already at max. pump power -> Quit loop pumpPower = Math.Min(pumpPower + step, 100.0f); /// Limit max. power to 100% } else if (pressOut.Val > testParams.PressureHi) { if (pumpPower == 0) break; /// Already at min. pump power -> Quit loop pumpPower = Math.Max(pumpPower - step, 0); /// Limit min. power to 0% } else { break; /// Pressure is in the range -> Quit loop } } pressure_set: startTime = StateMachine.Time; int estimtdEndTime = startTime + testParams.DurationPMax; //------------------------------------------------ Bridge.OnActivity(this, Strings.Test_in_progress); //------------------------------------------------ State.Create("LeakTest : Maximum water pressure set") .AddOperation(checkUiOp) .AddOperation(benchPath.TempIn.ReadTempOp(ref tempIn)) .AddOperation(benchPath.TempOut.ReadTempOp(ref tempOut)) .AddOperation(benchPath.PressIn.ReadPressureOp(ref pressIn)) .AddOperation(benchPath.PressOut.ReadPressureOp(ref pressOut)) .AddOperation(new Operations.TimerOp(testParams.DurationPMax)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); tstRslt.TempInStart = tempIn.Val; tstRslt.TempOutStart = tempOut.Val; tstRslt.PressInStart = pressIn.Val; tstRslt.PressOutStart = pressOut.Val; if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; } if (e.Contains(Event.UiCmdStop)) { retVal = Event.UiCmdStop; goto stopTest; } int remainingTime = Math.Max(estimtdEndTime - StateMachine.Time, 0); Bridge.OnActivity(this, string.Format("{0} ... {1} s", Strings.Test_in_progress, remainingTime)); } while (e.Contains(Event.TimerBusy)); closing_valve: //------------------------------------------------ Bridge.OnActivity(this, Strings.Closing_valve_Pump_off); //------------------------------------------------ State.Create("LeakTest : Closing the valve") .AddOperation(checkUiOp) .AddOperation(cBrd.SetValvesOp(null, outPath.StartValve)) .AddOperation(benchPath.TempIn.ReadTempOp(ref tempIn)) .AddOperation(benchPath.TempOut.ReadTempOp(ref tempOut)) .AddOperation(benchPath.PressIn.ReadPressureOp(ref pressIn)) .AddOperation(benchPath.PressOut.ReadPressureOp(ref pressOut)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); tstRslt.TempInStart = tempIn.Val; tstRslt.TempOutStart = tempOut.Val; tstRslt.PressInStart = pressIn.Val; tstRslt.PressOutStart = pressOut.Val; if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; } if (e.Contains(Event.UiCmdStop)) { retVal = Event.UiCmdStop; goto stopTest; } } while (e.Contains(Event.ValvesBusy)); valve_closed: if (inPath.Pump is GenericDevices.IPumpFM) (inPath.Pump as GenericDevices.IPumpFM).TurnOff(); State.Create("LeakTest : Closing the valve") .AddOperation(checkUiOp) .AddOperation(cBrd.SetValvesOp(null, inPath.Pump)) .AddOperation(benchPath.TempIn.ReadTempOp(ref tempIn)) .AddOperation(benchPath.TempOut.ReadTempOp(ref tempOut)) .AddOperation(benchPath.PressIn.ReadPressureOp(ref pressIn)) .AddOperation(benchPath.PressOut.ReadPressureOp(ref pressOut)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); tstRslt.TempInStart = tempIn.Val; tstRslt.TempOutStart = tempOut.Val; tstRslt.PressInStart = pressIn.Val; tstRslt.PressOutStart = pressOut.Val; if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; } if (e.Contains(Event.UiCmdStop)) { retVal = Event.UiCmdStop; goto stopTest; } } while (e.Contains(Event.ValvesBusy)); pump_is_off: tstRslt.TimeStart = DateTime.Now; startTime = StateMachine.Time; estimtdEndTime = startTime + testParams.DurationLeak; ResetAveragedData(); //------------------------------------------------ Bridge.OnActivity(this, Strings.Test_in_progress); //------------------------------------------------ State.Create("LeakTest : Starting the test") .AddOperation(checkUiOp) .AddOperation(benchPath.TempIn.ReadTempOp(ref tempIn)) .AddOperation(benchPath.TempOut.ReadTempOp(ref tempOut)) .AddOperation(benchPath.PressIn.ReadPressureOp(ref pressIn)) .AddOperation(benchPath.PressOut.ReadPressureOp(ref pressOut)) .AddOperation(new Operations.TimerOp(testParams.DurationLeak)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; } if (e.Contains(Event.UiCmdStop)) { retVal = Event.UiCmdStop; goto stopTest; } int remainingTime = Math.Max(estimtdEndTime - StateMachine.Time, 0); Bridge.OnActivity(this, string.Format("{0} ... {1} s", Strings.Test_in_progress, remainingTime)); tstRslt.TempInEnd = tempIn.Val; tstRslt.TempOutEnd = tempOut.Val; tstRslt.PressInEnd = pressIn.Val; tstRslt.PressOutEnd = pressOut.Val; tstRslt.MassEndRaw = mass.Val; AccumulateAveragedData(); } while (e.Contains(Event.TimerBusy)); /// Measurement loop - end test_completed: //------------------------------------------------ Bridge.OnActivity(this, Strings.Test_completed); //------------------------------------------------ /// Make sure the CycleBeginForm is closed so that water meter data (s/n) can be copied into results if (cycleBeginFormOpened) { cycleBeginFormOpened = false; if (WaitBeginFormClosed()) goto stopTest; } /// /// Populate TestResult data entity with data /// UpdateTestRsltWithAveragedData(tstRslt); tstRslt.TimeEnd = DateTime.Now; tstRslt.BatchNr = Program.LocalSettings.BatchNr; tstRslt.MassStart = 0; tstRslt.MassEnd = 0; tstRslt.MassDiff = 0; tstRslt.DensityIn = Formulas.WaterDensityFromTemp(tstRslt.TempInAvrg); /// [kg/m3] tstRslt.DensityOut = Formulas.WaterDensityFromTemp(tstRslt.TempOutAvrg);/// [kg/m3] tstRslt.DensityDiv = 0; /// [kg/m3] tstRslt.Time = testParams.DurationPMax; /// [s] measurement time tstRslt.FlowMass = 0; /// [kg/h] tstRslt.FlowVolume = 0; /// [l/h] tstRslt.PulsesMaster = 0; /// Pulses of the master flow meter (test total) tstRslt.VolumeMaster = 0; /// [l] volume from the master flow meter tstRslt.ConstMaster = 0; /// Convert the flow to [m3/h] tstRslt.VolumeCTV = 0; /// [l] 1000.0f is because density is in [kg/m3] tstRslt.ErrorMaster = 0; tstRslt.TimeDivStart0 = 0; tstRslt.TimeDivStart1 = 0; tstRslt.TimeDivStart2 = 0; tstRslt.TimeDivStart3 = 0; tstRslt.TimeDivStart4 = 0; tstRslt.TimeDivStart5 = 0; tstRslt.TimeDivEnd0 = 0; tstRslt.TimeDivEnd1 = 0; tstRslt.TimeDivEnd2 = 0; tstRslt.TimeDivEnd3 = 0; tstRslt.TimeDivEnd4 = 0; tstRslt.TimeDivEnd5 = 0; for (int i = 0; i < Program.WMsCount; i++) { if (sensPath.RegisterReaders[i] != null) { tstRslt.Meters[i].SerialNr = (WaterMeters.Count > i) ? WaterMeters[i].SerialNr : string.Empty; tstRslt.Meters[i].VolumeStart = 0; tstRslt.Meters[i].VolumeEnd = 0; tstRslt.Meters[i].VolumeMeter = 0; tstRslt.Meters[i].VolumeRef = 0; tstRslt.Meters[i].PulsesMeter = 0; tstRslt.Meters[i].PulsesMaster = 0; tstRslt.Meters[i].Time = testParams.DurationPMax; tstRslt.Meters[i].VolumeErrorPct = 0; tstRslt.Meters[i].Passed = true; } } /// Add data - end AddOrOverwriteResult(tstRslt); Bridge.OnTestCompleted(this, new TestCompletedEventArgs(tstRslt)); /// Append the results to the CSV-file allResults.Info(TestResult2CsvLine(tstRslt)); stopTest: /// /// Quit this sequence /// State.Create("LeakTest : Stopping diverter, gate, etc.") .AddOperation(checkUiOp) .AddOperation(cBrd.StopPreviousOp()) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.UiCmdStop)) { retVal = Event.UiCmdStop; goto stopTest; } } while (!e.Contains(Event.PreviousStopped)); /// Transition sequence at the end of test /// Prevent overwriting 'retVal' in case it was set to non-default value earlier switch (Transition(transitionStop, TransitionContext.TestEnd)) { case Event.Error: { if (retVal == Event.Done) retVal = Event.Error; break; } case Event.UiCmdStop: { if (retVal == Event.Done) retVal = Event.UiCmdStop; break; } } /// Create a list with one item 'retVal' (default is Event.Done) and return it IList retList = new List(1); retList.Add(retVal); return retList; } } }