SharedDatabase synchronized with Production-Monitoring.TracingDB

This commit is contained in:
Milan Hanajik
2021-12-01 13:13:59 +01:00
parent bbffc3b406
commit d1e0d1466d
16 changed files with 120 additions and 169 deletions
+78 -94
View File
@@ -87,8 +87,9 @@ namespace TBF.Rig.Output.DB.ProductionTracing
///
/// Previous workstep verification info
///
Process currentProcess; /// Currently used process
///
OrderInfo currentOrder;
Process currentWorkflow; /// Currently used workflow
Workstep currentWorkstep; /// Currently used workstep of this test bench
Workstep verifiedWorkstep;
Part verifiedPart; /// Applicable when verifyReferencePart == false
@@ -162,7 +163,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
SharedDatabase.TracingDB.SessionFactory = sessionFactory;
if (session != null) session.Dispose();
currentProcess = null;
currentWorkflow = null;
}
///
string GetIPAddress()
@@ -344,37 +345,40 @@ namespace TBF.Rig.Output.DB.ProductionTracing
{
Retv retVal = Retv.Error;
bool isAtLeastOneNOK = false;
using (ISession session = sessionFactory.OpenSession())
var sampleWM = waterMeters.FirstOrDefault<Results.Entities.WaterMeter>(x => x != null && x.Disabled == false);
if (sampleWM != null)
{
for (int i = 0; i < waterMeters.Count; i++)
bool isAtLeastOneNOK = false;
using (ISession session = sessionFactory.OpenSession())
{
Results.Entities.WaterMeter wm = waterMeters[i];
var cOrders = session.QueryOver<OrderInfo>()
.Where(x => x.POName == sampleWM.PurchaseOrder)
.And(x => (x.POState == (sbyte)OrderState.New || x.POState == (sbyte)OrderState.Active))
.List();
if ((wm != null) && !wm.Disabled)
if (cOrders.Count < 1)
{
CheckPreviousRecordOfSingleWM(session, wm, 1);
if (wm.LastRecordIsNok)
{
isAtLeastOneNOK = true;
}
cOrders = session.QueryOver<OrderInfo>()
.Where(x => x.POName == "0000001")
.List();
}
}
retVal = Retv.OK;
}
if (isAtLeastOneNOK && (sessionFactory2 != null))
{
using (ISession session2 = sessionFactory2.OpenSession())
{
for (int i = 0; i < waterMeters.Count; i++)
if (cOrders.Count == 1)
{
Results.Entities.WaterMeter wm = waterMeters[i];
currentOrder = cOrders[0];
if ((wm != null) && !wm.Disabled && wm.LastRecordIsNok)
for (int i = 0; i < waterMeters.Count; i++)
{
CheckPreviousRecordOfSingleWM(session2, wm, 2);
Results.Entities.WaterMeter wm = waterMeters[i];
if ((wm != null) && !wm.Disabled)
{
CheckPreviousRecordOfSingleWM(session, wm, 1);
if (wm.LastRecordIsNok)
{
isAtLeastOneNOK = true;
}
}
}
}
@@ -406,7 +410,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
ProjectionList projections = Projections.ProjectionList();
projections.Add(Projections.Property(() => refRecord.Id)); /// rslt[0] = reference record ID
projections.Add(Projections.Property(() => workstep.Id)); /// rslt[1] = workstep ID
projections.Add(Projections.Property(() => refRecord.Process.Id)); /// rslt[2] = process ID
projections.Add(Projections.Property(() => refRecord.Workflow.Id)); /// rslt[2] = process ID
projections.Add(Projections.Property(() => refRecord.TimeStamp)); /// rslt[3] = reference record time stamp
///
@@ -446,7 +450,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
if (processId == 0) return Retv.Error; /// This should never happen
if ((currentProcess == null) || (currentProcess.Id != processId))
if ((currentWorkflow == null) || (currentWorkflow.Id != processId))
{
///
/// Process changed ==> update currentProcess / currentWorkstep / verifiedWorkstep / verifiedPart / verifyReferencePart
@@ -458,7 +462,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
if (processes.Count != 1) return Retv.Error;
IList<Workstep> worksteps = session.QueryOver<Workstep>()
.Where(ws => (ws.Process == processes[0]))
.Where(ws => (ws.Workflow == processes[0]))
.And( ws => (ws.Name == WorkstepName))
.List();
@@ -471,7 +475,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
return Retv.Error; /// Unable to
}
///
currentProcess = processes[0];
currentWorkflow = processes[0];
currentWorkstep = worksteps[0];
verifiedWorkstep = info.Workstep;
verifiedPart = info.Part;
@@ -479,7 +483,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
}
wm.ProcessId = currentProcess.Id;
wm.ProcessId = currentWorkflow.Id;
wm.SessionId = sessionId;
wm.LastRecordIsNok = true; /// NOK (=default if no matching record found)
foreach (var rslt in results)
@@ -506,71 +510,51 @@ namespace TBF.Rig.Output.DB.ProductionTracing
ITransaction transaction = null;
ISession session = null;
///
/// Save to regular tracing DB
///
try
{
session = sessionFactory.OpenSession();
transaction = session.BeginTransaction();
int writtenRecordsCount = 0;
foreach (var wMtr in batch.WaterMeters)
{
if (wMtr.SessionId == 1)
{
writtenRecordsCount += SaveSingleWM2DB(session, wMtr);
}
}
transaction.Commit();
session.Flush();
log.ErrorFormat("Batch {0}: {1} of {2} watermeters written to the regular 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();
}
}
if (sessionFactory2 != null)
var sampleWM = batch.WaterMeters.FirstOrDefault<Results.Entities.WaterMeter>(x => x != null && x.Disabled == false);
if (sampleWM != null)
{
///
/// Save to alternative tracing DB
/// Save to regular tracing DB
///
transaction = null;
session = null;
try
{
session = sessionFactory2.OpenSession();
session = sessionFactory.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();
var cOrders = session.QueryOver<OrderInfo>()
.Where(x => x.POName == sampleWM.PurchaseOrder)
.And(x => (x.POState == (sbyte)OrderState.New || x.POState == (sbyte)OrderState.Active))
.List();
log.ErrorFormat("Batch {0}: {1} of {2} watermeters written to the alternative production tracing DB",
batch.BatchNr, writtenRecordsCount, batch.WaterMeters.Count);
if (cOrders.Count < 1)
{
cOrders = session.QueryOver<OrderInfo>()
.Where(x => x.POName == "0000001")
.List();
}
if (cOrders.Count == 1)
{
currentOrder = cOrders[0];
int writtenRecordsCount = 0;
foreach (var wMtr in batch.WaterMeters)
{
if (wMtr.SessionId == 1)
{
writtenRecordsCount += SaveSingleWM2DB(session, currentOrder, wMtr);
}
}
transaction.Commit();
session.Flush();
log.ErrorFormat("Batch {0}: {1} of {2} watermeters written to the regular production tracing DB",
batch.BatchNr, writtenRecordsCount, batch.WaterMeters.Count);
}
else
{
log.ErrorFormat("POrder not found: {0}", sampleWM);
}
}
catch (Exception exc)
{
@@ -601,17 +585,17 @@ namespace TBF.Rig.Output.DB.ProductionTracing
/// <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)
int SaveSingleWM2DB(ISession session, OrderInfo pOrder, Results.Entities.WaterMeter wm)
{
if (string.IsNullOrEmpty(wm.SerialNr) || (wm.ProcessId == 0))
{
/// Without PCB number there is no DB activity
return 0;
}
else if ((currentProcess != null) && (wm.ProcessId == currentProcess.Id))
else if ((currentWorkflow != null) && (wm.ProcessId == currentWorkflow.Id))
{
/// Process is already loaded => save a reference record for this WM
session.SaveOrUpdate(new ReferenceRecord(currentProcess, currentWorkstep, wm.SerialNr, GlobalData.CurrentUser.UserName, workplace, wm.Passed ? 0 : 1));
session.SaveOrUpdate(new ReferenceRecord(pOrder, currentWorkflow, currentWorkstep, wm.SerialNr, GlobalData.CurrentUser.UserName, workplace, wm.Passed ? 0 : 1));
return 1;
}
else if (wm.ProcessId != 0)
@@ -626,7 +610,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
{
if (ws.Name == WorkstepName)
{
session.SaveOrUpdate(new ReferenceRecord(processes[0], ws, wm.SerialNr, GlobalData.CurrentUser.UserName, workplace, wm.Passed ? 0 : 1));
session.SaveOrUpdate(new ReferenceRecord(pOrder, processes[0], ws, wm.SerialNr, GlobalData.CurrentUser.UserName, workplace, wm.Passed ? 0 : 1));
return 1;
}
}
@@ -656,9 +640,9 @@ namespace TBF.Rig.Output.DB.ProductionTracing
/// 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))
if ((refRecords.Count > 0) && workstepsDictionary.TryGetValue(refRecords[0].Workflow.Id, out worksteps) && (worksteps.Count == 1))
{
session.SaveOrUpdate(new ReferenceRecord(refRecords[0].Process, worksteps[0], wm.SerialNr, GlobalData.GetCurrentUserName(), workplace, wm.Passed ? 0 : 1));
session.SaveOrUpdate(new ReferenceRecord(pOrder, refRecords[0].Workflow, worksteps[0], wm.SerialNr, GlobalData.GetCurrentUserName(), workplace, wm.Passed ? 0 : 1));
return 1;
}
@@ -682,7 +666,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
foreach (var pr in processes)
{
IList<Workstep> worksteps = session.QueryOver<Workstep>()
.Where(ws => ((ws.Process == pr) && (ws.Name == WorkstepName)))
.Where(ws => ((ws.Workflow == pr) && (ws.Name == WorkstepName)))
.List();
processDictionary.Add(pr.Id, pr);
workstepsDictionary.Add(pr.Id, worksteps);