diff --git a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs index 9fce5ed..198673b 100644 --- a/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs +++ b/Meade.net.Telescope.UnitTests/TelescopeUnitTests.cs @@ -152,7 +152,8 @@ namespace Meade.net.Telescope.UnitTests { ConnectTelescope(); - _telescope.Action("site", site); + string parameters = $"select {site}"; + _telescope.Action("site", parameters); _sharedResourcesWrapperMock.Verify(x => x.SendBlind($":W{site}#"), Times.Once); } @@ -163,9 +164,78 @@ namespace Meade.net.Telescope.UnitTests { ConnectTelescope(); - var exception = Assert.Throws(() => { _telescope.Action("site", site); }); + string parameters = $"select {site}"; + var exception = Assert.Throws(() => { _telescope.Action("site", parameters); }); - Assert.That(exception.Message, Is.EqualTo($"Site {site} not allowed, must be between 1 and 4")); + Assert.That(exception.Message, Is.EqualTo($"Site {parameters} not allowed, must be between 1 and 4")); + } + + [TestCase("1", ":GM#", "Home")] + [TestCase("2", ":GN#", "Club")] + [TestCase("3", ":GO#", "GPS")] + [TestCase("4", ":GP#", "Parents")] + public void Action_Site_GetName_WhenCallingWithValidValues_ThenSelectsCorrectSite(string site, string telescopeCommand, string siteName) + { + ConnectTelescope(); + + _sharedResourcesWrapperMock.Setup(x => x.SendString(telescopeCommand)).Returns(siteName); + + string parameters = $"GetName {site}"; + var result = _telescope.Action("site", parameters); + + _sharedResourcesWrapperMock.Verify(x => x.SendString(telescopeCommand), Times.Once); + Assert.That(result, Is.EqualTo(siteName)); + } + + [TestCase("0")] + [TestCase("5")] + public void Action_Site_GetName_WhenCallingWithInCorrectValues_ThenThrowsException(string site) + { + ConnectTelescope(); + + string parameters = $"GetName {site}"; + var exception = Assert.Throws(() => { _telescope.Action("site", parameters); }); + + Assert.That(exception.Message, Is.EqualTo($"Site {parameters} not allowed, must be between 1 and 4")); + } + + [TestCase("1", ":SMHome#", "Home")] + [TestCase("2", ":SNClub#", "Club")] + [TestCase("3", ":SOGPS#", "GPS")] + [TestCase("4", ":SPParents#", "Parents")] + public void Action_Site_SetName_WhenCallingWithValidValues_ThenSelectsCorrectSite(string site, string telescopeCommand, string siteName) + { + ConnectTelescope(); + + _sharedResourcesWrapperMock.Setup(x => x.SendChar(telescopeCommand)).Returns("1"); + + string parameters = $"SetName {site} {siteName}"; + _telescope.Action("site", parameters); + + _sharedResourcesWrapperMock.Verify(x => x.SendChar(telescopeCommand), Times.Once); + } + + [TestCase("0")] + [TestCase("5")] + public void Action_Site_SetName_WhenCallingWithInCorrectValues_ThenThrowsException(string site) + { + ConnectTelescope(); + + string parameters = $"SetName {site}"; + var exception = Assert.Throws(() => { _telescope.Action("site", parameters); }); + + Assert.That(exception.Message, Is.EqualTo($"Site {parameters} not allowed, must be between 1 and 4")); + } + + [Test] + public void Action_Site_WhenCallingUnknownParam_ThenThrowsException() + { + ConnectTelescope(); + + string parameters = $"unknown"; + var exception = Assert.Throws(() => { _telescope.Action("site", parameters); }); + + Assert.That(exception.Message, Is.EqualTo($"Site parameters {parameters} not known")); } [Test] diff --git a/Meade.net.Telescope/StringExtensions.cs b/Meade.net.Telescope/StringExtensions.cs index 14909c1..2f2c089 100644 --- a/Meade.net.Telescope/StringExtensions.cs +++ b/Meade.net.Telescope/StringExtensions.cs @@ -11,5 +11,23 @@ namespace ASCOM.Meade.net { return double.Parse(str); } + + public static int Position(this string str, char find, int instance) + { + var currentInstance = 0; + for (var i = 0; i < str.Length; i++) + { + if (str[i] == find) + { + currentInstance++; + if (currentInstance == instance) + { + return i; + } + } + } + + return -1; + } } } \ No newline at end of file diff --git a/Meade.net.Telescope/Telescope.cs b/Meade.net.Telescope/Telescope.cs index ee1482d..6fac6ed 100644 --- a/Meade.net.Telescope/Telescope.cs +++ b/Meade.net.Telescope/Telescope.cs @@ -242,21 +242,68 @@ namespace ASCOM.Meade.net break; case "site": - switch (actionParameters.ToLower()) + var parames = actionParameters.ToLower().Split(' '); + switch (parames[0]) { - case "1": - case "2": - case "3": - case "4": - SelectSite(actionParameters.ToInteger()); + case "select": + switch (parames[1]) + { + case "1": + case "2": + case "3": + case "4": + SelectSite(parames[1].ToInteger()); + break; + default: + LogMessage("", "Action {0}, parameters {1} not implemented", actionName, + actionParameters); + throw new InvalidValueException( + $"Site {actionParameters} not allowed, must be between 1 and 4"); + + } + break; + case "getname": + switch (parames[1]) + { + case "1": + case "2": + case "3": + case "4": + return GetSiteName(parames[1].ToInteger()); + default: + LogMessage("", "Action {0}, parameters {1} not implemented", actionName, + actionParameters); + throw new InvalidValueException( + $"Site {actionParameters} not allowed, must be between 1 and 4"); + + } + break; + case "setname": + switch (parames[1]) + { + case "1": + case "2": + case "3": + case "4": + var sitename = actionParameters.Substring(actionParameters.Position(' ', 2)).Trim(); + + SetSiteName(parames[1].ToInteger(), sitename); + break; + default: + LogMessage("", "Action {0}, parameters {1} not implemented", actionName, + actionParameters); + throw new InvalidValueException( + $"Site {actionParameters} not allowed, must be between 1 and 4"); + + } break; default: - LogMessage("", "Action {0}, parameters {1} not implemented", actionName, actionParameters); - throw new InvalidValueException($"Site {actionParameters} not allowed, must be between 1 and 4"); - + throw new InvalidValueException( + $"Site parameters {actionParameters} not known"); } break; + default: LogMessage("", "Action {0}, parameters {1} not implemented", actionName, actionParameters); throw new ActionNotImplementedException($"{actionName}"); @@ -434,6 +481,96 @@ namespace ASCOM.Meade.net //Returns: Nothing } + private void SetSiteName(int site, string sitename) + { + CheckConnected("SetSiteName"); + + if (site < 1) + throw new ArgumentOutOfRangeException("site", site, "Site cannot be lower than 1"); + else if (site > 4) + throw new ArgumentOutOfRangeException("site", site, "Site cannot be higher than 4"); + + string command = String.Empty; + switch (site) + { + case 1: + command = $":SM{sitename}#"; + //:SM# + //Set site 1’s name to be.LX200s only accept 3 character strings. Other scopes accept up to 15 characters. + // Returns: + //0 – Invalid + //1 - Valid + break; + case 2: + command = $":SN{sitename}#"; + //:SN# + //Set site 2’s name to be.LX200s only accept 3 character strings. Other scopes accept up to 15 characters. + // Returns: + //0 – Invalid + //1 - Valid + break; + case 3: + command = $":SO{sitename}#"; + //:SO# + //Set site 3’s name to be.LX200s only accept 3 character strings. Other scopes accept up to 15 characters. + // Returns: + //0 – Invalid + //1 - Valid + break; + case 4: + command = $":SP{sitename}#"; + //:SP# + //Set site 4’s name to be.LX200s only accept 3 character strings. Other scopes accept up to 15 characters. + // Returns: + //0 – Invalid + //1 - Valid + break; + + } + + var result = _sharedResourcesWrapper.SendChar(command); + if (result != "1") + { + throw new InvalidOperationException("Failed to set site name."); + } + } + + private string GetSiteName(int site) + { + CheckConnected("GetSiteName"); + + if (site < 1) + throw new ArgumentOutOfRangeException("site", site, "Site cannot be lower than 1"); + else if (site > 4) + throw new ArgumentOutOfRangeException("site", site, "Site cannot be higher than 4"); + + switch (site) + { + case 1: + return _sharedResourcesWrapper.SendString(":GM#"); + //:GM# Get Site 1 Name + //Returns: # + //A ‘#’ terminated string with the name of the requested site. + case 2: + return _sharedResourcesWrapper.SendString(":GN#"); + //:GN# Get Site 2 Name + //Returns: # + //A ‘#’ terminated string with the name of the requested site. + case 3: + return _sharedResourcesWrapper.SendString(":GO#"); + //:GO# Get Site 3 Name + //Returns: # + //A ‘#’ terminated string with the name of the requested site. + case 4: + return _sharedResourcesWrapper.SendString(":GP#"); + //:GP# Get Site 4 Name + //Returns: # + //A ‘#’ terminated string with the name of the requested site. + } + + throw new ArgumentOutOfRangeException("site", site, "Site out of range"); + } + public string Description { // TODO customise this device description