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

@ -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";