Output.DB.SaveFlowmeterCorrections component completed.
This commit is contained in:
parent
cb62b0eb3a
commit
567ea533a9
@ -214,6 +214,20 @@ namespace TBF.BenchControl
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Converts a measurement error to a correction (used when preparing correction tables).
|
||||
/// </summary>
|
||||
/// <param name="measuredValue">Measured value (in arbitrary units)</param>
|
||||
/// <param name="error">Measurement error in %</param>
|
||||
/// <returns>Correction in the same units as the measured value</returns>
|
||||
public static double CorrectionFromError(double measuredValue, double error)
|
||||
{
|
||||
double trueValue = measuredValue / (1 + error/100);
|
||||
double correction = trueValue - measuredValue;
|
||||
return correction;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the heat coefficient for water
|
||||
/// </summary>
|
||||
|
||||
@ -5,6 +5,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using log4net;
|
||||
using NHibernate;
|
||||
using NHibernate.Criterion;
|
||||
using Config.Entities;
|
||||
using MonitoringDB.Entities;
|
||||
|
||||
@ -16,13 +17,12 @@ namespace TBF.BenchControl.Output.DB.SaveFlowmeterCorrections
|
||||
Error,
|
||||
}
|
||||
|
||||
public class SaveFlowmeterCorr : ComponentBase, IOperation, GenericDevices.IResultsWriter, Generic.IDevice
|
||||
public class SaveFlowmeterCorr : ComponentBase, IOperation, GenericDevices.IResultsWriter
|
||||
{
|
||||
private static readonly ILog log = LogManager.GetLogger(typeof(SaveFlowmeterCorr));
|
||||
public override string ToString() { return string.Format("Monitoring({0})", Cfg.ToString(1)); }
|
||||
public override string ToString() { return string.Format("SaveFlowmeterCorr({0})", Cfg.ToString(1)); }
|
||||
|
||||
SaveFlowmeterCorrCfg monitoringCfg;
|
||||
string workplace { get { return monitoringCfg.Flowmeter; } }
|
||||
SaveFlowmeterCorrCfg myCfg;
|
||||
|
||||
enum CurrentOp
|
||||
{
|
||||
@ -35,14 +35,10 @@ namespace TBF.BenchControl.Output.DB.SaveFlowmeterCorrections
|
||||
bool anyError;
|
||||
|
||||
/// <summary>
|
||||
/// Results to print
|
||||
/// Results to parse for reference flowmeter corrections
|
||||
/// </summary>
|
||||
Results.Entities.Batch batch;
|
||||
|
||||
ISessionFactory sessionFactory; /// Factory to create database sessions initialized in the constructor
|
||||
IList<Process> processes;
|
||||
Dictionary<int, Process> processDictionary;
|
||||
Dictionary<int, IList<Workstep>> workstepsDictionary;
|
||||
IList<MeasurementCorrection> newCorrections;
|
||||
|
||||
|
||||
public SaveFlowmeterCorr() {}
|
||||
@ -50,25 +46,12 @@ namespace TBF.BenchControl.Output.DB.SaveFlowmeterCorrections
|
||||
public SaveFlowmeterCorr(Generic.IComponentCfg cfg)
|
||||
: base(cfg)
|
||||
{
|
||||
monitoringCfg = cfg as SaveFlowmeterCorrCfg;
|
||||
myCfg = cfg as SaveFlowmeterCorrCfg;
|
||||
currentOp = CurrentOp.None;
|
||||
|
||||
log.Debug(this.ToString());
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
if (monitoringCfg.DebugLevel == DebugMode.Simulate) return;
|
||||
|
||||
/// TODO
|
||||
}
|
||||
|
||||
|
||||
public void RunDeviceBefore() { }
|
||||
public void RunDeviceAfter() {}
|
||||
public void StopDevice() {}
|
||||
public void StopDevice2() {}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Writes the test cycle results into a file, Events: Event.ResultsWritten
|
||||
@ -88,18 +71,59 @@ namespace TBF.BenchControl.Output.DB.SaveFlowmeterCorrections
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Start this operation</summary>
|
||||
public void Start()
|
||||
{
|
||||
opCompleted = false;
|
||||
anyError = false;
|
||||
|
||||
IList<MeasurementCorrection> GetCorrections(Results.Entities.Batch batch, string flowmeter, int rangeIx)
|
||||
{
|
||||
IList<MeasurementCorrection> rslt = new List<MeasurementCorrection>();
|
||||
|
||||
foreach (var tr in batch.TestRslts)
|
||||
{
|
||||
if (tr.Components.Flowmeter == flowmeter && tr.Evaluate())
|
||||
{
|
||||
float measurement = tr.FlowMean;
|
||||
float correction = (float)BenchControl.Formulas.CorrectionFromError(measurement, tr.ErrorMaster);
|
||||
rslt.Add(new MeasurementCorrection { RangeIx = rangeIx, Measurement = measurement, Correction = correction });
|
||||
}
|
||||
}
|
||||
|
||||
return rslt;
|
||||
}
|
||||
|
||||
/// <summary>Run this operation</summary>
|
||||
|
||||
void DeleteExistingCorrections(ISession session, Component cmpnt, int rangeIx)
|
||||
{
|
||||
foreach (var mc in cmpnt.Corrections) session.Delete(mc);
|
||||
cmpnt.Corrections.Clear();
|
||||
session.SaveOrUpdate(cmpnt);
|
||||
}
|
||||
|
||||
void SaveNewCorrections(ISession session, Component cmpnt, int rangeIx, IList<MeasurementCorrection> corrections)
|
||||
{
|
||||
if (cmpnt.Corrections == null) cmpnt.Corrections = new List<MeasurementCorrection>();
|
||||
|
||||
foreach (var mc in corrections)
|
||||
{
|
||||
cmpnt.Corrections.Add(mc);
|
||||
session.SaveOrUpdate(mc);
|
||||
}
|
||||
session.SaveOrUpdate(cmpnt);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>Start this operation</summary>
|
||||
public void Start()
|
||||
{
|
||||
opCompleted = false;
|
||||
anyError = false;
|
||||
|
||||
newCorrections = GetCorrections(batch, myCfg.Flowmeter, myCfg.RangeIx);
|
||||
}
|
||||
|
||||
/// <summary>Run this operation</summary>
|
||||
/// <returns>Event.ResultsWritten or Event.Error</returns>
|
||||
public Event Run()
|
||||
{
|
||||
if (monitoringCfg.DebugLevel == DebugMode.Simulate || batch.WaterMeters.Count == 0)
|
||||
if (myCfg.DebugLevel == DebugMode.Simulate || newCorrections == null || newCorrections.Count == 0)
|
||||
{
|
||||
opCompleted = true;
|
||||
return Event.ResultsWritten;
|
||||
@ -112,24 +136,31 @@ namespace TBF.BenchControl.Output.DB.SaveFlowmeterCorrections
|
||||
ITransaction transaction = null;
|
||||
try
|
||||
{
|
||||
ISession session = sessionFactory.OpenSession();
|
||||
ISession session = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config);
|
||||
transaction = session.BeginTransaction();
|
||||
|
||||
int wrCount = WriteResultsToDatabase(session, batch);
|
||||
|
||||
var cmpnts = session.QueryOver<Component>()
|
||||
.Where(cmpnt => (cmpnt.Name == myCfg.Flowmeter))
|
||||
.List();
|
||||
|
||||
if (cmpnts.Count != 1) throw (new Exception(string.Format("No unique component in SaveNewCorrections(.,{0},{1})", myCfg.Flowmeter, myCfg.RangeIx)));
|
||||
|
||||
DeleteExistingCorrections(session, cmpnts[0], myCfg.RangeIx);
|
||||
SaveNewCorrections(session, cmpnts[0], myCfg.RangeIx, newCorrections);
|
||||
|
||||
transaction.Commit();
|
||||
session.Flush();
|
||||
opCompleted = true;
|
||||
|
||||
log.ErrorFormat("Batch {0}: {1} of {2} watermeters written to production monitoring DB",
|
||||
batch.BatchNr, wrCount, batch.WaterMeters.Count);
|
||||
log.ErrorFormat("Measurement corrections of {0}/{1} saved", myCfg.Flowmeter, myCfg.RangeIx);
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
if (transaction != null && !transaction.WasCommitted) transaction.Rollback();
|
||||
anyError = true;
|
||||
|
||||
log.WarnFormat("Failed to write batch {0} results to production monitoring DB: {1}",
|
||||
batch.BatchNr, exc.Message);
|
||||
log.WarnFormat("Failed to write batch {0} results to measurement {1}/{2} corrections: {3}",
|
||||
batch.BatchNr, myCfg.Flowmeter, myCfg.RangeIx, exc.Message);
|
||||
}
|
||||
}
|
||||
|
||||
@ -149,80 +180,5 @@ namespace TBF.BenchControl.Output.DB.SaveFlowmeterCorrections
|
||||
{
|
||||
currentOp = CurrentOp.None;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Write results of a batch of water meters to the DB
|
||||
/// </summary>
|
||||
/// <param name="session">DB session</param>
|
||||
/// <param name="batch">Batch entity</param>
|
||||
int WriteResultsToDatabase(ISession session, Results.Entities.Batch batch)
|
||||
{
|
||||
int wrCount = 0;
|
||||
foreach (var wMtr in batch.WaterMeters)
|
||||
{
|
||||
wrCount += SaveSingleWM2DB(session, wMtr);
|
||||
}
|
||||
return wrCount;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Write one meter results to the DB
|
||||
/// </summary>
|
||||
/// <param name="session">DB session</param>
|
||||
/// <param name="wm">Watermeter entity</param>
|
||||
/// <returns>Number of written reference record</returns>
|
||||
int SaveSingleWM2DB(ISession session, Results.Entities.WaterMeter wm)
|
||||
{
|
||||
if (string.IsNullOrEmpty(wm.SerialNr)) return 0; /// Without PCB number there is no DB activity
|
||||
|
||||
///
|
||||
/// Get all already existing reference records with Code == wm.SerialNr, refRecords[0] will be the most recent one
|
||||
///
|
||||
IList<ReferenceRecord> refRecords;
|
||||
int trialsCount = 0;
|
||||
do
|
||||
{
|
||||
if (trialsCount > 0) ReadProcessesFromDB(session);
|
||||
|
||||
refRecords = session.QueryOver<ReferenceRecord>()
|
||||
.Where(rr => (rr.Code == wm.SerialNr))
|
||||
.OrderBy(rr => rr.TimeStamp).Desc
|
||||
.List();
|
||||
trialsCount++;
|
||||
}
|
||||
while (refRecords.Count == 0 && trialsCount <= 2);
|
||||
|
||||
///
|
||||
/// Save a new reference record from this test bench if workstep "Test_bench" is defined in the obtained WM process
|
||||
///
|
||||
IList<Workstep> worksteps;
|
||||
if ((refRecords.Count > 0) && workstepsDictionary.TryGetValue(refRecords[0].Process.Id, out worksteps) && (worksteps.Count == 1))
|
||||
{
|
||||
session.SaveOrUpdate(new ReferenceRecord(refRecords[0].Process, worksteps[0], wm.SerialNr, Users.GlobalData.CurrentUser.UserName, workplace, wm.Passed ? 0 : 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Updates processes, processDictionary and workstepsDictionary
|
||||
/// </summary>
|
||||
/// <param name="session">DB session</param>
|
||||
void ReadProcessesFromDB(ISession session)
|
||||
{
|
||||
processes = session.QueryOver<Process>().List();
|
||||
processDictionary = new Dictionary<int, Process>();
|
||||
workstepsDictionary = new Dictionary<int, IList<Workstep>>();
|
||||
foreach (var pr in processes)
|
||||
{
|
||||
IList<Workstep> worksteps = session.QueryOver<Workstep>().List();
|
||||
processDictionary.Add(pr.Id, pr);
|
||||
workstepsDictionary.Add(pr.Id, worksteps);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user