Refactored code to reduce duplication.

This commit is contained in:
2020-09-03 19:05:01 +01:00
parent 4266139429
commit da4e4fd81a
5 changed files with 141 additions and 211 deletions
@@ -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<IUtil>();
+4 -103
View File
@@ -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
{
/// <summary>
/// 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());
/// <summary>
/// Driver description that displays in the ASCOM Chooser.
/// </summary>
private static readonly string DriverDescription = "Meade Generic";
private static string _comPort; // Variables to hold the currrent device configuration
/// <summary>
/// Private variable to hold an ASCOM Utilities object
/// </summary>
@@ -66,19 +59,12 @@ namespace ASCOM.Meade.net
private readonly IAstroUtils _astroUtilities;
private readonly IAstroMaths _astroMaths;
/// <summary>
/// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify)
/// </summary>
private TraceLogger _tl;
private readonly ISharedResourcesWrapper _sharedResourcesWrapper;
/// <summary>
/// Initializes a new instance of the <see cref="Meade.net"/> class.
/// Must be public for COM registration.
/// </summary>
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
/// <summary>
/// Returns true if there is a valid connection to the driver hardware
/// </summary>
private bool IsConnected { get; set; }
/// <summary>
/// Use this function to throw an exception if we aren't connected to the hardware
/// </summary>
@@ -2384,25 +2316,6 @@ namespace ASCOM.Meade.net
}
}
/// <summary>
/// Read the device configuration from the ASCOM Profile store
/// </summary>
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);
}
/// <summary>
/// Log helper function that takes formatted strings and arguments
/// </summary>
/// <param name="identifier"></param>
/// <param name="message"></param>
/// <param name="args"></param>
private void LogMessage(string identifier, string message, params object[] args)
{
var msg = string.Format(message, args);
_tl.LogMessage(identifier, msg);
}
#endregion
}
}
+7 -107
View File
@@ -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
{
/// <summary>
/// ASCOM DeviceID (COM ProgID) for this driver.
@@ -38,68 +38,32 @@ namespace ASCOM.Meade.net
/// </summary>
//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
/// <summary>
/// Driver description that displays in the ASCOM Chooser.
/// </summary>
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;
/// <summary>
/// Private variable to hold an ASCOM Utilities object
/// </summary>
private readonly IUtil _utilities;
/// <summary>
/// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify)
/// </summary>
private static TraceLogger _tl;
private readonly ISharedResourcesWrapper _sharedResourcesWrapper;
/// <summary>
/// Initializes a new instance of the <see cref="Meade.net"/> class.
/// Must be public for COM registration.
/// </summary>
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
/// <summary>
/// Returns true if there is a valid connection to the driver hardware
/// </summary>
private bool IsConnected { get; set; }
/// <summary>
/// Use this function to throw an exception if we aren't connected to the hardware
/// </summary>
@@ -570,36 +500,6 @@ namespace ASCOM.Meade.net
throw new NotConnectedException($"Not connected to focuser when trying to execute: {message}");
}
}
/// <summary>
/// Read the device configuration from the ASCOM Profile store
/// </summary>
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}");
}
/// <summary>
/// Log helper function that takes formatted strings and arguments
/// </summary>
/// <param name="identifier"></param>
/// <param name="message"></param>
/// <param name="args"></param>
private static void LogMessage(string identifier, string message, params object[] args)
{
var msg = string.Format(message, args);
_tl.LogMessage(identifier, msg);
}
#endregion
}
}
+1
View File
@@ -134,6 +134,7 @@
</Compile>
<Compile Include="GarbageCollection.cs" />
<Compile Include="LocalServer.cs" />
<Compile Include="MeadeTelescopeBase.cs" />
<Compile Include="ProfileFactory.cs" />
<Compile Include="ProfileProperties.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+127
View File
@@ -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
{
/// <summary>
/// Variable to hold the trace logger object (creates a diagnostic log file with information that you specify)
/// </summary>
protected static TraceLogger _tl;
/// <summary>
/// Driver description that displays in the ASCOM Chooser.
/// </summary>
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}");
}
/// <summary>
/// Read the device configuration from the ASCOM Profile store
/// </summary>
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}");
}
/// <summary>
/// Log helper function that takes formatted strings and arguments
/// </summary>
/// <param name="identifier"></param>
/// <param name="message"></param>
/// <param name="args"></param>
public static void LogMessage(string identifier, string message, params object[] args)
{
var msg = String.Format(message, args);
_tl.LogMessage(identifier, msg);
}
/// <summary>
/// Returns true if there is a valid connection to the driver hardware
/// </summary>
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;
}
}
}
}