Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions src/PepperDash.Essentials.Core/Web/EssentialsWebApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,17 @@ private void SetupRoutes()
Name = "GetJoinMapsForDeviceKey",
RouteHandler = new GetJoinMapForDeviceKeyRequestHandler()
},
new HttpCwsRoute("debugSession")
{
Name = "DebugSession",
RouteHandler = new DebugSessionRequestHandler()
},
new HttpCwsRoute("doNotLoadConfigOnNextBoot")
{
Name = "DoNotLoadConfigOnNextBoot",
RouteHandler = new DoNotLoadConfigOnNextBootRequestHandler()
},
new HttpCwsRoute("restartProgram")
new HttpCwsRoute("debugSession")
{
Name = "DebugSession",
RouteHandler = new DebugSessionRequestHandler()
},
new HttpCwsRoute("doNotLoadConfigOnNextBoot")
{
Name = "DoNotLoadConfigOnNextBoot",
RouteHandler = new DoNotLoadConfigOnNextBootRequestHandler()
},
new HttpCwsRoute("restartProgram")
{
Name = "Restart Program",
RouteHandler = new RestartProgramRequestHandler()
Expand All @@ -164,12 +164,16 @@ private void SetupRoutes()
Name = "Load Config",
RouteHandler = new LoadConfigRequestHandler()
},
new HttpCwsRoute("getTielines")
new HttpCwsRoute("tielines")
{
Name = "Get TieLines",
RouteHandler = new GetTieLinesRequestHandler()
}

},
new HttpCwsRoute("device/{deviceKey}/routingPorts")
{
Name = "Get Routing Ports for a device",
RouteHandler = new GetRoutingPortsHandler()
},
};

AddRoute(routes);
Expand All @@ -196,13 +200,18 @@ public void AddRoute(List<HttpCwsRoute> routes)
}
}

/// <summary>
/// Initializes the CWS class
/// </summary>
public override void Initialize()
/// <summary>
/// Initializes the CWS class
/// </summary>
public override void Initialize()
{
// If running on an appliance
if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance)
AddRoute(new HttpCwsRoute("apiPaths") {
Name = "GetPaths",
RouteHandler = new GetRoutesHandler(_server.GetRouteCollection(), BasePath)
});

// If running on an appliance
if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance)
{
/*
WEBSERVER [ON | OFF | TIMEOUT <VALUE IN SECONDS> | MAXSESSIONSPERUSER <Number of sessions>]
Expand Down Expand Up @@ -247,8 +256,8 @@ public void GetPaths()
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}";
? $"https://{hostname}/VirtualControl/Rooms/{InitialParametersClass.RoomId}/cws{BasePath}"
: $"https://{currentIp}/cws{BasePath}";

Debug.LogMessage(LogEventLevel.Information, this, "Server:{path:l}", path);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Crestron.SimplSharp;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core.Web.RequestHandlers;

namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class GetRoutesHandler:WebApiBaseRequestHandler
{
private HttpCwsRouteCollection routeCollection;
private string basePath;

public GetRoutesHandler(HttpCwsRouteCollection routeCollection, string basePath) {
this.routeCollection = routeCollection;
this.basePath = basePath;
}

protected override void HandleGet(HttpCwsContext context)
{
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
? $"https://{hostname}/VirtualControl/Rooms/{InitialParametersClass.RoomId}/cws{basePath}"
: $"https://{currentIp}/cws{basePath}";

var response = JsonConvert.SerializeObject(new RoutesResponseObject()
{
Url = path,
Routes = routeCollection
});

context.Response.StatusCode = 200;
context.Response.ContentType = "application/json";
context.Response.Headers.Add("Content-Type", "application/json");
context.Response.Write(response, false);
context.Response.End();
}
}

public class RoutesResponseObject
{
[JsonProperty("url")]
public string Url { set; get; }

[JsonProperty("routes")]
public HttpCwsRouteCollection Routes { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core.Web.RequestHandlers;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class GetRoutingPortsHandler : WebApiBaseRequestHandler
{
public GetRoutingPortsHandler() : base(true) { }

protected override void HandleGet(HttpCwsContext context)
{
var routeData = context.Request.RouteData;

if (routeData == null)
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}

if(!routeData.Values.TryGetValue("deviceKey", out var deviceKey))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}

var device = DeviceManager.GetDeviceForKey(deviceKey.ToString());

if (device == null)
{
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Device Not Found";
context.Response.End();
return;
}

var inputPorts = (device as IRoutingInputs)?.InputPorts;
var outputPorts = (device as IRoutingOutputs)?.OutputPorts;

var response = JsonConvert.SerializeObject( new ReturnValue
{
InputPorts = inputPorts?.Select(p => p.Key).ToList(),
OutputPorts = outputPorts?.Select(p => p.Key).ToList()
});

context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(response, false);
context.Response.End();

}
}

internal class ReturnValue {
[JsonProperty("inputPorts", NullValueHandling = NullValueHandling.Ignore)]
public List<string> InputPorts { get; set; }

[JsonProperty("outputPorts", NullValueHandling = NullValueHandling.Ignore)]
public List<string> OutputPorts { get; set; }
}
}