From 49ae2a84c422364414f5c0f364799918d6645289 Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Thu, 30 Aug 2018 08:04:58 +0200 Subject: [PATCH] Remote DB : (1) Bug fix in DatabaseSettings.Clone(), (2) Compatibility check on startup. --- Config/DatabaseSettings.cs | 2 +- TBF/BenchControl/StateMachine.cs | 97 +++++++++++++++++++++++++++++++ TBF/MainWnd.cs | 37 +++++++++--- TBF/Resources/Strings.Designer.cs | 9 +++ TBF/Resources/Strings.resx | 3 + 5 files changed, 139 insertions(+), 9 deletions(-) diff --git a/Config/DatabaseSettings.cs b/Config/DatabaseSettings.cs index 7671d4784..e06fe807f 100644 --- a/Config/DatabaseSettings.cs +++ b/Config/DatabaseSettings.cs @@ -37,7 +37,7 @@ namespace Config result.IsRealBench = IsRealBench; result.ProceduresDBSettings = (Users.DBSettings)ProceduresDBSettings.Clone(); result.WaterMetersDBSettings = (Users.DBSettings)WaterMetersDBSettings.Clone(); - result.UsersDBSettings = (Users.DBSettings)ProceduresDBSettings.Clone(); + result.UsersDBSettings = (Users.DBSettings)UsersDBSettings.Clone(); return result; } diff --git a/TBF/BenchControl/StateMachine.cs b/TBF/BenchControl/StateMachine.cs index 9e1c21e78..6d3464e16 100644 --- a/TBF/BenchControl/StateMachine.cs +++ b/TBF/BenchControl/StateMachine.cs @@ -370,6 +370,103 @@ namespace TBF.BenchControl } } + /// + /// Checks remote and local configuration DB paths and transitions for compatibility + /// + /// true when DB-s are compatible + public static bool IsRemoteDBCompatible(out string message) + { + ISession localSession = Config.FluentCommon.CreateSession(Users.Entities.DBKind.Config); + var localFeedingPaths = localSession.QueryOver().List(); + var localBenchPaths = localSession.QueryOver().List(); + var localOutputPaths = localSession.QueryOver().List(); + var localMetersPaths = localSession.QueryOver().List(); + var localTransitions = localSession.QueryOver().List(); + + ISession remoteSession = Config.FluentCommon.CreateSession(Users.Entities.DBKind.RemoteConfig); + var remoteFeedingPaths = remoteSession.QueryOver().List(); + var remoteBenchPaths = remoteSession.QueryOver().List(); + var remoteOutputPaths = remoteSession.QueryOver().List(); + var remoteMetersPaths = remoteSession.QueryOver().List(); + var remoteTransitions = remoteSession.QueryOver().List(); + + string subMsg; + if (!IsCompatible(localFeedingPaths, remoteFeedingPaths, out subMsg)) + { + message = string.Format("Feeding: {0}", subMsg); + return false; + } + + if (!IsCompatible(localBenchPaths, remoteBenchPaths, out subMsg)) + { + message = string.Format("Bench: {0}", subMsg); + return false; + } + + if (!IsCompatible(localOutputPaths, remoteOutputPaths, out subMsg)) + { + message = string.Format("Output: {0}", subMsg); + return false; + } + + if (!IsCompatible(localMetersPaths, remoteMetersPaths, out subMsg)) + { + message = string.Format("Sensors: {0}", subMsg); + return false; + } + + if (!IsCompatible(localTransitions, remoteTransitions, out subMsg)) + { + message = string.Format("Transitions: {0}", subMsg); + return false; + } + + message = string.Empty; + return true; + } + + /// + /// + /// + /// FeedingPath, BenchPath, OutputPath, MetersPath or TransitionSequence + /// Local list of paths or transitions + /// Remote list of paths or transitions + /// Message specifying a cause of incompatibility + /// true when lists are compatible, otherwise false + private static bool IsCompatible(IList localList, IList remoteList, out string message) + { + message = "Local list is empty"; + if ((localList == null) || (localList.Count == 0) || !(localList[0] is IHasName)) return false; + + message = "Remote list is empty"; + if ((remoteList == null) || (remoteList.Count == 0) || !(remoteList[0] is IHasName)) return false; + + /// + /// Each path or transition on a remote list must exist on a local list + /// + foreach (var rItem in remoteList) + { + bool exists = false; + foreach (var lItem in localList) + { + if ((rItem as IHasName).Name == (lItem as IHasName).Name) + { + exists = true; + break; + } + } + + if (!exists) + { + message = string.Format("Remote item {0} does not exist on a local list", (rItem as IHasName).Name); + return false; + } + } + + message = string.Empty; + return true; + } + /// /// Start the state machine diff --git a/TBF/MainWnd.cs b/TBF/MainWnd.cs index 5c899ca54..488750f81 100644 --- a/TBF/MainWnd.cs +++ b/TBF/MainWnd.cs @@ -127,21 +127,42 @@ namespace TBF { try { + /// Load components, initialize the control board, etc. BenchControl.StateMachine.InitializeBoardEtc(ctrlBrdComponent); + + /// Check remote and local configuration DB compatibility + string msg; + BenchControl.GenericDevices.IBenchInfo benchInfo = BenchControl.Sequences.ProcessData.BenchInfo; + RemoteDBUse remoteDbUse = (benchInfo != null) ? benchInfo.RemoteDBUse : RemoteDBUse.LocalDBOnly; + log.FatalFormat("RemoteDbUse = {0}", remoteDbUse); + if ((remoteDbUse != RemoteDBUse.LocalDBOnly) && !BenchControl.StateMachine.IsRemoteDBCompatible(out msg)) + { + log.FatalFormat("{0} {1}", Strings.Remote_db_is_not_compatible, msg); + throw new Exception(string.Format("{0}{1}{2}", Strings.Remote_db_is_not_compatible, Environment.NewLine, msg)); + } + testProgressControls = new TestProgressControls(progressFlowLayoutPanel); + log.Fatal("Remote database is compatible"); } catch (Exception exc) { benchInitializationFailed = true; - string errMsg = string.Format(Strings.Failed_to_initialize_component_0_1_2, - BenchControl.TbfComponents.CurrentlyLoadedComponentName, - Environment.NewLine, - exc.Message); + string errMsg; + if (string.IsNullOrEmpty(BenchControl.TbfComponents.CurrentlyLoadedComponentName)) + { + errMsg = exc.Message; + } + else + { + errMsg = string.Format(Strings.Failed_to_initialize_component_0_1_2, + BenchControl.TbfComponents.CurrentlyLoadedComponentName, + Environment.NewLine, + exc.Message); + } + log.Fatal(errMsg); - MessageBox.Show(errMsg, Strings.Error, - System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Exclamation); + MessageBox.Show(errMsg, Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } } @@ -789,4 +810,4 @@ namespace TBF } } } -} \ No newline at end of file +} diff --git a/TBF/Resources/Strings.Designer.cs b/TBF/Resources/Strings.Designer.cs index 2ffd61986..50584d88a 100644 --- a/TBF/Resources/Strings.Designer.cs +++ b/TBF/Resources/Strings.Designer.cs @@ -3516,6 +3516,15 @@ namespace TBF.Resources { } } + /// + /// Looks up a localized string similar to Remote database is not compatible.. + /// + internal static string Remote_db_is_not_compatible { + get { + return ResourceManager.GetString("Remote_db_is_not_compatible", resourceCulture); + } + } + /// /// Looks up a localized string similar to < Remove. /// diff --git a/TBF/Resources/Strings.resx b/TBF/Resources/Strings.resx index 2dad68a63..aa1761fd9 100644 --- a/TBF/Resources/Strings.resx +++ b/TBF/Resources/Strings.resx @@ -1849,4 +1849,7 @@ Filter + + Remote database is not compatible. + \ No newline at end of file