From 998cc8d4d91a7f85389cd0918f127257576c2c13 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Mon, 13 Aug 2018 16:12:45 +0300 Subject: Logs and comments for PPC.UI ! --- .../PPCApplication/DefaultPPCApplicationManager.cs | 161 ++++++++++++++------- 1 file changed, 105 insertions(+), 56 deletions(-) (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs index f2fba05fe..e984b7452 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs @@ -44,6 +44,21 @@ namespace Tango.PPC.UI.PPCApplication /// public event EventHandler ModulesInitialized; + /// + /// Occurs when the all components are ready. + /// + public event EventHandler Ready; + + /// + /// Occurs when machine setup is required. + /// + public event EventHandler SetupRequired; + + /// + /// Occurs when the main window content has been rendered. + /// + public event EventHandler ContentRendered; + /// /// Gets a value indicating whether the application is shutting down. /// @@ -61,6 +76,17 @@ namespace Tango.PPC.UI.PPCApplication } } + /// + /// Gets the application build date. + /// + public String BuildDate + { + get + { + return AssemblyHelper.GetCurrentAssemblyBuildDate().ToShortDateString(); + } + } + /// /// Initializes a new instance of the class. /// @@ -72,78 +98,95 @@ namespace Tango.PPC.UI.PPCApplication { _notifiedViewModels = new List(); - PPCSettings settings = null; - - MainWindow.Instance.ContentRendered += async (_, __) => + MainWindow.Instance.ContentRendered += (_, __) => { - ContentRendered?.Invoke(this, new EventArgs()); + OnMainWindowContentRendered(); + }; + } + } - await Task.Factory.StartNew(() => - { - //TODO: Use this in the future. + /// + /// Called when the main window content has been rendered + /// + private async void OnMainWindowContentRendered() + { + LogManager.Log("Main window content rendered."); - //#if DEBUG - // DataSource = "localhost\\SQLEXPRESS"; - //#else - // DataBaseSettings.DefaultDataSource = Path.Combine(PathHelper.GetUserTangoFolder(), "DB", "Tango.db"); - //#endif + PPCSettings settings = null; - if (!File.Exists(CoreSettings.DefaultDataBaseSource)) - { - Directory.CreateDirectory(Path.GetDirectoryName(CoreSettings.DefaultDataBaseSource)); - File.Copy(Path.Combine(PathHelper.GetStartupPath(), "DB", "Tango.mdf"), CoreSettings.DefaultDataBaseSource); - } + ContentRendered?.Invoke(this, new EventArgs()); - settings = SettingsManager.Default.GetOrCreate(); - var coreSettings = SettingsManager.Default.GetOrCreate(); + await Task.Factory.StartNew(() => + { + LogManager.Log("Reading PPC settings..."); + settings = SettingsManager.Default.GetOrCreate(); - if (!SettingsManager.Default.IsFileExists()) - { - settings.Save(); - } + LogManager.Log(settings.ToJsonString()); - if (App.StartupArgs.Contains("-update_ok")) - { - settings.ApplicationState = ApplicationStates.Default; - settings.Save(); - } + LogManager.Log("Reading Core settings..."); + var coreSettings = SettingsManager.Default.GetOrCreate(); - if (settings.ApplicationState == ApplicationStates.Default) - { - ObservablesEntitiesAdapter.Instance.Initialize(); - } - }); + if (!SettingsManager.Default.IsFileExists()) + { + LogManager.Log("Settings file does not exists. creating..."); + settings.Save(); + } + if (App.StartupArgs.Contains("-update_ok")) + { + LogManager.Log("Application started with '-update_ok' startup arguments. The application has been successfully updated."); + settings.ApplicationState = ApplicationStates.Default; + settings.Save(); + } - if (settings.ApplicationState == ApplicationStates.PreSetup || settings.ApplicationState == ApplicationStates.SemiSetup) - { - SetupRequired?.Invoke(this, new EventArgs()); - } - else - { - PostSetup(); - } - }; + if (settings.ApplicationState == ApplicationStates.Default) + { + LogManager.Log("Initializing ObservablesEntitiesAdapter..."); + ObservablesEntitiesAdapter.Instance.Initialize(); + } + }); + + + if (settings.ApplicationState == ApplicationStates.PreSetup || settings.ApplicationState == ApplicationStates.SemiSetup) + { + LogManager.Log($"The application is in {settings.ApplicationState} state. database initialization skipped. Invoking setup required event!"); + SetupRequired?.Invoke(this, new EventArgs()); + } + else + { + OnDbInitialized(); } } - private void PostSetup() + /// + /// Called when the database has been initialized + /// + private void OnDbInitialized() { + LogManager.Log($"Raising {nameof(ApplicationStarted)} event..."); + ApplicationStarted?.Invoke(this, new EventArgs()); + LogManager.Log("Invoking PPC view models OnApplicationStarted methods..."); foreach (var vm in TangoIOC.Default.GetAllInstancesByBase()) { if (!_notifiedViewModels.Contains(vm)) { + LogManager.Log($"Invoking {vm.GetType().Name}.OnApplicationStarted..."); vm.OnApplicationStarted(); _notifiedViewModels.Add(vm); } } + LogManager.Log("Waiting for IPPCModuleLoader instance injection..."); TangoIOC.Default.GetInstanceWhenAvailable((loader) => { + LogManager.Log("Module loader instance has been registered. Registering for the ModulesLoaded event..."); + loader.ModulesLoaded += (x, y) => { + LogManager.Log($"{loader.UserModules.Count} loaded. Waiting for last module initialization..."); + if (loader.UserModules.Count > 0) { if (loader.UserModules.ToList().Exists(m => !m.IsInitialized)) @@ -162,19 +205,34 @@ namespace Tango.PPC.UI.PPCApplication }); } + /// + /// Finalizes the module initialization. + /// private void FinalizeModuleInitialization() { + LogManager.Log("Finalizing application initialization..."); + + LogManager.Log("Checking for un-notified PPC view models..."); foreach (var vm in TangoIOC.Default.GetAllInstancesByBase()) { if (!_notifiedViewModels.Contains(vm)) { + LogManager.Log($"Invoking {vm.GetType().Name}.OnApplicationStarted..."); vm.OnApplicationStarted(); _notifiedViewModels.Add(vm); } } + LogManager.Log($"Invoking {nameof(ModulesInitialized)} event."); + ModulesInitialized?.Invoke(this, new EventArgs()); + + LogManager.Log("Initializing Machine Provider..."); _machineProvider.Init(); + + LogManager.Log("Applications initialization completed!"); + + LogManager.Log($"Invoking {nameof(Ready)} event."); Ready?.Invoke(this, new EventArgs()); } @@ -183,6 +241,10 @@ namespace Tango.PPC.UI.PPCApplication /// public void ShutDown() { + //TODO: Needs some work on logging and shutdown procedures! Do I really need this? + + LogManager.Log("Shutting down application..."); + IsShuttingDown = true; foreach (var vm in TangoIOC.Default.GetAllInstancesByBase()) @@ -190,18 +252,5 @@ namespace Tango.PPC.UI.PPCApplication vm.OnApplicationShuttingDown(); } } - - public event EventHandler Ready; - public event EventHandler SetupRequired; - public event EventHandler ContentRendered; - - public String BuildDate - { - get - { - return AssemblyHelper.GetCurrentAssemblyBuildDate().ToShortDateString(); - } - } - } } -- cgit v1.3.1