From c311fb8cbef2787ac7fe02c64474b59710370e00 Mon Sep 17 00:00:00 2001 From: Colin Date: Thu, 22 Aug 2019 23:22:36 +0100 Subject: [PATCH] Second and subsequent Connections to the telescope driver now no longer trigger resetting the Guide rate and precision. It's assumed they are already set correctly. --- .../TelescopeUnitTests.cs | 33 +++++++++++++++---- Meade.net.Telescope/Telescope.cs | 22 ++++++++----- Meade.net.focuser/Focuser.cs | 6 ++-- Meade.net/ConnectionInfo.cs | 8 +++++ Meade.net/Meade.net.csproj | 1 + Meade.net/SharedResources.cs | 22 +++++++++++-- Meade.net/Wrapper/SharedResourcesWrapper.cs | 12 +++---- 7 files changed, 78 insertions(+), 26 deletions(-) create mode 100644 Meade.net/ConnectionInfo.cs diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index 6f0f5d0..5150e8d 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -23,6 +23,7 @@ namespace Meade.net.Telescope.UnitTests private Mock _astroMathsMock; private ProfileProperties _profileProperties; + private ConnectionInfo _connectionInfo; [SetUp] public void Setup() @@ -45,6 +46,10 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny>())).Returns>( (func) => func()); _sharedResourcesWrapperMock.Setup(x => x.Lock(It.IsAny>())).Returns>((func) => func()); + _connectionInfo = new ConnectionInfo {Connections = 1, SameDevice = 1}; + + _sharedResourcesWrapperMock.Setup(x => x.Connect("Serial", It.IsAny())).Returns( () => _connectionInfo ); + _sharedResourcesWrapperMock.Setup(x => x.ReadProfile()).Returns(_profileProperties); @@ -350,7 +355,7 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Setup(x => x.FirmwareVersion).Returns(firmware); _telescope.Connected = true; - _sharedResourcesWrapperMock.Verify( x => x.Connect("Serial"), Times.Once); + _sharedResourcesWrapperMock.Verify( x => x.Connect("Serial", It.IsAny()), Times.Once); _sharedResourcesWrapperMock.Verify(x => x.SendString(":GZ#"), Times.Once); _sharedResourcesWrapperMock.Verify(x => x.SendBlind($":Rg{_profileProperties.GuideRateArcSecondsPerSecond:00.0}#"),Times.Once); @@ -361,26 +366,26 @@ namespace Meade.net.Telescope.UnitTests public void Connected_Set_SettingTrueWhenTrue_ThenDoesNothing() { ConnectTelescope(); - _sharedResourcesWrapperMock.Verify( x => x.Connect(It.IsAny()),Times.Once); + _sharedResourcesWrapperMock.Verify( x => x.Connect(It.IsAny(), It.IsAny()),Times.Once); //act _telescope.Connected = true; //assert - _sharedResourcesWrapperMock.Verify(x => x.Connect(It.IsAny()), Times.Once); + _sharedResourcesWrapperMock.Verify(x => x.Connect(It.IsAny(), It.IsAny()), Times.Once); } [Test] public void Connected_Set_SettingFalseWhenTrue_ThenDisconnects() { ConnectTelescope(); - _sharedResourcesWrapperMock.Verify(x => x.Connect(It.IsAny()), Times.Once); + _sharedResourcesWrapperMock.Verify(x => x.Connect(It.IsAny(), It.IsAny()), Times.Once); //act _telescope.Connected = false; //assert - _sharedResourcesWrapperMock.Verify(x => x.Disconnect(It.IsAny()), Times.Once()); + _sharedResourcesWrapperMock.Verify(x => x.Disconnect(It.IsAny(), It.IsAny()), Times.Once()); } [Test] @@ -395,7 +400,7 @@ namespace Meade.net.Telescope.UnitTests _telescope.Connected = true; //assert - _sharedResourcesWrapperMock.Verify(x => x.Disconnect(It.IsAny()), Times.Once()); + _sharedResourcesWrapperMock.Verify(x => x.Disconnect(It.IsAny(), It.IsAny()), Times.Once()); } [TestCase("Autostar", "30Ab", false)] @@ -771,6 +776,22 @@ namespace Meade.net.Telescope.UnitTests _sharedResourcesWrapperMock.Verify(x => x.SendChar(":P#"), Times.AtLeastOnce); } + [TestCase("High", false, true)] + [TestCase("High", true, true)] + [TestCase("Low", false, false)] + [TestCase("Low", true, false)] + public void Precision_Set_WhenSecondConnectionMade_ThenTelescopePrecisionNotChanged(string desiredPresision, bool telescopePrecision, bool finalPrecision) + { + _profileProperties.Precision = desiredPresision; + + _connectionInfo.SameDevice = 2; + _connectionInfo.Connections = 2; + + _telescope.Connected = true; + + _sharedResourcesWrapperMock.Verify(x => x.SendChar(":P#"), Times.Never); + } + [Test] public void CanSetPark_Get_ReturnsFalse() { diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index 913033e..0ddd71b 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -277,7 +277,6 @@ namespace ASCOM.Meade.net $"Site {actionParameters} not allowed, must be between 1 and 4"); } - break; case "setname": switch (parames[1]) { @@ -374,12 +373,11 @@ namespace ASCOM.Meade.net ReadProfile(); LogMessage("Connected Set", "Connecting to port {0}", _comPort); - _sharedResourcesWrapper.Connect("Serial"); + var connectionInfo = _sharedResourcesWrapper.Connect("Serial", DriverId); try { LogMessage("Connected Set", $"Connected to port {_comPort}. Product: {_sharedResourcesWrapper.ProductName} Version:{_sharedResourcesWrapper.FirmwareVersion}"); - SetLongFormat(true); _userNewerPulseGuiding = IsNewPulseGuidingSupported(); _targetDeclination = InvalidParameter; _targetRightAscension = InvalidParameter; @@ -388,16 +386,22 @@ namespace ASCOM.Meade.net LogMessage("Connected Set", $"New Pulse Guiding Supported: {_userNewerPulseGuiding}"); IsConnected = true; - if (CanSetGuideRates) + if (connectionInfo.SameDevice == 1) { - SetNewGuideRate( _guideRate, "Connect" ); - } + //These settings are applied only when the first device connects to the telescope. + SetLongFormat(true); - SetTelescopePrecision("Connect"); + if (CanSetGuideRates) + { + SetNewGuideRate(_guideRate, "Connect"); + } + + SetTelescopePrecision("Connect"); + } } catch (Exception) { - _sharedResourcesWrapper.Disconnect("Serial"); + _sharedResourcesWrapper.Disconnect("Serial", DriverId); throw; } } @@ -409,7 +413,7 @@ namespace ASCOM.Meade.net else { LogMessage("Connected Set", "Disconnecting from port {0}", _comPort); - _sharedResourcesWrapper.Disconnect("Serial"); + _sharedResourcesWrapper.Disconnect("Serial", DriverId); IsConnected = false; } } diff --git a/Meade.net.focuser/Focuser.cs b/Meade.net.focuser/Focuser.cs index cbd9ad2..b675b31 100644 --- a/Meade.net.focuser/Focuser.cs +++ b/Meade.net.focuser/Focuser.cs @@ -179,7 +179,7 @@ namespace ASCOM.Meade.net try { ReadProfile(); - _sharedResourcesWrapper.Connect("Serial"); + _sharedResourcesWrapper.Connect("Serial", DriverId); try { SelectSite(1); @@ -189,7 +189,7 @@ namespace ASCOM.Meade.net } catch (Exception) { - _sharedResourcesWrapper.Disconnect("Serial"); + _sharedResourcesWrapper.Disconnect("Serial", DriverId); throw; } } @@ -201,7 +201,7 @@ namespace ASCOM.Meade.net else { LogMessage("Connected Set", "Disconnecting from port {0}", _comPort); - _sharedResourcesWrapper.Disconnect("Serial"); + _sharedResourcesWrapper.Disconnect("Serial", DriverId); IsConnected = false; } } diff --git a/Meade.net/ConnectionInfo.cs b/Meade.net/ConnectionInfo.cs new file mode 100644 index 0000000..4da4c7f --- /dev/null +++ b/Meade.net/ConnectionInfo.cs @@ -0,0 +1,8 @@ +namespace ASCOM.Meade.net +{ + public class ConnectionInfo + { + public int Connections { get; set; } + public int SameDevice { get; set; } + } +} \ No newline at end of file diff --git a/Meade.net/Meade.net.csproj b/Meade.net/Meade.net.csproj index eb6cc79..6c5a077 100644 --- a/Meade.net/Meade.net.csproj +++ b/Meade.net/Meade.net.csproj @@ -122,6 +122,7 @@ + Form diff --git a/Meade.net/SharedResources.cs b/Meade.net/SharedResources.cs index 8d27adb..cd5209f 100644 --- a/Meade.net/SharedResources.cs +++ b/Meade.net/SharedResources.cs @@ -260,12 +260,15 @@ namespace ASCOM.Meade.net /// private static readonly Dictionary _connectedDevices = new Dictionary(); + private static readonly Dictionary _connectedDeviceIds = new Dictionary(); + + /// /// This is called in the driver Connect(true) property, /// it add the device id to the list of devices if it's not there and increments the device count. /// /// - public static void Connect(string deviceId) + public static ConnectionInfo Connect(string deviceId, string driverId) { lock (LockObject) { @@ -273,6 +276,10 @@ namespace ASCOM.Meade.net _connectedDevices.Add(deviceId, new DeviceHardware()); _connectedDevices[deviceId].Count++; // increment the value + if (!_connectedDeviceIds.ContainsKey(driverId)) + _connectedDeviceIds.Add(driverId, new DeviceHardware()); + _connectedDeviceIds[driverId].Count++; // increment the value + if (deviceId == "Serial") { if (_connectedDevices[deviceId].Count == 1) @@ -292,10 +299,16 @@ namespace ASCOM.Meade.net FirmwareVersion = SendString(":GVN#"); } } + + return new ConnectionInfo + { + Connections = _connectedDevices[deviceId].Count, + SameDevice = _connectedDeviceIds[driverId].Count + }; } } - public static void Disconnect(string deviceId) + public static void Disconnect(string deviceId, string driverId) { lock (LockObject) { @@ -311,6 +324,11 @@ namespace ASCOM.Meade.net } } } + + if (_connectedDeviceIds.ContainsKey(driverId)) + { + _connectedDeviceIds[driverId].Count--; + } } } diff --git a/Meade.net/Wrapper/SharedResourcesWrapper.cs b/Meade.net/Wrapper/SharedResourcesWrapper.cs index 3417ce6..edb1232 100644 --- a/Meade.net/Wrapper/SharedResourcesWrapper.cs +++ b/Meade.net/Wrapper/SharedResourcesWrapper.cs @@ -4,8 +4,8 @@ namespace ASCOM.Meade.net.Wrapper { public interface ISharedResourcesWrapper { - void Connect(string deviceId); - void Disconnect(string deviceId); + ConnectionInfo Connect(string deviceId, string driverId); + void Disconnect(string deviceId, string driverId); string ProductName { get; } @@ -29,14 +29,14 @@ namespace ASCOM.Meade.net.Wrapper public class SharedResourcesWrapper : ISharedResourcesWrapper { - public void Connect(string deviceId) + public ConnectionInfo Connect(string deviceId, string driverId) { - SharedResources.Connect( deviceId); + return SharedResources.Connect(deviceId, driverId); } - public void Disconnect(string deviceId) + public void Disconnect(string deviceId, string driverId) { - SharedResources.Disconnect(deviceId); + SharedResources.Disconnect(deviceId, driverId); } public string ProductName => SharedResources.ProductName;