473 lines
19 KiB
C#
473 lines
19 KiB
C#
///
|
|
/// Copyright (c) 2015-2023 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using Common;
|
|
|
|
namespace Results.Entities
|
|
{
|
|
public class Batch
|
|
{
|
|
public virtual int Id { get; protected set; }
|
|
public virtual int BatchNr { get; set; }
|
|
public virtual string ProgramVersion { get; set; }
|
|
public virtual int TestBenchId { get; set; }
|
|
public virtual string TestBenchName { get; set; } /// CEVAK, not mapped to DB
|
|
public virtual string Address1 { get; set; } /// CEVAK, not mapped to DB
|
|
public virtual string Address2 { get; set; } /// CEVAK, not mapped to DB
|
|
public virtual string Address3 { get; set; } /// CEVAK, not mapped to DB
|
|
public virtual string Address4 { get; set; } /// CEVAK, not mapped to DB
|
|
public virtual string Address5 { get; set; } /// CEVAK, not mapped to DB
|
|
public virtual string UserName { get; set; }
|
|
public virtual int UserNumber { get; set; }
|
|
public virtual string ProcedureName { get; set; }
|
|
public virtual bool IsRemoteProcedure { get; set; } /// Stara Tura, not mapped to DB
|
|
public virtual string PurchaseOrder { get; set; } /// Stara Tura, Nanjing, not mapped to DB
|
|
public virtual string Workflow { get; set; } /// Stara Tura, Nanjing, not mapped to DB
|
|
public virtual string ProcedureDescription { get; set; } /// CEVAK, not mapped to DB
|
|
public virtual int ProcedureRevision { get; set; }
|
|
public virtual string WatermetersStr { get; set; } /// CEVAK, not mapped to DB
|
|
public virtual string ProtocolTitle { get; set; }
|
|
public virtual string Remark { get; set; }
|
|
public virtual double RealDensity { get; set; } /// Density correction in [kg/m3]
|
|
public virtual double AtTemperature { get; set; }
|
|
public virtual double Buoyancy { get; set; }
|
|
public virtual int Counter6 { get; set; }
|
|
public virtual int Counter7 { get; set; }
|
|
public virtual int Counter8 { get; set; }
|
|
public virtual int Counter9 { get; set; }
|
|
public virtual int Counter10 { get; set; }
|
|
public virtual DateTime StartTime { get; set; }
|
|
public virtual DateTime EndTime { get; set; }
|
|
public virtual DateTime SaveTime { get; set; }
|
|
public virtual bool Dirty { get; set; }
|
|
public virtual bool RsltsSent { get; set; }
|
|
public virtual bool RsltsPrinted { get; set; }
|
|
public virtual IList<TestData> Tests { get; set; }
|
|
public virtual IList<WaterMeter> WaterMeters { get; set; }
|
|
public virtual IList<TestRslt> TestRslts { get; set; }
|
|
public virtual bool Compound { get; set; } /// Not mapped to DB now
|
|
public virtual bool HeatMeter { get; set; } /// Not mapped to DB now
|
|
|
|
public virtual IList<TestData> RegularTests()
|
|
{
|
|
IList<TestData> rslt = new List<TestData>();
|
|
foreach (var td in Tests)
|
|
{
|
|
if ((td.Name.Length > 0 && td.Name[0] != '[') || !td.Name.Contains("]")) rslt.Add(td);
|
|
}
|
|
return rslt;
|
|
}
|
|
|
|
public virtual IList<TestRslt> RegularTestRslts()
|
|
{
|
|
IList<TestRslt> rslt = new List<TestRslt>();
|
|
foreach (var tr in TestRslts)
|
|
{
|
|
if ((tr.Name().Length > 0 && tr.Name()[0] != '[') || !tr.Name().Contains("]")) rslt.Add(tr);
|
|
}
|
|
return rslt;
|
|
}
|
|
|
|
public virtual IList<TestData> AutoTests(string param)
|
|
{
|
|
int len = param.Length;
|
|
IList<TestData> rslt = new List<TestData>();
|
|
foreach (var td in Tests)
|
|
{
|
|
if (td.Name.Length > len + 1 && td.Name[0] == '[' && td.Name.IndexOf(param) == 1 && td.Name[len + 1] == ']')
|
|
{
|
|
rslt.Add(td);
|
|
}
|
|
}
|
|
return rslt;
|
|
}
|
|
|
|
public virtual IList<TestRslt> AutoTestRslts(string param)
|
|
{
|
|
int len = param.Length;
|
|
IList<TestRslt> rslt = new List<TestRslt>();
|
|
foreach (var tr in TestRslts)
|
|
{
|
|
if (tr.Name().Length > len + 1 && tr.Name()[0] == '[' && tr.Name().IndexOf(param) == 1 && tr.Name()[len + 1] == ']')
|
|
{
|
|
rslt.Add(tr);
|
|
}
|
|
}
|
|
return rslt;
|
|
}
|
|
|
|
|
|
public Batch()
|
|
{
|
|
Tests = new List<TestData>();
|
|
WaterMeters = new List<WaterMeter>();
|
|
TestRslts = new List<TestRslt>();
|
|
|
|
ProgramVersion = string.Empty;
|
|
TestBenchName = string.Empty;
|
|
Address1 = string.Empty;
|
|
Address2 = string.Empty;
|
|
Address3 = string.Empty;
|
|
Address4 = string.Empty;
|
|
Address5 = string.Empty;
|
|
UserName = string.Empty;
|
|
ProcedureName = string.Empty;
|
|
IsRemoteProcedure = false;
|
|
ProcedureDescription = string.Empty;
|
|
WatermetersStr = string.Empty;
|
|
ProtocolTitle = string.Empty;
|
|
ProtocolTitle = string.Empty;
|
|
Remark = string.Empty;
|
|
RealDensity = Config.Data.RealDensity;
|
|
AtTemperature = Config.Data.AtTemperature;
|
|
Buoyancy = Config.Data.Buoyancy;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copy constructor, copies all except of Id.
|
|
/// Lists Tests, WaterMeters and TestRslts are empty. Have to be copied elwhere.
|
|
/// </summary>
|
|
public Batch(Batch oriBatch)
|
|
{
|
|
BatchNr = oriBatch.BatchNr;
|
|
ProgramVersion = oriBatch.ProgramVersion;
|
|
TestBenchId = oriBatch.TestBenchId;
|
|
TestBenchName = oriBatch.TestBenchName;
|
|
Address1 = oriBatch.Address1;
|
|
Address2 = oriBatch.Address2;
|
|
Address3 = oriBatch.Address3;
|
|
Address4 = oriBatch.Address4;
|
|
Address5 = oriBatch.Address5;
|
|
UserName = oriBatch.UserName;
|
|
UserNumber = oriBatch.UserNumber;
|
|
ProcedureName = oriBatch.ProcedureName;
|
|
IsRemoteProcedure = oriBatch.IsRemoteProcedure;
|
|
ProcedureDescription = oriBatch.ProcedureDescription;
|
|
ProcedureRevision = oriBatch.ProcedureRevision;
|
|
WatermetersStr = oriBatch.WatermetersStr;
|
|
ProtocolTitle = oriBatch.ProtocolTitle;
|
|
Remark = oriBatch.Remark;
|
|
RealDensity = oriBatch.RealDensity;
|
|
AtTemperature = oriBatch.AtTemperature;
|
|
Buoyancy = oriBatch.Buoyancy;
|
|
Counter6 = oriBatch.Counter6;
|
|
Counter7 = oriBatch.Counter7;
|
|
Counter8 = oriBatch.Counter8;
|
|
Counter9 = oriBatch.Counter9;
|
|
Counter10 = oriBatch.Counter10;
|
|
StartTime = oriBatch.StartTime;
|
|
EndTime = oriBatch.EndTime;
|
|
SaveTime = oriBatch.SaveTime;
|
|
Dirty = oriBatch.Dirty;
|
|
RsltsSent = oriBatch.RsltsSent;
|
|
RsltsPrinted = oriBatch.RsltsPrinted;
|
|
|
|
Tests = new List<TestData>();
|
|
WaterMeters = new List<WaterMeter>();
|
|
TestRslts = new List<TestRslt>();
|
|
}
|
|
|
|
public virtual int PassedMetersCount()
|
|
{
|
|
int count = 0;
|
|
foreach (var mtr in WaterMeters) if (mtr.PassedFromTests()) count++;
|
|
return count;
|
|
}
|
|
|
|
public virtual int FailedMetersCount()
|
|
{
|
|
return WaterMeters.Count - PassedMetersCount();
|
|
}
|
|
|
|
public virtual long ErrorFlags()
|
|
{
|
|
long result = 0;
|
|
foreach (var tr in TestRslts) { result |= tr.ErrorFlags; }
|
|
foreach (var wm in WaterMeters)
|
|
{
|
|
if (!wm.Disabled) { result |= wm.ErrorFlags; }
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public virtual long InfoFlags()
|
|
{
|
|
long result = 0;
|
|
foreach (var tr in TestRslts) { result |= tr.InfoFlags; }
|
|
foreach (var wm in WaterMeters)
|
|
{
|
|
if (!wm.Disabled) { result |= wm.InfoFlags; }
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public virtual double AmbTempMean()
|
|
{
|
|
double timeSum = 0;
|
|
double sum = 0;
|
|
|
|
foreach (var tr in TestRslts)
|
|
{
|
|
if (tr.TestDone && tr.AmbTempMean != 0)
|
|
{
|
|
double duration = (tr.EndTime - tr.StartTime).TotalSeconds;
|
|
timeSum += duration;
|
|
sum += (tr.AmbTempMean * duration);
|
|
}
|
|
}
|
|
|
|
return (timeSum == 0) ? 0 : (sum / timeSum);
|
|
}
|
|
|
|
public virtual double AmbPressMean()
|
|
{
|
|
double timeSum = 0;
|
|
double sum = 0;
|
|
|
|
foreach (var tr in TestRslts)
|
|
{
|
|
if (tr.TestDone && tr.AmbPressMean != 0)
|
|
{
|
|
double duration = (tr.EndTime - tr.StartTime).TotalSeconds;
|
|
timeSum += duration;
|
|
sum += (tr.AmbPressMean * duration);
|
|
}
|
|
}
|
|
|
|
return (timeSum == 0) ? 0 : (sum / timeSum);
|
|
}
|
|
|
|
public virtual double AmbHumiMean()
|
|
{
|
|
double timeSum = 0;
|
|
double sum = 0;
|
|
|
|
foreach (var tr in TestRslts)
|
|
{
|
|
if (tr.TestDone && tr.AmbHumiMean != 0)
|
|
{
|
|
double duration = (tr.EndTime - tr.StartTime).TotalSeconds;
|
|
timeSum += duration;
|
|
sum += (tr.AmbHumiMean * duration);
|
|
}
|
|
}
|
|
|
|
return (timeSum == 0) ? 0 : (sum / timeSum);
|
|
}
|
|
|
|
public virtual double AmbTempStart()
|
|
{
|
|
for (int i = 0; i < TestRslts.Count; i++)
|
|
{
|
|
if (TestRslts[i].Publish() == Common.Publish.Always && TestRslts[i].AmbTempStart != 0)
|
|
return TestRslts[i].AmbTempStart;
|
|
}
|
|
return 0;
|
|
}
|
|
public virtual double AmbPressStart()
|
|
{
|
|
for (int i = 0; i < TestRslts.Count; i++)
|
|
{
|
|
if (TestRslts[i].Publish() == Common.Publish.Always && TestRslts[i].AmbPressStart != 0)
|
|
return TestRslts[i].AmbPressStart;
|
|
}
|
|
return 0;
|
|
}
|
|
public virtual double AmbHumiStart()
|
|
{
|
|
for (int i = 0; i < TestRslts.Count; i++)
|
|
{
|
|
if (TestRslts[i].Publish() == Common.Publish.Always && TestRslts[i].AmbHumiStart != 0)
|
|
return TestRslts[i].AmbHumiStart;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public virtual double AmbTempEnd()
|
|
{
|
|
for (int i = TestRslts.Count - 1; i >= 0; i--)
|
|
{
|
|
if (TestRslts[i].Publish() == Common.Publish.Always && TestRslts[i].AmbTempEnd != 0)
|
|
return TestRslts[i].AmbTempEnd;
|
|
}
|
|
return 0;
|
|
}
|
|
public virtual double AmbPressEnd()
|
|
{
|
|
for (int i = TestRslts.Count - 1; i >= 0; i--)
|
|
{
|
|
if (TestRslts[i].Publish() == Common.Publish.Always && TestRslts[i].AmbPressEnd != 0)
|
|
return TestRslts[i].AmbPressEnd;
|
|
}
|
|
return 0;
|
|
}
|
|
public virtual double AmbHumiEnd()
|
|
{
|
|
for (int i = TestRslts.Count - 1; i >= 0; i--)
|
|
{
|
|
if (TestRslts[i].Publish() == Common.Publish.Always && TestRslts[i].AmbHumiEnd != 0)
|
|
return TestRslts[i].AmbHumiEnd;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public virtual TestRslt GetTestRslt(string name, int part)
|
|
{
|
|
foreach (var tr in TestRslts)
|
|
{
|
|
if (part == tr.Part && name.Equals(tr.Name())) return tr;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return string.Format("Batch {0} ({1}s) Start {2}, End {3}, Proc={4}",
|
|
BatchNr, (EndTime - StartTime).TotalSeconds, StartTime, EndTime, ProcedureName);
|
|
}
|
|
|
|
public virtual string ToString(int i)
|
|
{
|
|
return string.Format("Batch {0} ({1}s) ver={2} TestBenchId={3} User={4}/{5} Proc={6}{7} Rev={8} Remark={9} RealDensity={10} AtTemperature={11} Buoyancy={12} StartTime={13} EndTime={14} SaveTime={15} RsltsSent={16} RsltsPrinted={17}",
|
|
BatchNr,
|
|
(EndTime - StartTime).TotalSeconds,
|
|
ProgramVersion, TestBenchId, UserName, UserNumber,
|
|
ProcedureName, (IsRemoteProcedure ? "(R)" : ""), ProcedureRevision,
|
|
Remark, RealDensity, AtTemperature, Buoyancy,
|
|
StartTime, EndTime, SaveTime, RsltsSent, RsltsPrinted);
|
|
}
|
|
|
|
public virtual void WriteBinary(BinaryWriter writer)
|
|
{
|
|
writer.Write(BatchNr);
|
|
writer.Write((ProgramVersion != null) ? ProgramVersion : string.Empty);
|
|
writer.Write((ProcedureName != null) ? ProcedureName : string.Empty);
|
|
writer.Write(IsRemoteProcedure);
|
|
writer.Write(Id);
|
|
writer.Write(TestBenchId);
|
|
writer.Write((TestBenchName != null) ? TestBenchName : string.Empty);
|
|
writer.Write((Address1 != null) ? Address1 : string.Empty);
|
|
writer.Write((Address2 != null) ? Address2 : string.Empty);
|
|
writer.Write((Address3 != null) ? Address3 : string.Empty);
|
|
writer.Write((Address4 != null) ? Address4 : string.Empty);
|
|
writer.Write((Address5 != null) ? Address5 : string.Empty);
|
|
writer.Write((UserName != null) ? UserName : string.Empty);
|
|
writer.Write(UserNumber);
|
|
writer.Write((ProcedureDescription != null) ? ProcedureDescription : string.Empty);
|
|
writer.Write(ProcedureRevision);
|
|
writer.Write((WatermetersStr != null) ? WatermetersStr : string.Empty);
|
|
writer.Write((ProtocolTitle != null) ? ProtocolTitle : string.Empty);
|
|
writer.Write((Remark != null) ? Remark : string.Empty);
|
|
writer.Write(RealDensity);
|
|
writer.Write(AtTemperature);
|
|
writer.Write(Buoyancy);
|
|
writer.Write(Counter6);
|
|
writer.Write(Counter7);
|
|
writer.Write(Counter8);
|
|
writer.Write(Counter9);
|
|
writer.Write(Counter10);
|
|
writer.Write(StartTime.ToString());
|
|
writer.Write(EndTime.ToString());
|
|
writer.Write(SaveTime.ToString());
|
|
writer.Write(Dirty);
|
|
writer.Write(RsltsSent);
|
|
writer.Write(RsltsPrinted);
|
|
writer.Write(Compound);
|
|
writer.Write(HeatMeter);
|
|
|
|
IList<TestData> savedTestDatas = new List<TestData>(); /// Initially empty
|
|
writer.Write(TestRslts.Count);
|
|
for (int i = 0; i < TestRslts.Count; i++)
|
|
{
|
|
TestRslts[i].WriteBinary(writer, savedTestDatas);
|
|
}
|
|
|
|
IList<WaterMeterData> savedWMDatas = new List<WaterMeterData>(); /// Initially empty
|
|
writer.Write(WaterMeters.Count);
|
|
for (int i = 0; i < WaterMeters.Count; i++)
|
|
{
|
|
WaterMeters[i].WriteBinary(writer, savedWMDatas);
|
|
}
|
|
}
|
|
|
|
public static void ReadStart(BinaryReader reader, out int batchNr, out string programVersion, out string procedureName, out bool isRemoteProcedure)
|
|
{
|
|
batchNr = reader.ReadInt32();
|
|
programVersion = reader.ReadString();
|
|
procedureName = reader.ReadString();
|
|
isRemoteProcedure = reader.ReadBoolean();
|
|
}
|
|
|
|
public virtual void ReadBinary(BinaryReader reader)
|
|
{
|
|
/// ReadStart() is used to avoid code repetition
|
|
int batchNr;
|
|
string programVersion;
|
|
string procedureName;
|
|
bool isRemoteProcedure;
|
|
ReadStart(reader, out batchNr, out programVersion, out procedureName, out isRemoteProcedure);
|
|
|
|
BatchNr = batchNr;
|
|
ProgramVersion = programVersion;
|
|
ProcedureName = procedureName;
|
|
IsRemoteProcedure = isRemoteProcedure;
|
|
Id = reader.ReadInt32();
|
|
TestBenchId = reader.ReadInt32();
|
|
TestBenchName = reader.ReadString();
|
|
Address1 = reader.ReadString();
|
|
Address2 = reader.ReadString();
|
|
Address3 = reader.ReadString();
|
|
Address4 = reader.ReadString();
|
|
Address5 = reader.ReadString();
|
|
UserName = reader.ReadString();
|
|
UserNumber = reader.ReadInt32();
|
|
ProcedureDescription = reader.ReadString();
|
|
ProcedureRevision = reader.ReadInt32();
|
|
WatermetersStr = reader.ReadString();
|
|
ProtocolTitle = reader.ReadString();
|
|
Remark = reader.ReadString();
|
|
RealDensity = reader.ReadDouble();
|
|
AtTemperature = reader.ReadDouble();
|
|
Buoyancy = reader.ReadDouble();
|
|
Counter6 = reader.ReadInt32();
|
|
Counter7 = reader.ReadInt32();
|
|
Counter8 = reader.ReadInt32();
|
|
Counter9 = reader.ReadInt32();
|
|
Counter10 = reader.ReadInt32();
|
|
StartTime = DateTime.Parse(reader.ReadString());
|
|
EndTime = DateTime.Parse(reader.ReadString());
|
|
SaveTime = DateTime.Parse(reader.ReadString());
|
|
Dirty = reader.ReadBoolean();
|
|
RsltsSent = reader.ReadBoolean();
|
|
RsltsPrinted = reader.ReadBoolean();
|
|
Compound = reader.ReadBoolean();
|
|
HeatMeter = reader.ReadBoolean();
|
|
|
|
Tests = new List<TestData>(); /// Initially empty
|
|
|
|
int testRsltsCount = reader.ReadInt32();
|
|
TestRslts = new List<TestRslt>();
|
|
for (int i = 0; i < testRsltsCount; i++)
|
|
{
|
|
TestRslts.Add(new TestRslt());
|
|
TestRslts[i].ReadBinary(reader, Tests);
|
|
TestRslts[i].Batch = this;
|
|
}
|
|
|
|
IList<WaterMeterData> wmDatasRetirevedSoFar = new List<WaterMeterData>(); /// Initially empty
|
|
|
|
int waterMetersCount = reader.ReadInt32();
|
|
WaterMeters = new List<WaterMeter>();
|
|
for (int i = 0; i < waterMetersCount; i++)
|
|
{
|
|
WaterMeters.Add(new WaterMeter());
|
|
WaterMeters[i].ReadBinary(reader, wmDatasRetirevedSoFar, TestRslts);
|
|
WaterMeters[i].Batch = this;
|
|
}
|
|
}
|
|
}
|
|
}
|