using System; using System.Collections.Generic; using System.Linq; #if SIMPLSHARP using Crestron.SimplSharp; #endif using ICD.Common.Properties; using ICD.Common.Services; using ICD.Common.Services.Logging; using ICD.Common.Utils.Extensions; namespace ICD.Common.Utils { public static class ProgramUtils { private const string APPLICATION_NAME_KEY = "Application Name"; private const string APPLICATION_NAME_SIMPL_KEY = "System Name"; private const string PROGRAM_FILE_KEY = "Program File"; private const string COMPILED_ON_KEY = "Compiled On"; private const string COMPILER_REVISION_KEY = "Compiler Revision"; private const string COMPILER_REVISION_SIMPL_KEY = "Compiler Rev"; private static Dictionary s_ProgComments; #region Properties /// /// Lazy-load the prog-comments map. /// private static Dictionary ProgComments { get { return s_ProgComments ?? (s_ProgComments = ParseProgComments()); } } /// /// Gets the program number. /// [PublicAPI] public static uint ProgramNumber { get { #if SIMPLSHARP return InitialParametersClass.ApplicationNumber; #else return 1; #endif } } /// /// Gets the program number in the format XX, eg slot 1 is 01. /// [PublicAPI] public static string ProgramNumberFormatted { get { return string.Format("{0:D2}", ProgramNumber); } } /// /// Gets the compile date of the program. /// [PublicAPI] public static string CompiledDate { get { return ProgComments.GetDefault(COMPILED_ON_KEY, null); } } /// /// Gets the compiler revision version. /// [PublicAPI] public static Version CompilerRevision { get { string output; if (ProgComments.TryGetValue(COMPILER_REVISION_KEY, out output)) return new Version(output); if (ProgComments.TryGetValue(COMPILER_REVISION_SIMPL_KEY, out output)) return new Version(output); return new Version(0, 0); } } /// /// Gets the name of the program dll. /// [PublicAPI] public static string ProgramFile { get { return ProgComments.GetDefault(PROGRAM_FILE_KEY, null); } } /// /// Gets the name of the application. /// [PublicAPI] public static string ApplicationName { get { string output; if (ProgComments.TryGetValue(APPLICATION_NAME_KEY, out output)) return output; ProgComments.TryGetValue(APPLICATION_NAME_SIMPL_KEY, out output); return output; } } #endregion /// /// Fakes program info, e.g. "Min Firmware Version : 1.009.0029" /// /// /// [PublicAPI] public static void PrintProgramInfoLine(string name, object value) { name = (name ?? string.Empty).Trim(); switch (IcdEnvironment.RuntimeEnvironment) { case IcdEnvironment.eRuntimeEnvironment.SimplSharp: int length = Math.Min(13, name.Length); name = name.Substring(0, length).PadRight(13); break; case IcdEnvironment.eRuntimeEnvironment.SimplSharpPro: int proLength = Math.Min(26 - 1, name.Length); name = name.Substring(0, proLength).PadRight(26); break; case IcdEnvironment.eRuntimeEnvironment.Standard: name += ' '; break; default: throw new ArgumentOutOfRangeException(); } IcdConsole.PrintLine("{0}: {1}", name, value); } /// /// Parses the prog comments and pulls program information. /// private static Dictionary ParseProgComments() { Dictionary output = new Dictionary(); try { string progInfo = string.Empty; string command = string.Format("progcomments:{0}", ProgramNumber); if (!IcdConsole.SendControlSystemCommand(command, ref progInfo)) { ServiceProvider.TryGetService().AddEntry(eSeverity.Warning, "Failed to parse prog comments"); return output; } foreach (string line in progInfo.Split(new[] {"\n\r", "\r\n", "\n", "\r"})) { string[] pair = line.Split(':', 2).ToArray(); string key = pair[0].Trim(); string value = pair[1].Trim(); output[key] = value; } } catch (Exception e) { ServiceProvider.TryGetService() .AddEntry(eSeverity.Error, e, "Failed to parse prog comments - {0}", e.Message); } return output; } } }