feat: get it working

This commit is contained in:
Andrew Welker
2024-05-14 10:23:01 -05:00
parent 888f1b3888
commit 4bf026601f
12 changed files with 216 additions and 92 deletions

View File

@@ -163,6 +163,11 @@ namespace PepperDash.Essentials.Core.Web
{
Name = "Load Config",
RouteHandler = new LoadConfigRequestHandler()
},
new HttpCwsRoute("getTielines")
{
Name = "Get TieLines",
RouteHandler = new GetTieLinesRequestHandler()
}
};
@@ -233,32 +238,32 @@ namespace PepperDash.Essentials.Core.Web
/// </example>
public void GetPaths()
{
Debug.LogMessage(LogEventLevel.Verbose, this, "{0}", new String('-', 50));
Debug.LogMessage(LogEventLevel.Information, this, new string('-', 50));
var currentIp = CrestronEthernetHelper.GetEthernetParameter(
CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, 0);
var hostname = CrestronEthernetHelper.GetEthernetParameter(
CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME, 0);
var path = CrestronEnvironment.DevicePlatform == eDevicePlatform.Server
? $"http(s)://{hostname}/VirtualControl/Rooms/{InitialParametersClass.RoomId}/cws{BasePath}"
: $"http(s)://{currentIp}/cws{BasePath}";
var path = CrestronEnvironment.DevicePlatform == eDevicePlatform.Server
? string.Format("http(s)://{0}/VirtualControl/Rooms/{1}/cws{2}", hostname, InitialParametersClass.RoomId, BasePath)
: string.Format("http(s)://{0}/cws{1}", currentIp, BasePath);
Debug.LogMessage(LogEventLevel.Verbose, this, "Server:{0}", path);
Debug.LogMessage(LogEventLevel.Information, this, "Server:{path:l}", path);
var routeCollection = _server.GetRouteCollection();
if (routeCollection == null)
{
Debug.LogMessage(LogEventLevel.Verbose, this, "Server route collection is null");
Debug.LogMessage(LogEventLevel.Information, this, "Server route collection is null");
return;
}
Debug.LogMessage(LogEventLevel.Verbose, this, "Configured Routes:");
Debug.LogMessage(LogEventLevel.Information, this, "Configured Routes:");
foreach (var route in routeCollection)
{
Debug.LogMessage(LogEventLevel.Verbose, this, "{0}: {1}/{2}", route.Name, path, route.Url);
Debug.LogMessage(LogEventLevel.Information, this, "{routeName:l}: {routePath:l}/{routeUrl:l}", route.Name, path, route.Url);
}
Debug.LogMessage(LogEventLevel.Verbose, this, "{0}", new String('-', 50));
Debug.LogMessage(LogEventLevel.Information, this, new string('-', 50));
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
using Serilog.Events;
@@ -28,7 +29,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
if (context.Request.ContentLength < 0)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.StatusDescription = "Bad Request: no body";
context.Response.End();
return;
@@ -38,7 +39,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
if (string.IsNullOrEmpty(data))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.StatusDescription = "Bad Request: no body";
context.Response.End();
return;
@@ -46,6 +47,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
try
{
var daw = JsonConvert.DeserializeObject<DeviceActionWrapper>(data);
DeviceJsonApi.DoDeviceActionWithJson(data);
context.Response.StatusCode = 200;
@@ -54,12 +56,11 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
}
catch (Exception ex)
{
Debug.LogMessage(LogEventLevel.Error, "Exception Message: {0}", ex.Message);
Debug.LogMessage(LogEventLevel.Verbose, "Exception Stack Trace: {0}", ex.StackTrace);
if(ex.InnerException != null) Debug.LogMessage(LogEventLevel.Error, "Exception Inner: {0}", ex.InnerException);
Debug.LogMessage(ex, "Error handling device command: {Exception}");
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.Write(JsonConvert.SerializeObject(new { error = ex.Message }), false);
context.Response.End();
}
}

View File

@@ -1,6 +1,7 @@
using System.Text;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
@@ -25,6 +26,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
protected override void HandleGet(HttpCwsContext context)
{
var routeData = context.Request.RouteData;
Debug.LogMessage(Serilog.Events.LogEventLevel.Debug, "Getting DevMethods: {@routeData}", routeData);
if (routeData == null)
{
context.Response.StatusCode = 400;

View File

@@ -0,0 +1,34 @@
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core.Web.RequestHandlers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class GetTieLinesRequestHandler:WebApiBaseRequestHandler
{
public GetTieLinesRequestHandler() : base(true) { }
protected override void HandleGet(HttpCwsContext context)
{
var tieLineString = JsonConvert.SerializeObject(TieLineCollection.Default.Select((tl) => new {
sourceKey = tl.SourcePort.ParentDevice.Key,
sourcePort = tl.SourcePort.Key,
destinationKey = tl.DestinationPort.ParentDevice.Key,
destinationPort = tl.DestinationPort.Key
}));
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(tieLineString, false);
context.Response.End();
}
}
}