From d371677756adab52a105ee83aa7de41e1681bc1e Mon Sep 17 00:00:00 2001 From: Rick Burke Date: Thu, 21 Jul 2022 15:38:33 -0700 Subject: [PATCH] Clean up abnormal exit during startup Remove Telescope dependency on AstroUtils Bump version to 6.6.0.15 --- DeviceHub/App.xaml.cs | 8 +- .../ASCOM Classes/LocalServer.cs | 98 +++++++++++-------- DeviceHub/Business Object Classes/Globals.cs | 31 ++++++ .../DevHubTelescopeStatus.cs | 34 +++---- DeviceHub/DeviceManagers/TelescopeManager.cs | 22 +---- .../ViewModel Classes/MainWindowViewModel.cs | 4 +- Inno Setup/ASCOM Device Hub Setup.iss | 2 +- ProductAssemblyInfo.cs | 2 +- 8 files changed, 111 insertions(+), 90 deletions(-) diff --git a/DeviceHub/App.xaml.cs b/DeviceHub/App.xaml.cs index 5deb7f0..80b5057 100644 --- a/DeviceHub/App.xaml.cs +++ b/DeviceHub/App.xaml.cs @@ -62,10 +62,10 @@ protected override void OnStartup( StartupEventArgs e ) } } } - catch ( Exception xcp ) + catch ( Exception ) { - string msg = $"DeviceHub caught a fatal exception during startup\r\n{xcp.Message}"; - MessageBox.Show( msg, "DeviceHub", MessageBoxButton.OK, MessageBoxImage.Stop ); + //string msg = $"DeviceHub caught a fatal exception during startup\r\n{xcp.Message}"; + //MessageBox.Show( msg, "DeviceHub", MessageBoxButton.OK, MessageBoxImage.Stop ); if ( Application.Current != null ) { @@ -94,7 +94,5 @@ private static T GetAssemblyAttribute( bool inherit ) return (T)assembly.GetCustomAttributes( typeof( T ), inherit ).First(); } - - } } diff --git a/DeviceHub/Business Object Classes/ASCOM Classes/LocalServer.cs b/DeviceHub/Business Object Classes/ASCOM Classes/LocalServer.cs index e7038d0..7e80338 100644 --- a/DeviceHub/Business Object Classes/ASCOM Classes/LocalServer.cs +++ b/DeviceHub/Business Object Classes/ASCOM Classes/LocalServer.cs @@ -25,7 +25,7 @@ public static class Server private static int _domesInUse; // Keeps a count on the total number of domes alive. private static int _focusersInUse; // Keeps a count on the total number of focusers alive. private static int _serverLocks; // Keeps a lock count on this application. - private static List _driverTypes; // Served COM object types + private static List _driverTypes; // Served COM object types private static List _classFactories; // Served COM object class factories private static readonly string _appId = "{4f90ea04-044f-444e-963e-b52db2a87575}"; // Our AppId private static readonly Object _lockObject = new object(); @@ -34,7 +34,7 @@ public static class Server private static TraceLogger Logger { get; set; } - private static GarbageCollection GarbageCollector {get; set;} + private static GarbageCollection GarbageCollector { get; set; } private static MainWindow MainWindow { get; set; } // Reference to the main view private static MainWindowViewModel ViewModel { get; set; } // Reference to the main view model @@ -157,72 +157,84 @@ internal static void Startup( string[] args ) ServiceInjector.InjectUIServices( MainWindow ); - // Create the ViewModel + try + { + // Create the ViewModel + // Any errors starting the view models or device managers will raise an exception and short circuit + // the rest of the startup. - ViewModel = new MainWindowViewModel(); + ViewModel = new MainWindowViewModel(); - Logger.LogMessage( logId, "Setting the data context for the main view" ); + Logger.LogMessage( logId, "Setting the data context for the main view" ); - MainWindow.DataContext = ViewModel; - MainWindow.Closing += MainWindow_Closing; + MainWindow.DataContext = ViewModel; + MainWindow.Closing += MainWindow_Closing; - // Load the saved settings to ensure that everyone up-to-date. Be sure to do this - // after the main window is created so we can set its location. + // Load the saved settings to ensure that everyone up-to-date. Be sure to do this + // after the main window is created so we can set its location. - Logger.LogMessage( logId, "Loading the application settings" ); + Logger.LogMessage( logId, "Loading the application settings" ); - AppSettingsManager.LoadAppSettings(); + AppSettingsManager.LoadAppSettings(); - Logger.LogMessage( logId, "Loading the device driver settings" ); + Logger.LogMessage( logId, "Loading the device driver settings" ); - LoadDeviceSettings(); + LoadDeviceSettings(); - // Register the class factories of the served objects + // Register the class factories of the served objects - Logger.LogMessage( logId, "Registering class factories" ); + Logger.LogMessage( logId, "Registering class factories" ); - RegisterClassFactories(); + RegisterClassFactories(); - Logger.LogMessage( logId, "Starting garbage collection" ); + Logger.LogMessage( logId, "Starting garbage collection" ); - StartGarbageCollection( 60000 ); // Collect garbage once a minute. - - try - { - Logger.LogMessage( logId, "Starting main view" ); + StartGarbageCollection( 60000 ); // Collect garbage once a minute. - ShowMainWindow(); + try + { + Logger.LogMessage( logId, "Starting main view" ); - Logger.LogMessage( logId, "The main view has closed" ); - } - finally - { - Logger.LogMessage( logId, "Saving the application settings" ); - AppSettingsManager.SaveAppSettings(); + ShowMainWindow(); - // Revoke the class factories immediately. - // Don't wait until the thread has stopped before - // we perform revocation!!! + Logger.LogMessage( logId, "The main view has closed" ); + } + finally + { + Logger.LogMessage( logId, "Saving the application settings" ); + AppSettingsManager.SaveAppSettings(); - RevokeClassFactories(); + // Revoke the class factories immediately. + // Don't wait until the thread has stopped before + // we perform revocation!!! - Logger.LogMessage( logId, "Disposing the main view and viewmodel." ); + RevokeClassFactories(); - MainWindow.DataContext = null; - MainWindow = null; - ViewModel.Dispose(); - ViewModel = null; + Logger.LogMessage( logId, "Disposing the main view and viewmodel." ); - Logger.LogMessage( logId, "Unregistering all services" ); + MainWindow.DataContext = null; + MainWindow = null; + ViewModel.Dispose(); + ViewModel = null; - ServiceContainer.Instance.ClearAllServices(); + Logger.LogMessage( logId, "Unregistering all services" ); - // Now stop the Garbage Collector task. + ServiceContainer.Instance.ClearAllServices(); - Logger.LogMessage( logId, "Stopping garbage collection" ); + // Now stop the Garbage Collector task. - StopGarbageCollection(); + Logger.LogMessage( logId, "Stopping garbage collection" ); + StopGarbageCollection(); + } + } + catch ( Exception ) + { + // Any exception from starting the view models or device managers will bring us here. + // The exception was already logged via the AppLogger so we have nothing more to do but close the Logger and return. + } + finally + { Logger.LogMessage( logId, "Local server is shutting down" ); Logger.Dispose(); } diff --git a/DeviceHub/Business Object Classes/Globals.cs b/DeviceHub/Business Object Classes/Globals.cs index 976cd14..a42ae08 100644 --- a/DeviceHub/Business Object Classes/Globals.cs +++ b/DeviceHub/Business Object Classes/Globals.cs @@ -90,5 +90,36 @@ public static TraceLogger AppLogger return _appLogger; } } + + public static double ConditionHA( double ha ) + { + double lowerBound = -12.0; + double upperBound = 12.0; + double range = upperBound - lowerBound; + + double retval = ha; + + while ( retval < lowerBound ) + { + retval += range; + } + + while ( retval > upperBound ) + { + retval -= range; + } + + return retval; + } + + public static void CloseAppLogger() + { + if ( _appLogger != null ) + { + _appLogger.Enabled = false; + _appLogger.Dispose(); + _appLogger = null; + } + } } } diff --git a/DeviceHub/Business Object Classes/Telescope Classes/DevHubTelescopeStatus.cs b/DeviceHub/Business Object Classes/Telescope Classes/DevHubTelescopeStatus.cs index 773991b..6d19c87 100644 --- a/DeviceHub/Business Object Classes/Telescope Classes/DevHubTelescopeStatus.cs +++ b/DeviceHub/Business Object Classes/Telescope Classes/DevHubTelescopeStatus.cs @@ -121,7 +121,7 @@ public double CalculateHourAngle( double rightAscension ) // This method does not validate the RA!! double retval = SiderealTime - rightAscension; - retval = ConditionHA( retval ); + retval = Globals.ConditionHA( retval ); return retval; } @@ -176,26 +176,26 @@ private bool CalculateCounterWeightUp( PierSide pierSide, double hourAngle ) return retval; } - private double ConditionHA( double ha ) - { - double lowerBound = -12.0; - double upperBound = 12.0; - double range = upperBound - lowerBound; + //private double ConditionHA( double ha ) + //{ + // double lowerBound = -12.0; + // double upperBound = 12.0; + // double range = upperBound - lowerBound; - double retval = ha; + // double retval = ha; - while ( retval < lowerBound ) - { - retval += range; - } + // while ( retval < lowerBound ) + // { + // retval += range; + // } - while ( retval > upperBound ) - { - retval -= range; - } + // while ( retval > upperBound ) + // { + // retval -= range; + // } - return retval; - } + // return retval; + //} #endregion } diff --git a/DeviceHub/DeviceManagers/TelescopeManager.cs b/DeviceHub/DeviceManagers/TelescopeManager.cs index 9051a19..f6f6302 100644 --- a/DeviceHub/DeviceManagers/TelescopeManager.cs +++ b/DeviceHub/DeviceManagers/TelescopeManager.cs @@ -6,7 +6,6 @@ using System.Threading; using System.Threading.Tasks; -using ASCOM.Astrometry.AstroUtils; using ASCOM.Astrometry.Transform; using ASCOM.DeviceInterface; @@ -24,8 +23,6 @@ public partial class TelescopeManager : DeviceManagerBase, ITelescopeManager, ID private static TelescopeManager _instance = null; - private static AstroUtils AstroUtils { get; set; } - public static TelescopeManager Instance { get @@ -33,7 +30,6 @@ public static TelescopeManager Instance if ( _instance == null ) { _instance = new TelescopeManager(); - throw new Exception( "Unable to instantiate the TelescopeManager!" ); } return _instance; @@ -44,23 +40,7 @@ public static TelescopeManager Instance static TelescopeManager() { - string caller = "TelescopeManager static ctor"; TelescopeID = ""; - LogAppMessage( "Initialization started.", caller ); - - try - { - LogAppMessage( "Creating AstroUtils instance", caller ); - - AstroUtils = new AstroUtils(); - } - catch (Exception xcp) - { - string msg = $"Unable to create an instance of AstroUtils. Details follow:\r\n\r\n{xcp}"; - LogAppMessage( msg, "TelescopeManager static constructor" ); - } - - LogAppMessage( "Initialization complete", caller ); } public static void SetTelescopeID( string id ) @@ -741,7 +721,7 @@ public PierSide GetTargetSideOfPier( double rightAscension, double declination ) { // Unable to get side-of-pier for this German Equatorial Mount so we need to simulate it. - double hourAngle = AstroUtils.ConditionHA( Status.SiderealTime - rightAscension ); + double hourAngle = Globals.ConditionHA( Status.SiderealTime - rightAscension ); PierSide currentSOP = Status.SideOfPier; PierSide destinationSOP = currentSOP; // Favor the current side-of-pier for 0 hour angle; diff --git a/DeviceHub/ViewModel Classes/MainWindowViewModel.cs b/DeviceHub/ViewModel Classes/MainWindowViewModel.cs index 398a819..a628c86 100644 --- a/DeviceHub/ViewModel Classes/MainWindowViewModel.cs +++ b/DeviceHub/ViewModel Classes/MainWindowViewModel.cs @@ -76,7 +76,7 @@ public MainWindowViewModel() sb.Append( "Please contact the ASCOM Support Team for assistance." ); ShowMessage( sb.ToString(), "Fatal Device Hub Startup Error", MessageBoxButton.OK, MessageBoxImage.Error ); - Application.Current.Shutdown(); + throw; } LogAppMessage( "Setting the active device to Telescope", caller ); @@ -95,7 +95,7 @@ public MainWindowViewModel() Messenger.Default.Register( this, ( action ) => UpdateObjectsCount( action ) ); LogAppMessage( "Application Initializaiton is complete.", caller ); - + Globals.CloseAppLogger(); } #region Public Properties diff --git a/Inno Setup/ASCOM Device Hub Setup.iss b/Inno Setup/ASCOM Device Hub Setup.iss index 7b4febd..531a72e 100644 --- a/Inno Setup/ASCOM Device Hub Setup.iss +++ b/Inno Setup/ASCOM Device Hub Setup.iss @@ -4,7 +4,7 @@ ; #define MyAppName "ASCOM.DeviceHub" -#define MyAppVersion "6.6.0.14" +#define MyAppVersion "6.6.0.15" #define MyDestSubdirName "DeviceHub" ; #define MyPlatformRoot "D:\Github Repos\ASCOMPlatform\" #define MyPlatformRoot "D:\My Projects\Visual Studio 2022\Ascom\" diff --git a/ProductAssemblyInfo.cs b/ProductAssemblyInfo.cs index 84cd29e..2b303fc 100644 --- a/ProductAssemblyInfo.cs +++ b/ProductAssemblyInfo.cs @@ -20,4 +20,4 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion( "6.6.0.14" )] \ No newline at end of file +[assembly: AssemblyVersion( "6.6.0.15" )] \ No newline at end of file