using System.Linq; using Crestron.SimplSharp.WebScripting; using Newtonsoft.Json; using PepperDash.Core.Web.RequestHandlers; namespace PepperDash.Essentials.Core.Web.RequestHandlers { /// /// Represents a GetFeedbacksForDeviceRequestHandler /// public class GetFeedbacksForDeviceRequestHandler : WebApiBaseRequestHandler { /// /// Constructor /// /// /// base(true) enables CORS support by default /// public GetFeedbacksForDeviceRequestHandler() : base(true) { } /// /// Handles GET method requests /// /// 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; } object deviceObj; if (!routeData.Values.TryGetValue("deviceKey", out deviceObj)) { 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() where !string.IsNullOrEmpty(feedback.Key) select new { FeedbackKey = feedback.Key, Value = feedback.BoolValue }; var intFeedback = from feedback in device.Feedbacks.OfType() where !string.IsNullOrEmpty(feedback.Key) select new { FeedbackKey = feedback.Key, Value = feedback.IntValue }; var stringFeedback = from feedback in device.Feedbacks.OfType() where !string.IsNullOrEmpty(feedback.Key) select new { FeedbackKey = feedback.Key, Value = feedback.StringValue ?? string.Empty }; var responseObj = new { BoolValues = boolFeedback, IntValues = intFeedback, SerialValues = stringFeedback }; 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(js, false); context.Response.End(); } } }