Skip to content

Commit

Permalink
Clean up abnormal exit during startup
Browse files Browse the repository at this point in the history
Remove Telescope dependency on AstroUtils
Bump version to 6.6.0.15
  • Loading branch information
astroman133 committed Jul 21, 2022
1 parent a022122 commit d371677
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 90 deletions.
8 changes: 3 additions & 5 deletions DeviceHub/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -94,7 +94,5 @@ private static T GetAssemblyAttribute<T>( bool inherit )

return (T)assembly.GetCustomAttributes( typeof( T ), inherit ).First();
}


}
}
98 changes: 55 additions & 43 deletions DeviceHub/Business Object Classes/ASCOM Classes/LocalServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type> _driverTypes; // Served COM object types
private static List<Type> _driverTypes; // Served COM object types
private static List<ClassFactory> _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();
Expand All @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
31 changes: 31 additions & 0 deletions DeviceHub/Business Object Classes/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
}
Expand Down
22 changes: 1 addition & 21 deletions DeviceHub/DeviceManagers/TelescopeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading;
using System.Threading.Tasks;

using ASCOM.Astrometry.AstroUtils;
using ASCOM.Astrometry.Transform;
using ASCOM.DeviceInterface;

Expand All @@ -24,16 +23,13 @@ public partial class TelescopeManager : DeviceManagerBase, ITelescopeManager, ID

private static TelescopeManager _instance = null;

private static AstroUtils AstroUtils { get; set; }

public static TelescopeManager Instance
{
get
{
if ( _instance == null )
{
_instance = new TelescopeManager();
throw new Exception( "Unable to instantiate the TelescopeManager!" );
}

return _instance;
Expand All @@ -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 )
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions DeviceHub/ViewModel Classes/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -95,7 +95,7 @@ public MainWindowViewModel()
Messenger.Default.Register<ObjectCountMessage>( this, ( action ) => UpdateObjectsCount( action ) );

LogAppMessage( "Application Initializaiton is complete.", caller );

Globals.CloseAppLogger();
}

#region Public Properties
Expand Down
2 changes: 1 addition & 1 deletion Inno Setup/ASCOM Device Hub Setup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -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\"
Expand Down
2 changes: 1 addition & 1 deletion ProductAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" )]
[assembly: AssemblyVersion( "6.6.0.15" )]

0 comments on commit d371677

Please sign in to comment.