Splitting ProcessorUtils into SimplSharp and NetStandard portions

This commit is contained in:
Chris Cameron
2017-09-27 10:43:22 -04:00
parent f206741e3a
commit ac885e9837
4 changed files with 313 additions and 186 deletions

View File

@@ -85,6 +85,8 @@
<None Include="ObfuscationSettings.cs" />
<Compile Include="Extensions\ByteExtensions.cs" />
<Compile Include="Extensions\ListExtensions.cs" />
<Compile Include="ProcessorUtils.SimplSharp.cs" />
<Compile Include="ProcessorUtils.Standard.cs" />
<Compile Include="ProgramUtils.SimplSharp.cs" />
<Compile Include="ProgramUtils.Standard.cs" />
<Compile Include="Properties\Annotations.cs" />

View File

@@ -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
/// <summary>
/// Gets the version text from the console.
/// </summary>
private static string VersionResult
{
get
{
if (string.IsNullOrEmpty(s_VersionResult))
{
if (!IcdConsole.SendControlSystemCommand("version", ref s_VersionResult))
{
ServiceProvider.TryGetService<ILoggerService>()
.AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"",
typeof(ProcessorUtils).Name, "version");
}
}
return s_VersionResult;
}
}
/// <summary>
/// Gets the model name of the processor.
/// </summary>
[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<ILoggerService>()
.AddEntry(eSeverity.Warning, "Unable to get model name from \"{0}\"", VersionResult);
return string.Empty;
}
}
/// <summary>
/// Gets the processor firmware version.
/// </summary>
[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<ILoggerService>()
.AddEntry(eSeverity.Warning, "Unable to get model version from \"{0}\"", VersionResult);
return new Version(0, 0);
}
}
/// <summary>
/// Gets the ram usage in the range 0 - 1.
/// </summary>
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;
}
}
/// <summary>
/// Gets the total number of bytes of physical memory.
/// </summary>
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);
}
}
/// <summary>
/// Gets the total number of bytes of physical memory being used by the control system.
/// </summary>
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);
}
}
/// <summary>
/// Gets the total number of bytes of physical memory not being used by the control system.
/// </summary>
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);
}
}
/// <summary>
/// Gets the total number of bytes that can be reclaimed.
/// </summary>
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
/// <summary>
/// Restarts this program.
/// </summary>
[PublicAPI]
public static void RestartProgram()
{
string consoleResult = string.Empty;
string command = string.Format("progreset -p:{0:D2}", ProgramUtils.ProgramNumber);
IcdConsole.SendControlSystemCommand(command, ref consoleResult);
}
/// <summary>
/// Reboots the processor.
/// </summary>
[PublicAPI]
public static void Reboot()
{
string consoleResult = string.Empty;
IcdConsole.SendControlSystemCommand("reboot", ref consoleResult);
}
#endregion
/// <summary>
/// Gets the result from the ramfree console command.
/// </summary>
/// <returns></returns>
private static string GetRamFree()
{
string ramfree = null;
if (!IcdConsole.SendControlSystemCommand(RAMFREE_COMMAND, ref ramfree))
{
ServiceProvider.TryGetService<ILoggerService>()
.AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"",
typeof(ProcessorUtils).Name, RAMFREE_COMMAND);
}
return ramfree;
}
}
}
#endif

View File

@@ -0,0 +1,117 @@
#if !SIMPLSHARP
using System;
using ICD.Common.Properties;
namespace ICD.Common.Utils
{
public static partial class ProcessorUtils
{
#region Properties
/// <summary>
/// Gets the model name of the processor.
/// </summary>
[PublicAPI]
public static string ModelName { get { return Environment.MachineName; } }
/// <summary>
/// Gets the processor firmware version.
/// </summary>
[PublicAPI]
public static Version ModelVersion
{
get
{
// TODO
return new Version("1.0.0.0");
}
}
/// <summary>
/// Gets the ram usage in the range 0 - 1.
/// </summary>
public static float RamUsagePercent
{
get
{
// TODO
return 0.0f;
}
}
/// <summary>
/// Gets the total number of bytes of physical memory.
/// </summary>
public static ulong RamTotalBytes
{
get
{
// TODO
return 0;
}
}
/// <summary>
/// Gets the total number of bytes of physical memory being used by the control system.
/// </summary>
public static ulong RamUsedBytes
{
get
{
// TODO
return 0;
}
}
/// <summary>
/// Gets the total number of bytes of physical memory not being used by the control system.
/// </summary>
public static ulong RamBytesFree
{
get
{
// TODO
return 0;
}
}
/// <summary>
/// Gets the total number of bytes that can be reclaimed.
/// </summary>
public static ulong RamBytesReclaimable
{
get
{
// TODO
return 0;
}
}
#endregion
#region Methods
/// <summary>
/// Restarts this program.
/// </summary>
[PublicAPI]
public static void RestartProgram()
{
// TODO
throw new NotImplementedException();
}
/// <summary>
/// Reboots the processor.
/// </summary>
[PublicAPI]
public static void Reboot()
{
// TODO
throw new NotImplementedException();
}
#endregion
}
}
#endif

View File

@@ -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
/// <summary>
/// Gets the version text from the console.
/// </summary>
private static string VersionResult
{
get
{
if (string.IsNullOrEmpty(s_VersionResult))
{
if (!IcdConsole.SendControlSystemCommand("version", ref s_VersionResult))
{
ServiceProvider.TryGetService<ILoggerService>()
.AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"",
typeof(ProcessorUtils).Name, "version");
}
}
return s_VersionResult;
}
}
/// <summary>
/// Gets the model name of the processor.
/// </summary>
[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<ILoggerService>()
.AddEntry(eSeverity.Warning, "Unable to get model name from \"{0}\"", VersionResult);
return string.Empty;
}
}
/// <summary>
/// Gets the processor firmware version.
/// </summary>
[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<ILoggerService>()
.AddEntry(eSeverity.Warning, "Unable to get model version from \"{0}\"", VersionResult);
return new Version(0, 0);
}
}
/// <summary>
/// Gets the ram usage in the range 0 - 1.
/// </summary>
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;
}
}
/// <summary>
/// Gets the total number of bytes of physical memory.
/// </summary>
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);
}
}
/// <summary>
/// Gets the total number of bytes of physical memory being used by the control system.
/// </summary>
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);
}
}
/// <summary>
/// Gets the total number of bytes of physical memory not being used by the control system.
/// </summary>
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);
}
}
/// <summary>
/// Gets the total number of bytes that can be reclaimed.
/// </summary>
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
/// <summary>
/// Restarts this program.
/// </summary>
[PublicAPI]
public static void RestartProgram()
{
string consoleResult = string.Empty;
string command = string.Format("progreset -p:{0:D2}", ProgramUtils.ProgramNumber);
IcdConsole.SendControlSystemCommand(command, ref consoleResult);
}
/// <summary>
/// Reboots the processor.
/// </summary>
[PublicAPI]
public static void Reboot()
{
string consoleResult = string.Empty;
IcdConsole.SendControlSystemCommand("reboot", ref consoleResult);
}
#endregion
/// <summary>
/// Gets the result from the ramfree console command.
/// </summary>
/// <returns></returns>
private static string GetRamFree()
{
string ramfree = null;
if (!IcdConsole.SendControlSystemCommand(RAMFREE_COMMAND, ref ramfree))
{
ServiceProvider.TryGetService<ILoggerService>()
.AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"",
typeof(ProcessorUtils).Name, RAMFREE_COMMAND);
}
return ramfree;
}
}
}