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

279 lines
11 KiB
C#

///
/// Copyright (c) 2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using Config.Entities;
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)
{
StringBuilder sb = new StringBuilder();
IList<GenericDevices.IWaterMeter> wMtrs = new List<GenericDevices.IWaterMeter>();
foreach (var rr in sensPath.RegisterReaders)
{
if (rr is GenericDevices.IWaterMeter)
{
GenericDevices.IWaterMeter wm = rr as GenericDevices.IWaterMeter;
wMtrs.Add(wm);
sb.AppendFormat(" {0}", wm.Name);
}
}
log.ErrorFormat("OpenIPerlCommForm(wms:{0})", sb);
myRef.modelessDlg = new iPerlCommunicationForm(WaterMeters, cfg, testParams);
myRef.modelessDlg.Show();
}
void CloseIPerlCommForm()
{
UiBridge.Bridge.OnCloseModelessForm(this, null);
modelessDlg = null;
}
/// <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(Config.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
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);
TestResult oriTestResult = null;
foreach (var tr in results)
{
if ((tr.TestName == fromTestName)) { oriTestResult = tr; break; }
}
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.ToLower().Contains("simulate "))
{
TestResult tstRslt;
if (testParams.Activity.ToLower().Contains("q3")) tstRslt = MakeSimulated(test, 1, -0.5f);
else if (testParams.Activity.ToLower().Contains("q2")) tstRslt = MakeSimulated(test, 1, 0.5f);
else if (testParams.Activity.ToLower().Contains("q1")) tstRslt = MakeSimulated(test, 1, -5.1f);
else if (testParams.Activity.ToLower().Contains("compound ok")) tstRslt = MakeSimulatedCompound(test, CompoundTestType.Regular, 1, 0.7f);
else if (testParams.Activity.ToLower().Contains("compound nok")) tstRslt = MakeSimulatedCompound(test, CompoundTestType.Regular, 1, 4.7f);
else if (testParams.Activity.ToLower().Contains("compound rise")) tstRslt = MakeSimulatedCompound(test, CompoundTestType.DetectionRise, 1, 0.7f);
else if (testParams.Activity.ToLower().Contains("compound fall")) tstRslt = MakeSimulatedCompound(test, CompoundTestType.DetectionFall, 1, 0.7f);
else tstRslt = null;
AddOrOverwriteResult(tstRslt);
Bridge.OnTestCompleted(this, new TestCompletedEventArgs(tstRslt));
allResults.Info(TestResult2CsvLine(tstRslt)); /// 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)
{
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<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>
TestResult MakeCorrectedFrom(Test test, TestResult oriTestRslt)
{
TestResult tstRslt = new TestResult(test, 1, MetersKind.Single);
tstRslt.BatchNr = Program.LocalSettings.BatchNr;
tstRslt.TimeStart = oriTestRslt.TimeStart;
tstRslt.TimeEnd = oriTestRslt.TimeEnd;
tstRslt.AmbientTempAve = oriTestRslt.AmbientTempAve;
tstRslt.AmbientPressAve = oriTestRslt.AmbientPressAve;
tstRslt.AmbientHumiAve = oriTestRslt.AmbientHumiAve;
tstRslt.PressInAvrg = oriTestRslt.PressInAvrg;
tstRslt.PressInStart = oriTestRslt.PressInStart;
tstRslt.PressInEnd = oriTestRslt.PressInEnd;
tstRslt.PressOutAvrg = oriTestRslt.PressOutAvrg;
tstRslt.PressOutStart = oriTestRslt.PressOutStart;
tstRslt.PressOutEnd = oriTestRslt.PressOutEnd;
tstRslt.TempInAvrg = oriTestRslt.TempInAvrg;
tstRslt.TempInStart = oriTestRslt.TempInStart;
tstRslt.TempInEnd = oriTestRslt.TempInEnd;
tstRslt.TempOutAvrg = oriTestRslt.TempOutAvrg;
tstRslt.TempOutStart = oriTestRslt.TempOutStart;
tstRslt.TempOutEnd = oriTestRslt.TempOutEnd;
tstRslt.TempDivAvrg = oriTestRslt.TempDivAvrg;
tstRslt.TempDivStart = oriTestRslt.TempDivStart;
tstRslt.TempDivEnd = oriTestRslt.TempDivEnd;
tstRslt.MassStartRaw = oriTestRslt.MassStartRaw;
tstRslt.MassStart = oriTestRslt.MassStart;
tstRslt.MassEndRaw = oriTestRslt.MassEndRaw;
tstRslt.MassEnd = oriTestRslt.MassEnd;
tstRslt.MassDiff = 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.Time = oriTestRslt.Time;
tstRslt.ErrorMaster = oriTestRslt.ErrorMaster;
tstRslt.PulsesMaster = oriTestRslt.PulsesMaster;
tstRslt.ConstMaster = oriTestRslt.ConstMaster;
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(oriTestRslt.Meters[i].VolumeErrorPct) <= 0.5f ||
oriTestRslt.Meters[i].VolumeErrorPct < oriTestRslt.ErrLimLo ||
oriTestRslt.Meters[i].VolumeErrorPct > oriTestRslt.ErrLimHi)
{
/// 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;
}
}
}