From 6fb22273d15ed476ccbd3820de6af9bc0deee793 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Mon, 26 Nov 2018 14:19:06 +0200 Subject: Implemented Application Initialization Error and Handling. --- .../Application/IPPCApplicationManager.cs | 10 +++ .../Tango.PPC.Common/Navigation/NavigationView.cs | 1 + .../PPC/Tango.PPC.UI/Images/warning-red.png | Bin 0 -> 8274 bytes .../PPCApplication/DefaultPPCApplicationManager.cs | 95 +++++++++++++++------ .../PPC/Tango.PPC.UI/Tango.PPC.UI.csproj | 9 ++ .../PPC/Tango.PPC.UI/ViewModelLocator.cs | 9 ++ .../Tango.PPC.UI/ViewModels/LoadingErrorViewVM.cs | 43 ++++++++++ .../PPC/Tango.PPC.UI/ViewModels/LoadingViewVM.cs | 11 +++ .../PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml | 28 ++++++ .../Tango.PPC.UI/Views/LoadingErrorView.xaml.cs | 28 ++++++ .../PPC/Tango.PPC.UI/Views/MainView.xaml | 1 + 11 files changed, 208 insertions(+), 27 deletions(-) create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/Images/warning-red.png create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/LoadingErrorViewVM.cs create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml create mode 100644 Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml.cs (limited to 'Software/Visual_Studio/PPC') diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Application/IPPCApplicationManager.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Application/IPPCApplicationManager.cs index 0feb551b1..003229e65 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Application/IPPCApplicationManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Application/IPPCApplicationManager.cs @@ -40,6 +40,11 @@ namespace Tango.PPC.Common.Application /// event EventHandler SetupRequired; + /// + /// Occurs when the application has encountered an error when initializing. + /// + event EventHandler ApplicationInitializationError; + /// /// Gets a value indicating whether the application is shutting down. /// @@ -50,6 +55,11 @@ namespace Tango.PPC.Common.Application /// void ShutDown(); + /// + /// Restarts the application. + /// + void Restart(); + /// /// Gets the application version. /// diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs index c86cf148b..eefb298f7 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/Navigation/NavigationView.cs @@ -12,6 +12,7 @@ namespace Tango.PPC.Common.Navigation public enum NavigationView { LoadingView, + LoadingErrorView, LayoutView, LoginView, MachineSetupView, diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Images/warning-red.png b/Software/Visual_Studio/PPC/Tango.PPC.UI/Images/warning-red.png new file mode 100644 index 000000000..97e703bb3 Binary files /dev/null and b/Software/Visual_Studio/PPC/Tango.PPC.UI/Images/warning-red.png differ 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 714b83dfa..4021d89ef 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/PPCApplication/DefaultPPCApplicationManager.cs @@ -23,6 +23,7 @@ using Tango.SQLExaminer; using System.Data.SqlClient; using Tango.BL.Builders; using Tango.PPC.Common.Threading; +using System.Diagnostics; namespace Tango.PPC.UI.PPCApplication { @@ -63,6 +64,11 @@ namespace Tango.PPC.UI.PPCApplication /// public event EventHandler ContentRendered; + /// + /// Occurs when the application has encountered an error when initializing. + /// + public event EventHandler ApplicationInitializationError; + /// /// Gets a value indicating whether the application is shutting down. /// @@ -125,46 +131,72 @@ namespace Tango.PPC.UI.PPCApplication { PPCSettings settings = null; + bool initialized = false; + await Task.Factory.StartNew(() => { - LogManager.Log("Reading PPC settings..."); - settings = SettingsManager.Default.GetOrCreate(); + try + { + LogManager.Log("Reading PPC settings..."); + settings = SettingsManager.Default.GetOrCreate(); - LogManager.Log(settings.ToJsonString()); + LogManager.Log(settings.ToJsonString()); - LogManager.Log("Reading Core settings..."); - var coreSettings = SettingsManager.Default.GetOrCreate(); + LogManager.Log("Reading Core settings..."); + var coreSettings = SettingsManager.Default.GetOrCreate(); - if (!SettingsManager.Default.IsFileExists()) - { - LogManager.Log("Settings file does not exists. creating..."); - settings.Save(); - } + 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.Ready; - 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.Ready; + settings.Save(); + } - if (settings.ApplicationState == ApplicationStates.Ready) + if (settings.ApplicationState == ApplicationStates.Ready) + { + LogManager.Log("Initializing ObservablesStaticCollections..."); + ObservablesStaticCollections.Instance.Initialize(); + LogManager.Log("Loading machine from database..."); + _machine = new MachineBuilder(ObservablesContext.CreateDefault()).SetFirst().WithOrganization().WithConfiguration().Build(); + } + + initialized = true; + } + catch (Exception ex) { - LogManager.Log("Loading machine from database..."); - ObservablesStaticCollections.Instance.Initialize(); - _machine = new MachineBuilder(ObservablesContext.CreateDefault()).SetFirst().WithOrganization().WithConfiguration().Build(); + LogManager.Log(ex, "Application Initialization Error!"); + ApplicationInitializationError?.Invoke(this, ex); + return; } }); - if (settings.ApplicationState == ApplicationStates.PreSetup || settings.ApplicationState == ApplicationStates.SemiSetup) + if (initialized) { - LogManager.Log($"The application is in {settings.ApplicationState} state. database initialization skipped. Invoking setup required event!"); - SetupRequired?.Invoke(this, new EventArgs()); - } - else - { - PostDbInitialize(); + try + { + 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 + { + PostDbInitialize(); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Application Post Initialization Error!"); + ApplicationInitializationError?.Invoke(this, ex); + return; + } } } @@ -273,5 +305,14 @@ namespace Tango.PPC.UI.PPCApplication vm.OnApplicationShuttingDown(); } } + + /// + /// Restarts the application. + /// + public void Restart() + { + Process.Start(Application.ResourceAssembly.Location); + Environment.Exit(0); + } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj b/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj index 2ff219906..e552d096b 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Tango.PPC.UI.csproj @@ -134,6 +134,7 @@ + @@ -148,6 +149,9 @@ LayoutView.xaml + + LoadingErrorView.xaml + LoadingView.xaml @@ -203,6 +207,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + Designer MSBuild:Compile @@ -381,6 +389,7 @@ Tango.ColorLib.dll PreserveNewest + diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs index 703a2f6ae..4bf83242f 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModelLocator.cs @@ -88,6 +88,7 @@ namespace Tango.PPC.UI TangoIOC.Default.Register(); TangoIOC.Default.Register(); TangoIOC.Default.Register(); + TangoIOC.Default.Register(); TangoIOC.Default.GetInstance().ContentRendered += (_, __) => @@ -161,5 +162,13 @@ namespace Tango.PPC.UI return TangoIOC.Default.GetInstance(); } } + + public static LoadingErrorViewVM LoadingErrorViewVM + { + get + { + return TangoIOC.Default.GetInstance(); + } + } } } \ No newline at end of file diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/LoadingErrorViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/LoadingErrorViewVM.cs new file mode 100644 index 000000000..3c05e3690 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/LoadingErrorViewVM.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core.Commands; +using Tango.PPC.Common; +using Tango.PPC.Common.Navigation; + +namespace Tango.PPC.UI.ViewModels +{ + public class LoadingErrorViewVM : PPCViewModel, INavigationObjectReceiver + { + private String _error; + public String Error + { + get { return _error; } + set { _error = value; RaisePropertyChangedAuto(); } + } + + public RelayCommand RestartCommand { get; set; } + + public LoadingErrorViewVM() + { + RestartCommand = new RelayCommand(Restart); + } + + public override void OnApplicationStarted() + { + + } + + public void OnNavigatedToWithObject(Exception ex) + { + Error = ex.Message; + } + + private void Restart() + { + ApplicationManager.Restart(); + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/LoadingViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/LoadingViewVM.cs index 486829103..5598362be 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/LoadingViewVM.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/LoadingViewVM.cs @@ -54,6 +54,17 @@ namespace Tango.PPC.UI.ViewModels { Task.Delay(1000).ContinueWith((x) => { IsLoading = true; }); } + + applicationManager.ApplicationInitializationError += ApplicationManager_ApplicationInitializationError; + } + + private void ApplicationManager_ApplicationInitializationError(object sender, Exception ex) + { + InvokeUI(() => + { + NavigationManager.NavigateWithObject(NavigationView.LoadingErrorView, ex); + IsLoading = false; + }); } /// diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml b/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml new file mode 100644 index 000000000..7682ba4ce --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml @@ -0,0 +1,28 @@ + + + + + + Application Loading Error + The application has failed to load properly due to the following reason. + + + + + + RESTART + + + + + diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml.cs new file mode 100644 index 000000000..0713cc320 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LoadingErrorView.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace Tango.PPC.UI.Views +{ + /// + /// Interaction logic for LoadingErrorView.xaml + /// + public partial class LoadingErrorView : UserControl + { + public LoadingErrorView() + { + InitializeComponent(); + } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/MainView.xaml b/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/MainView.xaml index 7eca6dba6..4ca7e39ec 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/MainView.xaml +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/MainView.xaml @@ -77,6 +77,7 @@ + -- cgit v1.3.1