tbf/TestBenchFramework/BenchControl/TestMethods/iPerlCommunication/iPerlCommunicationSeq.cs

320 lines
12 KiB
C#

///
/// 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, string activity);
///
void OpenIPerlCommForm(iPerlCommunicationSeq myRef, TestMethodCfg cfg, string activity)
{
IList<GenericDevices.IWaterMeter> wMtrs = new List<GenericDevices.IWaterMeter>();
foreach (var rr in sensPath.RegisterReaders)
{
if (rr is GenericDevices.IWaterMeter) wMtrs.Add(rr as GenericDevices.IWaterMeter);
}
myRef.modelessDlg = new iPerlCommunicationForm(cfg, wMtrs, activity);
modelessDlg.Show();
}
/// <summary>
/// Flying start mass collection method sequence
/// </summary>
/// <param name="test">Test entity</param>
/// <returns>
/// 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
/// </returns>
public IList<Event> Execute(Entities.Test test, TestMethodCfg cfg, iPerlCommunicationParams testParams)
{
IList<Event> e = new List<Event>(); /// 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(transitionBefore, TransitionContext.BeforeTest))
{
case Event.Error: { retVal = Event.Error; goto stopTest; }
case Event.UiCmdStop: { retVal = Event.UiCmdStop; goto stopTest; }
}
const string Q2correctedFromCmd = "Q2 corrected from ";
if (testParams.Activity.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<Event> rList = new List<Event>(1);
rList.Add(Event.Done);
return rList;
}
else if (testParams.Activity.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
}
}
IList<Event> rList = new List<Event>(1);
rList.Add(Event.Done);
return rList;
}
else
{
///
/// Show the modeless dialog with error indication
///
Program.MainWnd.Invoke(new iPerlCommFormDlgt(OpenIPerlCommForm), new object[] { this, cfg, testParams.Activity });
}
loop:
//------------------------------------------------
Bridge.OnActivity(this, Strings.Test_in_progress);
//------------------------------------------------
/// Make sure the CycleBeginForm is closed so that water meter data (s/n) can be copied into results
if (UIFlowControl.Stop == WaitBeginFormClosed()) goto stopTest;
/// Test 'Quit'
if ((modelessDlg is GenericDevices.IHasCompleted) && !(modelessDlg as GenericDevices.IHasCompleted).Completed)
{
goto loop; /// Modeless dilaog not closed, keep looping
}
modelessDlg = null; /// Modeless dialog is closed now
stopTest:
/// Transition sequence at the end of test
/// Prevent overwriting 'retVal' in case it was set to non-default value earlier
switch (Transition(transitionAfter, TransitionContext.AfterTest))
{
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<Event> retList = new List<Event>(1);
retList.Add(retVal);
return retList;
}
/// <summary>
/// Virtually apply Q2 correction to a test used for the correction calculation.
/// </summary>
/// <param name="oriTestRslt">Q2Adj test result</param>
/// <returns>Virtual Q2 test result</returns>
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;
}
/// <summary>
/// Virtually apply Q2 correction to a test used for the correction calculation.
/// </summary>
/// <param name="oriTestRslt">Q2Adj test result</param>
/// <returns>Virtual Q2 test result</returns>
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;
}
}
}