feat: added static methods for GetJoinMaps, added GetFeedbacksForDevice request handler

This commit is contained in:
jdevito
2023-02-01 11:15:13 -06:00
parent 15efed02a5
commit 0793a09095
6 changed files with 246 additions and 21 deletions

View File

@@ -198,6 +198,8 @@
<Compile Include="Crestron IO\Relay\ISwitchedOutput.cs" />
<Compile Include="Crestron IO\StatusSign\StatusSignController.cs" />
<Compile Include="Web\RequestHandlers\AppDebugRequestHandler.cs" />
<Compile Include="Web\RequestHandlers\GetFeedbacksForDeviceRequestHandler.cs" />
<Compile Include="Web\RequestHandlers\GetJoinMapHelpers.cs" />
<Compile Include="Web\RequestHandlers\GetTypesByFilterRequestHandler.cs" />
<Compile Include="Web\RequestHandlers\GetJoinMapForDeviceKeyRequestHandler.cs" />
<Compile Include="Web\RequestHandlers\DefaultRequestHandler.cs" />

View File

@@ -147,6 +147,11 @@ namespace PepperDash.Essentials.Core.Web
{
Name = "GetJoinMapsForDeviceKey",
RouteHandler = new GetJoinMapForDeviceKeyRequestHandler()
},
new HttpCwsRoute("feedbacks/{deviceKey}")
{
Name = "GetFeedbacksForDeviceKey",
RouteHandler = new GetFeedbacksForDeviceRequestHandler()
}
};

View File

@@ -0,0 +1,191 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class GetFeedbacksForDeviceRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "GetFeedbacksForDeviceRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary>
/// Handles CONNECT method requests
/// </summary>
/// <param name="context"></param>
protected override void HandleConnect(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
/// <summary>
/// Handles DELETE method requests
/// </summary>
/// <param name="context"></param>
protected override void HandleDelete(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
/// <summary>
/// Handles GET method requests
/// </summary>
/// <param name="context"></param>
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;
}
//var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented);
//Debug.Console(Verbose, "[{0}] routeData:\n{1}", Key.ToLower(), routeDataJson);
object deviceObj;
if (!routeData.Values.TryGetValue("deviceKey", out deviceObj))
{
Debug.Console(Verbose, "TryGetValue filter failed");
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var device = DeviceManager.GetDeviceForKey(deviceObj.ToString()) as IHasFeedback;
if (device == null)
{
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Not Found";
context.Response.End();
return;
}
var boolFeedback =
from feedback in device.Feedbacks.OfType<BoolFeedback>()
where !string.IsNullOrEmpty(feedback.Key)
select new
{
FeedbackKey = feedback.Key,
Value = feedback.BoolValue
};
var intFeedback =
from feedback in device.Feedbacks.OfType<IntFeedback>()
where !string.IsNullOrEmpty(feedback.Key)
select new
{
FeedbackKey = feedback.Key,
Value = feedback.IntValue
};
var stringFeedback =
from feedback in device.Feedbacks.OfType<StringFeedback>()
where !string.IsNullOrEmpty(feedback.Key)
select new
{
FeedbackKey = feedback.Key,
Value = feedback.StringValue ?? string.Empty
};
var respnse = new
{
BoolValues = boolFeedback,
IntValues = intFeedback,
SerialValues = stringFeedback
};
var final = JsonConvert.SerializeObject(respnse, Formatting.Indented);
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(final, false);
context.Response.End();
}
/// <summary>
/// Handles HEAD method requests
/// </summary>
/// <param name="context"></param>
protected override void HandleHead(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
/// <summary>
/// Handles OPTIONS method requests
/// </summary>
/// <param name="context"></param>
protected override void HandleOptions(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
/// <summary>
/// Handles PATCH method requests
/// </summary>
/// <param name="context"></param>
protected override void HandlePatch(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
/// <summary>
/// Handles POST method requests
/// </summary>
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
/// <summary>
/// Handles PUT method requests
/// </summary>
/// <param name="context"></param>
protected override void HandlePut(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
/// <summary>
/// Handles TRACE method requests
/// </summary>
/// <param name="context"></param>
protected override void HandleTrace(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
}
}

View File

@@ -1,4 +1,7 @@
using Crestron.SimplSharp.WebScripting;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
@@ -51,10 +54,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented);
Debug.Console(Verbose, "routeData:\n{0}", routeDataJson);
//Debug.Console(Verbose, "routeData:\n{0}", routeDataJson);
object bridgeObj;
if (!routeData.Values.TryGetValue("bridgeKey", out bridgeObj))
@@ -78,7 +79,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var joinMap = bridge.JoinMaps;
var joinMap = bridge.JoinMaps.Select(j => GetJoinMapHelpers.MapJoinToObject(j)).ToList();
if (joinMap == null)
{
context.Response.StatusCode = 400;
@@ -88,14 +89,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var js = JsonConvert.SerializeObject(joinMap, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.None
});
var js = JsonConvert.SerializeObject(joinMap, Formatting.Indented);
Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
context.Response.StatusCode = 200;
@@ -171,5 +165,7 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
context.Response.StatusDescription = "Not Implemented";
context.Response.End();
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
@@ -100,15 +101,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var joinMap = new Dictionary<string, JoinMapBaseAdvanced>
{
{
deviceObj.ToString(),
deviceJoinMap
}
};
var joinMap = GetJoinMapHelpers.MapJoinToObject(deviceObj.ToString(), deviceJoinMap);
var js = JsonConvert.SerializeObject(joinMap, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,

View File

@@ -0,0 +1,37 @@
using System.Collections.Generic;
using System.Linq;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class GetJoinMapHelpers
{
public static object MapJoinToObject(string key, JoinMapBaseAdvanced join)
{
var kp = new KeyValuePair<string, JoinMapBaseAdvanced>(key, join);
return MapJoinToObject(kp);
}
public static object MapJoinToObject(KeyValuePair<string, JoinMapBaseAdvanced> join)
{
return new
{
DeviceKey = join.Key,
Joins = join.Value.Joins.Select(j => MapJoinDatacompleteToObject(j))
};
}
public static object MapJoinDatacompleteToObject(KeyValuePair<string, JoinDataComplete> joinData)
{
return new
{
Signal = joinData.Key,
Description = joinData.Value.Metadata.Description,
JoinNumber = joinData.Value.JoinNumber,
JoinSpan = joinData.Value.JoinSpan,
JoinType = joinData.Value.Metadata.JoinType.ToString(),
JoinCapabilities = joinData.Value.Metadata.JoinCapabilities.ToString()
};
}
}
}