Tracing: CurrentPcsCount/CurrentSN/CurrentRA incremented, OrderInfo, Workflow and Workplace re-read from DB after sel., ver. 3.1.1835.

This commit is contained in:
Milan Hanajik
2021-12-13 15:03:52 +01:00
parent 43caffd73d
commit 47511ef873
7 changed files with 142 additions and 184 deletions
@@ -1,48 +0,0 @@
///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System;
namespace TBF.Rig.Output.DB.ProductionTracing
{
public enum CodeType
{
UniqueNr,
FlowtubeNr, /// Prvé 3 znaky sa zhodujú s poslednými 3 znakmi názvu
FlowtubeNrLU, /// Začiatok kódu sa zhoduje s názvom
BatchNr,
BatchNrContainingPartName,
DateMMYY,
QualityCheck,
}
public enum CodeForm
{
Barcode,
QRCode,
Keyboard,
OkNok,
}
public enum CodeLocation
{
OnPart,
OnPallet,
}
public enum LifetimeManagement
{
None,
OneYear,
Count,
}
public enum ReleaseStatus
{
In_preparation,
Released,
ReleasedActive,
Deactivated,
Count,
}
}
+96 -122
View File
@@ -10,6 +10,7 @@ using NHibernate;
using NHibernate.Criterion;
using Common;
using Config.Entities;
using SharedDatabase;
using SharedDatabase.Entities;
using TBF.Rig.Sequences;
@@ -80,8 +81,8 @@ namespace TBF.Rig.Output.DB.ProductionTracing
public ISessionFactory SessionFactory; /// Factory to create database sessions that is initialized in the constructor
IList<SharedDatabase.Entities.Process> processes;
Dictionary<int, SharedDatabase.Entities.Process> processDictionary;
IList<SharedDatabase.Entities.Process> workflows;
Dictionary<int, SharedDatabase.Entities.Process> workflowDictionary;
Dictionary<int, IList<SharedDatabase.Entities.Workstep>> workstepsDictionary;
ISessionFactory sessionFactory2; /// Factory to create database sessions that is initialized in the constructor
@@ -201,28 +202,29 @@ namespace TBF.Rig.Output.DB.ProductionTracing
public void StopDevice2() {}
public Process GetWorkflowAndWorkstep(IOrderInfo order, out Workstep workstep)
public Process GetWorkflowAndWorkstep(IOrderInfo order, out Workstep workstep, ISession session = null)
{
bool openAndCloseSession = (session == null) || !session.IsOpen;
workstep = null;
if (order is OrderInfo)
{
string workflowName = (order as OrderInfo).Workflow;
try
{
var session = SessionFactory.OpenSession();
if (openAndCloseSession) session = SessionFactory.OpenSession();
var wflows = session.QueryOver<Process>()
.Where(x => x.Name == (order as OrderInfo).Workflow)
.Where(x => x.Name == workflowName)
.And(x => (x.ReleaseStatus == ReleaseStatus.ToBeApproved ||
x.ReleaseStatus == ReleaseStatus.Released ||
x.ReleaseStatus == ReleaseStatus.ReleasedActive))
.List();
if (wflows.Count == 1)
{
var wsteps = session.QueryOver<Workstep>()
.Where(x => x.Workflow == wflows[0])
.And(x => x.Name.ToLower().Contains("test"))
.List();
if (wsteps.Count == 1) workstep = wsteps[0];
workstep = wflows[0].Worksteps.FirstOrDefault(x => x.Name.ToLower().Contains("test"));
}
session.Close();
if (openAndCloseSession) session.Close();
if (wflows.Count == 1)
{
@@ -255,6 +257,29 @@ namespace TBF.Rig.Output.DB.ProductionTracing
}
}
public OrderInfo ReReadOneOrder(OrderInfo order, ISession session = null)
{
bool openAndCloseSession = (session == null) || !session.IsOpen;
try
{
if (openAndCloseSession) session = SessionFactory.OpenSession();
var list = session.QueryOver<OrderInfo>()
.Where(x => (x.Id == order.Id))
.List();
if (openAndCloseSession) session.Close();
return (list.Count == 1) ? list[0] : null;
}
catch (Exception exc)
{
log.ErrorFormat("Cannot read orders from the tracing DB: {0}", exc.Message);
return null;
}
}
/// <summary>
/// Read reference records belonging to a specified order
@@ -394,83 +419,50 @@ namespace TBF.Rig.Output.DB.ProductionTracing
/// <returns>Event.ResultsWritten or Event.Error</returns>
public Event Run()
{
log.WarnFormat("{0} : Run() : currentOp = {1}", Name, currentOpState);
if (currentOpState == OpState.CheckPreviousRecordsRunning)
{
if (tracingCfg.DebugLevel == DebugMode.Simulate)
{
return Event.InfoRead;
}
else if (!opCompleted)
{
log.WarnFormat("{0} : Run() : currentOp = {1}", Name, currentOpState);
opCompleted = true;
if (CheckPreviousRecords(waterMeters) != Retv.OK) anyError = true;
return anyError ? Event.InfoNotRead : Event.InfoRead;
}
else
{
return anyError ? Event.InfoNotRead : Event.InfoRead;
}
if (tracingCfg.DebugLevel == DebugMode.Simulate) return Event.InfoRead;
if (opCompleted) return anyError ? Event.InfoNotRead : Event.InfoRead;
/// Run once
opCompleted = true;
if (CheckPreviousRecords(waterMeters) != Retv.OK) anyError = true;
return anyError ? Event.InfoNotRead : Event.InfoRead;
}
else if (currentOpState == OpState.SaveTracingRecordsRunning)
if (currentOpState == OpState.SaveTracingRecordsRunning)
{
if (tracingCfg.DebugLevel == DebugMode.Simulate || batch.WaterMeters.Count == 0)
{
return Event.ResultsWritten;
}
else if (!opCompleted)
{
log.WarnFormat("{0} : Run() : currentOp = {1}", Name, currentOpState);
opCompleted = true;
if (SaveTracingRecords(batch) != Retv.OK) anyError = true;
if (tracingCfg.DebugLevel == DebugMode.Simulate || batch.WaterMeters.Count == 0) return Event.ResultsWritten;
if (opCompleted) return anyError ? Event.ErrorProcessingResults : Event.ResultsWritten;
if (anyError && !string.IsNullOrEmpty(SharedDatabase.EventsDB.ConnectionString))
/// Run once
opCompleted = true;
if (SaveTracingRecords(batch) != Retv.OK) anyError = true;
if (anyError && !string.IsNullOrEmpty(SharedDatabase.EventsDB.ConnectionString))
{
try
{
try
{
NHibernate.ISession session = SharedDatabase.EventsDB.CreateSession();
SharedDatabase.EventsDB.LoadSubscribers(session);
NHibernate.ISession session = SharedDatabase.EventsDB.CreateSession();
SharedDatabase.EventsDB.LoadSubscribers(session);
TBF.UiBridge.Bridge.TriggerEvent(session, Name, EventClass.ComputerResources, Severity.Error,
string.Format("Nepodarilo sa uložiť výsledky do sledovacej DB: Číslo dávky={0}", batch.BatchNr),
string.Format("Nepodarilo sa uložiť výsledky do sledovacej DB\r\nČíslo dávky = {0}", batch.BatchNr),
SubscriberGroup.Metrology | SubscriberGroup.Maintenance | SubscriberGroup.Production);
}
catch (Exception exc)
{
log.ErrorFormat("Triggering event(s) failed: {0}", exc.Message);
}
TBF.UiBridge.Bridge.TriggerEvent(session, Name, EventClass.ComputerResources, Severity.Error,
string.Format("Nepodarilo sa uložiť výsledky do sledovacej DB: Číslo dávky={0}", batch.BatchNr),
string.Format("Nepodarilo sa uložiť výsledky do sledovacej DB\r\nČíslo dávky = {0}", batch.BatchNr),
SubscriberGroup.Metrology | SubscriberGroup.Maintenance | SubscriberGroup.Production);
}
return anyError ? Event.ErrorProcessingResults : Event.ResultsWritten;
}
else
{
if (anyError && !string.IsNullOrEmpty(SharedDatabase.EventsDB.ConnectionString))
catch (Exception exc)
{
try
{
NHibernate.ISession session = SharedDatabase.EventsDB.CreateSession();
SharedDatabase.EventsDB.LoadSubscribers(session);
TBF.UiBridge.Bridge.TriggerEvent(session, Name, EventClass.ComputerResources, Severity.Error,
string.Format("Nepodarilo sa uložiť výsledky do sledovacej DB: Číslo dávky={0}", batch.BatchNr),
string.Format("Nepodarilo sa uložiť výsledky do sledovacej DB\r\nČíslo dávky = {0}", batch.BatchNr),
SubscriberGroup.Metrology | SubscriberGroup.Maintenance | SubscriberGroup.Production);
}
catch (Exception exc)
{
log.ErrorFormat("Triggering event(s) failed: {0}", exc.Message);
}
log.ErrorFormat("Triggering event(s) failed: {0}", exc.Message);
}
return anyError ? Event.ErrorProcessingResults : Event.ResultsWritten;
}
return anyError ? Event.ErrorProcessingResults : Event.ResultsWritten;
}
else
{
return Event.None;
}
return Event.None;
}
/// <summary>Stop this operation</summary>
@@ -534,8 +526,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
{
/// 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
wm.LastRecordIsNok = false; /// OK, this is an RFID comm. error, not a production tracing error
return Retv.OK;
}
@@ -566,8 +557,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
{
/// No records found
wm.ProcessId = 0;
wm.SessionId = sessionId;
wm.LastRecordIsNok = true; /// NOK
wm.LastRecordIsNok = true; /// NOK
///
return Retv.OK;
}
@@ -623,7 +613,6 @@ namespace TBF.Rig.Output.DB.ProductionTracing
wm.ProcessId = currentWorkflow.Id;
wm.SessionId = sessionId;
wm.LastRecordIsNok = true; /// NOK (=default if no matching record found)
foreach (var rslt in results)
{
@@ -650,7 +639,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
ISession session = null;
var order = ProcessData.OrderInfo as SharedDatabase.Entities.OrderInfo;
if (order != null && ProcessData.Workflow != null && ProcessData.Workstep != null)
if (order != null)
{
///
/// Save to regular tracing DB
@@ -665,21 +654,35 @@ namespace TBF.Rig.Output.DB.ProductionTracing
.List();
if (reloadedOrder.Count == 1)
{
reloadedOrder[0].POState = (sbyte)OrderState.Active;
reloadedOrder[0].CurrentPcsCount = order.CurrentPcsCount;
reloadedOrder[0].CurrentSN = order.CurrentSN;
reloadedOrder[0].CurrentRA = order.CurrentRA;
if (order.CurrentPcsCount > 0)
{
reloadedOrder[0].POState = (sbyte)OrderState.Active;
}
session.SaveOrUpdate(reloadedOrder[0]);
log.WarnFormat("Order={0} CurrentPcsCount={1} CurrentSN={2} CurrentRA={3}",
order.POName, order.CurrentPcsCount, order.CurrentSN, order.CurrentRA);
}
else
{
log.ErrorFormat("Unexpected reloadedOrder.Count == {0} in SaveTracingRecords()", reloadedOrder.Count);
}
int writtenRecordsCount = 0;
foreach (var wMtr in batch.WaterMeters)
if (ProcessData.Workflow != null && ProcessData.Workstep != null)
{
if (wMtr.SessionId == 1)
foreach (var wMtr in batch.WaterMeters)
{
writtenRecordsCount += SaveSingleWM2DB(session, order, wMtr);
if (!string.IsNullOrEmpty(wMtr.SerialNr))
{
SaveSingleWM2DB(session, order, wMtr, ProcessData.Workflow, ProcessData.Workstep);
writtenRecordsCount++;
}
}
}
transaction.Commit();
session.Flush();
@@ -690,7 +693,7 @@ namespace TBF.Rig.Output.DB.ProductionTracing
{
if (transaction != null && !transaction.WasCommitted) transaction.Rollback();
log.WarnFormat("Failed to write results of batch {0} to production tracing DB: {1}",
log.ErrorFormat("Failed to write results of batch {0} to production tracing DB: {1}",
batch.BatchNr, exc.Message);
return Retv.Error;
@@ -708,47 +711,18 @@ namespace TBF.Rig.Output.DB.ProductionTracing
return Retv.OK;
}
/// <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, OrderInfo order, Results.Entities.WaterMeter wm)
void SaveSingleWM2DB(ISession session, OrderInfo order, Results.Entities.WaterMeter wm, Process workflow, Workstep workstep)
{
if (!string.IsNullOrEmpty(wm.SerialNr) && ProcessData.Workflow != null && ProcessData.Workstep != null)
{
/// Process is already loaded => save a reference record for this WM
session.SaveOrUpdate(new ReferenceRecord(order, ProcessData.Workflow, ProcessData.Workstep,
wm.SerialNr, GlobalData.CurrentUser.UserName, workplace, wm.Passed ? 0 : 1));
return 1;
}
return 0;
session.SaveOrUpdate(new ReferenceRecord(order, workflow, workstep,
wm.SerialNr,
GlobalData.CurrentUser.UserName,
workplace,
wm.Passed ? 0 : 1));
}
/// <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>()
.Where(ws => ((ws.Workflow == pr) && (ws.Name == WorkstepName)))
.List();
processDictionary.Add(pr.Id, pr);
workstepsDictionary.Add(pr.Id, worksteps);
}
}
}
}