From f140e8aee151dde631184b0c9a740e50b7bd790c Mon Sep 17 00:00:00 2001 From: Colin Date: Sun, 31 Jan 2021 15:52:16 +0000 Subject: [PATCH] Added support for saving the site elevation into the driver profile --- .../TelescopeUnitTests.cs | 54 ++++-- Meade.net.Telescope/Telescope.cs | 20 ++- Meade.net/MeadeTelescopeBase.cs | 10 ++ Meade.net/ProfileProperties.cs | 1 + Meade.net/SetupDialogForm.cs | 22 ++- Meade.net/SetupDialogForm.designer.cs | 26 +++ Meade.net/SetupDialogForm.resx | 164 +++++++++++++----- Meade.net/SharedResources.cs | 4 + 8 files changed, 242 insertions(+), 59 deletions(-) diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index 2a0403c..0885f04 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -1537,25 +1537,57 @@ namespace Meade.net.Telescope.UnitTests } [Test] - public void SiteElevation_Get_ThenThrowsException() + public void SiteElevation_Get_WhenNotConnectedThrowsException() { - var excpetion = Assert.Throws(() => + var exception = Assert.Throws(() => { - var result = _telescope.SiteElevation; - Assert.Fail($"{result} should not have returned"); + var elevation = _telescope.SiteElevation; }); - - Assert.That(excpetion.Property, Is.EqualTo("SiteElevation")); - Assert.That(excpetion.AccessorSet, Is.False); + Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: SiteElevation Get")); } [Test] - public void SiteElevation_Set_ThenThrowsException() + public void SiteElevation_Get_WhenConnectedReturnsExpectedValue() { - var excpetion = Assert.Throws(() => { _telescope.SiteElevation = 0; }); + double expectedValue = 2000; - Assert.That(excpetion.Property, Is.EqualTo("SiteElevation")); - Assert.That(excpetion.AccessorSet, Is.True); + _profileProperties.SiteElevation = expectedValue; + + ConnectTelescope(); + + var result = _telescope.SiteElevation; + Assert.That(result, Is.EqualTo(expectedValue)); + } + + [Test] + public void SiteElevation_Set_WhenNotConnectedThrowsException() + { + var exception = Assert.Throws(() => + { + _telescope.SiteElevation = 1000; + }); + Assert.That(exception.Message, Is.EqualTo("Not connected to telescope when trying to execute: SiteElevation Set")); + } + + [Test] + public void SiteElevation_Set_WhenConnectedCanPersistNewValue() + { + double newElevation = 1000; + + double writtenSiteElevation = 0; + _sharedResourcesWrapperMock.Setup(x => x.WriteProfile(It.IsAny())).Callback( + profile => + { + writtenSiteElevation = profile.SiteElevation; + }); + + ConnectTelescope(); + + _telescope.SiteElevation = newElevation; + + Assert.That(_telescope.SiteElevation, Is.EqualTo(newElevation)); + _sharedResourcesWrapperMock.Verify( x => x.WriteProfile(It.IsAny()), Times.Once); + Assert.That(writtenSiteElevation, Is.EqualTo(newElevation)); } [Test] diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index d0716fb..eb7d34b 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -1540,14 +1540,26 @@ namespace ASCOM.Meade.net { get { - LogMessage("SiteElevation Get", "Not implemented"); - throw new PropertyNotImplementedException("SiteElevation", false); + CheckConnected("SiteElevation Get"); + + LogMessage("SiteElevation", $"Get {base.SiteElevation}"); + return base.SiteElevation; } // ReSharper disable once ValueParameterNotUsed set { - LogMessage("SiteElevation Set", "Not implemented"); - throw new PropertyNotImplementedException("SiteElevation", true); + CheckConnected("SiteElevation Set"); + + LogMessage("SiteElevation", $"Set: {value}"); + if (value == base.SiteElevation) + { + LogMessage("SiteElevation", $"Set: no change detected"); + return; + } + + LogMessage("SiteElevation", $"Set: {value} was {base.SiteElevation}"); + base.SiteElevation = value; + base.UpdateSiteElevation(); } } diff --git a/Meade.net/MeadeTelescopeBase.cs b/Meade.net/MeadeTelescopeBase.cs index 8728524..1250461 100644 --- a/Meade.net/MeadeTelescopeBase.cs +++ b/Meade.net/MeadeTelescopeBase.cs @@ -26,6 +26,7 @@ namespace ASCOM.Meade.net protected double GuideRate; protected string Precision; protected string GuidingStyle; + protected double SiteElevation; protected readonly ISharedResourcesWrapper SharedResourcesWrapper; @@ -65,6 +66,7 @@ namespace ASCOM.Meade.net GuideRate = profileProperties.GuideRateArcSecondsPerSecond; Precision = profileProperties.Precision; GuidingStyle = profileProperties.GuidingStyle.ToLower(); + SiteElevation = profileProperties.SiteElevation; LogMessage("ReadProfile", $"Trace logger enabled: {Tl.Enabled}"); LogMessage("ReadProfile", $"Com Port: {ComPort}"); @@ -73,6 +75,7 @@ namespace ASCOM.Meade.net LogMessage("ReadProfile", $"Guide Rate: {GuideRate}"); LogMessage("ReadProfile", $"Precision: {Precision}"); LogMessage("ReadProfile", $"Guiding Style: {GuidingStyle}"); + LogMessage("ReadProfile", $"Site Elevation: {SiteElevation}"); } /// @@ -133,5 +136,12 @@ namespace ASCOM.Meade.net } #endregion + + protected void UpdateSiteElevation() + { + var profileProperties = SharedResourcesWrapper.ReadProfile(); + profileProperties.SiteElevation = SiteElevation; + SharedResourcesWrapper.WriteProfile(profileProperties); + } } } \ No newline at end of file diff --git a/Meade.net/ProfileProperties.cs b/Meade.net/ProfileProperties.cs index 520b05f..f5155c4 100644 --- a/Meade.net/ProfileProperties.cs +++ b/Meade.net/ProfileProperties.cs @@ -12,5 +12,6 @@ namespace ASCOM.Meade.net public bool ReverseFocusDirection { get; set; } public bool DynamicBreaking { get; set; } public bool RtsDtrEnabled { get; set; } + public double SiteElevation { get; set; } } } \ No newline at end of file diff --git a/Meade.net/SetupDialogForm.cs b/Meade.net/SetupDialogForm.cs index 1f4f699..b6a7208 100644 --- a/Meade.net/SetupDialogForm.cs +++ b/Meade.net/SetupDialogForm.cs @@ -84,6 +84,7 @@ namespace ASCOM.Meade.net } txtBacklashSteps.Text = profileProperties.BacklashCompensation.ToString(CultureInfo.CurrentCulture); + txtElevation.Text = profileProperties.SiteElevation.ToString(CultureInfo.CurrentCulture); cbxReverseDirection.Checked = profileProperties.ReverseFocusDirection; cbxDynamicBreaking.Checked = profileProperties.DynamicBreaking; @@ -101,7 +102,8 @@ namespace ASCOM.Meade.net GuidingStyle = cboGuidingStyle.SelectedItem.ToString(), BacklashCompensation = int.Parse(txtBacklashSteps.Text), ReverseFocusDirection = cbxReverseDirection.Checked, - DynamicBreaking = cbxDynamicBreaking.Checked + DynamicBreaking = cbxDynamicBreaking.Checked, + SiteElevation = double.Parse(txtElevation.Text) }; return profileProperties; @@ -160,5 +162,23 @@ namespace ASCOM.Meade.net //txtGuideRate.Enabled = false; //cboPrecision.Enabled = false; } + + private void txtElevation_TextChanged_1(object sender, EventArgs e) + { + if (System.Text.RegularExpressions.Regex.IsMatch(txtElevation.Text, "[^0-9]")) + { + MessageBox.Show("Please enter only numbers."); + txtElevation.Text = txtElevation.Text.Remove(txtElevation.Text.Length - 1); + } + } + + private void txtBacklashSteps_TextChanged(object sender, EventArgs e) + { + if (System.Text.RegularExpressions.Regex.IsMatch(txtBacklashSteps.Text, "[^0-9]")) + { + MessageBox.Show("Please enter only numbers."); + txtBacklashSteps.Text = txtElevation.Text.Remove(txtBacklashSteps.Text.Length - 1); + } + } } } \ No newline at end of file diff --git a/Meade.net/SetupDialogForm.designer.cs b/Meade.net/SetupDialogForm.designer.cs index c970e28..4d23524 100644 --- a/Meade.net/SetupDialogForm.designer.cs +++ b/Meade.net/SetupDialogForm.designer.cs @@ -58,6 +58,9 @@ namespace ASCOM.Meade.net this.cbxDynamicBreaking = new System.Windows.Forms.CheckBox(); this.cbxRtsDtr = new System.Windows.Forms.CheckBox(); this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); + this.label12 = new System.Windows.Forms.Label(); + this.txtElevation = new System.Windows.Forms.TextBox(); + this.label13 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.picASCOM)).BeginInit(); this.SuspendLayout(); // @@ -176,6 +179,7 @@ namespace ASCOM.Meade.net // resources.ApplyResources(this.txtBacklashSteps, "txtBacklashSteps"); this.txtBacklashSteps.Name = "txtBacklashSteps"; + this.txtBacklashSteps.TextChanged += new System.EventHandler(this.txtBacklashSteps_TextChanged); // // label9 // @@ -211,10 +215,29 @@ namespace ASCOM.Meade.net this.toolTip1.SetToolTip(this.cbxRtsDtr, resources.GetString("cbxRtsDtr.ToolTip")); this.cbxRtsDtr.UseVisualStyleBackColor = true; // + // label12 + // + resources.ApplyResources(this.label12, "label12"); + this.label12.Name = "label12"; + // + // txtElevation + // + resources.ApplyResources(this.txtElevation, "txtElevation"); + this.txtElevation.Name = "txtElevation"; + this.txtElevation.TextChanged += new System.EventHandler(this.txtElevation_TextChanged_1); + // + // label13 + // + resources.ApplyResources(this.label13, "label13"); + this.label13.Name = "label13"; + // // SetupDialogForm // resources.ApplyResources(this, "$this"); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.label13); + this.Controls.Add(this.txtElevation); + this.Controls.Add(this.label12); this.Controls.Add(this.cbxRtsDtr); this.Controls.Add(this.cbxDynamicBreaking); this.Controls.Add(this.cbxReverseDirection); @@ -279,5 +302,8 @@ namespace ASCOM.Meade.net private CheckBox cbxDynamicBreaking; private CheckBox cbxRtsDtr; private ToolTip toolTip1; + private Label label12; + private TextBox txtElevation; + private Label label13; } } \ No newline at end of file diff --git a/Meade.net/SetupDialogForm.resx b/Meade.net/SetupDialogForm.resx index ca12791..a71d20e 100644 --- a/Meade.net/SetupDialogForm.resx +++ b/Meade.net/SetupDialogForm.resx @@ -123,7 +123,7 @@ - 281, 413 + 281, 457 59, 24 @@ -145,13 +145,13 @@ $this - 23 + 26 Bottom, Right - 281, 443 + 281, 487 59, 25 @@ -172,7 +172,7 @@ $this - 22 + 25 12, 9 @@ -196,7 +196,7 @@ $this - 21 + 24 Top, Right @@ -223,7 +223,7 @@ $this - 20 + 23 True @@ -250,7 +250,7 @@ $this - 19 + 22 True @@ -277,7 +277,7 @@ $this - 18 + 21 97, 87 @@ -298,13 +298,13 @@ $this - 17 + 20 True - 12, 202 + 12, 278 61, 13 @@ -325,10 +325,10 @@ $this - 16 + 19 - 97, 199 + 97, 275 46, 20 @@ -349,13 +349,13 @@ $this - 15 + 18 True - 149, 202 + 149, 278 122, 13 @@ -376,13 +376,13 @@ $this - 14 + 17 True - 149, 215 + 149, 291 105, 13 @@ -403,13 +403,13 @@ $this - 13 + 16 True - 12, 234 + 12, 310 50, 13 @@ -430,7 +430,7 @@ $this - 12 + 15 Unchanged @@ -442,7 +442,7 @@ High - 97, 231 + 97, 307 90, 21 @@ -460,7 +460,7 @@ $this - 11 + 14 True @@ -469,7 +469,7 @@ NoControl - 12, 261 + 12, 337 67, 13 @@ -490,7 +490,7 @@ $this - 10 + 13 Auto @@ -502,7 +502,7 @@ Pulse guiding - 97, 258 + 97, 334 90, 21 @@ -520,7 +520,7 @@ $this - 9 + 12 True @@ -550,7 +550,7 @@ $this - 8 + 11 True @@ -562,7 +562,7 @@ NoControl - 12, 317 + 12, 393 52, 13 @@ -583,10 +583,10 @@ $this - 7 + 10 - 97, 335 + 97, 411 46, 20 @@ -607,7 +607,7 @@ $this - 5 + 8 True @@ -616,7 +616,7 @@ NoControl - 12, 338 + 12, 414 79, 13 @@ -637,7 +637,7 @@ $this - 6 + 9 True @@ -646,7 +646,7 @@ NoControl - 149, 338 + 149, 414 20, 13 @@ -667,7 +667,7 @@ $this - 4 + 7 True @@ -700,13 +700,13 @@ $this - 3 + 6 True - 97, 361 + 97, 437 109, 17 @@ -727,13 +727,13 @@ $this - 2 + 5 True - 97, 384 + 97, 460 112, 17 @@ -754,8 +754,11 @@ $this - 1 + 4 + + 17, 17 + True @@ -774,9 +777,6 @@ Enable RTS/DTR - - 17, 17 - Useful for Meade LS scopes @@ -790,6 +790,84 @@ $this + 3 + + + 17, 17 + + + True + + + 12, 205 + + + 72, 13 + + + 22 + + + Site Elevation + + + label12 + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 2 + + + 97, 202 + + + 100, 20 + + + 23 + + + txtElevation + + + System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + 1 + + + True + + + 203, 205 + + + 38, 13 + + + 24 + + + meters + + + label13 + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + 0 @@ -799,7 +877,7 @@ 6, 13 - 350, 476 + 350, 520 CenterScreen diff --git a/Meade.net/SharedResources.cs b/Meade.net/SharedResources.cs index df7fcae..625e9ed 100644 --- a/Meade.net/SharedResources.cs +++ b/Meade.net/SharedResources.cs @@ -145,6 +145,7 @@ namespace ASCOM.Meade.net private const string BacklashCompensationName = "Backlash Compensation"; private const string ReverseFocusDirectionName = "Reverse Focuser Direction"; private const string DynamicBreakingName = "Dynamic Breaking"; + private const string SiteElevationName = "Site Elevation"; public static void WriteProfile(ProfileProperties profileProperties) { @@ -162,6 +163,7 @@ namespace ASCOM.Meade.net driverProfile.WriteValue(DriverId, BacklashCompensationName, profileProperties.BacklashCompensation.ToString()); driverProfile.WriteValue(DriverId, ReverseFocusDirectionName, profileProperties.ReverseFocusDirection.ToString()); driverProfile.WriteValue(DriverId, DynamicBreakingName, profileProperties.DynamicBreaking.ToString()); + driverProfile.WriteValue(DriverId, SiteElevationName, profileProperties.SiteElevation.ToString()); } } } @@ -175,6 +177,7 @@ namespace ASCOM.Meade.net private const string BacklashCompensationDefault = "3000"; private const string ReverseFocuserDiectionDefault = "true"; private const string DynamicBreakingDefault = "true"; + private const string SiteElevationDefault = "0"; public static ProfileProperties ReadProfile() { @@ -193,6 +196,7 @@ namespace ASCOM.Meade.net profileProperties.BacklashCompensation = Convert.ToInt32(driverProfile.GetValue(DriverId, BacklashCompensationName, string.Empty, BacklashCompensationDefault)); profileProperties.ReverseFocusDirection = Convert.ToBoolean(driverProfile.GetValue(DriverId, ReverseFocusDirectionName, string.Empty, ReverseFocuserDiectionDefault)); profileProperties.DynamicBreaking = Convert.ToBoolean(driverProfile.GetValue(DriverId, DynamicBreakingName, string.Empty, DynamicBreakingDefault)); + profileProperties.SiteElevation = Convert.ToInt32(driverProfile.GetValue(DriverId, SiteElevationName, string.Empty, SiteElevationDefault)); } return profileProperties;