/// /// Copyright (c) 2015-2016 Sensus Metering Systems /// using System; using System.Collections.Generic; using System.Text; using log4net; using Config.Entities; using TBF.BenchControl; using TBF.BenchControl.Sequences; using TBF.Boxes; using TBF.Resources; using TBF.UiBridge; namespace TBF.BenchControl.TestMethods.iPerlCommunication { public class iPerlCommunicationSeq : Sequences.SequenceBase { private static readonly ILog log = LogManager.GetLogger(typeof(iPerlCommunicationSeq)); System.Windows.Forms.Form modelessDlg; /// delegate void iPerlCommFormDlgt(iPerlCommunicationSeq myRef, TestMethodCfg cfg, Test test, iPerlCommunicationParams testParams); /// void OpenIPerlCommForm(iPerlCommunicationSeq myRef, TestMethodCfg cfg, Test test, iPerlCommunicationParams testParams) { myRef.modelessDlg = new iPerlCommunicationForm(cfg, test, testParams); myRef.modelessDlg.Show(); } void CloseIPerlCommForm() { UiBridge.Bridge.OnCloseModelessForm(this, null); modelessDlg = null; } /// /// 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(Config.Entities.Test test, TestMethodCfg cfg, iPerlCommunicationParams testParams) { IList e = new List(); /// Events from currently running operations Event retVal = Event.Done; checkUiOp = new Operations.CheckUIOp(true); /// Runs in more then one state modelessDlg = null; processDataLoggingOp = new TBF.BenchControl.Operations.ProcessDataLoggingOp(processDataLogger, this, false); const string Q2correctedFromCmd = "q2 corrected from "; const string Hz2correctedFromCmd = "2Hz corrected from "; if (testParams.Activity.ToLower().Contains(Q2correctedFromCmd)) { TestProgressEventArgs.SetEstimatedTimes(new int[] { 0, 0, 0, 60, 0, 0, 0 }); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.JustStarted)); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.FlowSetting)); string fromTestName = testParams.Activity.Substring(Q2correctedFromCmd.Length); MakeQ2CorrectedFrom(test.Name, fromTestName); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.Completed)); Bridge.OnTestCompleted(this, new TestCompletedEventArgs(test.Name, ProcessData.BatchRslts.GetTestRslt(test.Name, 0))); allResults.Info(TestResult2CsvLine(test.Name, 0)); /// Append the results to the CSV-file IList rList = new List(1); rList.Add(Event.Done); return rList; } //else if (testParams.Activity.ToLower().Contains(Hz2correctedFromCmd)) //{ // TestProgressEventArgs.SetEstimatedTimes(new int[] { 0, 0, 0, 60, 0, 0, 0 }); // Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.JustStarted)); // Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.FlowSetting)); // string fromTestsNames = testParams.Activity.Substring(Hz2correctedFromCmd.Length); // string[] fromTestsArr = fromTestsNames.Split(new char[] { ' ' }); // if (fromTestsArr.Length == 2) // { // //MakeCorrectedFrom(test.Name, fromTestsArr[0], fromTestsArr[1]); // Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.Completed)); // Bridge.OnTestCompleted(this, new TestCompletedEventArgs(test.Name, ProcessData.BatchRslts.GetTestRslt(test.Name, 0))); // allResults.Info(TestResult2CsvLine(test.Name, 0)); /// Append the results to the CSV-file // IList rList = new List(1); // rList.Add(Event.Done); // return rList; // } // else // { // IList rList = new List(1); // rList.Add(Event.Done); // return rList; // } //} else if (testParams.Activity.ToLower().Contains("simulate ")) { TestProgressEventArgs.SetEstimatedTimes(new int[] { 0, 0, 0, 60, 0, 0, 0 } ); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.JustStarted)); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.FlowSetting)); if (testParams.Activity.ToLower().Contains("q3")) MakeSimulated(test.Name, 1, 1, 0, -0.5f); else if (testParams.Activity.ToLower().Contains("q2")) MakeSimulated(test.Name, 1, 1, 0, 0.5f); else if (testParams.Activity.ToLower().Contains("q1")) MakeSimulated(test.Name, 1, 1, 0, -5.1f); else if (testParams.Activity.ToLower().Contains("compound ok")) MakeSimulatedCompound(test.Name, 1, 1, 0, 0.7f, 1.0f); else if (testParams.Activity.ToLower().Contains("compound nok")) MakeSimulatedCompound(test.Name, 1, 1, 0, 4.7f, 0.9f); else if (testParams.Activity.ToLower().Contains("compound rise")) MakeSimulatedCompound(test.Name, 1, 1, 0, 0.7f, 0.0f); else if (testParams.Activity.ToLower().Contains("compound fall")) MakeSimulatedCompound(test.Name, 1, 1, 0, 0.7f, 0.9f); Bridge.OnTestProgress(this, new TestProgressEventArgs(test, Config.Entities.Progress.Completed)); Bridge.OnTestCompleted(this, new TestCompletedEventArgs(test.Name, ProcessData.BatchRslts.GetTestRslt(Results.Utils.GetTestName(test.Name, 1, 1), 0))); allResults.Info(TestResult2CsvLine(test.Name, 0)); /// Append the results to the CSV-file for (int i = 0; i < Config.Data.WMsCount; i++) { WaterMeters.iPerl.WaterMeter iPerl = ((sensPath.RegisterReaders != null) && (i < sensPath.RegisterReaders.Length)) ? (sensPath.RegisterReaders[i] as WaterMeters.iPerl.WaterMeter) : null; if (iPerl != null) { /// TODO: Reimplement //iPerl.LastTestResult = meterRslt; /// Save this water meter test result to iPerl WM iPerl.NominalTestFlow = 500.0 * (double)(test.Qfrom + test.Qto); /// Ave. + convert to liter/hour } } //------------------------------------------------ Bridge.OnActivity(this, testParams.Activity); //------------------------------------------------ State.Create(string.Format("iPerlCommunicationSeq : {0}", testParams.Activity)) .AddOperation(checkUiOp) .EnterState(); e = StateMachine.WaitRunDevsRunOps(); if (TestAndLogUiCmdStop(test,e)) retVal = Event.UiCmdStop; } else { /// /// Show the modeless dialog with error indication /// Program.MainWnd.Invoke(new iPerlCommFormDlgt(OpenIPerlCommForm), new object[] { this, cfg, test, testParams }); //------------------------------------------------ Bridge.OnActivity(this, Strings.iPerl_Communication_in_progress); //------------------------------------------------ bool stopPressed = false; /// true when STOP button pressed bool completed = false; State.Create("iPerlCommunicationSeq : Wait until the entry form is closed") .AddOperation(checkUiOp) .EnterState(); do { e = StateMachine.WaitRunDevsRunOps(); stopPressed = TestAndLogUiCmdStop(test,e); completed = (modelessDlg is GenericDevices.IHasCompleted) && (modelessDlg as GenericDevices.IHasCompleted).Completed; } while (!stopPressed && !completed); if (stopPressed) { CloseIPerlCommForm(); retVal = Event.UiCmdStop; } else { TBF.UiBridge.Bridge.OnTestProgress(null, new TBF.UiBridge.TestProgressEventArgs(test.Name, Config.Entities.Progress.Completed)); } /// Test 'Quit' modelessDlg = null; /// Modeless dialog is closed now } /// Create a list with one item 'retVal' (default is Event.Done) and return it IList retList = new List(1); retList.Add(retVal); return retList; } /// /// Virtually apply Q2 correction to a test used for the correction calculation. /// /// Q2Adj test result /// Virtual Q2 test result /// Assuming this test does not have multiple parts (part = 0) void MakeQ2CorrectedFrom(string testName, string oriTestName) { Results.Entities.TestRslt oriTestRslt = ProcessData.BatchRslts.GetTestRslt(oriTestName, 0); Results.Entities.TestRslt tstRslt = ProcessData.BatchRslts.GetTestRslt(testName, 0); if (oriTestRslt == null || tstRslt == null) return; tstRslt.Components = oriTestRslt.Components; tstRslt.StartTime = oriTestRslt.StartTime; tstRslt.EndTime = oriTestRslt.EndTime; tstRslt.FlowSetTime = oriTestRslt.FlowSetTime; tstRslt.TestTime = oriTestRslt.TestTime; tstRslt.PulsesMaster = oriTestRslt.PulsesMaster; tstRslt.ConstMaster = oriTestRslt.ConstMaster; tstRslt.MassStartRaw = oriTestRslt.MassStartRaw; tstRslt.MassStart = oriTestRslt.MassStart; tstRslt.MassEndRaw = oriTestRslt.MassEndRaw; tstRslt.MassEnd = oriTestRslt.MassEnd; tstRslt.DensityIn = oriTestRslt.DensityIn; tstRslt.DensityOut = oriTestRslt.DensityOut; tstRslt.DensityDiv = oriTestRslt.DensityDiv; tstRslt.Buoyancy = oriTestRslt.Buoyancy; tstRslt.FlowMass = oriTestRslt.FlowMass; tstRslt.FlowVolume = oriTestRslt.FlowVolume; tstRslt.VolumeCTV = oriTestRslt.VolumeCTV; tstRslt.VolumeMaster = oriTestRslt.VolumeMaster; tstRslt.ErrorMaster = oriTestRslt.ErrorMaster; tstRslt.AmbientTempAve = oriTestRslt.AmbientTempAve; tstRslt.AmbientPressAve = oriTestRslt.AmbientPressAve; tstRslt.AmbientHumiAve = oriTestRslt.AmbientHumiAve; tstRslt.PressUpAvrg = oriTestRslt.PressUpAvrg; tstRslt.PressUpStart = oriTestRslt.PressUpStart; tstRslt.PressUpEnd = oriTestRslt.PressUpEnd; tstRslt.PressUpMin = oriTestRslt.PressUpMin; tstRslt.PressUpMax = oriTestRslt.PressUpMax; tstRslt.PressDownAvrg = oriTestRslt.PressDownAvrg; tstRslt.PressDownStart = oriTestRslt.PressDownStart; tstRslt.PressDownEnd = oriTestRslt.PressDownEnd; tstRslt.PressDownMin = oriTestRslt.PressDownMin; tstRslt.PressDownMax = oriTestRslt.PressDownMax; tstRslt.TempInAvrg = oriTestRslt.TempInAvrg; tstRslt.TempInStart = oriTestRslt.TempInStart; tstRslt.TempInEnd = oriTestRslt.TempInEnd; tstRslt.TempInMin = oriTestRslt.TempInMin; tstRslt.TempInMax = oriTestRslt.TempInMax; tstRslt.TempOutAvrg = oriTestRslt.TempOutAvrg; tstRslt.TempOutStart = oriTestRslt.TempOutStart; tstRslt.TempOutEnd = oriTestRslt.TempOutEnd; tstRslt.TempOutMin = oriTestRslt.TempOutMin; tstRslt.TempOutMax = oriTestRslt.TempOutMax; tstRslt.TempDivAvrg = oriTestRslt.TempDivAvrg; tstRslt.TempDivStart = oriTestRslt.TempDivStart; tstRslt.TempDivEnd = oriTestRslt.TempDivEnd; tstRslt.TempDivMin = oriTestRslt.TempDivMin; tstRslt.TempDivMax = oriTestRslt.TempDivMax; tstRslt.FlowMin = oriTestRslt.FlowMin; tstRslt.FlowMax = oriTestRslt.FlowMax; tstRslt.Custom1 = oriTestRslt.Custom1; tstRslt.Custom2 = oriTestRslt.Custom2; tstRslt.Custom3 = oriTestRslt.Custom3; tstRslt.Custom4 = oriTestRslt.Custom4; tstRslt.Custom5 = oriTestRslt.Custom5; tstRslt.Custom6 = oriTestRslt.Custom6; tstRslt.Custom7 = oriTestRslt.Custom7; tstRslt.Custom8 = oriTestRslt.Custom8; for (int i = 0; i < ProcessData.BatchRslts.WMPositionsCount; i++) { Results.Entities.MeterTestRslt oriMeterRslt = ProcessData.BatchRslts.GetMeterTestRslt(oriTestName, i, CompoundMeterId.Single); Results.Entities.MeterTestRslt meterRslt = ProcessData.BatchRslts.GetMeterTestRslt(testName, i, CompoundMeterId.Single); /// Reference to iPerl water meter or null: WaterMeters.iPerl.WaterMeter iPerl = ((sensPath.RegisterReaders != null) && (i < sensPath.RegisterReaders.Length)) ? (sensPath.RegisterReaders[i] as WaterMeters.iPerl.WaterMeter) : null; if (meterRslt != null && oriMeterRslt != null) { meterRslt.PulsesMeter = oriMeterRslt.PulsesMeter; meterRslt.PulsesMaster = oriMeterRslt.PulsesMaster; meterRslt.PulsesPerLiter = oriMeterRslt.PulsesPerLiter; meterRslt.VolumeRef = oriMeterRslt.VolumeRef; meterRslt.TestTime = oriMeterRslt.TestTime; if (iPerl == null || Math.Abs(oriMeterRslt.Error) <= 0.5f || oriMeterRslt.Error < oriTestRslt.ErrLimLo() + oriTestRslt.Uncertainty() || oriMeterRslt.Error > oriTestRslt.ErrLimHi() - oriTestRslt.Uncertainty()) { /// Normal meter or iPerl without correction meterRslt.VolumeStart = oriMeterRslt.VolumeStart; meterRslt.VolumeEnd = oriMeterRslt.VolumeEnd; meterRslt.VolumeMeter = oriMeterRslt.VolumeMeter; meterRslt.Error = oriMeterRslt.Error; meterRslt.Passed = oriMeterRslt.Passed; meterRslt.TestDone = true; } else { /// corrected iPerl meterRslt.VolumeStart = 0; meterRslt.VolumeEnd = 0; meterRslt.VolumeMeter = oriMeterRslt.VolumeRef; meterRslt.Error = 0; meterRslt.Passed = true; meterRslt.TestDone = true; } } } } } }