#if SIMPLSHARP using System; using System.Text.RegularExpressions; using Crestron.SimplSharp; using ICD.Common.Properties; using ICD.Common.Services; using ICD.Common.Services.Logging; namespace ICD.Common.Utils { public static class CrestronUtils { 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 default mac address of the processor. /// [PublicAPI] public static string DefaultMacAddress { get { 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); return CrestronEthernetHelper.GetEthernetParameter(param, id); } } /// /// Gets the version text from the console. /// private static string VersionResult { get { if (string.IsNullOrEmpty(s_VersionResult)) { if (!CrestronConsole.SendControlSystemCommand("version", ref s_VersionResult)) { ServiceProvider.TryGetService() .AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"", typeof(CrestronUtils).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); CrestronConsole.SendControlSystemCommand(command, ref consoleResult); } /// /// Reboots the processor. /// [PublicAPI] public static void Reboot() { string consoleResult = string.Empty; CrestronConsole.SendControlSystemCommand("reboot", ref consoleResult); } /// /// Runs CrestronInvoke but catches any unhandled exceptions to prevent the program from terminating. /// http://www.crestronlabs.com/showthread.php?12205-Exception-in-CrestronInvoke-thread-crashes-the-program /// /// [PublicAPI] public static object SafeInvoke(Action callback) { return SafeInvoke(unused => callback(), null); } /// /// Runs CrestronInvoke but catches any unhandled exceptions to prevent the program from terminating. /// http://www.crestronlabs.com/showthread.php?12205-Exception-in-CrestronInvoke-thread-crashes-the-program /// /// /// /// [PublicAPI] public static object SafeInvoke(Action callback, T param) { return CrestronInvoke.BeginInvoke(unused => { try { callback(param); } catch (Exception e) { ServiceProvider.TryGetService() .AddEntry(eSeverity.Error, e, e.Message); } }, null); } #endregion /// /// Gets the result from the ramfree console command. /// /// private static string GetRamFree() { string ramfree = null; if (!CrestronConsole.SendControlSystemCommand(RAMFREE_COMMAND, ref ramfree)) { ServiceProvider.TryGetService() .AddEntry(eSeverity.Warning, "{0} - Failed to send console command \"{1}\"", typeof(CrestronUtils).Name, RAMFREE_COMMAND); } return ramfree; } } } #endif