Output.DB.ProductionTracing : Alternative profuction tracing DB possible.

This commit is contained in:
Milan Hanajik
2019-12-18 17:53:12 +01:00
parent 4bf4445672
commit 94b9f564cc
5 changed files with 163 additions and 34 deletions
@@ -76,11 +76,16 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
Results.Entities.Batch batch;
ISessionFactory sessionFactory; /// Factory to create database sessions that is initialized in the constructor
ISessionFactory sessionFactory; /// Factory to create database sessions that is initialized in the constructor
IList<Process> processes;
Dictionary<int, Process> processDictionary;
Dictionary<int, IList<Workstep>> workstepsDictionary;
ISessionFactory sessionFactory2; /// Factory to create database sessions that is initialized in the constructor
IList<Process> processes2;
Dictionary<int, Process> processDictionary2;
Dictionary<int, IList<Workstep>> workstepsDictionary2;
///
/// Previous workstep verification info
///
@@ -125,6 +130,19 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<TracingDB.Entities.Process>())
.ExposeConfiguration(TracingDB.DB.BuildSchema)
.BuildSessionFactory();
///
/// Session factory 2 is used to create the 2nd database sessions
///
if (!string.IsNullOrEmpty(tracingCfg.ConnStr2))
{
sessionFactory2 = FluentNHibernate.Cfg.Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard.ConnectionString(tracingCfg.ConnStr2))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<TracingDB.Entities.Process>())
.ExposeConfiguration(TracingDB.DB.BuildSchema)
.BuildSessionFactory();
}
///
/// Register this test bench in the tracing DB for approx. 2 weeks
///
@@ -290,6 +308,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
{
Retv retVal = Retv.Error;
bool isAtLeastOneNOK = false;
using (ISession session = sessionFactory.OpenSession())
{
for (int i = 0; i < waterMeters.Length; i++)
@@ -298,22 +317,45 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
if ((wm != null) && !wm.Disabled)
{
CheckPreviousRecordOfSingleWM(session, wm);
CheckPreviousRecordOfSingleWM(session, wm, 1);
if (wm.LastRecordIsNok)
{
isAtLeastOneNOK = true;
}
}
}
retVal = Retv.OK;
}
if (isAtLeastOneNOK && (sessionFactory2 != null))
{
using (ISession session2 = sessionFactory2.OpenSession())
{
for (int i = 0; i < waterMeters.Length; i++)
{
Results.Entities.WaterMeter wm = waterMeters[i];
if ((wm != null) && !wm.Disabled && wm.LastRecordIsNok)
{
CheckPreviousRecordOfSingleWM(session2, wm, 2);
}
}
retVal = Retv.OK;
}
}
return retVal;
}
Retv CheckPreviousRecordOfSingleWM(ISession session, Results.Entities.WaterMeter wm)
Retv CheckPreviousRecordOfSingleWM(ISession session, Results.Entities.WaterMeter wm, int sessionId)
{
if (string.IsNullOrEmpty(wm.SerialNr))
{
/// S/N is missing
wm.ProcessId = 0;
wm.SessionId = 0;
wm.LastRecordIsNok = false; /// OK, this is an RFID comm. error, not a production tracing error
return Retv.OK;
}
@@ -345,6 +387,7 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
{
/// No records found
wm.ProcessId = 0;
wm.SessionId = sessionId;
wm.LastRecordIsNok = true; /// NOK
///
return Retv.OK;
@@ -403,7 +446,8 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
wm.ProcessId = currentProcess.Id;
wm.LastRecordIsNok = true; /// NOK (=default if no matching record found)
wm.SessionId = sessionId;
wm.LastRecordIsNok = true; /// NOK (=default if no matching record found)
foreach (var rslt in results)
{
if ((verifiedWorkstep.Id == (int)rslt[1]) && (processId == (int)rslt[2]))
@@ -427,6 +471,10 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
{
ITransaction transaction = null;
ISession session = null;
///
/// Save to regular tracing DB
///
try
{
session = sessionFactory.OpenSession();
@@ -435,15 +483,16 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
int writtenRecordsCount = 0;
foreach (var wMtr in batch.WaterMeters)
{
writtenRecordsCount += SaveSingleWM2DB(session, wMtr);
if (wMtr.SessionId == 1)
{
writtenRecordsCount += SaveSingleWM2DB(session, wMtr);
}
}
transaction.Commit();
session.Flush();
log.ErrorFormat("Batch {0}: {1} of {2} watermeters written to production tracing DB",
log.ErrorFormat("Batch {0}: {1} of {2} watermeters written to the regular production tracing DB",
batch.BatchNr, writtenRecordsCount, batch.WaterMeters.Count);
return Retv.OK;
}
catch (Exception exc)
{
@@ -462,7 +511,54 @@ namespace TBF.BenchControl.Output.DB.ProductionTracing
session.Dispose();
}
}
}
if (sessionFactory2 != null)
{
///
/// Save to alternative tracing DB
///
transaction = null;
session = null;
try
{
session = sessionFactory2.OpenSession();
transaction = session.BeginTransaction();
int writtenRecordsCount = 0;
foreach (var wMtr in batch.WaterMeters)
{
if (wMtr.SessionId == 2)
{
writtenRecordsCount += SaveSingleWM2DB(session, wMtr);
}
}
transaction.Commit();
session.Flush();
log.ErrorFormat("Batch {0}: {1} of {2} watermeters written to the alternative production tracing DB",
batch.BatchNr, writtenRecordsCount, batch.WaterMeters.Count);
}
catch (Exception exc)
{
if (transaction != null && !transaction.WasCommitted) transaction.Rollback();
log.WarnFormat("Failed to write results of batch {0} to production tracing DB: {1}",
batch.BatchNr, exc.Message);
return Retv.Error;
}
finally
{
if (session != null)
{
session.Close();
session.Dispose();
}
}
}
return Retv.OK;
}
/// <summary>