diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index 7989e84..62c63ef 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using System.Reflection; using ASCOM; using ASCOM.Astrometry.AstroUtils; @@ -22,6 +23,7 @@ namespace Meade.net.Telescope.UnitTests private Mock _astroUtilsMock; private Mock _sharedResourcesWrapperMock; private Mock _astroMathsMock; + private Mock _clockMock; private ProfileProperties _profileProperties; private ConnectionInfo _connectionInfo; @@ -63,8 +65,10 @@ namespace Meade.net.Telescope.UnitTests _astroMathsMock = new Mock(); + _clockMock = new Mock(); + _telescope = new ASCOM.Meade.net.Telescope(_utilMock.Object, _utilExtraMock.Object, _astroUtilsMock.Object, - _sharedResourcesWrapperMock.Object, _astroMathsMock.Object); + _sharedResourcesWrapperMock.Object, _astroMathsMock.Object, _clockMock.Object); } private void ConnectTelescope(string productName = TelescopeList.Autostar497, string firmwareVersion = TelescopeList.Autostar497_31Ee) @@ -1654,18 +1658,14 @@ namespace Meade.net.Telescope.UnitTests Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: SlewSettleTime Set")); } - [TestCase(5)] - [TestCase(10)] - [TestCase(2)] - public void SlewSettleTime_Get_ReturnsExpectedValue(short settleTime) + [Test] + public void SlewSettleTime_Get_ReturnsExpectedValue() { - _profileProperties.SettleTime = settleTime; - ConnectTelescope(); var result = _telescope.SlewSettleTime; - Assert.That(result, Is.EqualTo(settleTime)); + Assert.That(result, Is.EqualTo(0)); } [TestCase(8)] @@ -2339,6 +2339,64 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Verify(x => x.SendString(":D#"),Times.Once); } + + [TestCase(0, 0, "2021-10-03T20:36:00", "2021-10-03T20:36:01", false)] + [TestCase(5, 0, "2021-10-03T20:36:00", "2021-10-03T20:36:01", true)] + [TestCase(5, 0, "2021-10-03T20:36:00", "2021-10-03T20:36:06", false)] + [TestCase(10, 0, "2021-10-03T20:36:00", "2021-10-03T20:36:06", true)] + [TestCase(10, 0, "2021-10-03T20:36:00", "2021-10-03T20:36:09", true)] + [TestCase(10, 0, "2021-10-03T20:36:00", "2021-10-03T20:36:10", false)] + [TestCase(0, 5, "2021-10-03T20:36:00", "2021-10-03T20:36:01", true)] + [TestCase(0, 5, "2021-10-03T20:36:00", "2021-10-03T20:36:05", false)] + [TestCase(0, 10, "2021-10-03T20:36:00", "2021-10-03T20:36:05", true)] + [TestCase(0, 10, "2021-10-03T20:36:00", "2021-10-03T20:36:10", false)] + [TestCase(15, 10, "2021-10-03T20:36:00", "2021-10-03T20:36:10", true)] + [TestCase(15, 10, "2021-10-03T20:36:00", "2021-10-03T20:36:24", true)] + [TestCase(15, 10, "2021-10-03T20:36:00", "2021-10-03T20:36:25", false)] + public void Slewing_WhenTelescopeIsSlewing_ThenReturnsExpectedValueForSettleTime( short settleTime, short profileSettleTime, string startSlewing, string endSlewing, bool isSlewing) + { + _profileProperties.SettleTime = profileSettleTime; + + var timescalled = 0; + DateTime startSlewingDateTime = DateTime.ParseExact(startSlewing, "yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture); + DateTime endSlewingDatetime = DateTime.ParseExact(endSlewing, "yyyy-MM-dd'T'HH:mm:ss", CultureInfo.InvariantCulture); + + _clockMock.Setup(x => x.UtcNow).Returns(() => + { + if (timescalled == 0) + { + timescalled++; + return startSlewingDateTime; + } + + return endSlewingDatetime; + }); + + var slewingText = "|"; + var notSlewingText = String.Empty; + + _sharedResourcesWrapperMock.Setup(x => x.SendString(":D#")).Returns( () => + { + if (timescalled == 0) + { + return slewingText; + } + + return notSlewingText; + }); + + ConnectTelescope(); + + _telescope.SlewSettleTime = settleTime; + + var result = _telescope.Slewing; + + Assert.That(result, Is.EqualTo(true)); + + result = _telescope.Slewing; + + Assert.That(result, Is.EqualTo(isSlewing)); + } [TestCase(TelescopeList.LX200CLASSIC,"","|", true)] [TestCase(TelescopeList.LX200CLASSIC, "", "||||||||", true)] diff --git a/Meade.net.Telescope/Clock.cs b/Meade.net.Telescope/Clock.cs new file mode 100644 index 0000000..745e9a7 --- /dev/null +++ b/Meade.net.Telescope/Clock.cs @@ -0,0 +1,9 @@ +using System; + +namespace ASCOM.Meade.net +{ + public class Clock : IClock + { + public DateTime UtcNow => DateTime.UtcNow; + } +} \ No newline at end of file diff --git a/Meade.net.Telescope/IClock.cs b/Meade.net.Telescope/IClock.cs new file mode 100644 index 0000000..2745543 --- /dev/null +++ b/Meade.net.Telescope/IClock.cs @@ -0,0 +1,9 @@ +using System; + +namespace ASCOM.Meade.net +{ + public interface IClock + { + DateTime UtcNow { get; } + } +} \ No newline at end of file diff --git a/Meade.net.Telescope/Meade.net.Telescope.csproj b/Meade.net.Telescope/Meade.net.Telescope.csproj index af7987b..cfae8a4 100644 --- a/Meade.net.Telescope/Meade.net.Telescope.csproj +++ b/Meade.net.Telescope/Meade.net.Telescope.csproj @@ -124,8 +124,10 @@ + + diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index b1fe144..8524fac 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -60,16 +60,20 @@ namespace ASCOM.Meade.net private readonly IAstroMaths _astroMaths; + private readonly IClock _clock; + /// /// Private variable to hold number of decimals for RA /// private int _digitsRa = 2; /// - /// Private variable to hold number of decimals for DE + /// Private variable to hold number of decimals for Dec /// private int _digitsDe = 2; + private short _settleTime; + /// /// Initializes a new instance of the class. /// Must be public for COM registration. @@ -84,6 +88,7 @@ namespace ASCOM.Meade.net _utilitiesExtra = util; //Initialise util object _astroUtilities = new AstroUtils(); // Initialise astro utilities object _astroMaths = new AstroMaths.AstroMaths(); + _clock = new Clock(); Initialise(nameof(Telescope)); } @@ -116,8 +121,9 @@ namespace ASCOM.Meade.net sb.AppendLine(); } - public Telescope( IUtil util, IUtilExtra utilExtra, IAstroUtils astroUtilities, ISharedResourcesWrapper sharedResourcesWrapper, IAstroMaths astroMaths) : base(sharedResourcesWrapper) + public Telescope( IUtil util, IUtilExtra utilExtra, IAstroUtils astroUtilities, ISharedResourcesWrapper sharedResourcesWrapper, IAstroMaths astroMaths, IClock clock) : base(sharedResourcesWrapper) { + _clock = clock; _utilities = util; //Initialise util object _utilitiesExtra = utilExtra; //Initialise util object _astroUtilities = astroUtilities; // Initialise astro utilities object @@ -1775,15 +1781,14 @@ namespace ASCOM.Meade.net get { CheckConnected("SlewSettleTime Get"); - LogMessage("SlewSettleTime Get", $"{SettleTime} Seconds"); - return SettleTime; + LogMessage("SlewSettleTime Get", $"{_settleTime} Seconds"); + return _settleTime; } - // ReSharper disable once ValueParameterNotUsed set { CheckConnected("SlewSettleTime Set"); - LogMessage("SlewSettleTime Set", $"Setting from {SettleTime} to {value}"); - SettleTime = value; + LogMessage("SlewSettleTime Set", $"Setting from {_settleTime} to {value}"); + _settleTime = value; } } @@ -1964,16 +1969,29 @@ namespace ASCOM.Meade.net return _movingPrimary || _movingSecondary; } + private DateTime _earliestNonSlewingTime = DateTime.MinValue; + public bool Slewing { get { var isSlewing = GetSlewing(); + + if (isSlewing) + _earliestNonSlewingTime = _clock.UtcNow + GetTotalSlewingSettleTime(); + else if (_clock.UtcNow < _earliestNonSlewingTime) + isSlewing = true; + LogMessage("Slewing", $"Result = {isSlewing}"); return isSlewing; } } + private TimeSpan GetTotalSlewingSettleTime() + { + return TimeSpan.FromSeconds( SlewSettleTime + ProfileSettleTime ); + } + private bool GetSlewing() { if (!Connected) return false; @@ -2001,7 +2019,7 @@ namespace ASCOM.Meade.net bool isSlewing = false; try { - if (string.IsNullOrWhiteSpace(result)) + if (string.IsNullOrEmpty(result)) { isSlewing = false; return isSlewing; diff --git a/Meade.net/MeadeTelescopeBase.cs b/Meade.net/MeadeTelescopeBase.cs index 6fd1d53..51e0892 100644 --- a/Meade.net/MeadeTelescopeBase.cs +++ b/Meade.net/MeadeTelescopeBase.cs @@ -27,7 +27,7 @@ namespace ASCOM.Meade.net protected string Precision; protected string GuidingStyle; protected double SiteElevation; - protected short SettleTime; + protected short ProfileSettleTime; protected readonly ISharedResourcesWrapper SharedResourcesWrapper; @@ -68,7 +68,7 @@ namespace ASCOM.Meade.net Precision = profileProperties.Precision; GuidingStyle = profileProperties.GuidingStyle.ToLower(); SiteElevation = profileProperties.SiteElevation; - SettleTime = profileProperties.SettleTime; + ProfileSettleTime = profileProperties.SettleTime; LogMessage("ReadProfile", $"Trace logger enabled: {Tl.Enabled}"); LogMessage("ReadProfile", $"Com Port: {ComPort}"); @@ -78,7 +78,7 @@ namespace ASCOM.Meade.net LogMessage("ReadProfile", $"Precision: {Precision}"); LogMessage("ReadProfile", $"Guiding Style: {GuidingStyle}"); LogMessage("ReadProfile", $"Site Elevation: {SiteElevation}"); - LogMessage("ReadProfile", $"Settle Time after slew: {SettleTime}"); + LogMessage("ReadProfile", $"Settle Time after slew: {ProfileSettleTime}"); } ///