refactor: Clean up and streamline Debug and WebApiServer classes for improved readability and performance

This commit is contained in:
Neil Dorin 2026-04-07 15:53:00 -06:00
parent 5c26438a1c
commit c98b48ff87
7 changed files with 379 additions and 280 deletions

View file

@ -123,68 +123,48 @@ namespace PepperDash.Essentials.Core.Config;
{
Debug.LogMessage(LogEventLevel.Information, "Loading config file: '{0}'", filePath);
var fileContents = fs.ReadToEnd();
if (localConfigFound)
{
ConfigObject = JObject.Parse(fs.ReadToEnd()).ToObject<EssentialsConfig>();
if (localConfigFound)
{
ConfigObject = JObject.Parse(fs.ReadToEnd()).ToObject<EssentialsConfig>();
Debug.LogMessage(LogEventLevel.Information, "Successfully Loaded Local Config");
return true;
}
else
{
var parsedConfig = JObject.Parse(fs.ReadToEnd());
// Check if it's a v2 config (check for "version" node)
// this means it's already merged by the Portal API
// from the v2 config tool
var isV2Config = parsedConfig["versions"] != null;
if (isV2Config)
{
Debug.LogMessage(LogEventLevel.Information, "Config file is a v2 format, no merge necessary.");
ConfigObject = parsedConfig.ToObject<EssentialsConfig>();
Debug.LogMessage(LogEventLevel.Information, "Successfully Loaded v2 Config");
return true;
}
// Extract SystemUrl and TemplateUrl into final config output
ConfigObject = PortalConfigReader.MergeConfigs(parsedConfig).ToObject<EssentialsConfig>();
if (parsedConfig["system_url"] != null)
{
ConfigObject.SystemUrl = parsedConfig["system_url"].Value<string>();
}
if (parsedConfig["template_url"] != null)
{
ConfigObject.TemplateUrl = parsedConfig["template_url"].Value<string>();
}
}
Debug.LogMessage(LogEventLevel.Information, "Successfully Loaded Merged Config");
return true;
ConfigObject = JObject.Parse(fileContents).ToObject<EssentialsConfig>();
Debug.LogMessage(LogEventLevel.Information, "Successfully Loaded Local Config");
return ConfigObject != null;
}
else
{
var doubleObj = JObject.Parse(fs.ReadToEnd());
ConfigObject = PortalConfigReader.MergeConfigs(doubleObj).ToObject<EssentialsConfig>();
var parsedConfig = JObject.Parse(fileContents);
// Extract SystemUrl and TemplateUrl into final config output
// Check if it's a v2 config (check for "version" node)
// this means it's already merged by the Portal API
// from the v2 config tool
var isV2Config = parsedConfig["versions"] != null;
if (doubleObj["system_url"] != null)
if (isV2Config)
{
ConfigObject.SystemUrl = doubleObj["system_url"].Value<string>();
Debug.LogMessage(LogEventLevel.Information, "Config file is a v2 format, no merge necessary.");
ConfigObject = parsedConfig.ToObject<EssentialsConfig>();
Debug.LogMessage(LogEventLevel.Information, "Successfully Loaded v2 Config");
return ConfigObject != null;
}
if (doubleObj["template_url"] != null)
// Extract SystemUrl and TemplateUrl into final config output
ConfigObject = PortalConfigReader.MergeConfigs(parsedConfig).ToObject<EssentialsConfig>();
if (ConfigObject == null)
{
ConfigObject.TemplateUrl = doubleObj["template_url"].Value<string>();
Debug.LogMessage(LogEventLevel.Warning, "Config merge produced a null ConfigObject.");
return false;
}
if (parsedConfig["system_url"] != null)
{
ConfigObject.SystemUrl = parsedConfig["system_url"].Value<string>();
}
if (parsedConfig["template_url"] != null)
{
ConfigObject.TemplateUrl = parsedConfig["template_url"].Value<string>();
}
}

View file

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Crestron.SimplSharp;
using Crestron.SimplSharp.WebScripting;
using PepperDash.Core;
@ -26,10 +27,6 @@ public class EssentialsWebApi : EssentialsDevice
? string.Format("/app{0:00}/api", InitialParametersClass.ApplicationNumber)
: "/api";
private const int DebugTrace = 0;
private const int DebugInfo = 1;
private const int DebugVerbose = 2;
/// <summary>
/// Gets or sets the BasePath
/// </summary>
@ -230,7 +227,7 @@ public class EssentialsWebApi : EssentialsDevice
_server.Start();
GetPaths();
PrintPaths();
return;
}
@ -240,11 +237,11 @@ public class EssentialsWebApi : EssentialsDevice
_server.Start();
GetPaths();
PrintPaths();
}
/// <summary>
/// Print the available pahts
/// Print the available paths
/// </summary>
/// <example>
/// http(s)://{ipaddress}/cws/{basePath}
@ -253,7 +250,7 @@ public class EssentialsWebApi : EssentialsDevice
/// <summary>
/// GetPaths method
/// </summary>
public void GetPaths()
public void PrintPaths()
{
Debug.LogMessage(LogEventLevel.Information, this, new string('-', 50));
@ -267,7 +264,7 @@ public class EssentialsWebApi : EssentialsDevice
? $"https://{hostname}/VirtualControl/Rooms/{InitialParametersClass.RoomId}/cws{BasePath}"
: $"https://{currentIp}/cws{BasePath}";
Debug.LogMessage(LogEventLevel.Information, this, "Server:{path:l}", path);
Debug.LogMessage(LogEventLevel.Information, this, "Server: {path:l}", path);
var routeCollection = _server.GetRouteCollection();
if (routeCollection == null)

View file

@ -6,18 +6,23 @@ using PepperDash.Core.Web.RequestHandlers;
using Serilog.Events;
using System;
using System.Text;
using System.Threading.Tasks;
namespace PepperDash.Essentials.Core.Web.RequestHandlers;
/// <summary>
/// Represents a DebugSessionRequestHandler
/// </summary>
public class DebugSessionRequestHandler : WebApiBaseRequestHandler
{
private readonly DebugWebsocketSink _sink = new DebugWebsocketSink();
{
/// <summary>
/// Constructor
/// </summary>
public DebugSessionRequestHandler()
: base(true)
{
}
/// <summary>
/// Gets details for a debug session
/// </summary>
@ -41,21 +46,30 @@ public class DebugSessionRequestHandler : WebApiBaseRequestHandler
var port = 0;
if (!_sink.IsRunning)
if (!Debug.WebsocketSink.IsRunning)
{
Debug.LogMessage(LogEventLevel.Information, "Starting WS Server");
// Generate a random port within a specified range
port = new Random().Next(65435, 65535);
// Start the WS Server
_sink.StartServerAndSetPort(port);
Debug.WebsocketSink.StartServerAndSetPort(port);
Debug.SetWebSocketMinimumDebugLevel(Serilog.Events.LogEventLevel.Verbose);
}
var url = _sink.Url;
if (!Debug.WebsocketSink.IsRunning)
{
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.Write(JsonConvert.SerializeObject(new { error = "Failed to start WebSocket debug server. Check logs for details." }), false);
context.Response.End();
return;
}
var url = Debug.WebsocketSink.Url;
object data = new
{
url = _sink.Url
url = Debug.WebsocketSink.Url
};
Debug.LogMessage(LogEventLevel.Information, "Debug Session URL: {0}", url);
@ -82,7 +96,8 @@ public class DebugSessionRequestHandler : WebApiBaseRequestHandler
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
_sink.StopServer();
Task.Run(() => Debug.WebsocketSink.StopServer());
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";