fix: resolved issues with setDeviceStreamDebug; refactor: added static method GetRequestBody

This commit is contained in:
jdevito
2023-02-01 13:22:12 -06:00
parent a3346d5ef4
commit e1d7374f1e
6 changed files with 129 additions and 35 deletions

View File

@@ -1,11 +1,23 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crestron.SimplSharp.WebScripting;
using PepperDash.Core;
namespace PepperDash.Essentials.Core.Web
{
public class EssentialsWebApiHelpers
{
public static string GetRequestBody(HttpCwsRequest request)
{
var bytes = new Byte[request.ContentLength];
request.InputStream.Read(bytes, 0, request.ContentLength);
return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
public static object MapToAssemblyObject(LoadedAssembly assembly)
{
return new

View File

@@ -95,9 +95,15 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);
var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
var data = EssentialsWebApiHelpers.GetRequestBody(context.Request);
if (string.IsNullOrEmpty(data))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var appDebug = new AppDebug();
var requestBody = JsonConvert.DeserializeAnonymousType(data, appDebug);

View File

@@ -88,12 +88,16 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);
var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
//Debug.Console(0, "Request data:\n{0}", data);
var data = EssentialsWebApiHelpers.GetRequestBody(context.Request);
if (string.IsNullOrEmpty(data))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
try
{
DeviceJsonApi.DoDeviceActionWithJson(data);

View File

@@ -89,9 +89,15 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);
var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
var data = EssentialsWebApiHelpers.GetRequestBody(context.Request);
if (string.IsNullOrEmpty(data))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var o = new DeviceActionWrapper();
var body = JsonConvert.DeserializeAnonymousType(data, o);

View File

@@ -93,19 +93,20 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
Value = feedback.StringValue ?? string.Empty
};
var respnse = new
var responseObj = new
{
BoolValues = boolFeedback,
IntValues = intFeedback,
SerialValues = stringFeedback
};
var final = JsonConvert.SerializeObject(respnse, Formatting.Indented);
var js = JsonConvert.SerializeObject(responseObj, 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.Write(js, false);
context.Response.End();
}

View File

@@ -1,7 +1,7 @@
using System;
using System.Text;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
@@ -89,24 +89,28 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
var bytes = new Byte[context.Request.ContentLength];
context.Request.InputStream.Read(bytes, 0, context.Request.ContentLength);
var data = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
var o = new
var data = EssentialsWebApiHelpers.GetRequestBody(context.Request);
if (data == null)
{
DeviceKey = "",
Type = "",
Timeout = 15
};
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
var body = JsonConvert.DeserializeAnonymousType(data, o);
return;
}
if (string.IsNullOrEmpty(body.DeviceKey) || string.IsNullOrEmpty(body.Type)
|| !body.Type.ToLower().Contains("off")
|| !body.Type.ToLower().Contains("tx")
|| !body.Type.ToLower().Contains("rx")
|| !body.Type.ToLower().Contains("both"))
var config = new SetDeviceStreamDebugConfig();
var body = JsonConvert.DeserializeAnonymousType(data, config);
if (body == null)
{
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
return;
}
if (string.IsNullOrEmpty(body.DeviceKey) || string.IsNullOrEmpty(body.Setting))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
@@ -115,11 +119,52 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return;
}
DeviceManager.SetDeviceStreamDebugging(string.Format("setdevicestreamdebug {0} {1} {2}", body.DeviceKey, body.Type, body.Timeout));
var device = DeviceManager.GetDeviceForKey(body.DeviceKey) as IStreamDebugging;
if (device == null)
{
context.Response.StatusCode = 404;
context.Response.StatusDescription = "Not Found";
context.Response.End();
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.End();
return;
}
eStreamDebuggingSetting debugSetting;
try
{
debugSetting = (eStreamDebuggingSetting) Enum.Parse(typeof (eStreamDebuggingSetting), body.Setting, true);
}
catch (Exception ex)
{
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
return;
}
try
{
var mins = Convert.ToUInt32(body.Timeout);
if (mins > 0)
{
device.StreamDebugging.SetDebuggingWithSpecificTimeout(debugSetting, mins);
}
else
{
device.StreamDebugging.SetDebuggingWithDefaultTimeout(debugSetting);
}
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.End();
}
catch (Exception ex)
{
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
}
}
/// <summary>
@@ -144,4 +189,24 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
context.Response.End();
}
}
public class SetDeviceStreamDebugConfig
{
[JsonProperty("deviceKey", NullValueHandling = NullValueHandling.Include)]
public string DeviceKey { get; set; }
[JsonProperty("setting", NullValueHandling = NullValueHandling.Include)]
public string Setting { get; set; }
[JsonProperty("timeout")]
public int Timeout { get; set; }
public SetDeviceStreamDebugConfig()
{
DeviceKey = null;
Setting = null;
Timeout = 15;
}
}
}