diff --git a/Meade.net.Focuser.UnitTests/FocuserUnitTests.cs b/Meade.net.Focuser.UnitTests/FocuserUnitTests.cs index 1056378..2ba4e76 100644 --- a/Meade.net.Focuser.UnitTests/FocuserUnitTests.cs +++ b/Meade.net.Focuser.UnitTests/FocuserUnitTests.cs @@ -28,7 +28,8 @@ namespace Meade.net.Focuser.UnitTests TraceLogger = false, ComPort = "TestCom1", GuideRateArcSecondsPerSecond = 1.23, - Precision = "Unchanged" + Precision = "Unchanged", + GuidingStyle = "Auto" }; _utilMock = new Mock(); diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index 3d79ae1..a0ccfe8 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -38,7 +38,7 @@ namespace ASCOM.Meade.net [ServedClassName("Meade Generic")] [ClassInterface(ClassInterfaceType.None)] [ComVisible(true)] - public class Telescope : ReferenceCountedObjectBase, ITelescopeV3 + public class Telescope : MeadeTelescopeBase, ITelescopeV3 { /// /// ASCOM DeviceID (COM ProgID) for this driver. @@ -47,13 +47,6 @@ namespace ASCOM.Meade.net //internal static string driverID = "ASCOM.Meade.net.Telescope"; private static readonly string DriverId = Marshal.GenerateProgIdForType(MethodBase.GetCurrentMethod().DeclaringType ?? throw new System.InvalidOperationException()); - /// - /// Driver description that displays in the ASCOM Chooser. - /// - private static readonly string DriverDescription = "Meade Generic"; - - private static string _comPort; // Variables to hold the currrent device configuration - /// /// Private variable to hold an ASCOM Utilities object /// @@ -66,19 +59,12 @@ namespace ASCOM.Meade.net private readonly IAstroUtils _astroUtilities; private readonly IAstroMaths _astroMaths; - - /// - /// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify) - /// - private TraceLogger _tl; - - private readonly ISharedResourcesWrapper _sharedResourcesWrapper; - + /// /// Initializes a new instance of the class. /// Must be public for COM registration. /// - public Telescope() + public Telescope() : base() { try { @@ -87,7 +73,6 @@ namespace ASCOM.Meade.net _utilities = util; _utilitiesExtra = util; //Initialise util object _astroUtilities = new AstroUtils(); // Initialise astro utilities object - _sharedResourcesWrapper = new SharedResourcesWrapper(); _astroMaths = new AstroMaths.AstroMaths(); Initialise(); @@ -121,33 +106,17 @@ namespace ASCOM.Meade.net sb.AppendLine(); } - public Telescope( IUtil util, IUtilExtra utilExtra, IAstroUtils astroUtilities, ISharedResourcesWrapper sharedResourcesWrapper, IAstroMaths astroMaths) + public Telescope( IUtil util, IUtilExtra utilExtra, IAstroUtils astroUtilities, ISharedResourcesWrapper sharedResourcesWrapper, IAstroMaths astroMaths) : base(sharedResourcesWrapper) { _utilities = util; //Initialise util object _utilitiesExtra = utilExtra; //Initialise util object _astroUtilities = astroUtilities; // Initialise astro utilities object - _sharedResourcesWrapper = sharedResourcesWrapper; _astroMaths = astroMaths; Initialise(); } - private double _guideRate; private bool _isGuiding; - - private void Initialise() - { - //todo move the TraceLogger out to a factory class. - _tl = new TraceLogger("", "Meade.Generic.Telescope"); - ReadProfile(); // Read device configuration from the ASCOM Profile store - - IsConnected = false; // Initialise connected to false - - LogMessage("Telescope", "Completed initialisation"); - LogMessage("Telescope", $"Driver version: {DriverVersion}"); - } - - // // PUBLIC COM INTERFACE ITelescopeV3 IMPLEMENTATION // @@ -704,36 +673,6 @@ namespace ASCOM.Meade.net } } - public string Description - { - get - { - LogMessage("Description Get", DriverDescription); - return DriverDescription; - } - } - - public string DriverInfo - { - get - { - string driverInfo = $"{Description} .net driver. Version: {DriverVersion}"; - LogMessage("DriverInfo Get", driverInfo); - return driverInfo; - } - } - - public string DriverVersion - { - get - { - Version version = Assembly.GetExecutingAssembly().GetName().Version; - string driverVersion = $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}"; - LogMessage("DriverVersion Get", driverVersion); - return driverVersion; - } - } - public short InterfaceVersion { // set by the driver wizard @@ -2095,8 +2034,6 @@ namespace ASCOM.Meade.net } private DriveRates _trackingRate = DriveRates.driveSidereal; - private string _precision; - private string _guidingStyle; public DriveRates TrackingRate { @@ -2367,11 +2304,6 @@ namespace ASCOM.Meade.net #endregion - /// - /// Returns true if there is a valid connection to the driver hardware - /// - private bool IsConnected { get; set; } - /// /// Use this function to throw an exception if we aren't connected to the hardware /// @@ -2384,25 +2316,6 @@ namespace ASCOM.Meade.net } } - /// - /// Read the device configuration from the ASCOM Profile store - /// - private void ReadProfile() - { - ProfileProperties profileProperties = _sharedResourcesWrapper.ReadProfile(); - _tl.Enabled = profileProperties.TraceLogger; - _comPort = profileProperties.ComPort; - _guideRate = profileProperties.GuideRateArcSecondsPerSecond; - _precision = profileProperties.Precision; - _guidingStyle = profileProperties.GuidingStyle.ToLower(); - - LogMessage("ReadProfile", $"Trace logger enabled: {_tl.Enabled}"); - LogMessage("ReadProfile", $"Com Port: {_comPort}"); - LogMessage("ReadProfile", $"Guide Rate: {_guideRate}"); - LogMessage("ReadProfile", $"Precision: {_precision}"); - LogMessage("ReadProfile", $"Guiding Style: {_guidingStyle}"); - } - private void WriteProfile() { var profileProperties = new ProfileProperties @@ -2414,18 +2327,6 @@ namespace ASCOM.Meade.net _sharedResourcesWrapper.WriteProfile(profileProperties); } - - /// - /// Log helper function that takes formatted strings and arguments - /// - /// - /// - /// - private void LogMessage(string identifier, string message, params object[] args) - { - var msg = string.Format(message, args); - _tl.LogMessage(identifier, msg); - } #endregion } } diff --git a/Meade.net.focuser/Focuser.cs b/Meade.net.focuser/Focuser.cs index e02b527..5e9f23e 100644 --- a/Meade.net.focuser/Focuser.cs +++ b/Meade.net.focuser/Focuser.cs @@ -30,7 +30,7 @@ namespace ASCOM.Meade.net [ServedClassName("Meade Generic")] [ClassInterface(ClassInterfaceType.None)] [ComVisible(true)] - public class Focuser : ReferenceCountedObjectBase, IFocuserV3 + public class Focuser : MeadeTelescopeBase, IFocuserV3 { /// /// ASCOM DeviceID (COM ProgID) for this driver. @@ -38,68 +38,32 @@ namespace ASCOM.Meade.net /// //internal static string driverID = "ASCOM.Meade.net.Focuser"; private static readonly string DriverId = Marshal.GenerateProgIdForType(MethodBase.GetCurrentMethod().DeclaringType ?? throw new System.InvalidOperationException()); - // TODO Change the descriptive string for your driver then remove this line - /// - /// Driver description that displays in the ASCOM Chooser. - /// - private static readonly string DriverDescription = "Meade Generic"; - - private static string _comPort; // Variables to hold the currrent device configuration - - private static int _backlashCompensation; - - private static bool _reverseFocusDirection; - - private static bool _useDynamicBreaking; /// /// Private variable to hold an ASCOM Utilities object /// private readonly IUtil _utilities; - - /// - /// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify) - /// - private static TraceLogger _tl; - - private readonly ISharedResourcesWrapper _sharedResourcesWrapper; - + /// /// Initializes a new instance of the class. /// Must be public for COM registration. /// - public Focuser() + public Focuser() : base() { //todo move this out to IOC var util = new Util(); //Initialise util object _utilities = util; - _sharedResourcesWrapper = new SharedResourcesWrapper(); Initialise(); } - public Focuser(IUtil util, ISharedResourcesWrapper sharedResourcesWrapper) + public Focuser(IUtil util, ISharedResourcesWrapper sharedResourcesWrapper) : base(sharedResourcesWrapper) { _utilities = util; - _sharedResourcesWrapper = sharedResourcesWrapper; Initialise(); } - - private void Initialise() - { - //todo move the TraceLogger out to a factory class. - _tl = new TraceLogger("", "Meade.Generic.focusser"); - - ReadProfile(); // Read device configuration from the ASCOM Profile store - - IsConnected = false; // Initialise connected to false - - LogMessage("Focuser", "Completed initialisation"); - LogMessage("Focuser", $"Driver version: {DriverVersion}"); - } - - + // // PUBLIC COM INTERFACE IFocuserV3 IMPLEMENTATION // @@ -217,36 +181,6 @@ namespace ASCOM.Meade.net } } - public string Description - { - get - { - _tl.LogMessage("Description Get", DriverDescription); - return DriverDescription; - } - } - - public string DriverInfo - { - get - { - string driverInfo = $"{Description} .net driver. Version: {DriverVersion}"; - LogMessage("DriverInfo Get", driverInfo); - return driverInfo; - } - } - - public string DriverVersion - { - get - { - Version version = Assembly.GetExecutingAssembly().GetName().Version; - string driverVersion = $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}"; - LogMessage("DriverVersion Get", driverVersion); - return driverVersion; - } - } - public short InterfaceVersion { // set by the driver wizard @@ -476,6 +410,7 @@ namespace ASCOM.Meade.net #region ASCOM Registration private static IProfileFactory _profileFactory; + public static IProfileFactory ProfileFactory { get => _profileFactory ?? (_profileFactory = new ProfileFactory()); @@ -553,12 +488,7 @@ namespace ASCOM.Meade.net } #endregion - - /// - /// Returns true if there is a valid connection to the driver hardware - /// - private bool IsConnected { get; set; } - + /// /// Use this function to throw an exception if we aren't connected to the hardware /// @@ -570,36 +500,6 @@ namespace ASCOM.Meade.net throw new NotConnectedException($"Not connected to focuser when trying to execute: {message}"); } } - - /// - /// Read the device configuration from the ASCOM Profile store - /// - private void ReadProfile() - { - var profileProperties = _sharedResourcesWrapper.ReadProfile(); - _tl.Enabled = profileProperties.TraceLogger; - _comPort = profileProperties.ComPort; - _backlashCompensation = profileProperties.BacklashCompensation; - _reverseFocusDirection = profileProperties.ReverseFocusDirection; - _useDynamicBreaking = profileProperties.DynamicBreaking; - - LogMessage("ReadProfile", $"Trace logger enabled: {_tl.Enabled}"); - LogMessage("ReadProfile", $"Com Port: {_comPort}"); - LogMessage("ReadProfile", $"Backlash Steps: {_backlashCompensation}"); - LogMessage("ReadProfile", $"Dynamic breaking: {_useDynamicBreaking}"); - } - - /// - /// Log helper function that takes formatted strings and arguments - /// - /// - /// - /// - private static void LogMessage(string identifier, string message, params object[] args) - { - var msg = string.Format(message, args); - _tl.LogMessage(identifier, msg); - } #endregion } } diff --git a/Meade.net/Meade.net.csproj b/Meade.net/Meade.net.csproj index 18ea8c1..f3a6c03 100644 --- a/Meade.net/Meade.net.csproj +++ b/Meade.net/Meade.net.csproj @@ -134,6 +134,7 @@ + diff --git a/Meade.net/MeadeTelescopeBase.cs b/Meade.net/MeadeTelescopeBase.cs new file mode 100644 index 0000000..43b0b59 --- /dev/null +++ b/Meade.net/MeadeTelescopeBase.cs @@ -0,0 +1,127 @@ +using System; +using System.Reflection; +using System.Runtime.InteropServices; +using ASCOM.Meade.net.Wrapper; +using ASCOM.Utilities; + +namespace ASCOM.Meade.net +{ + [ComVisible(false)] + public class MeadeTelescopeBase : ReferenceCountedObjectBase + { + /// + /// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify) + /// + protected static TraceLogger _tl; + + /// + /// Driver description that displays in the ASCOM Chooser. + /// + protected static readonly string DriverDescription = "Meade Generic"; + + protected static string _comPort; // Variables to hold the currrent device configuration + protected static int _backlashCompensation; + protected static bool _reverseFocusDirection; + protected static bool _useDynamicBreaking; + protected double _guideRate; + protected string _precision; + protected string _guidingStyle; + + protected readonly ISharedResourcesWrapper _sharedResourcesWrapper; + + public MeadeTelescopeBase() + { + _sharedResourcesWrapper = new SharedResourcesWrapper(); + } + + public MeadeTelescopeBase(ISharedResourcesWrapper sharedResourcesWrapper) + { + _sharedResourcesWrapper = sharedResourcesWrapper; + } + + protected void Initialise() + { + var typeName = GetType().Name; + + _tl = new TraceLogger("", $"Meade.Generic.{typeName}"); + + ReadProfile(); // Read device configuration from the ASCOM Profile store + + IsConnected = false; // Initialise connected to false + + LogMessage(typeName, "Completed initialisation"); + LogMessage(typeName, $"Driver version: {DriverVersion}"); + } + + /// + /// Read the device configuration from the ASCOM Profile store + /// + protected void ReadProfile() + { + var profileProperties = _sharedResourcesWrapper.ReadProfile(); + _tl.Enabled = profileProperties.TraceLogger; + _comPort = profileProperties.ComPort; + _backlashCompensation = profileProperties.BacklashCompensation; + _reverseFocusDirection = profileProperties.ReverseFocusDirection; + _useDynamicBreaking = profileProperties.DynamicBreaking; + _guideRate = profileProperties.GuideRateArcSecondsPerSecond; + _precision = profileProperties.Precision; + _guidingStyle = profileProperties.GuidingStyle.ToLower(); + + LogMessage("ReadProfile", $"Trace logger enabled: {_tl.Enabled}"); + LogMessage("ReadProfile", $"Com Port: {_comPort}"); + LogMessage("ReadProfile", $"Backlash Steps: {_backlashCompensation}"); + LogMessage("ReadProfile", $"Dynamic breaking: {_useDynamicBreaking}"); + LogMessage("ReadProfile", $"Guide Rate: {_guideRate}"); + LogMessage("ReadProfile", $"Precision: {_precision}"); + LogMessage("ReadProfile", $"Guiding Style: {_guidingStyle}"); + } + + /// + /// Log helper function that takes formatted strings and arguments + /// + /// + /// + /// + public static void LogMessage(string identifier, string message, params object[] args) + { + var msg = String.Format(message, args); + _tl.LogMessage(identifier, msg); + } + + /// + /// Returns true if there is a valid connection to the driver hardware + /// + protected bool IsConnected { get; set; } + + public string Description + { + get + { + _tl.LogMessage("Description Get", DriverDescription); + return DriverDescription; + } + } + + public string DriverInfo + { + get + { + string driverInfo = $"{Description} .net driver. Version: {DriverVersion}"; + LogMessage("DriverInfo Get", driverInfo); + return driverInfo; + } + } + + public string DriverVersion + { + get + { + Version version = Assembly.GetExecutingAssembly().GetName().Version; + string driverVersion = $"{version.Major}.{version.Minor}.{version.Build}.{version.Revision}"; + LogMessage("DriverVersion Get", driverVersion); + return driverVersion; + } + } + } +} \ No newline at end of file