From 7557e01c9f5c42548a940c106e0a2b0c30ce050f Mon Sep 17 00:00:00 2001 From: Jack Kanarish Date: Thu, 7 Feb 2019 10:45:06 -0500 Subject: [PATCH 01/12] feat: add processor utilities and environment utilities for telemetry --- ICD.Common.Utils/IcdEnvironment.SimplSharp.cs | 54 ++++++++- ICD.Common.Utils/ProcessorUtils.SimplSharp.cs | 110 +++++++++++++++++- 2 files changed, 157 insertions(+), 7 deletions(-) diff --git a/ICD.Common.Utils/IcdEnvironment.SimplSharp.cs b/ICD.Common.Utils/IcdEnvironment.SimplSharp.cs index 68a8ef9..ce5f344 100644 --- a/ICD.Common.Utils/IcdEnvironment.SimplSharp.cs +++ b/ICD.Common.Utils/IcdEnvironment.SimplSharp.cs @@ -27,7 +27,17 @@ namespace ICD.Common.Utils CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS; const EthernetAdapterType type = EthernetAdapterType.EthernetLANAdapter; short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type); - yield return CrestronEthernetHelper.GetEthernetParameter(param, id); + var address1 = CrestronEthernetHelper.GetEthernetParameter(param, id); + + if (!string.IsNullOrEmpty(address1)) + yield return address1; + + const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter; + short adapter2Type = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType); + var address2 = CrestronEthernetHelper.GetEthernetParameter(param, adapter2Type); + + if (!string.IsNullOrEmpty(address2)) + yield return address2; } } @@ -47,6 +57,48 @@ namespace ICD.Common.Utils } } + /// + /// Gets the dhcp status of the processor. + /// + [PublicAPI] + public static string DhcpStatus + { + get + { + const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param = + CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_DHCP_STATE; + const EthernetAdapterType type = EthernetAdapterType.EthernetLANAdapter; + short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type); + return CrestronEthernetHelper.GetEthernetParameter(param, id); + } + } + + /// + /// Gets the hostname of the processor. + /// + [PublicAPI] + public static IEnumerable Hostname + { + get + { + const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param = + CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME; + const EthernetAdapterType type = EthernetAdapterType.EthernetLANAdapter; + short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type); + var address1 = CrestronEthernetHelper.GetEthernetParameter(param, id); + + if (!string.IsNullOrEmpty(address1)) + yield return address1; + + const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter; + short adapter2Type = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType); + var address2 = CrestronEthernetHelper.GetEthernetParameter(param, adapter2Type); + + if (!string.IsNullOrEmpty(address2)) + yield return address2; + } + } + /// /// Static constructor. /// diff --git a/ICD.Common.Utils/ProcessorUtils.SimplSharp.cs b/ICD.Common.Utils/ProcessorUtils.SimplSharp.cs index 637d764..5dff4ed 100644 --- a/ICD.Common.Utils/ProcessorUtils.SimplSharp.cs +++ b/ICD.Common.Utils/ProcessorUtils.SimplSharp.cs @@ -9,8 +9,12 @@ namespace ICD.Common.Utils { public static partial class ProcessorUtils { - private const string MODEL_NAME_REGEX = @"^(\S*)"; - private const string MODEL_VERSION_REGEX = @" [[]v(\S*)"; + private const string VER_REGEX = + @"(?'model'\S+) (?'type'\S+) (?'lang'\S+) \[v(?'version'\d+.\d+.\d+.\d+) \((?'date'\S+ \d+ \d+)\), #(?'serial'[A-F0-9]+)\] @E-(?'mac'[a-z0-9]+)"; + + private const string UPTIME_COMMAND = "uptime"; + private const string PROGUPTIME_COMMAND_ROOT = "proguptime:{0}"; + private const string UPTIME_REGEX = @".*(?'uptime'\d+ days \d{2}:\d{2}:\d{2}\.\d+)"; private const string RAMFREE_COMMAND = "ramfree"; private const string RAMFREE_DIGITS_REGEX = @"^(\d*)"; @@ -48,11 +52,11 @@ namespace ICD.Common.Utils { get { - Regex regex = new Regex(MODEL_NAME_REGEX); + Regex regex = new Regex(VER_REGEX); Match match = regex.Match(VersionResult); if (match.Success) - return match.Groups[1].Value; + return match.Groups["model"].Value; ServiceProvider.TryGetService() .AddEntry(eSeverity.Warning, "Unable to get model name from \"{0}\"", VersionResult); @@ -68,11 +72,11 @@ namespace ICD.Common.Utils { get { - Regex regex = new Regex(MODEL_VERSION_REGEX); + Regex regex = new Regex(VER_REGEX); Match match = regex.Match(VersionResult); if (match.Success) - return new Version(match.Groups[1].Value); + return new Version(match.Groups["version"].Value); ServiceProvider.TryGetService() .AddEntry(eSeverity.Warning, "Unable to get model version from \"{0}\"", VersionResult); @@ -80,6 +84,51 @@ namespace ICD.Common.Utils } } + /// + /// Gets the date that the firmware was updated. + /// + [PublicAPI] + public static string ModelVersionDate + { + get + { + Regex regex = new Regex(VER_REGEX); + Match match = regex.Match(VersionResult); + + if (match.Success) + return match.Groups["date"].Value; + + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Warning, "Unable to get model version date from \"{0}\"", VersionResult); + + return string.Empty; + } + } + + /// + /// Gets the serial number of the processor + /// + [PublicAPI] + public static string ProcessorSerialNumber + { + get + { + Regex regex = new Regex(VER_REGEX); + Match match = regex.Match(VersionResult); + + if (!match.Success) + { + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Warning, "Unable to get serial number from \"{0}\"", VersionResult); + + return string.Empty; + } + + int decValue = int.Parse(match.Groups["serial"].Value, System.Globalization.NumberStyles.HexNumber); + return decValue.ToString(); + } + } + /// /// Gets the ram usage in the range 0 - 1. /// @@ -170,6 +219,31 @@ namespace ICD.Common.Utils IcdConsole.SendControlSystemCommand("reboot", ref consoleResult); } + /// + /// Gets the uptime for the system + /// + /// + [PublicAPI] + public static string GetSystemUptime() + { + string uptime = GetUptime(); + Match match = Regex.Match(uptime, UPTIME_REGEX); + return match.Groups["uptime"].Value; + } + + /// + /// Gets the uptime + /// + /// + /// + [PublicAPI] + public static string GetProgramUptime(int progslot) + { + string uptime = GetUptime(progslot); + Match match = Regex.Match(uptime, UPTIME_REGEX); + return match.Groups["uptime"].Value; + } + #endregion /// @@ -187,6 +261,30 @@ namespace ICD.Common.Utils } return ramfree; } + + private static string GetUptime() + { + string uptime = null; + if (!IcdConsole.SendControlSystemCommand(UPTIME_COMMAND, ref uptime)) + { + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"", + typeof(ProcessorUtils).Name, UPTIME_COMMAND); + } + return uptime; + } + + private static string GetUptime(int programSlot) + { + string uptime = null; + if (!IcdConsole.SendControlSystemCommand(string.Format(PROGUPTIME_COMMAND_ROOT, programSlot), ref uptime)) + { + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"", + typeof(ProcessorUtils).Name, UPTIME_COMMAND); + } + return uptime; + } } } From c55e35ac6e68d67dcab01df3f9adf6a9ee3e8ce7 Mon Sep 17 00:00:00 2001 From: Jack Kanarish Date: Mon, 25 Mar 2019 13:51:29 -0400 Subject: [PATCH 02/12] Saftey commit for CC --- ICD.Common.Utils/Attributes/RangeAttribute.cs | 76 +++++++++---------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/ICD.Common.Utils/Attributes/RangeAttribute.cs b/ICD.Common.Utils/Attributes/RangeAttribute.cs index 31907e0..5c733f8 100644 --- a/ICD.Common.Utils/Attributes/RangeAttribute.cs +++ b/ICD.Common.Utils/Attributes/RangeAttribute.cs @@ -11,11 +11,17 @@ namespace ICD.Common.Utils.Attributes AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = true)] - public class RangeAttribute : AbstractIcdAttribute + public sealed class RangeAttribute : AbstractIcdAttribute { + #region Properties + public object Min { get; private set; } public object Max { get; private set; } + #endregion + + #region Constructors + public RangeAttribute(ushort min, ushort max) { Min = min; @@ -82,6 +88,20 @@ namespace ICD.Common.Utils.Attributes Max = max; } + #endregion + + #region Methods + + public T GetMin() + { + return (T)Convert.ChangeType(Min, typeof(T), null); + } + + public T GetMax() + { + return (T)Convert.ChangeType(Max, typeof(T), null); + } + public bool IsInRange(object value) { if (value is ushort) @@ -89,10 +109,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is ushort)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (ushort)Min; - var castMax = (ushort)Max; var castVal = (ushort)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is short) @@ -100,10 +118,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is short)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (short)Min; - var castMax = (short)Max; var castVal = (short)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is uint) @@ -111,10 +127,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is uint)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (uint)Min; - var castMax = (uint)Max; var castVal = (uint)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is int) @@ -122,10 +136,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is int)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (int)Min; - var castMax = (int)Max; var castVal = (int)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is ulong) @@ -133,10 +145,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is ulong)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (ulong)Min; - var castMax = (ulong)Max; var castVal = (ulong)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is long) @@ -144,10 +154,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is long)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (long)Min; - var castMax = (long)Max; var castVal = (long)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is float) @@ -155,10 +163,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is float)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (float)Min; - var castMax = (float)Max; var castVal = (float)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is double) @@ -166,10 +172,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is double)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (double)Min; - var castMax = (double)Max; var castVal = (double)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is decimal) @@ -177,10 +181,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is decimal)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (decimal)Min; - var castMax = (decimal)Max; var castVal = (decimal)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is byte) @@ -188,10 +190,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is byte)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (byte)Min; - var castMax = (byte)Max; var castVal = (byte)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } if (value is sbyte) @@ -199,10 +199,8 @@ namespace ICD.Common.Utils.Attributes if (!(Min is sbyte)) throw new ArgumentException("the type of value does not match the type of min / max"); - var castMin = (sbyte)Min; - var castMax = (sbyte)Max; var castVal = (sbyte)value; - return (castVal >= castMin && castVal <= castMax); + return (castVal >= GetMin() && castVal <= GetMax()); } throw new ArgumentException("the type of value is not a numeric type."); @@ -210,22 +208,24 @@ namespace ICD.Common.Utils.Attributes public ushort RemapRangeToUshort(double value) { - return (ushort)MathUtils.MapRange((double)Min, (double)Max, ushort.MinValue, ushort.MaxValue, value); + return (ushort)MathUtils.MapRange(GetMin(), GetMax(), ushort.MinValue, ushort.MaxValue, value); } public ushort RemapRangeToUshort(float value) { - return (ushort)MathUtils.MapRange((float)Min, (float)Max, ushort.MinValue, ushort.MaxValue, value); + return (ushort)MathUtils.MapRange(GetMin(), GetMax(), ushort.MinValue, ushort.MaxValue, value); } public ushort RemapRangeToUshort(int value) { - return (ushort)MathUtils.MapRange((int)Min, (int)Max, ushort.MinValue, ushort.MaxValue, value); + return (ushort)MathUtils.MapRange(GetMin(), GetMax(), ushort.MinValue, ushort.MaxValue, value); } public ushort RemapRangeToUshort(ushort value) { - return MathUtils.MapRange((ushort)Min, (ushort)Max, ushort.MinValue, ushort.MaxValue, value); + return MathUtils.MapRange(GetMin(), GetMax(), ushort.MinValue, ushort.MaxValue, value); } + + #endregion } } \ No newline at end of file From 88a4801f8e25d70ea47b9edbf3f08372feffe1c0 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 26 Mar 2019 11:28:15 -0400 Subject: [PATCH 03/12] feat: Added extension method for generating stable string hashcodes --- .../Extensions/StringExtensions.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ICD.Common.Utils/Extensions/StringExtensions.cs b/ICD.Common.Utils/Extensions/StringExtensions.cs index d1828e3..e1be90e 100644 --- a/ICD.Common.Utils/Extensions/StringExtensions.cs +++ b/ICD.Common.Utils/Extensions/StringExtensions.cs @@ -221,5 +221,29 @@ namespace ICD.Common.Utils.Extensions return extends.Contains(character.ToString()); } + + /// + /// Generates a hashcode that is consistent between program executions. + /// + /// + /// + public static int GetStableHashCode(this string extends) + { + unchecked + { + int hash1 = 5381; + int hash2 = hash1; + + for (int i = 0; i < extends.Length && extends[i] != '\0'; i += 2) + { + hash1 = ((hash1 << 5) + hash1) ^ extends[i]; + if (i == extends.Length - 1 || extends[i + 1] == '\0') + break; + hash2 = ((hash2 << 5) + hash2) ^ extends[i + 1]; + } + + return hash1 + (hash2 * 1566083941); + } + } } } From 87d1f4da166c91088de560bad636a68239fa5fc7 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 26 Mar 2019 11:28:59 -0400 Subject: [PATCH 04/12] feat: Added util method for generating seeded guids --- ICD.Common.Utils/GuidUtils.cs | 17 +++++++++++++++++ .../ICD.Common.Utils_SimplSharp.csproj | 1 + 2 files changed, 18 insertions(+) create mode 100644 ICD.Common.Utils/GuidUtils.cs diff --git a/ICD.Common.Utils/GuidUtils.cs b/ICD.Common.Utils/GuidUtils.cs new file mode 100644 index 0000000..d68feb6 --- /dev/null +++ b/ICD.Common.Utils/GuidUtils.cs @@ -0,0 +1,17 @@ +using System; + +namespace ICD.Common.Utils +{ + public static class GuidUtils + { + public static Guid GenerateSeeded(int seed) + { + Random seeded = new Random(seed); + byte[] bytes = new byte[16]; + + seeded.NextBytes(bytes); + + return new Guid(bytes); + } + } +} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 103d153..297b013 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -112,6 +112,7 @@ + From 3fd344a3087673d9804f5d63e44d3bbbdd23a998 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 27 Mar 2019 10:49:29 -0400 Subject: [PATCH 05/12] fix: Adding missing methods to Net Standard --- ICD.Common.Utils/IcdEnvironment.Standard.cs | 36 ++++++++++++--- ICD.Common.Utils/ProcessorUtils.Standard.cs | 49 +++++++++++++++++++++ 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/ICD.Common.Utils/IcdEnvironment.Standard.cs b/ICD.Common.Utils/IcdEnvironment.Standard.cs index e852ba6..a7a618a 100644 --- a/ICD.Common.Utils/IcdEnvironment.Standard.cs +++ b/ICD.Common.Utils/IcdEnvironment.Standard.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text.RegularExpressions; @@ -13,11 +14,6 @@ namespace ICD.Common.Utils { public static string NewLine { get { return Environment.NewLine; } } - public static DateTime GetLocalTime() - { - return DateTime.Now; - } - public static eRuntimeEnvironment RuntimeEnvironment { get { return eRuntimeEnvironment.Standard; } } /// @@ -53,6 +49,36 @@ namespace ICD.Common.Utils } } + /// + /// Gets the dhcp status of the processor. + /// + [PublicAPI] + public static string DhcpStatus + { + get + { + bool enabled = + NetworkInterface.GetAllNetworkInterfaces() + .Where(ni => ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || + ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) + .Select(ni => ni.GetIPProperties().GetIPv4Properties().IsDhcpEnabled) + .FirstOrDefault(); + + return enabled.ToString(); + } + } + + /// + /// Gets the hostname of the processor. + /// + [PublicAPI] + public static IEnumerable Hostname { get { yield return Dns.GetHostName(); } } + + public static DateTime GetLocalTime() + { + return DateTime.Now; + } + /// /// Converts 12 digit address to XX:XX:XX... format /// diff --git a/ICD.Common.Utils/ProcessorUtils.Standard.cs b/ICD.Common.Utils/ProcessorUtils.Standard.cs index d3fd703..619dc75 100644 --- a/ICD.Common.Utils/ProcessorUtils.Standard.cs +++ b/ICD.Common.Utils/ProcessorUtils.Standard.cs @@ -27,6 +27,32 @@ namespace ICD.Common.Utils } } + /// + /// Gets the date that the firmware was updated. + /// + [PublicAPI] + public static string ModelVersionDate + { + get + { + // TODO + return null; + } + } + + /// + /// Gets the serial number of the processor + /// + [PublicAPI] + public static string ProcessorSerialNumber + { + get + { + // TODO + return null; + } + } + /// /// Gets the ram usage in the range 0 - 1. /// @@ -109,6 +135,29 @@ namespace ICD.Common.Utils throw new NotSupportedException(); } + /// + /// Gets the uptime for the system + /// + /// + [PublicAPI] + public static string GetSystemUptime() + { + // TODO + return null; + } + + /// + /// Gets the uptime + /// + /// + /// + [PublicAPI] + public static string GetProgramUptime(int progslot) + { + // TODO + return null; + } + #endregion } } From ef19e20d67020ce9531bc6a46bd8c2faed3772a0 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 27 Mar 2019 11:24:35 -0400 Subject: [PATCH 06/12] docs: Updating changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5adbe94..8149b30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added + - Added GUID utils for generating seeded GUIDs + - Added extension method for getting stable hashcodes from strings + - Added environment and processor utilities for determining DNS status and hostname + +### Changed + - RangeAttribute improvements for better type safety + ## [8.3.0] - 2019-01-25 ### Added - Added SimplSharpProMono to eRuntimeEnvironment enum From c054a9d75289a63b70f06354a1b378e23cb78d8d Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Thu, 28 Mar 2019 10:36:04 -0400 Subject: [PATCH 07/12] fix: Fixing argument exceptions when looking up LAN2 properties on a system without multiple network adapters --- ICD.Common.Utils/IcdEnvironment.SimplSharp.cs | 104 +++++++++++++++--- 1 file changed, 87 insertions(+), 17 deletions(-) diff --git a/ICD.Common.Utils/IcdEnvironment.SimplSharp.cs b/ICD.Common.Utils/IcdEnvironment.SimplSharp.cs index ce42e79..b85dc8f 100644 --- a/ICD.Common.Utils/IcdEnvironment.SimplSharp.cs +++ b/ICD.Common.Utils/IcdEnvironment.SimplSharp.cs @@ -30,16 +30,33 @@ namespace ICD.Common.Utils { const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param = CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS; - const EthernetAdapterType type = EthernetAdapterType.EthernetLANAdapter; - short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type); - var address1 = CrestronEthernetHelper.GetEthernetParameter(param, id); + const EthernetAdapterType primaryType = EthernetAdapterType.EthernetLANAdapter; + const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter; + string address1 = null; + + try + { + short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(primaryType); + address1 = CrestronEthernetHelper.GetEthernetParameter(param, id); + } + catch (ArgumentException) + { + } + if (!string.IsNullOrEmpty(address1)) yield return address1; - const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter; - short adapter2Type = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType); - var address2 = CrestronEthernetHelper.GetEthernetParameter(param, adapter2Type); + string address2 = null; + + try + { + short adapter2Type = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType); + address2 = CrestronEthernetHelper.GetEthernetParameter(param, adapter2Type); + } + catch (ArgumentException) + { + } if (!string.IsNullOrEmpty(address2)) yield return address2; @@ -56,9 +73,36 @@ namespace ICD.Common.Utils { const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param = CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_MAC_ADDRESS; - const EthernetAdapterType type = EthernetAdapterType.EthernetLANAdapter; - short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type); - yield return CrestronEthernetHelper.GetEthernetParameter(param, id); + const EthernetAdapterType primaryType = EthernetAdapterType.EthernetLANAdapter; + const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter; + + string macAddress1 = null; + + try + { + short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(primaryType); + macAddress1 = CrestronEthernetHelper.GetEthernetParameter(param, id); + } + catch (ArgumentException) + { + } + + if (!string.IsNullOrEmpty(macAddress1)) + yield return macAddress1; + + string macAddress2 = null; + + try + { + short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType); + macAddress2 = CrestronEthernetHelper.GetEthernetParameter(param, id); + } + catch (ArgumentException) + { + } + + if (!string.IsNullOrEmpty(macAddress2)) + yield return macAddress2; } } @@ -73,8 +117,16 @@ namespace ICD.Common.Utils const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param = CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_DHCP_STATE; const EthernetAdapterType type = EthernetAdapterType.EthernetLANAdapter; - short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type); - return CrestronEthernetHelper.GetEthernetParameter(param, id); + + try + { + short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type); + return CrestronEthernetHelper.GetEthernetParameter(param, id); + } + catch (ArgumentException) + { + return null; + } } } @@ -88,16 +140,34 @@ namespace ICD.Common.Utils { const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param = CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME; - const EthernetAdapterType type = EthernetAdapterType.EthernetLANAdapter; - short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type); - var address1 = CrestronEthernetHelper.GetEthernetParameter(param, id); + const EthernetAdapterType primaryType = EthernetAdapterType.EthernetLANAdapter; + const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter; + + string address1 = null; + + try + { + short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(primaryType); + address1 = CrestronEthernetHelper.GetEthernetParameter(param, id); + } + catch (ArgumentException) + { + } if (!string.IsNullOrEmpty(address1)) yield return address1; - const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter; - short adapter2Type = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType); - var address2 = CrestronEthernetHelper.GetEthernetParameter(param, adapter2Type); + string address2 = null; + + try + { + + short adapter2Type = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType); + address2 = CrestronEthernetHelper.GetEthernetParameter(param, adapter2Type); + } + catch (ArgumentException) + { + } if (!string.IsNullOrEmpty(address2)) yield return address2; From 24859aaa313aa8bbd97a2f434d8ee69c349982ce Mon Sep 17 00:00:00 2001 From: Jack Kanarish Date: Wed, 10 Apr 2019 17:16:26 -0400 Subject: [PATCH 08/12] feat: add types to maprange --- ICD.Common.Utils/MathUtils.cs | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/ICD.Common.Utils/MathUtils.cs b/ICD.Common.Utils/MathUtils.cs index 1f44412..e4427ac 100644 --- a/ICD.Common.Utils/MathUtils.cs +++ b/ICD.Common.Utils/MathUtils.cs @@ -78,6 +78,24 @@ namespace ICD.Common.Utils return outputStart + slope * (value - inputStart); } + /// + /// Returns the value after the input range has been mapped to a new range + /// + /// Input start. + /// Input end. + /// Output start. + /// Output end. + /// Value. + /// The newly mapped value + public static decimal MapRange(decimal inputStart, decimal inputEnd, decimal outputStart, decimal outputEnd, decimal value) + { + if (inputStart.Equals(inputEnd)) + throw new DivideByZeroException(); + + decimal slope = (outputEnd - outputStart) / (inputEnd - inputStart); + return outputStart + slope * (value - inputStart); + } + /// /// Returns the value after the input range has been mapped to a new range /// @@ -120,6 +138,42 @@ namespace ICD.Common.Utils return (ushort)MapRange((double)inputStart, inputEnd, outputStart, outputEnd, value); } + /// + /// Returns the value after the input range has been mapped to a new range + /// + /// Input start. + /// Input end. + /// Output start. + /// Output end. + /// Value. + /// The newly mapped value + public static ulong MapRange(ulong inputStart, ulong inputEnd, ulong outputStart, ulong outputEnd, ulong value) + { + if (inputStart.Equals(inputEnd)) + throw new DivideByZeroException(); + + ulong slope = (outputEnd - outputStart) / (inputEnd - inputStart); + return outputStart + slope * (value - inputStart); + } + + /// + /// Returns the value after the input range has been mapped to a new range + /// + /// Input start. + /// Input end. + /// Output start. + /// Output end. + /// Value. + /// The newly mapped value + public static long MapRange(long inputStart, long inputEnd, long outputStart, long outputEnd, long value) + { + if (inputStart.Equals(inputEnd)) + throw new DivideByZeroException(); + + long slope = (outputEnd - outputStart) / (inputEnd - inputStart); + return outputStart + slope * (value - inputStart); + } + /// /// Maps the date in the given range to the float range 0.0f to 1.0f. /// 0.5f - The date is half way between the end points. From 0931d6f1ece5b54cf12fffeb1d08d3b2b24e7c62 Mon Sep 17 00:00:00 2001 From: Jack Kanarish Date: Wed, 10 Apr 2019 17:17:30 -0400 Subject: [PATCH 09/12] feat: add helper method for un-mapping a range from ushorts --- ICD.Common.Utils/Attributes/RangeAttribute.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/ICD.Common.Utils/Attributes/RangeAttribute.cs b/ICD.Common.Utils/Attributes/RangeAttribute.cs index 5c733f8..1574f9e 100644 --- a/ICD.Common.Utils/Attributes/RangeAttribute.cs +++ b/ICD.Common.Utils/Attributes/RangeAttribute.cs @@ -206,6 +206,8 @@ namespace ICD.Common.Utils.Attributes throw new ArgumentException("the type of value is not a numeric type."); } + #region Range -> UShort + public ushort RemapRangeToUshort(double value) { return (ushort)MathUtils.MapRange(GetMin(), GetMax(), ushort.MinValue, ushort.MaxValue, value); @@ -227,5 +229,75 @@ namespace ICD.Common.Utils.Attributes } #endregion + + #region UShort -> Range + + public object RemapUshortToRange(ushort value) + { + if (Min is ushort) + { + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), value); + } + + if (Min is short) + { + var castVal = (short)value; + return (short)MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is uint) + { + var castVal = (uint)value; + return (uint)MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is int) + { + var castVal = (int)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is ulong) + { + var castVal = (ulong)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is long) + { + var castVal = (long)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is float) + { + var castVal = (float)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is double) + { + var castVal = (double)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is decimal) + { + var castVal = (decimal)value; + return MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + if (Min is byte) + { + var castVal = (byte)value; + return (byte)MathUtils.MapRange(ushort.MinValue, ushort.MaxValue, GetMin(), GetMax(), castVal); + } + + throw new NotSupportedException("Value type of range attribute is not supported."); + } + + #endregion + + #endregion } } \ No newline at end of file From 475f381991899181899342c296e618ea3e57d48e Mon Sep 17 00:00:00 2001 From: Drew Tingen Date: Thu, 2 May 2019 20:21:09 -0400 Subject: [PATCH 10/12] feat: Breaking out Program/Common config directories from the full paths --- ICD.Common.Utils/PathUtils.cs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/ICD.Common.Utils/PathUtils.cs b/ICD.Common.Utils/PathUtils.cs index 64e21a6..37085d1 100644 --- a/ICD.Common.Utils/PathUtils.cs +++ b/ICD.Common.Utils/PathUtils.cs @@ -61,11 +61,19 @@ namespace ICD.Common.Utils { get { - if (IcdEnvironment.RuntimeEnvironment == IcdEnvironment.eRuntimeEnvironment.SimplSharpProMono) - return Join(RootConfigPath, "ProgramConfig"); + return Join(RootConfigPath, ProgramConfigDirectory); + } + } - string directoryName = string.Format("Program{0:D2}Config", ProgramUtils.ProgramNumber); - return Join(RootConfigPath, directoryName); + [PublicAPI] + public static string ProgramConfigDirectory + { + get + { + if (IcdEnvironment.RuntimeEnvironment == IcdEnvironment.eRuntimeEnvironment.SimplSharpProMono) + return "ProgramConfig"; + + return string.Format("Program{0:D2}Config", ProgramUtils.ProgramNumber); } } @@ -73,7 +81,10 @@ namespace ICD.Common.Utils /// Returns the absolute path to the common configuration directory. /// [PublicAPI] - public static string CommonConfigPath { get { return Join(RootConfigPath, "CommonConfig"); } } + public static string CommonConfigPath { get { return Join(RootConfigPath, CommonConfigDirectory); } } + + [PublicAPI] + public static string CommonConfigDirectory { get { return "CommonConfig"; }} /// /// Returns the absolute path to the common config library directory. From 294afd1834d3e4efc4f09c61f23ab17a165870e2 Mon Sep 17 00:00:00 2001 From: Drew Tingen Date: Thu, 2 May 2019 20:22:54 -0400 Subject: [PATCH 11/12] chore: update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d51037..b5ae245 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed - RangeAttribute improvements for better type safety + - PathUtils breaking out ProgramConfigDirectory and CommonConfigDirectory from the full paths ## [8.3.1] - 2019-04-05 ### Changed From 2b376bff4786aa73de6e5bc6787539b8af97cac0 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 15 May 2019 15:59:09 -0400 Subject: [PATCH 12/12] chore: Updating changelog, incrementing minor version --- CHANGELOG.md | 1 + ICD.Common.Utils/Properties/AssemblyInfo.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ac4bad..1714900 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [8.4.0] - 2019-05-15 ### Added - Added GUID utils for generating seeded GUIDs - Added extension method for getting stable hashcodes from strings diff --git a/ICD.Common.Utils/Properties/AssemblyInfo.cs b/ICD.Common.Utils/Properties/AssemblyInfo.cs index a70fc82..97275b5 100644 --- a/ICD.Common.Utils/Properties/AssemblyInfo.cs +++ b/ICD.Common.Utils/Properties/AssemblyInfo.cs @@ -4,4 +4,4 @@ using System.Reflection; [assembly: AssemblyCompany("ICD Systems")] [assembly: AssemblyProduct("ICD.Common.Utils")] [assembly: AssemblyCopyright("Copyright © ICD Systems 2019")] -[assembly: AssemblyVersion("8.3.1.0")] +[assembly: AssemblyVersion("8.4.0.0")]