mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-01-29 04:15:00 +00:00
104 lines
2.9 KiB
C#
104 lines
2.9 KiB
C#
using System;
|
|
using Crestron.SimplSharp.WebScripting;
|
|
using Newtonsoft.Json;
|
|
using PepperDash.Core;
|
|
using PepperDash.Core.Web.RequestHandlers;
|
|
using PepperDash.Essentials.Core.Web.Attributes;
|
|
using Serilog.Events;
|
|
|
|
namespace PepperDash.Essentials.Core.Web.RequestHandlers
|
|
{
|
|
[HttpPost]
|
|
[OpenApiOperation(
|
|
Summary = "DevJson",
|
|
Description = "Send a command to a specific device",
|
|
OperationId = "sendDeviceCommand")]
|
|
[OpenApiParameter("deviceKey", Description = "The key of the device to send the command to")]
|
|
[OpenApiRequestBody(Description = "Device command data")]
|
|
[OpenApiResponse(200, Description = "Command executed successfully")]
|
|
[OpenApiResponse(400, Description = "Bad Request")]
|
|
public class DevJsonRequestHandler : WebApiBaseRequestHandler
|
|
{
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// base(true) enables CORS support by default
|
|
/// </remarks>
|
|
public DevJsonRequestHandler()
|
|
: base(true)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles POST method requests
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
protected override void HandlePost(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;
|
|
}
|
|
|
|
if (context.Request.ContentLength < 0)
|
|
{
|
|
context.Response.StatusCode = 400;
|
|
context.Response.StatusDescription = "Bad Request: no body";
|
|
context.Response.End();
|
|
|
|
return;
|
|
}
|
|
|
|
var data = context.Request.GetRequestBody();
|
|
|
|
if (string.IsNullOrEmpty(data))
|
|
{
|
|
context.Response.StatusCode = 400;
|
|
context.Response.StatusDescription = "Bad Request: no body";
|
|
context.Response.End();
|
|
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
var daw = new DeviceActionWrapper { DeviceKey = (string) deviceKey};
|
|
|
|
JsonConvert.PopulateObject(data, daw);
|
|
|
|
Debug.LogMessage(LogEventLevel.Verbose, "Device Action Wrapper: {@wrapper}", null, daw);
|
|
|
|
DeviceJsonApi.DoDeviceAction(daw);
|
|
|
|
context.Response.StatusCode = 200;
|
|
context.Response.StatusDescription = "OK";
|
|
context.Response.End();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogMessage(ex, "Error handling device command: {Exception}");
|
|
|
|
context.Response.StatusCode = 400;
|
|
context.Response.StatusDescription = "Bad Request";
|
|
context.Response.Write(JsonConvert.SerializeObject(new { error = ex.Message }), false);
|
|
context.Response.End();
|
|
}
|
|
}
|
|
}
|
|
} |