From 56642267b0bb7e3d7fb18b879913954638605188 Mon Sep 17 00:00:00 2001 From: Colin Dawson Date: Mon, 17 Feb 2025 15:37:40 +0000 Subject: [PATCH] Altered the profile read exception handling a little. --- Meade.net/Meade.net.csproj | 1 + Meade.net/SharedResources.cs | 34 ++++++------- Meade.net/Wrapper/GetValueException.cs | 28 +++++++++++ Meade.net/Wrapper/IProfileWrapper.cs | 68 +++++++++++++++++++++++++- 4 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 Meade.net/Wrapper/GetValueException.cs diff --git a/Meade.net/Meade.net.csproj b/Meade.net/Meade.net.csproj index 25ad3a8..fca6d92 100644 --- a/Meade.net/Meade.net.csproj +++ b/Meade.net/Meade.net.csproj @@ -148,6 +148,7 @@ + diff --git a/Meade.net/SharedResources.cs b/Meade.net/SharedResources.cs index 6a403e1..3544a53 100644 --- a/Meade.net/SharedResources.cs +++ b/Meade.net/SharedResources.cs @@ -283,30 +283,30 @@ namespace ASCOM.Meade.net { driverProfile.DeviceType = "Telescope"; profileProperties.ComPort = driverProfile.GetValue(DriverId, ComPortProfileName, string.Empty, ComPortDefault); - profileProperties.TimeoutMs = Convert.ToInt32(driverProfile.GetValue(DriverId, TimeoutMsName, string.Empty, TimeoutMsDefault)); - profileProperties.RtsDtrEnabled = Convert.ToBoolean(driverProfile.GetValue(DriverId, RtsDtrProfileName, string.Empty, RtsDtrDefault)); - profileProperties.TraceLogger = Convert.ToBoolean(driverProfile.GetValue(DriverId, TraceStateProfileName, string.Empty, TraceStateDefault)); - profileProperties.GuideRateArcSecondsPerSecond = double.Parse(driverProfile.GetValue(DriverId, GuideRateProfileName, string.Empty, GuideRateProfileNameDefault), NumberFormatInfo.InvariantInfo); + profileProperties.TimeoutMs = driverProfile.GetValueInt(DriverId, TimeoutMsName, string.Empty, TimeoutMsDefault); + profileProperties.RtsDtrEnabled = driverProfile.GetValueBool(DriverId, RtsDtrProfileName, string.Empty, RtsDtrDefault); + profileProperties.TraceLogger = driverProfile.GetValueBool(DriverId, TraceStateProfileName, string.Empty, TraceStateDefault); + profileProperties.GuideRateArcSecondsPerSecond = driverProfile.GetValueDouble(DriverId, GuideRateProfileName, string.Empty, GuideRateProfileNameDefault); profileProperties.Precision = driverProfile.GetValue(DriverId, PrecisionProfileName, string.Empty, PrecisionDefault); profileProperties.GuidingStyle = driverProfile.GetValue(DriverId, GuidingStyleProfileName, string.Empty, GuidingStyleDefault); - 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)); - profileProperties.SettleTime = Convert.ToInt16(driverProfile.GetValue(DriverId, SettleTimeName, string.Empty, SettleTimeDefault)); + profileProperties.BacklashCompensation = driverProfile.GetValueInt(DriverId, BacklashCompensationName, string.Empty, BacklashCompensationDefault); + profileProperties.ReverseFocusDirection = driverProfile.GetValueBool(DriverId, ReverseFocusDirectionName, string.Empty, ReverseFocuserDiectionDefault); + profileProperties.DynamicBreaking = driverProfile.GetValueBool(DriverId, DynamicBreakingName, string.Empty, DynamicBreakingDefault); + profileProperties.SiteElevation = driverProfile.GetValueInt(DriverId, SiteElevationName, string.Empty, SiteElevationDefault); + profileProperties.SettleTime = driverProfile.GetValueShort(DriverId, SettleTimeName, string.Empty, SettleTimeDefault); profileProperties.StopBits = driverProfile.GetValue(DriverId, StopBitsName, string.Empty, StopBitsDefault); - profileProperties.DataBits = Convert.ToInt32(driverProfile.GetValue(DriverId, DataBitsName, string.Empty, DataBitsDefault)); + profileProperties.DataBits = driverProfile.GetValueInt(DriverId, DataBitsName, string.Empty, DataBitsDefault); profileProperties.Handshake = driverProfile.GetValue(DriverId, HandShakeName, string.Empty, HandShakeDefault); - profileProperties.Speed = Convert.ToInt32(driverProfile.GetValue(DriverId, SpeedName, string.Empty, SpeedDefault)); + profileProperties.Speed = driverProfile.GetValueInt(DriverId, SpeedName, string.Empty, SpeedDefault); profileProperties.Parity = driverProfile.GetValue(DriverId, ParityName, string.Empty, ParityDefault); - profileProperties.SendDateTime = Convert.ToBoolean(driverProfile.GetValue(DriverId, SendDateTimeName, string.Empty, SendDateTimeDefault)); + profileProperties.SendDateTime = driverProfile.GetValueBool(DriverId, SendDateTimeName, string.Empty, SendDateTimeDefault); profileProperties.ParkedBehaviour = EnumExtensionMethods.GetValueFromDescription(driverProfile.GetValue(DriverId, ParkedBehaviourName, string.Empty, ParkedBehaviourDefault)); - profileProperties.ParkedAlt = double.Parse(driverProfile.GetValue(DriverId, ParkedAltName, string.Empty, ParkedAltDefault), NumberFormatInfo.InvariantInfo); - profileProperties.ParkedAz = double.Parse(driverProfile.GetValue(DriverId, ParkedAzimuthName, string.Empty, ParkedAzimuthDefault), NumberFormatInfo.InvariantInfo); - profileProperties.FocalLength = double.Parse(driverProfile.GetValue(DriverId, FocalLengthName, string.Empty, FocalLengthDefault), NumberFormatInfo.InvariantInfo); - profileProperties.ApertureArea = double.Parse(driverProfile.GetValue(DriverId, ApertureAreaName, string.Empty, ApertureAreaDefault), NumberFormatInfo.InvariantInfo); - profileProperties.ApertureDiameter = double.Parse(driverProfile.GetValue(DriverId, ApertureDiameterName, string.Empty, ApertureDiameterDefault), NumberFormatInfo.InvariantInfo); + profileProperties.ParkedAlt = driverProfile.GetValueDouble(DriverId, ParkedAltName, string.Empty, ParkedAltDefault); + profileProperties.ParkedAz = driverProfile.GetValueDouble(DriverId, ParkedAzimuthName, string.Empty, ParkedAzimuthDefault); + profileProperties.FocalLength = driverProfile.GetValueDouble(DriverId, FocalLengthName, string.Empty, FocalLengthDefault); + profileProperties.ApertureArea = driverProfile.GetValueDouble(DriverId, ApertureAreaName, string.Empty, ApertureAreaDefault); + profileProperties.ApertureDiameter = driverProfile.GetValueDouble(DriverId, ApertureDiameterName, string.Empty, ApertureDiameterDefault); } return profileProperties; diff --git a/Meade.net/Wrapper/GetValueException.cs b/Meade.net/Wrapper/GetValueException.cs new file mode 100644 index 0000000..ea1175c --- /dev/null +++ b/Meade.net/Wrapper/GetValueException.cs @@ -0,0 +1,28 @@ +using System; + +namespace ASCOM.Meade.net.Wrapper +{ + public class GetValueException : Exception + { + public GetValueException( + string driverId, + string name, + string subKey, + string defaultValue, + Exception innerException + ) : base ( $"Error getting value {name} from driver {driverId} in {subKey} and default value {defaultValue} cannot be used.", innerException) + { + } + + public GetValueException( + string driverId, + string name, + string subKey, + string defaultValue, + string value, + Exception innerException): + base($"Error converting value {name} from {value}", innerException) + { + } + } +} \ No newline at end of file diff --git a/Meade.net/Wrapper/IProfileWrapper.cs b/Meade.net/Wrapper/IProfileWrapper.cs index ae9d434..5682ff6 100644 --- a/Meade.net/Wrapper/IProfileWrapper.cs +++ b/Meade.net/Wrapper/IProfileWrapper.cs @@ -1,5 +1,6 @@ using System; using System.Collections; +using System.Globalization; using ASCOM.Utilities; using ASCOM.Utilities.Interfaces; @@ -7,7 +8,11 @@ namespace ASCOM.Meade.net.Wrapper { public interface IProfileWrapper : IProfile, IProfileExtra, IDisposable { + int GetValueInt(string driverId, string name, string subKey, string defaultValue); + short GetValueShort(string driverId, string name, string subKey, string defaultValue); + bool GetValueBool(string driverId, string name, string subKey, string defaultValue); + double GetValueDouble(string driverId, string name, string subKey, string defaultValue); } public class ProfileWrapper : IProfileWrapper @@ -36,7 +41,68 @@ namespace ASCOM.Meade.net.Wrapper public string GetValue(string driverId, string name, string subKey, string defaultValue) { - return _profile.GetValue(driverId, name, subKey, defaultValue); + try + { + return _profile.GetValue(driverId, name, subKey, defaultValue); + } + catch (Exception e) + { + throw new GetValueException(driverId, name, subKey, defaultValue, e); + } + + } + + public int GetValueInt(string driverId, string name, string subKey, string defaultValue) + { + var value = _profile.GetValue(driverId, name, subKey, defaultValue); + try + { + return int.Parse(value, CultureInfo.InvariantCulture); + } + catch (Exception e) + { + throw new GetValueException(driverId, name, subKey, defaultValue, value, e); + } + + } + + public short GetValueShort(string driverId, string name, string subKey, string defaultValue) + { + var value = _profile.GetValue(driverId, name, subKey, defaultValue); + try + { + return short.Parse(value, CultureInfo.InvariantCulture); + } + catch (Exception e) + { + throw new GetValueException(driverId, name, subKey, defaultValue, value, e); + } + } + + public bool GetValueBool(string driverId, string name, string subKey, string defaultValue) + { + var value = _profile.GetValue(driverId, name, subKey, defaultValue); + try + { + return bool.Parse(value); + } + catch (Exception e) + { + throw new GetValueException(driverId, name, subKey, defaultValue, value, e); + } + } + + public double GetValueDouble(string driverId, string name, string subKey, string defaultValue) + { + var value = _profile.GetValue(driverId, name, subKey, defaultValue); + try + { + return double.Parse(value, CultureInfo.InvariantCulture); + } + catch (Exception e) + { + throw new GetValueException(driverId, name, subKey, defaultValue, value, e); + } } public void WriteValue(string driverId, string name, string value, string subKey)