feat: remove context file for storing values

This commit is contained in:
Andrew Welker
2025-07-04 16:03:00 -05:00
parent 6d2cd75cbe
commit 1cbc8194ec
2 changed files with 1056 additions and 1050 deletions

View File

@@ -18,15 +18,16 @@ using System.Text.RegularExpressions;
namespace PepperDash.Core;
/// <summary>
/// Contains debug commands for use in various situations
/// </summary>
public static class Debug
{
/// <summary>
/// Contains debug commands for use in various situations
/// </summary>
public static class Debug
{
private static readonly string LevelStoreKey = "ConsoleDebugLevel";
private static readonly string WebSocketLevelStoreKey = "WebsocketDebugLevel";
private static readonly string ErrorLogLevelStoreKey = "ErrorLogDebugLevel";
private static readonly string FileLevelStoreKey = "FileDebugLevel";
private static readonly string DoNotLoadOnNextBootKey = "DoNotLoadOnNextBoot";
private static readonly Dictionary<uint, LogEventLevel> _logLevels = new Dictionary<uint, LogEventLevel>()
{
@@ -117,6 +118,8 @@ namespace PepperDash.Core;
public static LoggerConfiguration LoggerConfiguration => _loggerConfiguration;
static Debug()
{
try
{
CrestronDataStoreStatic.InitCrestronDataStore();
@@ -162,21 +165,6 @@ namespace PepperDash.Core;
levelSwitch: _fileLevelSwitch
);
try
{
if (InitialParametersClass.NumberOfRemovableDrives > 0)
{
CrestronConsole.PrintLine("{0} RM Drive(s) Present. Initializing CrestronLogger", InitialParametersClass.NumberOfRemovableDrives);
_defaultLoggerConfiguration.WriteTo.Sink(new DebugCrestronLoggerSink());
}
else
CrestronConsole.PrintLine("No RM Drive(s) Present. Not using Crestron Logger");
}
catch (Exception e)
{
CrestronConsole.PrintLine("Initializing of CrestronLogger failed: {0}", e);
}
// Instantiate the root logger
_loggerConfiguration = _defaultLoggerConfiguration;
@@ -193,7 +181,7 @@ namespace PepperDash.Core;
CrestronConsole.PrintLine(msg);
LogMessage(LogEventLevel.Information,msg);
LogMessage(LogEventLevel.Information, msg);
IncludedExcludedKeys = new Dictionary<string, object>();
@@ -218,13 +206,9 @@ namespace PepperDash.Core;
CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler;
LoadMemory();
DoNotLoadConfigOnNextBoot = GetDoNotLoadOnNextBoot();
var context = _contexts.GetOrCreateItem("DEFAULT");
Level = context.Level;
DoNotLoadConfigOnNextBoot = context.DoNotLoadOnNextBoot;
if(DoNotLoadConfigOnNextBoot)
if (DoNotLoadConfigOnNextBoot)
CrestronConsole.PrintLine(string.Format("Program {0} will not load config after next boot. Use console command go:{0} to load the config manually", InitialParametersClass.ApplicationNumber));
_consoleLoggingLevelSwitch.MinimumLevelChanged += (sender, args) =>
@@ -232,6 +216,24 @@ namespace PepperDash.Core;
LogMessage(LogEventLevel.Information, "Console debug level set to {minimumLevel}", _consoleLoggingLevelSwitch.MinimumLevel);
};
}
catch (Exception ex)
{
LogError(ex, "Exception in Debug static constructor: {message}", ex.Message);
}
}
private static bool GetDoNotLoadOnNextBoot()
{
var err = CrestronDataStoreStatic.GetLocalBoolValue(DoNotLoadOnNextBootKey, out var doNotLoad);
if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS)
{
LogError("Error retrieving DoNotLoadOnNextBoot value: {err}", err);
doNotLoad = false;
}
return doNotLoad;
}
public static void UpdateLoggerConfiguration(LoggerConfiguration config)
{
@@ -571,11 +573,13 @@ namespace PepperDash.Core;
public static void SetDoNotLoadConfigOnNextBoot(bool state)
{
DoNotLoadConfigOnNextBoot = state;
_contexts.GetOrCreateItem("DEFAULT").DoNotLoadOnNextBoot = state;
SaveMemoryOnTimeout();
CrestronConsole.ConsoleCommandResponse("[Application {0}], Do Not Load Config on Next Boot set to {1}",
InitialParametersClass.ApplicationNumber, DoNotLoadConfigOnNextBoot);
var err = CrestronDataStoreStatic.SetLocalBoolValue(DoNotLoadOnNextBootKey, state);
if (err != CrestronDataStore.CDS_ERROR.CDS_SUCCESS)
LogError("Error saving console debug level setting: {err}", err);
LogInformation("Do Not Load Config on Next Boot set to {state}", DoNotLoadConfigOnNextBoot);
}
/// <summary>

View File

@@ -15,15 +15,15 @@ using X509Certificate2 = System.Security.Cryptography.X509Certificates.X509Certi
namespace PepperDash.Core;
/// <summary>
/// Provides a WebSocket-based logging sink for debugging purposes, allowing log events to be broadcast to connected
/// WebSocket clients.
/// </summary>
/// <remarks>This class implements the <see cref="ILogEventSink"/> interface and is designed to send
/// formatted log events to WebSocket clients connected to a secure WebSocket server. The server is hosted locally
/// and uses a self-signed certificate for SSL/TLS encryption.</remarks>
public class DebugWebsocketSink : ILogEventSink, IKeyed
{
/// <summary>
/// Provides a WebSocket-based logging sink for debugging purposes, allowing log events to be broadcast to connected
/// WebSocket clients.
/// </summary>
/// <remarks>This class implements the <see cref="ILogEventSink"/> interface and is designed to send
/// formatted log events to WebSocket clients connected to a secure WebSocket server. The server is hosted locally
/// and uses a self-signed certificate for SSL/TLS encryption.</remarks>
public class DebugWebsocketSink : ILogEventSink, IKeyed
{
private HttpServer _httpsServer;
private readonly string _path = "/debug/join/";
@@ -107,8 +107,10 @@ namespace PepperDash.Core;
//Crestron fails to let us do this...perhaps it should be done through their Dll's but haven't tested
var separator = Path.DirectorySeparatorChar;
utility.CertificatePassword = _certificatePassword;
utility.WriteCertificate(certificate, @"\user\", _certificateName);
utility.WriteCertificate(certificate, @$"{separator}user{separator}", _certificateName);
}
catch (Exception ex)
{
@@ -228,15 +230,15 @@ namespace PepperDash.Core;
_httpsServer = null;
}
}
}
/// <summary>
/// Configures the logger to write log events to a debug WebSocket sink.
/// </summary>
/// <remarks>This extension method allows you to direct log events to a WebSocket sink for debugging
/// purposes.</remarks>
public static class DebugWebsocketSinkExtensions
{
/// <summary>
/// Configures the logger to write log events to a debug WebSocket sink.
/// </summary>
/// <remarks>This extension method allows you to direct log events to a WebSocket sink for debugging
/// purposes.</remarks>
public static class DebugWebsocketSinkExtensions
{
/// <summary>
/// Configures a logger to write log events to a debug WebSocket sink.
/// </summary>
@@ -251,17 +253,17 @@ namespace PepperDash.Core;
{
return loggerConfiguration.Sink(new DebugWebsocketSink(formatProvider));
}
}
}
/// <summary>
/// Represents a WebSocket client for debugging purposes, providing connection lifecycle management and message
/// handling functionality.
/// </summary>
/// <remarks>The <see cref="DebugClient"/> class extends <see cref="WebSocketBehavior"/> to handle
/// WebSocket connections, including events for opening, closing, receiving messages, and errors. It tracks the
/// duration of the connection and logs relevant events for debugging.</remarks>
public class DebugClient : WebSocketBehavior
{
/// <summary>
/// Represents a WebSocket client for debugging purposes, providing connection lifecycle management and message
/// handling functionality.
/// </summary>
/// <remarks>The <see cref="DebugClient"/> class extends <see cref="WebSocketBehavior"/> to handle
/// WebSocket connections, including events for opening, closing, receiving messages, and errors. It tracks the
/// duration of the connection and logs relevant events for debugging.</remarks>
public class DebugClient : WebSocketBehavior
{
private DateTime _connectionTime;
/// <summary>