-
Notifications
You must be signed in to change notification settings - Fork 84
fix: add routes to get routing ports & all defined routes #1228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/PepperDash.Essentials.Core/Web/RequestHandlers/GetRoutesHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
src/PepperDash.Essentials.Core/Web/RequestHandlers/GetRoutingPortsHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.