refactor: devlist request handler, added static method for creating list

This commit is contained in:
jdevito
2023-02-01 11:50:26 -06:00
parent faaa2a354e
commit 4dc6d5b5ac
2 changed files with 26 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using PepperDash.Core;
namespace PepperDash.Essentials.Core.Web namespace PepperDash.Essentials.Core.Web
{ {
@@ -14,6 +15,17 @@ namespace PepperDash.Essentials.Core.Web
}; };
} }
public static object MapToDeviceListObject(IKeyed device)
{
return new
{
Key = device.Key,
Name = (device is IKeyName)
? (device as IKeyName).Name
: "---"
};
}
public static object MapJoinToObject(string key, JoinMapBaseAdvanced join) public static object MapJoinToObject(string key, JoinMapBaseAdvanced join)
{ {
var kp = new KeyValuePair<string, JoinMapBaseAdvanced>(key, join); var kp = new KeyValuePair<string, JoinMapBaseAdvanced>(key, join);
@@ -26,11 +38,11 @@ namespace PepperDash.Essentials.Core.Web
return new return new
{ {
DeviceKey = join.Key, DeviceKey = join.Key,
Joins = join.Value.Joins.Select(j => MapJoinDatacompleteToObject(j)) Joins = join.Value.Joins.Select(j => MapJoinDataCompleteToObject(j))
}; };
} }
public static object MapJoinDatacompleteToObject(KeyValuePair<string, JoinDataComplete> joinData) public static object MapJoinDataCompleteToObject(KeyValuePair<string, JoinDataComplete> joinData)
{ {
return new return new
{ {

View File

@@ -8,11 +8,6 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
public class DevListRequestHandler : WebApiBaseRequestHandler public class DevListRequestHandler : WebApiBaseRequestHandler
{ {
private const string Key = "DevListRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests
/// </summary> /// </summary>
@@ -42,24 +37,20 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
protected override void HandleGet(HttpCwsContext context) protected override void HandleGet(HttpCwsContext context)
{ {
var allDevices = DeviceManager.AllDevices; var allDevices = DeviceManager.AllDevices;
if (allDevices == null)
{
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Not Found";
context.Response.End();
return;
}
allDevices.Sort((a, b) => System.String.Compare(a.Key, b.Key, System.StringComparison.Ordinal)); allDevices.Sort((a, b) => System.String.Compare(a.Key, b.Key, System.StringComparison.Ordinal));
var devices = allDevices.Select(device => new var deviceList = allDevices.Select(d => EssentialsWebApiHelpers.MapToDeviceListObject(d)).ToList();
{
Key = device.Key,
Name = device is IKeyName ? (device as IKeyName).Name : "---"
}).Cast<object>().ToList(); var js = JsonConvert.SerializeObject(deviceList, Formatting.Indented);
var js = JsonConvert.SerializeObject(devices, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.None
});
//Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK"; context.Response.StatusDescription = "OK";