/// /// Copyright (c) 2015-2021 Sensus Slovensko a.s. /// using System; using System.Collections.Generic; using System.Text; using log4net; using Common; using Config.Entities; using TBF.Rig; using TBF.Boxes; using TBF.Resources; using TBF.UiBridge; //using AppDiagnostic; namespace TBF.Rig.TestMethods.PMaxTest { public class PMaxTestSeq : Sequences.SequenceBase { private static readonly ILog log = LogManager.GetLogger(typeof(PMaxTestSeq)); /// /// Check capabilities of devces in the output path required for this test method /// /// Devices /// true when capabilities of devices are OK public static bool CheckDeviceCaps(Config.Entities.Test test, OutputPath devices, out string message) { if (!(StateMachine.ControlBoardMain is ControlBoard.Papouch.PapouchCB) && !(StateMachine.ControlBoardMain is ControlBoard.Uni.UniCB)) { /// Control board does not support this method message = string.Format("{0}: {1}", test.Name, Strings.Test_bench_does_not_suport_selected_test_method); return false; } message = string.Empty; return true; } /// /// Pressure test 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(Config.Entities.Test test, int repetitionNr, bool isLastRepetition, PMaxTestParams testParams) { IList e; /// Events from currently running operations Event retVal = Event.Done; TBF.Rig.ControlBoard.IControlBoard cBrd = StateMachine.ControlBoardMain; checkUiOp = new Operations.CheckUIOp(true); /// Runs in more then one state processDataLoggingOp = new TBF.Rig.Operations.ProcessDataLoggingOp(processDataLogger, this, false); IList readTempPressOps = new List(); if (benchPath.TempMtrUp != null) readTempPressOps.Add(benchPath.TempMtrUp.ReadTempOp(ref TempUp)); if (benchPath.TempMtrDown != null) readTempPressOps.Add(benchPath.TempMtrDown.ReadTempOp(ref TempDown)); if (outPath.TempMtrDiv != null) readTempPressOps.Add(outPath.TempMtrDiv.ReadTempOp(ref TempDiv)); if (benchPath.PressMtrUp != null) readTempPressOps.Add(benchPath.PressMtrUp.ReadPressureOp(ref PressUp)); if (benchPath.PressMtrDown != null) readTempPressOps.Add(benchPath.PressMtrDown.ReadPressureOp(ref PressDown)); if (benchPath.PressMtrDelta != null) readTempPressOps.Add(benchPath.PressMtrDelta.ReadPressureOp(ref PressDelta)); if (benchPath.ElectricMtrUp != null) readTempPressOps.Add(benchPath.ElectricMtrUp.ReadPressureOp(ref ElectricUp)); if (benchPath.ElectricMtrDown != null) readTempPressOps.Add(benchPath.ElectricMtrDown.ReadPressureOp(ref ElectricDown)); if (benchPath.ElectricMtrDelta != null) readTempPressOps.Add(benchPath.ElectricMtrDelta.ReadPressureOp(ref ElectricDelta)); if (heatMetersPath != null) readTempPressOps.Add(heatMetersPath.TMeterRefWarm1.ReadTempOp(ref TempRefHi1)); if (heatMetersPath != null) readTempPressOps.Add(heatMetersPath.TMeterRefWarm2.ReadTempOp(ref TempRefHi2)); if (heatMetersPath != null) readTempPressOps.Add(heatMetersPath.TMeterRefCold1.ReadTempOp(ref TempRefLo1)); if (heatMetersPath != null) readTempPressOps.Add(heatMetersPath.TMeterRefCold2.ReadTempOp(ref TempRefLo2)); /// Read pressure and temperature once before calling Bridge.OnTestSelected(...) State.Create(string.Format("{0}({1}) : Measuring process data", test.Method, test.Name)) .AddOperation(checkUiOp) .AddOperations(readTempPressOps) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; } if (TestAndLogUiCmdStop(test, e)) { retVal = Event.UiCmdStop; goto stopTest; } } while (e.Contains(Event.Busy)); /// Start the test, initialize test results Bridge.OnTestSelected(this, new TestSelectedEventArgs(test, 1, inPath, benchPath, outPath, sensPath, heatMetersPath)); string testName = Results.Utils.GetTestName(test.Name, test.Repeats, 1); TestStartTime = DateTime.Now; //------------------------------------------------ Bridge.OnActivity(this, Strings.Starting_the_pump); //------------------------------------------------ if (inPath.Pump is GenericDevices.IPumpFM) (inPath.Pump as GenericDevices.IPumpFM).TurnOn(test.PumpPower); State.Create(string.Format("{0}({1}) : Starting the pump", test.Method, test.Name)) .AddOperation(checkUiOp) .AddOperations(readTempPressOps) //.AddOperation(new Operations.SetAllRegulValvesOp(inPath.RegulValves, inPath.RegulValvesPct, 60)) .AddOperation(inPath.Pump != null ? cBrd.SetValvesOp(inPath.Pump, null) : null) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (TestAndLogUiCmdStop(test, e)) { retVal = Event.UiCmdStop; goto stopTest; } if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; } } while (e.Contains(Event.ValvesBusy) /* || !e.Contains(Event.AllPositionsReached)*/); //------------------------------------------------ Bridge.OnActivity(this, Strings.Setting_the_water_pressure); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Progress.FlowSetting)); //------------------------------------------------ float pumpPower = test.PumpPower; while (true) { if (inPath.Pump is GenericDevices.IPumpFM) (inPath.Pump as GenericDevices.IPumpFM).TurnOn(pumpPower); State.Create(string.Format("{0}({1}) : Setting the water pressure", test.Method, test.Name)) .AddOperation(checkUiOp) .AddOperations(readTempPressOps) .AddOperation(new Operations.TimerOp(5)) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (TestAndLogUiCmdStop(test,e)) { /// User pressed 'Stop' button retVal = Event.UiCmdStop; goto stopTest; } if (testParams.PressSetTimeout > 0 && DateTime.Now - TestStartTime >= new TimeSpan(0, 0, testParams.PressSetTimeout)) { /// Pressure setting timeout retVal = Event.RecoverableError; goto stopTest; } } while (e.Contains(Event.TimerBusy) && !e.Contains(Event.Next)); float step = (pumpPower < 40.0f) ? 4.7f : ((pumpPower < 60.0f) ? 3.3f : ((pumpPower < 80.0f) ? 2.2f : ((pumpPower < 90.0f) ? 1.5f : 1.2f))); if (e.Contains(Event.Next)) { /// 'Next' button pressed (in debug mode) => Quit the loop and continue in PMaxTest break; } else if (PressDown.Val < testParams.PressureLo) { /// Low pressure => Increase the pump power, limit max. power to 100% pumpPower = Math.Min(pumpPower + step, 100.0f); } else if (PressDown.Val > testParams.PressureHi) { /// Hi pressure => Decrease the pump power, limit min. power to 0% pumpPower = Math.Max(pumpPower - step, 0); } else { /// Pressure is in the required range => Quit the loop and continue in PMaxTest break; } } /// /// Pressure is set, PMaxTest start /// int flowSetTime = (int)Math.Round((DateTime.Now - TestStartTime).TotalSeconds); StartNewStatistics(null, BatchRslts.Batch.BatchNr, test, repetitionNr, 0); UpdateAllStatistics(); int startTime = StateMachine.Time; int endTime = startTime + testParams.DurationPMax; //TODO BUMI - check this possition for sstart logging - when finish remove this comment //--- log start process in this section LogProcessDataTestInfo(processDataLogger, test.Procedure.Name, test.Name); //------------------------------------------------ Bridge.OnActivity(this, Strings.Test_in_progress); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Progress.Test)); //------------------------------------------------ //TODO BUMI - check if is ok possition to start logging - at last remove comment //--- log start process in this section LogProcessDataHeader(processDataLogger, "Starting the test"); /*//---new1 LogProcessDataTestInfo(processDataLogger, test.Procedure.Name, test.Name); if (heatMetersPath == null) LogProcessDataHeader(processDataLogger, "Start mass"); else LogProcessDataHeaderHeatMeters(processDataLogger, "Start mass"); //---endNew1*/ //DiagApi.addLog(); State.Create(string.Format("{0}({1}) : Starting the test", test.Method, test.Name)) .AddOperation(checkUiOp) .AddOperations(readTempPressOps) .AddOperation(new Operations.TimerOp(testParams.DurationPMax)) .AddOperation(processDataLoggingOp) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); if (e.Contains(Event.Error)) { retVal = Event.Error; goto stopTest; } if (TestAndLogUiCmdStop(test,e)) { retVal = Event.UiCmdStop; goto stopTest; } UpdateAllStatistics(); int remainingTime = Math.Max(endTime - StateMachine.Time, 0); if (remainingTime > 60) Bridge.OnActivity(this, string.Format("{0} ... {1} {2} {3} {4}", Strings.Test_in_progress, remainingTime / 60, "min", remainingTime % 60, Strings.sec)); else Bridge.OnActivity(this, string.Format("{0} ... {1} s", Strings.Test_in_progress, remainingTime)); } while (e.Contains(Event.TimerBusy)); /// /// PMaxTest completed /// TestEndTime = DateTime.Now; StopRecordingStatistics(); //------------------------------------------------ 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 (UIFlowControl.Stop == WaitBeginFormClosed()) goto stopTest; /// /// Populate TestResult data entity with data /// Results.Entities.TestRslt tstRslt = BatchRslts.GetTestRslt(testName, test.Part); if (tstRslt != null) { Results.Utils.GetCounterStates(tstRslt, Program.LocalSettings.Counters); UpdateTempPressDensAmb(tstRslt); tstRslt.DensityDiv = 0; /// Main results tstRslt.MethodClass = TbfComponents.FindComponent(test.Method).ClassName; tstRslt.StartTime = TestStartTime; tstRslt.EndTime = TestEndTime; tstRslt.FlowSetTime = flowSetTime; /// [s] FlowSetTime is pressure set time in this case tstRslt.TestTime = Convert.ToDouble(testParams.DurationPMax); /// [s] PMaxTest time tstRslt.PulsesMaster = 0; /// Pulses of the master flow meter (test total) tstRslt.MassStartRaw = 0; tstRslt.MassStart = 0; tstRslt.MassEndRaw = 0; tstRslt.MassEnd = 0; tstRslt.MassOfEvapWater = 0; tstRslt.Flow = 0; /// [m3/h] tstRslt.VolumeMaster = 0; /// [l] volume from the master flow meter tstRslt.VolumeCTV = 0; /// [l] 1000.0f is because density is in [kg/m3] tstRslt.ConstMasterRaw = 0; /// Convert the flow to [m3/h] tstRslt.ConstMaster = 0; /// Convert the flow to [m3/h] tstRslt.ErrorMaster = 0; tstRslt.ErrorFlags = 0; tstRslt.InfoFlags = 0; for (int i = 0; i < BatchRslts.WMPositionsCount; i++) { if (!test.IsPartCompatible(Utils.PartNr(i + 1, BatchRslts.Batch.Compound))) continue; if (StateMachine.Procedure.MetersKind == MetersKind.Single) { Results.Entities.MeterTestRslt meterRslt = BatchRslts.GetMeterTestRslt(testName, i, Common.CompoundMeterId.Single); if (meterRslt != null) { meterRslt.TestTime = tstRslt.TestTime; meterRslt.Error = 0; meterRslt.Passed = true; meterRslt.TestDone = true; tstRslt.TestDone = true; } } else { Results.Entities.MeterTestRslt mainMeterRslt = BatchRslts.GetMeterTestRslt(testName, i, Common.CompoundMeterId.CompoundMain); Results.Entities.MeterTestRslt auxMeterRslt = BatchRslts.GetMeterTestRslt(testName, i, Common.CompoundMeterId.CompoundAux); for (int isAux = 0; isAux <= 1; isAux++) /// 0=main, 1=aux { Results.Entities.MeterTestRslt meterRslt = (isAux == 0) ? mainMeterRslt : auxMeterRslt; if (meterRslt != null) { meterRslt.TestTime = tstRslt.TestTime; meterRslt.Error = 0; meterRslt.Passed = true; } } Results.Entities.MeterTestRslt compoundMeterRslt = BatchRslts.GetMeterTestRslt(testName, i, Common.CompoundMeterId.Compound); if (compoundMeterRslt != null) { compoundMeterRslt.TestTime = tstRslt.TestTime; compoundMeterRslt.Error = 0; compoundMeterRslt.Passed = true; mainMeterRslt.TestDone = auxMeterRslt.TestDone = compoundMeterRslt.TestDone = true; tstRslt.TestDone = true; } } } tstRslt.Components = Results.Entities.Components .UpdateList(BatchRslts.ComponentsList, new Results.Entities.Components((BenchInfo != null) ? BenchInfo.TestBenchId : 1, (BenchInfo != null) ? BenchInfo.TestBenchName : "testbench", inPath.Pump != null ? inPath.Pump.Name : string.Empty, outPath.FlowMeter != null ? outPath.FlowMeter.Name : string.Empty, outPath.Scale != null ? outPath.Scale.Name : string.Empty, outPath.RegValve != null ? outPath.RegValve.Name : string.Empty, outPath.Diverter != null ? outPath.Diverter.Name : string.Empty)); /// Update results Bridge.OnTestCompleted(this, new TestCompletedEventArgs(testName, tstRslt)); } Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Progress.TransitionAfter)); /// Append the results to the CSV-file allResults.Info(TestResult2CsvLine(testName, test.Part)); stopTest: StopRecordingStatistics(); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, repetitionNr, Progress.TransitionAfter)); /// /// Quit this sequence /// if (isLastRepetition || retVal == Event.UiCmdStop || retVal == Event.OpArgumentError || retVal == Event.RecoverableError || retVal == Event.ErrorFlagsStop || retVal == Event.Error || retVal == Event.ConfigurationError) { cBrd.StopAll(false); } return new List { retVal }; } } }