From 7557e01c9f5c42548a940c106e0a2b0c30ce050f Mon Sep 17 00:00:00 2001 From: Jack Kanarish Date: Thu, 7 Feb 2019 10:45:06 -0500 Subject: [PATCH] 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; + } } }