diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 199025f..9fe90d2 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -85,6 +85,8 @@ + + diff --git a/ICD.Common.Utils/ProcessorUtils.SimplSharp.cs b/ICD.Common.Utils/ProcessorUtils.SimplSharp.cs new file mode 100644 index 0000000..2c98761 --- /dev/null +++ b/ICD.Common.Utils/ProcessorUtils.SimplSharp.cs @@ -0,0 +1,192 @@ +#if SIMPLSHARP +using System; +using System.Text.RegularExpressions; +using ICD.Common.Properties; +using ICD.Common.Services; +using ICD.Common.Services.Logging; + +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 RAMFREE_COMMAND = "ramfree"; + private const string RAMFREE_DIGITS_REGEX = @"^(\d*)"; + + private static string s_VersionResult; + + #region Properties + + /// + /// Gets the version text from the console. + /// + private static string VersionResult + { + get + { + if (string.IsNullOrEmpty(s_VersionResult)) + { + if (!IcdConsole.SendControlSystemCommand("version", ref s_VersionResult)) + { + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"", + typeof(ProcessorUtils).Name, "version"); + } + } + + return s_VersionResult; + } + } + + /// + /// Gets the model name of the processor. + /// + [PublicAPI] + public static string ModelName + { + get + { + Regex regex = new Regex(MODEL_NAME_REGEX); + Match match = regex.Match(VersionResult); + + if (match.Success) + return match.Groups[1].Value; + + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Warning, "Unable to get model name from \"{0}\"", VersionResult); + return string.Empty; + } + } + + /// + /// Gets the processor firmware version. + /// + [PublicAPI] + public static Version ModelVersion + { + get + { + Regex regex = new Regex(MODEL_VERSION_REGEX); + Match match = regex.Match(VersionResult); + + if (match.Success) + return new Version(match.Groups[1].Value); + + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Warning, "Unable to get model version from \"{0}\"", VersionResult); + return new Version(0, 0); + } + } + + /// + /// Gets the ram usage in the range 0 - 1. + /// + public static float RamUsagePercent + { + get + { + string ramFree = GetRamFree(); + string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[0].Groups[1].Value; + return float.Parse(digits) / 100.0f; + } + } + + /// + /// Gets the total number of bytes of physical memory. + /// + public static ulong RamTotalBytes + { + get + { + string ramFree = GetRamFree(); + string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[1].Groups[1].Value; + return ulong.Parse(digits); + } + } + + /// + /// Gets the total number of bytes of physical memory being used by the control system. + /// + public static ulong RamUsedBytes + { + get + { + string ramFree = GetRamFree(); + string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[2].Groups[1].Value; + return ulong.Parse(digits); + } + } + + /// + /// Gets the total number of bytes of physical memory not being used by the control system. + /// + public static ulong RamBytesFree + { + get + { + string ramFree = GetRamFree(); + string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[3].Groups[1].Value; + return ulong.Parse(digits); + } + } + + /// + /// Gets the total number of bytes that can be reclaimed. + /// + public static ulong RamBytesReclaimable + { + get + { + string ramFree = GetRamFree(); + string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[4].Groups[1].Value; + return ulong.Parse(digits); + } + } + + #endregion + + #region Methods + + /// + /// Restarts this program. + /// + [PublicAPI] + public static void RestartProgram() + { + string consoleResult = string.Empty; + string command = string.Format("progreset -p:{0:D2}", ProgramUtils.ProgramNumber); + IcdConsole.SendControlSystemCommand(command, ref consoleResult); + } + + /// + /// Reboots the processor. + /// + [PublicAPI] + public static void Reboot() + { + string consoleResult = string.Empty; + IcdConsole.SendControlSystemCommand("reboot", ref consoleResult); + } + + #endregion + + /// + /// Gets the result from the ramfree console command. + /// + /// + private static string GetRamFree() + { + string ramfree = null; + if (!IcdConsole.SendControlSystemCommand(RAMFREE_COMMAND, ref ramfree)) + { + ServiceProvider.TryGetService() + .AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"", + typeof(ProcessorUtils).Name, RAMFREE_COMMAND); + } + return ramfree; + } + } +} +#endif diff --git a/ICD.Common.Utils/ProcessorUtils.Standard.cs b/ICD.Common.Utils/ProcessorUtils.Standard.cs new file mode 100644 index 0000000..4ae0d32 --- /dev/null +++ b/ICD.Common.Utils/ProcessorUtils.Standard.cs @@ -0,0 +1,117 @@ +#if !SIMPLSHARP +using System; +using ICD.Common.Properties; + +namespace ICD.Common.Utils +{ + public static partial class ProcessorUtils + { + #region Properties + + /// + /// Gets the model name of the processor. + /// + [PublicAPI] + public static string ModelName { get { return Environment.MachineName; } } + + /// + /// Gets the processor firmware version. + /// + [PublicAPI] + public static Version ModelVersion + { + get + { + // TODO + return new Version("1.0.0.0"); + } + } + + /// + /// Gets the ram usage in the range 0 - 1. + /// + public static float RamUsagePercent + { + get + { + // TODO + return 0.0f; + } + } + + /// + /// Gets the total number of bytes of physical memory. + /// + public static ulong RamTotalBytes + { + get + { + // TODO + return 0; + } + } + + /// + /// Gets the total number of bytes of physical memory being used by the control system. + /// + public static ulong RamUsedBytes + { + get + { + // TODO + return 0; + } + } + + /// + /// Gets the total number of bytes of physical memory not being used by the control system. + /// + public static ulong RamBytesFree + { + get + { + // TODO + return 0; + } + } + + /// + /// Gets the total number of bytes that can be reclaimed. + /// + public static ulong RamBytesReclaimable + { + get + { + // TODO + return 0; + } + } + + #endregion + + #region Methods + + /// + /// Restarts this program. + /// + [PublicAPI] + public static void RestartProgram() + { + // TODO + throw new NotImplementedException(); + } + + /// + /// Reboots the processor. + /// + [PublicAPI] + public static void Reboot() + { + // TODO + throw new NotImplementedException(); + } + + #endregion + } +} +#endif diff --git a/ICD.Common.Utils/ProcessorUtils.cs b/ICD.Common.Utils/ProcessorUtils.cs index ed6f84f..b37a50d 100644 --- a/ICD.Common.Utils/ProcessorUtils.cs +++ b/ICD.Common.Utils/ProcessorUtils.cs @@ -1,190 +1,6 @@ -using System; -using System.Text.RegularExpressions; -using ICD.Common.Properties; -using ICD.Common.Services; -using ICD.Common.Services.Logging; - -namespace ICD.Common.Utils +namespace ICD.Common.Utils { - public static class ProcessorUtils + public static partial class ProcessorUtils { - private const string MODEL_NAME_REGEX = @"^(\S*)"; - private const string MODEL_VERSION_REGEX = @" [[]v(\S*)"; - - private const string RAMFREE_COMMAND = "ramfree"; - private const string RAMFREE_DIGITS_REGEX = @"^(\d*)"; - - private static string s_VersionResult; - - #region Properties - - /// - /// Gets the version text from the console. - /// - private static string VersionResult - { - get - { - if (string.IsNullOrEmpty(s_VersionResult)) - { - if (!IcdConsole.SendControlSystemCommand("version", ref s_VersionResult)) - { - ServiceProvider.TryGetService() - .AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"", - typeof(ProcessorUtils).Name, "version"); - } - } - - return s_VersionResult; - } - } - - /// - /// Gets the model name of the processor. - /// - [PublicAPI] - public static string ModelName - { - get - { - Regex regex = new Regex(MODEL_NAME_REGEX); - Match match = regex.Match(VersionResult); - - if (match.Success) - return match.Groups[1].Value; - - ServiceProvider.TryGetService() - .AddEntry(eSeverity.Warning, "Unable to get model name from \"{0}\"", VersionResult); - return string.Empty; - } - } - - /// - /// Gets the processor firmware version. - /// - [PublicAPI] - public static Version ModelVersion - { - get - { - Regex regex = new Regex(MODEL_VERSION_REGEX); - Match match = regex.Match(VersionResult); - - if (match.Success) - return new Version(match.Groups[1].Value); - - ServiceProvider.TryGetService() - .AddEntry(eSeverity.Warning, "Unable to get model version from \"{0}\"", VersionResult); - return new Version(0, 0); - } - } - - /// - /// Gets the ram usage in the range 0 - 1. - /// - public static float RamUsagePercent - { - get - { - string ramFree = GetRamFree(); - string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[0].Groups[1].Value; - return float.Parse(digits) / 100.0f; - } - } - - /// - /// Gets the total number of bytes of physical memory. - /// - public static ulong RamTotalBytes - { - get - { - string ramFree = GetRamFree(); - string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[1].Groups[1].Value; - return ulong.Parse(digits); - } - } - - /// - /// Gets the total number of bytes of physical memory being used by the control system. - /// - public static ulong RamUsedBytes - { - get - { - string ramFree = GetRamFree(); - string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[2].Groups[1].Value; - return ulong.Parse(digits); - } - } - - /// - /// Gets the total number of bytes of physical memory not being used by the control system. - /// - public static ulong RamBytesFree - { - get - { - string ramFree = GetRamFree(); - string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[3].Groups[1].Value; - return ulong.Parse(digits); - } - } - - /// - /// Gets the total number of bytes that can be reclaimed. - /// - public static ulong RamBytesReclaimable - { - get - { - string ramFree = GetRamFree(); - string digits = Regex.Matches(ramFree, RAMFREE_DIGITS_REGEX, RegexOptions.Multiline)[4].Groups[1].Value; - return ulong.Parse(digits); - } - } - - #endregion - - #region Methods - - /// - /// Restarts this program. - /// - [PublicAPI] - public static void RestartProgram() - { - string consoleResult = string.Empty; - string command = string.Format("progreset -p:{0:D2}", ProgramUtils.ProgramNumber); - IcdConsole.SendControlSystemCommand(command, ref consoleResult); - } - - /// - /// Reboots the processor. - /// - [PublicAPI] - public static void Reboot() - { - string consoleResult = string.Empty; - IcdConsole.SendControlSystemCommand("reboot", ref consoleResult); - } - - #endregion - - /// - /// Gets the result from the ramfree console command. - /// - /// - private static string GetRamFree() - { - string ramfree = null; - if (!IcdConsole.SendControlSystemCommand(RAMFREE_COMMAND, ref ramfree)) - { - ServiceProvider.TryGetService() - .AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"", - typeof(ProcessorUtils).Name, RAMFREE_COMMAND); - } - return ramfree; - } } }