From 9a8f8722bd63a18ffa6701d552c8f2f5195a6d3b Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Tue, 30 Jan 2018 11:14:12 +0100 Subject: [PATCH] TBF.BenchControl.Output.DB.ProductionMonitoring completed, ver. 2.18.792 --- MonitoringDB/Enums.cs | 1 + MonitoringDB/Forms/TrivialLoginDlg.cs | 6 + .../DB/ProductionMonitoring/Monitoring.cs | 170 +++++++++++------- 3 files changed, 116 insertions(+), 61 deletions(-) diff --git a/MonitoringDB/Enums.cs b/MonitoringDB/Enums.cs index 4b5aeb50c..b68d7fc22 100644 --- a/MonitoringDB/Enums.cs +++ b/MonitoringDB/Enums.cs @@ -54,6 +54,7 @@ None, TeamLeader, Administrator, + MH, Count, } diff --git a/MonitoringDB/Forms/TrivialLoginDlg.cs b/MonitoringDB/Forms/TrivialLoginDlg.cs index b0f668f68..3c3a66e5a 100644 --- a/MonitoringDB/Forms/TrivialLoginDlg.cs +++ b/MonitoringDB/Forms/TrivialLoginDlg.cs @@ -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; diff --git a/TBF/BenchControl/Output/DB/ProductionMonitoring/Monitoring.cs b/TBF/BenchControl/Output/DB/ProductionMonitoring/Monitoring.cs index 32c2eb46b..24a428fc9 100644 --- a/TBF/BenchControl/Output/DB/ProductionMonitoring/Monitoring.cs +++ b/TBF/BenchControl/Output/DB/ProductionMonitoring/Monitoring.cs @@ -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; - /// /// Results to print /// Results.Entities.Batch batch; - - MonitoringCfg monitoringCfg; - string workplace { get { return monitoringCfg.Workplace; } } - + ISessionFactory sessionFactory; /// Factory to create database sessions initialized in the constructor IList processes; + Dictionary processDictionary; + Dictionary> 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()) - .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().List(); + sessionFactory = FluentNHibernate.Cfg.Fluently + .Configure() + .Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard.ConnectionString(monitoringCfg.ConnStr)) + .Mappings(m => m.FluentMappings.AddFromAssemblyOf()) + .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 /// Event.ResultsWritten or Event.Error 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) + /// + /// Write results of a batch of water meters to the DB + /// + /// DB session + /// Batch entity + 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; } /// - /// Write one meter results into the database + /// Write one meter results to the DB /// - /// 0-based water meter number - /// Sorted list of results to save - static void SaveSingleWM2DB(ISession session, Results.Entities.WaterMeter wm) + /// DB session + /// Watermeter entity + /// Number of written reference record + 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() + /// + /// Get all already existing reference records with Code == wm.SerialNr, refRecords[0] will be the most recent one + /// + IList refRecords; + int trialsCount = 0; + do + { + if (trialsCount > 0) ReadProcessesFromDB(session); + + refRecords = session.QueryOver() .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 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; } - } + + + /// + /// Updates processes, processDictionary and workstepsDictionary + /// + /// DB session + void ReadProcessesFromDB(ISession session) + { + processes = session.QueryOver().List(); + processDictionary = new Dictionary(); + workstepsDictionary = new Dictionary>(); + foreach (var pr in processes) + { + IList worksteps = session.QueryOver().Where(ws => ((ws.Process == pr) && (ws.Name == WorkstepName))).List(); + processDictionary.Add(pr.Id, pr); + workstepsDictionary.Add(pr.Id, worksteps); + } + } + } }