tbf/Results/ItemSpec.cs

197 lines
14 KiB
C#

///
/// Copyright (c) 2013-2015 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using Results.Entities;
namespace Results
{
public class ItemSpec
{
///
/// Public fields
///
public readonly string Name; /// Name-s of all items must be unique
public readonly string ClmnHeaderText; /// Text printed in the column header
///
/// Function to pick a MeterTestResult field and convert it into a string
///
public delegate string PrintDlgt(MeterTestRslt meterTestRslt);
private readonly PrintDlgt printDlgt;
public bool CanPrintSingle { get { return (printDlgt != null); } }
/// <summary> Safe wrapper which replaces null-s by empty strings </summary>
public string Print(MeterTestRslt meterTestRslt)
{
string str = printDlgt(meterTestRslt);
return (str == null) ? string.Empty : str;
}
///
/// Function to pick a MeterTestResult field and convert it into a string
///
public delegate string PrintCombinedDlgt(MeterTestRslt mainMtrTestResult,
MeterTestRslt auxMtrTestResult,
MeterTestRslt compoundMtrTestResult);
private readonly PrintCombinedDlgt printCombinedDlgt;
public bool CanPrintCombined { get { return (printCombinedDlgt != null); } }
/// <summary> Safe wrapper which replaces null-s by empty strings </summary>
public string PrintCombined(MeterTestRslt mainMtrTestResult,
MeterTestRslt auxMtrTestResult,
MeterTestRslt compoundMtrTestResult)
{
string str = printCombinedDlgt(mainMtrTestResult, auxMtrTestResult, compoundMtrTestResult);
return (str == null) ? string.Empty : str;
}
///
/// Public constructor
///
public ItemSpec(string name, string clmnHeaderText, PrintDlgt printDlgt, PrintCombinedDlgt printCombinedDlgt)
{
Name = name;
ClmnHeaderText = clmnHeaderText;
this.printDlgt = printDlgt;
this.printCombinedDlgt = printCombinedDlgt;
}
/// Static list of all available items
public static readonly IList<ItemSpec> AllItems;
/// Static constructor that initializes the list of all items
static ItemSpec()
{
AllItems = new List<ItemSpec>();
/// Common
//AllItems.Add(new ResultItemSpec("Bench ID", "Bench ID", x => x.BenchID, (x, y, z) => x.BenchID));
AllItems.Add(new ItemSpec("Test name", "Test", x => x.Name(), (x, y, z) => z.Name()));
AllItems.Add(new ItemSpec("Procedure name", "Procedure", x => x.ProcedureName(), (x, y, z) => z.ProcedureName()));
AllItems.Add(new ItemSpec("Batch number", "Batch nr.", x => x.BatchNr().ToString(), (x, y, z) => z.BatchNr().ToString()));
AllItems.Add(new ItemSpec("Test method", "Test method", x => x.Method(), (x, y, z) => z.Method()));
AllItems.Add(new ItemSpec("Density", "Density", x => x.DensityOut().ToString("F1"), (x, y, z) => z.DensityOut().ToString("F1"))); /// [kg/m3]
/// Target values
AllItems.Add(new ItemSpec("Q from", "Q from [m3/h]", x => x.Qfrom().ToString("F3"), (x, y, z) => z.Qfrom().ToString("F3")));
AllItems.Add(new ItemSpec("Q to", "Q to [m3/h]", x => x.Qto().ToString("F3"), (x, y, z) => z.Qto().ToString("F3")));
AllItems.Add(new ItemSpec("Test volume", "Test vol. [l]", x => x.TargetVolume().ToString("F1"), (x, y, z) => z.TargetVolume().ToString("F1")));
AllItems.Add(new ItemSpec("Error limit Lo", "Err.Lim.Lo [%]", x => x.ErrLimLo().ToString("F1"), (x, y, z) => z.ErrLimLo().ToString("F1")));
AllItems.Add(new ItemSpec("Error limit Hi", "Err.Lim.Hi [%]", x => x.ErrLimHi().ToString("F1"), (x, y, z) => z.ErrLimHi().ToString("F1")));
AllItems.Add(new ItemSpec("Uncertainty", "Uncertainty [%]", x => x.Uncertainty().ToString("F2"), (x, y, z) => z.Uncertainty().ToString("F2")));
/// Test results
AllItems.Add(new ItemSpec("Test start time", "T start", x => x.StartTime().ToShortTimeString(), (x, y, z) => z.StartTime().ToShortTimeString()));
AllItems.Add(new ItemSpec("Test end time", "T end", x => x.EndTime().ToShortTimeString(), (x, y, z) => z.EndTime().ToShortTimeString()));
AllItems.Add(new ItemSpec("Test time", "T [s]", x => x.TestTime.ToString("F2"), (x, y, z) => z.TestTime.ToString("F2")));
AllItems.Add(new ItemSpec("Flow", "Flow [m3/h]", x => Utils.DoubleToStr(x.FlowVolume(), 4), (x, y, z) => Utils.DoubleToStr(z.FlowVolume(), 4)));
AllItems.Add(new ItemSpec("T in", "T in", x => x.TestRslt.TempInAvrg.ToString("F2"), (x, y, z) => z.TestRslt.TempInAvrg.ToString("F2")));
AllItems.Add(new ItemSpec("T out", "T out", x => x.TestRslt.TempOutAvrg.ToString("F2"), (x, y, z) => z.TestRslt.TempOutAvrg.ToString("F2")));
AllItems.Add(new ItemSpec("T div", "T div", x => x.TestRslt.TempDivAvrg.ToString("F2"), (x, y, z) => z.TestRslt.TempDivAvrg.ToString("F2")));
AllItems.Add(new ItemSpec("P up", "P up", x => x.TestRslt.PressUpAvrg.ToString("F3"), (x, y, z) => z.TestRslt.PressUpAvrg.ToString("F3")));
AllItems.Add(new ItemSpec("P up start", "P up start", x => x.TestRslt.PressUpStart.ToString("F3"), (x, y, z) => z.TestRslt.PressUpStart.ToString("F3")));
AllItems.Add(new ItemSpec("P up end", "P up end", x => x.TestRslt.PressUpEnd.ToString("F3"), (x, y, z) => z.TestRslt.PressUpEnd.ToString("F3")));
AllItems.Add(new ItemSpec("P down", "P down", x => x.TestRslt.PressDownAvrg.ToString("F3"), (x, y, z) => z.TestRslt.PressDownAvrg.ToString("F3")));
AllItems.Add(new ItemSpec("P down start", "P down start", x => x.TestRslt.PressDownStart.ToString("F3"), (x, y, z) => z.TestRslt.PressDownStart.ToString("F3")));
AllItems.Add(new ItemSpec("P down end", "P down end", x => x.TestRslt.PressDownEnd.ToString("F3"), (x, y, z) => z.TestRslt.PressDownEnd.ToString("F3")));
AllItems.Add(new ItemSpec("Reference error", "Err.ref. [%]", x => x.TestRslt.ErrorMaster.ToString("F3"), (x, y, z) => z.TestRslt.ErrorMaster.ToString("F3")));
AllItems.Add(new ItemSpec("Ambient temperature","T amb", x => x.TestRslt.AmbientTempAve.ToString("F1"), (x, y, z) => z.TestRslt.AmbientTempAve.ToString("F1")));
AllItems.Add(new ItemSpec("Ambient pressure", "P amb", x => x.TestRslt.AmbientPressAve.ToString("F0"), (x, y, z) => z.TestRslt.AmbientPressAve.ToString("F0")));
AllItems.Add(new ItemSpec("Ambient humidity", "H amb [%]", x => x.TestRslt.AmbientHumiAve.ToString("F1"), (x, y, z) => z.TestRslt.AmbientHumiAve.ToString("F1")));
/// Single and combined meter results
AllItems.Add(new ItemSpec("Volume", "Volume [l]", x => x.VolumeMeter.ToString("F3"), (x, y, z) => z.VolumeMeter.ToString("F3")));
AllItems.Add(new ItemSpec("Reference volume", "Vol.ref. [l]", x => x.VolumeRef.ToString("F3"), (x, y, z) => z.VolumeRef.ToString("F3")));
AllItems.Add(new ItemSpec("Error", "Error [%]", x => x.Error.ToString("F2"), (x, y, z) => z.Error.ToString("F2")));
AllItems.Add(new ItemSpec("Passed", "Result", x => x.PassedColorStr(), (x, y, z) => z.PassedColorStr()));
/// Water meter info
AllItems.Add(new ItemSpec("Watermeter type", "WM Type", x => x.WaterMeterData().ProductName, (x, y, z) => z.WaterMeterData().ProductName));
AllItems.Add(new ItemSpec("Producer", "Producer", x => x.WaterMeterData().Producer, (x, y, z) => z.WaterMeterData().Producer));
AllItems.Add(new ItemSpec("Metrological class", "M.Class", x => x.WaterMeterData().MetrologicalClass, (x, y, z) => z.WaterMeterData().MetrologicalClass));
AllItems.Add(new ItemSpec("Approval info", "Approval", x => x.WaterMeterData().ApprovalInfo, (x, y, z) => z.WaterMeterData().ApprovalInfo));
AllItems.Add(new ItemSpec("Q4", "Q4", x => x.WaterMeterData().Q4_Qmax.ToString(), (x, y, z) => z.WaterMeterData().Q4_Qmax.ToString()));
AllItems.Add(new ItemSpec("Q3", "Q3", x => x.WaterMeterData().Q3_Qn.ToString(), (x, y, z) => z.WaterMeterData().Q3_Qn.ToString()));
AllItems.Add(new ItemSpec("Q2", "Q2", x => x.WaterMeterData().Q2_Qt.ToString(), (x, y, z) => z.WaterMeterData().Q2_Qt.ToString()));
AllItems.Add(new ItemSpec("Q1", "Q1", x => x.WaterMeterData().Q1_Qmin.ToString(), (x, y, z) => z.WaterMeterData().Q1_Qmin.ToString()));
AllItems.Add(new ItemSpec("Qmax", "Qmax", x => x.WaterMeterData().Q4_Qmax.ToString(), (x, y, z) => z.WaterMeterData().Q4_Qmax.ToString()));
AllItems.Add(new ItemSpec("Qn", "Qn", x => x.WaterMeterData().Q3_Qn.ToString(), (x, y, z) => z.WaterMeterData().Q3_Qn.ToString()));
AllItems.Add(new ItemSpec("Qt", "Qt", x => x.WaterMeterData().Q2_Qt.ToString(), (x, y, z) => z.WaterMeterData().Q2_Qt.ToString()));
AllItems.Add(new ItemSpec("Qmin", "Qmin", x => x.WaterMeterData().Q1_Qmin.ToString(), (x, y, z) => z.WaterMeterData().Q1_Qmin.ToString()));
/// Water meters results
AllItems.Add(new ItemSpec("Serial Nr", "s/n", x => x.WaterMeter.SerialNr, (x, y, z) => z.WaterMeter.SerialNr));
AllItems.Add(new ItemSpec("Purchase order", "PO", x => x.WaterMeter.PurchaseOrder, (x, y, z) => z.WaterMeter.PurchaseOrder));
AllItems.Add(new ItemSpec("Year of production", "Year", x => x.WaterMeter.YearOfProduction.ToString(), (x, y, z) => z.WaterMeter.YearOfProduction.ToString()));
AllItems.Add(new ItemSpec("WM Position", "Pos.", x => x.WaterMeter.WMPosition.ToString(), (x, y, z) => z.WaterMeter.WMPosition.ToString()));
/// Water meters results (single)
AllItems.Add(new ItemSpec("End state", "End state", x => x.EndState(), null));
AllItems.Add(new ItemSpec("Pulses", "Pulses", x => x.PulsesMeter.ToString(), null));
AllItems.Add(new ItemSpec("Pulses/liter", "Pulses/liter", x => x.PulsesPerLiter.ToString(), null));
AllItems.Add(new ItemSpec("Reference pulses", "Ref.pulses", x => x.PulsesMaster.ToString(), null));
/// Water meters results (combined)
AllItems.Add(new ItemSpec("Q rise", "Q rise [m3/h]", null, (x, y, z) => (z.QRise() == 0) ? "-" : Utils.DoubleToStr(z.QRise(), 4)));
AllItems.Add(new ItemSpec("Q fall", "Q fall [m3/h]", null, (x, y, z) => (z.QFall() == 0) ? "-" : Utils.DoubleToStr(z.QFall(), 4)));
AllItems.Add(new ItemSpec("Error large WM", "Error-L [%]", null, (x, y, z) => x.Error.ToString("F2")));
AllItems.Add(new ItemSpec("Error small WM", "Error-S [%]", null, (x, y, z) => y.Error.ToString("F2")));
AllItems.Add(new ItemSpec("Serial Nr large WM", "s/n", null, (x, y, z) => x.SerialNr()));
AllItems.Add(new ItemSpec("Serial Nr small WM", "s/n", null, (x, y, z) => y.SerialNr()));
AllItems.Add(new ItemSpec("End state large WM", "End state", null, (x, y, z) => x.EndState()));
AllItems.Add(new ItemSpec("End state small WM", "End state", null, (x, y, z) => y.EndState()));
/// Batch info
AllItems.Add(new ItemSpec("Batch number", "Batch#", x => x.Batch().BatchNr.ToString(), (x, y, z) => x.Batch().BatchNr.ToString()));
AllItems.Add(new ItemSpec("Test bench ID", "Bench", x => x.Batch().TestBenchId.ToString(), (x, y, z) => x.Batch().TestBenchId.ToString()));
AllItems.Add(new ItemSpec("Program version", "Ver.", x => x.Batch().ProgramVersion, (x, y, z) => x.Batch().ProgramVersion));
AllItems.Add(new ItemSpec("User name", "User", x => x.Batch().UserName, (x, y, z) => x.Batch().UserName));
AllItems.Add(new ItemSpec("User nr.", "User#", x => x.Batch().UserNumber.ToString(), (x, y, z) => x.Batch().UserNumber.ToString()));
AllItems.Add(new ItemSpec("Procedure name", "Procedure", x => x.Batch().ProcedureName, (x, y, z) => x.Batch().ProcedureName));
AllItems.Add(new ItemSpec("Watermeters", "WMs", x => x.Batch().WatermetersStr, (x, y, z) => x.Batch().WatermetersStr));
AllItems.Add(new ItemSpec("Protocol title", "Protocol", x => x.Batch().ProtocolTitle, (x, y, z) => x.Batch().ProtocolTitle));
AllItems.Add(new ItemSpec("Batch start time", "Start", x => x.Batch().StartTime.ToShortTimeString(), (x, y, z) => x.Batch().StartTime.ToShortTimeString()));
AllItems.Add(new ItemSpec("Batch end time", "End", x => x.Batch().EndTime.ToShortTimeString(), (x, y, z) => x.Batch().EndTime.ToShortTimeString()));
}
public static ItemSpec GetItem(string name)
{
foreach (var item in AllItems) if (item.Name.Equals(name)) return item;
return null;
}
public static string[] ToStrArray(IList<ItemSpec> items)
{
int count = (items != null) ? items.Count : 0;
string[] result = new string[count];
for (int i = 0; i < count; i++) result[i] = items[i].Name;
return result;
}
public static IList<ItemSpec> FromStrArray(string[] strArray)
{
IList<ItemSpec> result = new List<ItemSpec>();
if (strArray != null)
{
for (int i = 0; i < strArray.Length; i++)
{
ItemSpec item = GetItem(strArray[i]);
if (item != null) result.Add(item);
}
}
return result;
}
}
}