feat: worked on adding get/post for appdebug, devprops, devjson, disableallstreamdebug, setdevicestreamdebug

This commit is contained in:
jdevito
2023-01-30 22:08:13 -06:00
parent 705e750419
commit dd2aca9aa4
9 changed files with 165 additions and 25 deletions

View File

@@ -272,7 +272,7 @@ namespace PepperDash.Essentials
{
Debug.Console(2, this,
@"Attempting to run action:
DeviceKey: {0}
Key: {0}
MethodName: {1}
Params: {2}"
, a.DeviceKey, a.MethodName, a.Params);

View File

@@ -93,21 +93,31 @@ namespace PepperDash.Essentials.Core.Web
Name = "DevList",
RouteHandler = new DevListRequestHandler()
},
new HttpCwsRoute("devprops/{key}")
new HttpCwsRoute("devprops")
{
Name = "DevProps",
RouteHandler = new DevPropsRequestHandler()
},
//new HttpCwsRoute("devprops/{key}")
//{
// Name = "DevProps",
// RouteHandler = new DevPropsRequestHandler()
//},
new HttpCwsRoute("devjson")
{
Name = "DevJson",
RouteHandler = new DevJsonRequestHandler()
},
new HttpCwsRoute("setdevicestreamdebug/{deviceKey}/{state}")
new HttpCwsRoute("setdevicestreamdebug")
{
Name = "SetDeviceStreamDebug",
RouteHandler = new SetDeviceStreamDebugRequestHandler()
Name = "SetDeviceStreamDebug",
RouteHandler = new SetDeviceStreamDebugRequestHandler()
},
//new HttpCwsRoute("setdevicestreamdebug/{deviceKey}/{state}")
//{
// Name = "SetDeviceStreamDebug",
// RouteHandler = new SetDeviceStreamDebugRequestHandler()
//},
new HttpCwsRoute("disableallstreamdebug")
{
Name = "DisableAllStreamDebug",

View File

@@ -1,10 +1,19 @@
using Crestron.SimplSharp.WebScripting;
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
{
public class AppDebugRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "AppDebugRequestHandler";
private const uint Trace = 0;
private const uint Info = 0;
private const uint Verbose = 0;
/// <summary>
/// Handles CONNECT method requests
/// </summary>
@@ -33,8 +42,14 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandleGet(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
var o = new AppDebug();
o.Level = Debug.Level;
var body = JsonConvert.SerializeObject(o, Formatting.Indented);
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.Write(body, false);
context.Response.End();
}
@@ -77,8 +92,25 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
if (context.Request.ContentLength < 0) 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(Info, "[{0}] Request data:\n{1}", Key.ToLower(), data);
var o = new AppDebug();
var requestBody = JsonConvert.DeserializeAnonymousType(data, o);
Debug.SetDebugLevel(requestBody.Level);
o.Level = Debug.Level;
var responseBody = JsonConvert.SerializeObject(o, Formatting.Indented);
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.Write(responseBody, false);
context.Response.End();
}
@@ -104,4 +136,10 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
context.Response.End();
}
}
public class AppDebug
{
[JsonProperty("level", NullValueHandling = NullValueHandling.Ignore)]
public int Level { get; set; }
}
}

View File

@@ -1,10 +1,18 @@
using Crestron.SimplSharp.WebScripting;
using System;
using System.Text;
using Crestron.SimplSharp.WebScripting;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class DevJsonRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "DevJsonRequestHandler";
private const uint Trace = 0;
private const uint Info = 0;
private const uint Verbose = 0;
/// <summary>
/// Handles CONNECT method requests
/// </summary>
@@ -77,8 +85,17 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
if (context.Request.ContentLength < 0) 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(Info, "[{0}] Request data:\n{1}", Key.ToLower(), data);
DeviceJsonApi.DoDeviceActionWithJson(data);
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.End();
}

View File

@@ -1,10 +1,8 @@
using System.Diagnostics;
using System.Linq;
using System.Linq;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
using Debug = PepperDash.Core.Debug;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{

View File

@@ -1,10 +1,19 @@
using Crestron.SimplSharp.WebScripting;
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
{
public class DevPropsRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "DevPropsRequestHandler";
private const uint Trace = 0;
private const uint Info = 0;
private const uint Verbose = 0;
/// <summary>
/// Handles CONNECT method requests
/// </summary>
@@ -77,8 +86,34 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
if (context.Request.ContentLength < 0) 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(Info, "[{0}] Request data:\n{1}", Key.ToLower(), data);
var o = new DeviceActionWrapper();
var body = JsonConvert.DeserializeAnonymousType(data, o);
if (string.IsNullOrEmpty(body.DeviceKey))
{
Debug.Console(Info, "[{0}] Request body is null or empty", Key.ToLower());
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
var deviceProps = DeviceJsonApi.GetProperties(body.DeviceKey);
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = Encoding.UTF8;
context.Response.Write(deviceProps, false);
context.Response.End();
}

View File

@@ -77,8 +77,10 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
DeviceManager.DisableAllDeviceStreamDebugging();
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.End();
}

View File

@@ -1,7 +1,6 @@
using System.Linq;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers

View File

@@ -1,10 +1,20 @@
using Crestron.SimplSharp.WebScripting;
using System;
using System.Text;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers;
namespace PepperDash.Essentials.Core.Web.RequestHandlers
{
public class SetDeviceStreamDebugRequestHandler : WebApiBaseRequestHandler
{
private const string Key = "SetDeviceStreamDebugRequestHandler";
private const uint Trace = 0;
private const uint Info = 0;
private const uint Verbose = 0;
/// <summary>
/// Handles CONNECT method requests
/// </summary>
@@ -77,8 +87,39 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context)
{
context.Response.StatusCode = 501;
context.Response.StatusDescription = "Not Implemented";
if (context.Request.ContentLength < 0) 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(Info, "[{0}] Request data:\n{1}", Key.ToLower(), data);
var o = new
{
DeviceKey = "",
Type = "",
Timeout = 15
};
var body = JsonConvert.DeserializeAnonymousType(data, o);
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"))
{
context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request";
context.Response.End();
return;
}
DeviceManager.SetDeviceStreamDebugging(string.Format("setdevicestreamdebug {0} {1} {2}", body.DeviceKey, body.Type, body.Timeout));
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.End();
}