TBF.BenchControl.Output.DB.ProductionMonitoring completed, ver. 2.18.792
This commit is contained in:
@@ -54,6 +54,7 @@
|
||||
None,
|
||||
TeamLeader,
|
||||
Administrator,
|
||||
MH,
|
||||
Count,
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,12 @@ namespace MonitoringDB.Forms
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
else if (passwordTextBox.Text.ToLower().Equals("napajedla"))
|
||||
{
|
||||
LoginLevel = LoginLevel.MH;
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
else
|
||||
{
|
||||
DialogResult = DialogResult.Cancel;
|
||||
|
||||
@@ -22,29 +22,31 @@ namespace TBF.BenchControl.Output.DB.ProductionMonitoring
|
||||
public override string ToString() { return string.Format("Monitoring({0})", Cfg.ToString(1)); }
|
||||
|
||||
|
||||
ISessionFactory sessionFactory;
|
||||
public const string WorkstepName = "Test_bench";
|
||||
|
||||
|
||||
MonitoringCfg monitoringCfg;
|
||||
string workplace { get { return monitoringCfg.Workplace; } }
|
||||
|
||||
enum CurrentOp
|
||||
{
|
||||
None,
|
||||
LoadProcesses,
|
||||
SaveMonitoringRecords,
|
||||
}
|
||||
|
||||
CurrentOp currentOp;
|
||||
bool opCompleted;
|
||||
bool anyError;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Results to print
|
||||
/// </summary>
|
||||
Results.Entities.Batch batch;
|
||||
|
||||
|
||||
MonitoringCfg monitoringCfg;
|
||||
string workplace { get { return monitoringCfg.Workplace; } }
|
||||
|
||||
ISessionFactory sessionFactory; /// Factory to create database sessions initialized in the constructor
|
||||
IList<Process> processes;
|
||||
Dictionary<int, Process> processDictionary;
|
||||
Dictionary<int, IList<Workstep>> workstepsDictionary;
|
||||
|
||||
|
||||
public Monitoring() {}
|
||||
@@ -55,12 +57,6 @@ namespace TBF.BenchControl.Output.DB.ProductionMonitoring
|
||||
monitoringCfg = cfg as MonitoringCfg;
|
||||
currentOp = CurrentOp.None;
|
||||
|
||||
sessionFactory = FluentNHibernate.Cfg.Fluently
|
||||
.Configure()
|
||||
.Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard.ConnectionString(monitoringCfg.ConnStr))
|
||||
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MonitoringDB.Entities.Process>())
|
||||
.ExposeConfiguration(MonitoringDB.FluentNH.BuildSchema).BuildSessionFactory();
|
||||
|
||||
log.Debug(this.ToString());
|
||||
}
|
||||
|
||||
@@ -69,12 +65,24 @@ namespace TBF.BenchControl.Output.DB.ProductionMonitoring
|
||||
if (monitoringCfg.DebugLevel == DebugMode.Simulate) return;
|
||||
|
||||
///
|
||||
/// Do something with the database to see if the connection works well
|
||||
/// Session factory is used to create database sessions
|
||||
///
|
||||
processes = sessionFactory.OpenSession().QueryOver<Process>().List();
|
||||
sessionFactory = FluentNHibernate.Cfg.Fluently
|
||||
.Configure()
|
||||
.Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard.ConnectionString(monitoringCfg.ConnStr))
|
||||
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MonitoringDB.Entities.Process>())
|
||||
.ExposeConfiguration(MonitoringDB.FluentNH.BuildSchema)
|
||||
.BuildSessionFactory();
|
||||
///
|
||||
/// Read processes and worksteps from the database (this verifies DB connection as well)
|
||||
///
|
||||
log.WarnFormat("Reading processes/worksteps from production monitoring DB started");
|
||||
ReadProcessesFromDB(sessionFactory.OpenSession());
|
||||
log.WarnFormat("Reading processes/worksteps from production monitoring DB completed");
|
||||
}
|
||||
|
||||
public void RunDeviceBefore() {}
|
||||
|
||||
public void RunDeviceBefore() { }
|
||||
public void RunDeviceAfter() {}
|
||||
public void StopDevice() {}
|
||||
public void StopDevice2() {}
|
||||
@@ -109,26 +117,37 @@ namespace TBF.BenchControl.Output.DB.ProductionMonitoring
|
||||
/// <returns>Event.ResultsWritten or Event.Error</returns>
|
||||
public Event Run()
|
||||
{
|
||||
if (monitoringCfg.DebugLevel == DebugMode.Simulate || batch.WaterMeters.Count == 0)
|
||||
{
|
||||
opCompleted = true;
|
||||
return Event.ResultsWritten;
|
||||
}
|
||||
|
||||
if (currentOp == CurrentOp.SaveMonitoringRecords)
|
||||
{
|
||||
if (!opCompleted)
|
||||
{
|
||||
if (batch.WaterMeters.Count == 0) return Event.ResultsWritten;
|
||||
|
||||
if (monitoringCfg.DebugLevel != DebugMode.Simulate && sessionFactory != null)
|
||||
ITransaction transaction = null;
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
ISession session = sessionFactory.OpenSession();
|
||||
if (WriteResultsToDatabase(session, batch) == Retv.Error) anyError = true;
|
||||
session.Close();
|
||||
opCompleted = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
log.ErrorFormat("Write to ProductionMonitoring database failed: {0}", exc.Message);
|
||||
anyError = true;
|
||||
}
|
||||
ISession session = sessionFactory.OpenSession();
|
||||
transaction = session.BeginTransaction();
|
||||
|
||||
int wrCount = WriteResultsToDatabase(session, batch);
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,49 +169,78 @@ namespace TBF.BenchControl.Output.DB.ProductionMonitoring
|
||||
}
|
||||
|
||||
|
||||
public static Retv WriteResultsToDatabase(ISession session, Results.Entities.Batch batch)
|
||||
/// <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)
|
||||
{
|
||||
ITransaction transaction = session.BeginTransaction();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var wMtr in batch.WaterMeters)
|
||||
{
|
||||
SaveSingleWM2DB(session, wMtr);
|
||||
}
|
||||
|
||||
transaction.Commit();
|
||||
session.Flush();
|
||||
}
|
||||
catch (Exception exc)
|
||||
int wrCount = 0;
|
||||
foreach (var wMtr in batch.WaterMeters)
|
||||
{
|
||||
if (transaction != null) transaction.Rollback();
|
||||
|
||||
log.ErrorFormat("Results of batch {0} not written to ORACLE database: {1}",
|
||||
batch.BatchNr, exc.Message);
|
||||
|
||||
return Retv.Error;
|
||||
wrCount += SaveSingleWM2DB(session, wMtr);
|
||||
}
|
||||
|
||||
return Retv.OK;
|
||||
return wrCount;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Write one meter results into the database
|
||||
/// Write one meter results to the DB
|
||||
/// </summary>
|
||||
/// <param name="wmNr">0-based water meter number</param>
|
||||
/// <param name="results">Sorted list of results to save</param>
|
||||
static void SaveSingleWM2DB(ISession session, Results.Entities.WaterMeter wm)
|
||||
/// <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; /// No database activity possible w/o PCB number
|
||||
if (string.IsNullOrEmpty(wm.SerialNr)) return 0; /// Without PCB number there is no DB activity
|
||||
|
||||
var refRecords = session.QueryOver<ReferenceRecord>()
|
||||
///
|
||||
/// 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);
|
||||
|
||||
//SaveOrUpdateWM(conn, wm, MAX_Pruefindex, Hydr_Pruefung);
|
||||
///
|
||||
/// 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 ? 1 : 0));
|
||||
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>().Where(ws => ((ws.Process == pr) && (ws.Name == WorkstepName))).List();
|
||||
processDictionary.Add(pr.Id, pr);
|
||||
workstepsDictionary.Add(pr.Id, worksteps);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user