/// /// Copyright (c) 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.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, iPerlCommunicationParams testParams); /// void OpenIPerlCommForm(iPerlCommunicationSeq myRef, TestMethodCfg cfg, iPerlCommunicationParams testParams) { IList wMtrs = new List(); foreach (var rr in sensPath.RegisterReaders) { if (rr is GenericDevices.IWaterMeter) wMtrs.Add(rr as GenericDevices.IWaterMeter); } myRef.modelessDlg = new iPerlCommunicationForm(wMtrs, cfg, 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(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); const string Q2correctedFromCmd = "q2 corrected from "; if (testParams.Activity.ToLower().Contains(Q2correctedFromCmd)) { string fromTestName = testParams.Activity.Substring(Q2correctedFromCmd.Length); Entities.TestResult oriTestResult = null; foreach (var tr in results) { if ((tr.TestName == fromTestName)) { oriTestResult = tr; break; } } Entities.TestResult tstRslt = MakeCorrectedFrom(test, oriTestResult); AddOrOverwriteResult(tstRslt); Bridge.OnTestCompleted(this, new TestCompletedEventArgs(tstRslt)); allResults.Info(TestResult2CsvLine(tstRslt)); /// Append the results to the CSV-file IList rList = new List(1); rList.Add(Event.Done); return rList; } else if (testParams.Activity.ToLower().Contains("simulate ")) { Entities.TestResult tstRslt; if (testParams.Activity.Contains("Q3")) tstRslt = MakeSimulated(test, 1, 0.5f); else if (testParams.Activity.Contains("Q2")) tstRslt = MakeSimulated(test, 1, 1.7f); else tstRslt = MakeSimulated(test, 1, 2.2f); /// Q1 AddOrOverwriteResult(tstRslt); Bridge.OnTestCompleted(this, new TestCompletedEventArgs(tstRslt)); allResults.Info(TestResult2CsvLine(tstRslt)); /// Append the results to the CSV-file for (int i = 0; i < Program.WMsCount; i++) { WaterMeters.iPerl.WaterMeter iPerl = ((sensPath.RegisterReaders != null) && (i < sensPath.RegisterReaders.Length)) ? (sensPath.RegisterReaders[i] as WaterMeters.iPerl.WaterMeter) : null; if (iPerl != null) { iPerl.LastTestResult = tstRslt.Meters[i]; /// 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 (e.Contains(Event.UiCmdStop)) retVal = Event.UiCmdStop; } else { /// /// Show the modeless dialog with error indication /// Program.MainWnd.Invoke(new iPerlCommFormDlgt(OpenIPerlCommForm), new object[] { this, cfg, 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 = e.Contains(Event.UiCmdStop); completed = (modelessDlg is GenericDevices.IHasCompleted) && (modelessDlg as GenericDevices.IHasCompleted).Completed; } while (!stopPressed && !completed); if (stopPressed) { CloseIPerlCommForm(); retVal = Event.UiCmdStop; } /// 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 Entities.TestResult MakeCorrectedFrom(Entities.Test test, Entities.TestResult oriTestRslt) { Entities.TestResult tstRslt = new Entities.TestResult(test, 1, Entities.MetersKind.Single); tstRslt.BatchNr = Program.LocalSettings.BatchNr; tstRslt.MassStart = oriTestRslt.MassStart; tstRslt.MassEnd = oriTestRslt.MassEnd; tstRslt.MassDiff = oriTestRslt.MassEnd; tstRslt.DensityIn = oriTestRslt.DensityIn; tstRslt.DensityOut = oriTestRslt.DensityOut; tstRslt.DensityDiv = oriTestRslt.DensityDiv; tstRslt.Time = oriTestRslt.Time; tstRslt.FlowMass = oriTestRslt.FlowMass; tstRslt.FlowVolume = oriTestRslt.FlowVolume; tstRslt.VolumeCTV = oriTestRslt.VolumeCTV; tstRslt.VolumeMaster = oriTestRslt.VolumeMaster; tstRslt.PulsesMaster = oriTestRslt.PulsesMaster; tstRslt.ConstMaster = oriTestRslt.ConstMaster; tstRslt.ErrorMaster = oriTestRslt.ErrorMaster; tstRslt.TimeDivStart0 = oriTestRslt.TimeDivStart0; tstRslt.TimeDivStart1 = oriTestRslt.TimeDivStart1; tstRslt.TimeDivStart2 = oriTestRslt.TimeDivStart2; tstRslt.TimeDivStart3 = oriTestRslt.TimeDivStart3; tstRslt.TimeDivStart4 = oriTestRslt.TimeDivStart4; tstRslt.TimeDivStart5 = oriTestRslt.TimeDivStart5; tstRslt.TimeDivEnd0 = oriTestRslt.TimeDivEnd0; tstRslt.TimeDivEnd1 = oriTestRslt.TimeDivEnd1; tstRslt.TimeDivEnd2 = oriTestRslt.TimeDivEnd2; tstRslt.TimeDivEnd3 = oriTestRslt.TimeDivEnd3; tstRslt.TimeDivEnd4 = oriTestRslt.TimeDivEnd4; tstRslt.TimeDivEnd5 = oriTestRslt.TimeDivEnd5; for (int i = 0; i < Math.Min(tstRslt.Meters.Count, oriTestRslt.Meters.Count); i++) { /// 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; tstRslt.Meters[i].SerialNr = oriTestRslt.Meters[i].SerialNr; tstRslt.Meters[i].PulsesPerLiter = oriTestRslt.Meters[i].PulsesPerLiter; tstRslt.Meters[i].VolumeRef = oriTestRslt.Meters[i].VolumeRef; tstRslt.Meters[i].Time = oriTestRslt.Meters[i].Time; tstRslt.Meters[i].TimeStart = oriTestRslt.Meters[i].TimeStart; tstRslt.Meters[i].TimeEnd = oriTestRslt.Meters[i].TimeEnd; tstRslt.Meters[i].PulsesMeter = oriTestRslt.Meters[i].PulsesMeter; tstRslt.Meters[i].PulsesMaster = oriTestRslt.Meters[i].PulsesMaster; tstRslt.Meters[i].Disabled = oriTestRslt.Meters[i].Disabled; if ((iPerl == null) || (Math.Abs(tstRslt.Meters[i].VolumeErrorPct) <= 0.5f)) { /// Normal meter or iPerl without correction tstRslt.Meters[i].VolumeStart = oriTestRslt.Meters[i].VolumeStart; tstRslt.Meters[i].VolumeEnd = oriTestRslt.Meters[i].VolumeEnd; tstRslt.Meters[i].VolumeMeter = oriTestRslt.Meters[i].VolumeMeter; tstRslt.Meters[i].VolumeErrorPct = oriTestRslt.Meters[i].VolumeErrorPct; tstRslt.Meters[i].Passed = oriTestRslt.Meters[i].Passed; } else { /// corrected iPerl tstRslt.Meters[i].VolumeStart = 0; tstRslt.Meters[i].VolumeEnd = 0; tstRslt.Meters[i].VolumeMeter = oriTestRslt.Meters[i].VolumeRef; tstRslt.Meters[i].VolumeErrorPct = 0; tstRslt.Meters[i].Passed = true; } } return tstRslt; } /// /// Virtually apply Q2 correction to a test used for the correction calculation. /// /// Q2Adj test result /// Virtual Q2 test result Entities.TestResult MakeSimulated(Entities.Test test, int repetitionNr, float errorPct) { Entities.TestResult tstRslt = new Entities.TestResult(test, repetitionNr, Entities.MetersKind.Single); tstRslt.TimeStart = DateTime.Now; tstRslt.TimeEnd = DateTime.Now; tstRslt.AmbientTempAve = 20.0f; tstRslt.AmbientPressAve = 1000.0f; tstRslt.AmbientHumiAve = 50.0f; tstRslt.PressInStart = 1000.0f; tstRslt.PressOutStart = 1000.0f; tstRslt.TempInStart = 20.0f; tstRslt.TempOutStart = 20.0f; tstRslt.TempDivStart = 20.0f; tstRslt.PressInEnd = 1000.0f; tstRslt.PressOutEnd = 1000.0f; tstRslt.TempInEnd = 20.0f; tstRslt.TempOutEnd = 20.0f; tstRslt.TempDivEnd = 20.0f; tstRslt.PressInAvrg = 1000.0f; tstRslt.PressOutAvrg = 1000.0f; tstRslt.TempInAvrg = 20.0f; tstRslt.TempOutAvrg = 20.0f; tstRslt.TempDivAvrg = 20.0f; tstRslt.BatchNr = Program.LocalSettings.BatchNr; tstRslt.MassStartRaw = 0; tstRslt.MassStart = 0; tstRslt.MassEndRaw = test.Volume * Program.LocalSettings.RealDensity / 1000.0f; tstRslt.MassEnd = tstRslt.MassEndRaw; tstRslt.MassDiff = tstRslt.MassEnd; tstRslt.DensityIn = Program.LocalSettings.RealDensity; tstRslt.DensityOut = Program.LocalSettings.RealDensity; tstRslt.DensityDiv = Program.LocalSettings.RealDensity; tstRslt.Time = test.TstTime; tstRslt.FlowMass = tstRslt.MassDiff / test.TstTime; tstRslt.FlowVolume = test.Volume / test.TstTime; tstRslt.VolumeCTV = test.Volume; tstRslt.VolumeMaster = test.Volume; tstRslt.PulsesMaster = (ltrPerRefPulse > 1E-6) ? (test.Volume / ltrPerRefPulse) : 1; tstRslt.ConstMaster = ltrPerRefPulse; tstRslt.ErrorMaster = 0; for (int i = 0; i < Program.WMsCount; i++) { /// 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 ((sensPath.RegisterReaders[i] != null) && (WaterMeters.Count > i) && !WaterMeters[i].Disabled) { tstRslt.Meters[i].SerialNr = (WaterMeters.Count > i) ? WaterMeters[i].SerialNr : string.Empty; tstRslt.Meters[i].PulsesPerLiter = sensPath.RegisterReaders[i].PulsesPerLtr; tstRslt.Meters[i].VolumeRef = test.Volume; tstRslt.Meters[i].Time = test.TstTime; tstRslt.Meters[i].TimeStart = 0; tstRslt.Meters[i].TimeEnd = test.TstTime; tstRslt.Meters[i].PulsesMaster = tstRslt.PulsesMaster; tstRslt.Meters[i].VolumeStart = 0; tstRslt.Meters[i].VolumeErrorPct = errorPct; tstRslt.Meters[i].Passed = (test.ErrLimLo <= errorPct) && (errorPct <= test.ErrLimLo); tstRslt.Meters[i].VolumeMeter = test.Volume * (1.0f + 0.01f * errorPct); tstRslt.Meters[i].PulsesMeter = sensPath.RegisterReaders[i].PulsesPerLtr * tstRslt.Meters[i].VolumeMeter; tstRslt.Meters[i].Disabled = false; if (iPerl == null) { tstRslt.Meters[i].VolumeEnd = 0; /// Normal meter } else { /// iPerl tstRslt.Meters[i].VolumeEnd = tstRslt.Meters[i].VolumeStart + tstRslt.Meters[i].VolumeMeter; } } } return tstRslt; } } }