using System; using System.Collections.Generic; using System.Linq; using Crestron.SimplSharp; using Crestron.SimplSharp.WebScripting; using PepperDash.Core; using PepperDash.Core.Web; using PepperDash.Essentials.Core.Web.RequestHandlers; namespace PepperDash.Essentials.Core.Web { public class EssentialsWebApi : EssentialsDevice { private readonly WebApiServer _server; /// /// http(s)://{ipaddress}/cws/{basePath} /// http(s)://{ipaddress}/VirtualControl/Rooms/{roomId}/cws/{basePath} /// private readonly string _defaultBasePath = CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance ? string.Format("/app{0:00}/api", InitialParametersClass.ApplicationNumber) : "/api"; private const int DebugTrace = 0; private const int DebugInfo = 1; private const int DebugVerbose = 2; /// /// CWS base path /// public string BasePath { get; private set; } /// /// Tracks if CWS is registered /// public bool IsRegistered { get { return _server.IsRegistered; } } /// /// Constructor /// /// /// public EssentialsWebApi(string key, string name) : this(key, name, null) { } /// /// Constructor /// /// /// /// public EssentialsWebApi(string key, string name, EssentialsWebApiPropertiesConfig config) : base(key, name) { Key = key; if (config == null) BasePath = _defaultBasePath; else BasePath = string.IsNullOrEmpty(config.BasePath) ? _defaultBasePath : config.BasePath; _server = new WebApiServer(Key, Name, BasePath); SetupRoutes(); } private void SetupRoutes() { var routes = new List { new HttpCwsRoute("versions") { Name = "ReportVersions", RouteHandler = new ReportVersionsRequestHandler() }, new HttpCwsRoute("appdebug") { Name = "AppDebug", RouteHandler = new AppDebugRequestHandler() }, new HttpCwsRoute("devices") { Name = "DevList", RouteHandler = new DevListRequestHandler() }, new HttpCwsRoute("deviceCommands") { Name = "DevJson", RouteHandler = new DevJsonRequestHandler() }, new HttpCwsRoute("deviceProperties/{deviceKey}") { Name = "DevProps", RouteHandler = new DevPropsRequestHandler() }, new HttpCwsRoute("deviceMethods/{deviceKey}") { Name = "DevMethods", RouteHandler = new DevMethodsRequestHandler() }, new HttpCwsRoute("deviceFeedbacks/{deviceKey}") { Name = "GetFeedbacksForDeviceKey", RouteHandler = new GetFeedbacksForDeviceRequestHandler() }, new HttpCwsRoute("deviceStreamDebug") { Name = "SetDeviceStreamDebug", RouteHandler = new SetDeviceStreamDebugRequestHandler() }, new HttpCwsRoute("disableAllStreamDebug") { Name = "DisableAllStreamDebug", RouteHandler = new DisableAllStreamDebugRequestHandler() }, new HttpCwsRoute("config") { Name = "ShowConfig", RouteHandler = new ShowConfigRequestHandler() }, new HttpCwsRoute("types") { Name = "GetTypes", RouteHandler = new GetTypesRequestHandler() }, new HttpCwsRoute("types/{filter}") { Name = "GetTypesByFilter", RouteHandler = new GetTypesByFilterRequestHandler() }, new HttpCwsRoute("joinMap/{bridgeKey}") { Name = "GetJoinMapsForBridgeKey", RouteHandler = new GetJoinMapForBridgeKeyRequestHandler() }, new HttpCwsRoute("joinMap/{bridgeKey}/{deviceKey}") { Name = "GetJoinMapsForDeviceKey", RouteHandler = new GetJoinMapForDeviceKeyRequestHandler() }, new HttpCwsRoute("debugSession") { Name = "DebugSession", RouteHandler = new DebugSessionRequestHandler() }, new HttpCwsRoute("doNotLoadConfigOnNextBoot") { Name = "DoNotLoadConfigOnNextBoot", RouteHandler = new DoNotLoadConfigOnNextBootRequestHandler() }, new HttpCwsRoute("restartProgram") { Name = "Restart Program", RouteHandler = new RestartProgramRequestHandler() }, new HttpCwsRoute("loadConfig") { Name = "Load Config", RouteHandler = new LoadConfigRequestHandler() } }; AddRoute(routes); } /// /// Add a single route to the API. MUST be done during the activation phase /// /// public void AddRoute(HttpCwsRoute route) { _server.AddRoute(route); } /// /// Add a collection of routes to the API. MUST be done during the activation phase /// /// public void AddRoute(List routes) { foreach (var route in routes) { AddRoute(route); } } /// /// Initializes the CWS class /// public override void Initialize() { // If running on an appliance if (CrestronEnvironment.DevicePlatform == eDevicePlatform.Appliance) { /* WEBSERVER [ON | OFF | TIMEOUT | MAXSESSIONSPERUSER ] */ var response = string.Empty; CrestronConsole.SendControlSystemCommand("webserver", ref response); if (response.Contains("OFF")) return; var is4Series = eCrestronSeries.Series4 == (Global.ProcessorSeries & eCrestronSeries.Series4); Debug.Console(DebugTrace, Debug.ErrorLogLevel.Notice, "Starting Essentials Web API on {0} Appliance", is4Series ? "4-series" : "3-series"); _server.Start(); GetPaths(); return; } // Automatically start CWS when running on a server (Linux OS, Virtual Control) Debug.Console(DebugTrace, Debug.ErrorLogLevel.Notice, "Starting Essentials Web API on Virtual Control Server"); _server.Start(); GetPaths(); } /// /// Print the available pahts /// /// /// http(s)://{ipaddress}/cws/{basePath} /// http(s)://{ipaddress}/VirtualControl/Rooms/{roomId}/cws/{basePath} /// public void GetPaths() { Debug.Console(DebugTrace, this, "{0}", new String('-', 50)); var currentIp = CrestronEthernetHelper.GetEthernetParameter( CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS, 0); var hostname = CrestronEthernetHelper.GetEthernetParameter( CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME, 0); var path = CrestronEnvironment.DevicePlatform == eDevicePlatform.Server ? string.Format("http(s)://{0}/VirtualControl/Rooms/{1}/cws{2}", hostname, InitialParametersClass.RoomId, BasePath) : string.Format("http(s)://{0}/cws{1}", currentIp, BasePath); Debug.Console(DebugTrace, this, "Server:{0}", path); var routeCollection = _server.GetRouteCollection(); if (routeCollection == null) { Debug.Console(DebugTrace, this, "Server route collection is null"); return; } Debug.Console(DebugTrace, this, "Configured Routes:"); foreach (var route in routeCollection) { Debug.Console(DebugTrace, this, "{0}: {1}/{2}", route.Name, path, route.Url); } Debug.Console(DebugTrace, this, "{0}", new String('-', 50)); } } }