From 9866578cab10e86b9570254d0849131a121b44b6 Mon Sep 17 00:00:00 2001 From: Colin Dawson Date: Sat, 7 May 2022 12:22:46 +0100 Subject: [PATCH] Added more error checks for connected, and found a couple of logic holds in the code that sets the IsConnected property. --- .../TelescopeUnitTests.cs | 18 +++++------------- Meade.net.Telescope/Telescope.cs | 2 ++ 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index f6fe1d2..f668703 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -221,9 +221,7 @@ namespace Meade.net.Telescope.UnitTests { string expectedResult = "test result string"; _sharedResourcesWrapperMock.Setup(x => x.SendString("ED", false)).Returns(expectedResult); - _telescope.Connected = true; - - + ConnectTelescope(); var actualResult = _telescope.Action("handbox", "readdisplay"); @@ -830,21 +828,17 @@ namespace Meade.net.Telescope.UnitTests [TestCase(TelescopeList.Autostar497, TelescopeList.Autostar497_43Eg, AlignmentModes.algGermanPolar, "AP")] public void AlignmentMode_Set_WhenConnected_ThenSendsExpectedCommand(string productName, string firmware, AlignmentModes alignmentMode, string expectedCommand) { - _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(productName); - _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(firmware); - _telescope.Connected = true; + ConnectTelescope(productName, firmware); _telescope.AlignmentMode = alignmentMode; _sharedResourcesWrapperMock.Verify(x => x.SendBlind(expectedCommand, false), Times.Once); } - [TestCase("AUTOSTAR", "43Ef")] + [TestCase(TelescopeList.Autostar497, "43Ef")] public void AlignmentMode_Set_WhenAutostarFirmwareToLow_ThenThrowsException(string productName, string firmware) { - _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(productName); - _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(firmware); - _telescope.Connected = true; + ConnectTelescope(productName, firmware); var excpetion = Assert.Throws(() => _telescope.AlignmentMode = AlignmentModes.algAltAz); @@ -983,9 +977,7 @@ namespace Meade.net.Telescope.UnitTests [Test] public void CanSetGuideRates_Get_WhenConnectedToLX200GPS_ThenReturnsTrue() { - _sharedResourcesWrapperMock.Setup(x => x.ProductName).Returns(() => TelescopeList.LX200GPS); - _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(() => TelescopeList.LX200GPS_42G); - _telescope.Connected = true; + ConnectTelescope(TelescopeList.LX200GPS, TelescopeList.LX200GPS_42G); var result = _telescope.CanSetGuideRates; diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index 96a22e1..ffeead3 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -508,12 +508,14 @@ namespace ASCOM.Meade.net } catch (Exception) { + IsConnected = false; SharedResourcesWrapper.Disconnect("Serial", DriverId); throw; } } catch (Exception ex) { + IsConnected = false; LogMessage("Connected Set", "Error connecting to port {0} - {1}", _profileProperties.ComPort, ex.Message); } }