コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CwsConsoleWriter"/> class.
        /// </summary>
        /// <param name="cwsServer">The <see cref="HttpCwsServer"/> to register with.</param>
        /// <param name="routePath">The name of the CWS route the console should be created at, such as: "/console/program01/".</param>
        /// <param name="secureWebsocket"><see langword="true"/> to indicate the websocket server should use a secure TCP server; otherwise <see langword="false"/>.</param>
        /// <param name="websocketPort">The port the websocket server should listen on.</param>
        /// <param name="authenticateRoute"><see langword="true"/> to authenticate the route. Only valid on 4-Series processors.</param>
        public CwsConsoleWriter(HttpCwsServer cwsServer, string routePath, bool secureWebsocket, int websocketPort, bool authenticateRoute)
        {
            if (cwsServer == null)
            {
                throw new ArgumentNullException("cwsServer");
            }

            this.server = cwsServer;

            try
            {
                if (CrestronEnvironment.ProgramCompatibility == eCrestronSeries.Series3 || CrestronEnvironment.ProgramCompatibility == eCrestronSeries.Unspecified)
                {
                    consoleRoute = new HttpCwsRoute(routePath);
                }
                else
                {
                    consoleRoute = new HttpCwsRoute(routePath, authenticateRoute);
                }
            }
            catch
            {
                consoleRoute = new HttpCwsRoute(routePath);
            }

            consoleRouteHandler       = new ConsoleRoute(websocketPort, secureWebsocket);
            consoleRoute.RouteHandler = consoleRouteHandler;
            cwsServer.AddRoute(this.consoleRoute);
        }
コード例 #2
0
        public override void InitializeSystem()
        {
            try
            {
                _api = new HttpCwsServer("/api");

                var hello = new HttpCwsRoute("hello/");
                hello.RouteHandler = new HelloRequest();
                _api.AddRoute(hello);

                var helloName = new HttpCwsRoute("hello/{NAME}");
                helloName.RouteHandler = new HelloRequest();
                _api.AddRoute(helloName);

                var roomHandler = new RoomRequest();

                var room = new HttpCwsRoute("room");
                room.RouteHandler = roomHandler;
                _api.AddRoute(room);

                var roomProp = new HttpCwsRoute("room/{PROPERTY}");
                roomProp.RouteHandler = roomHandler;
                _api.AddRoute(roomProp);

                _api.Register();
            }
            catch (Exception e)
            {
                ErrorLog.Error("Error in InitializeSystem: {0}", e.Message);
            }
        }
コード例 #3
0
        /// <summary>
        /// Add a handler to a route
        /// </summary>
        /// <param name="handler">The request handler to deal with incoming routes</param>
        /// <param name="url">The URL pattern for the route.</param>
        /// <remarks>
        /// URL pattern cannot start with a '/' or '~' character and it cannot contain a '?' character.
        ///             It should not also start with the virtual folder the server is registered for,
        ///             i.e. {device}/Presets/{id}/Recall is correct while /API/{device}/Presets/{id}/Recall is not in both ways.
        ///
        /// </remarks>
        public void RegisterHandler(BaseRequestHandler handler, string url)
        {
            var route = new HttpCwsRoute(url)
            {
                RouteHandler = handler
            };

#if DEBUG
            CrestronConsole.PrintLine("Added route for {0}, url: {1}", handler.Name, route.Url);
#endif

            _server.AddRoute(route);
        }
コード例 #4
0
        /// <summary>
        /// Disposes of resources.
        /// </summary>
        public void Dispose()
        {
            if (consoleRoute != null)
            {
                if (server != null)
                {
                    server.RemoveRoute(consoleRoute);
                }

                consoleRoute = null;
            }

            if (consoleRouteHandler != null)
            {
                consoleRouteHandler.Dispose();
                consoleRouteHandler = null;
            }

            server = null;
        }
コード例 #5
0
        public RestfulRelayServer(CrestronCollection <Relay> relay_collection, string hostname)
        {
            try
            {
                _numberOfRelays = relay_collection.Count;
                _relays         = relay_collection;
                _root           = "http://" + hostname;
                notifier        = new HttpClient();

                // To ensure that the template.json file appears in the control system's application directory,
                // set "Build Action" to "Content" and "Copy to Output Directory" to "Copy always" in the file's Properties menu
                _cjTemplate = File.ReadToEnd(Directory.GetApplicationDirectory() + "\\template.json", Encoding.UTF8);

                // set up subscription lists for the Relay collection and for each Relay
                sublists = new List <Dictionary <string, Subscription> >(NumberOfRelays + 1); // Both the collection and each relay gets its own subscription dictionary
                for (int i = 0; i < NumberOfRelays + 1; i++)
                {
                    sublists.Add(new Dictionary <string, Subscription>()); // client will use a GuID to locate the subscription resource if it wants to DELETE it.
                }

                // start up the server
                server = new HttpCwsServer("/api");

                /*
                 * URL Routing table:
                 *  /relays
                 *  /relays/web-hooks
                 *  /relays/web-hooks/{subid}
                 *  /relays/{id}
                 *  /relays/{id}/web-hooks
                 *  /relays/{id}/web-hooks/{subid}
                 */

                // subscribe to events
                server.ReceivedRequestEvent += new HttpCwsRequestEventHandler(server_ReceivedRequestEvent);
                server.HttpRequestHandler    = new DefaultHandler(this); // This is the default handler. It will process unrouted requests.
                foreach (Relay r in relay_collection)
                {
                    r.StateChange += new RelayEventHandler(r_StateChange);
                }

                // Add each route to server's routing table: In this example, each route is
                // associated with its own RouteHandler class, which will process the HTTP request and write its response.
                // Other designs might use a single route handler that interprets the full requested path and performs the
                // route handling in this handler's ProcessRequest method

                // The order in which you add the routes matters. The HttpCwsServer will search in this order for a route matching
                // the requested URL, stopping at the first match it finds and calling the corresponding handler's
                // ProcessRequest method, or that of the HttpRequestHandler (called "DefaultHandler" in this example)
                // if no route-specific handler exists.
                HttpCwsRoute route = new HttpCwsRoute("relays")
                {
                    Name = "relay_collection"
                };                                                                             // do not include the leading '/' in the HttpCwsRoute constructor
                route.RouteHandler = new RelaysHandler(this);
                server.Routes.Add(route);

                route = new HttpCwsRoute("relays/web-hooks")
                {
                    Name = "relay_collection_subscriptions"
                };
                route.RouteHandler = new RelaysWebhooksHandler(this);
                server.Routes.Add(route);

                route = new HttpCwsRoute("relays/web-hooks/{subid}")
                {
                    Name = "relay_collection_subscription"
                };
                route.RouteHandler = new RelaysWebhooksSubidHandler(this);
                server.Routes.Add(route);

                route = new HttpCwsRoute("relays/{id}")
                {
                    Name = "individual_relay"
                };
                route.RouteHandler = new RelaysIdHandler(this);
                server.Routes.Add(route);

                route = new HttpCwsRoute("relays/{id}/web-hooks")
                {
                    Name = "individual_relay_subscriptions"
                };
                route.RouteHandler = new RelaysIdWebhooksHandler(this);
                server.Routes.Add(route);

                route = new HttpCwsRoute("relays/{id}/web-hooks/{subid}")
                {
                    Name = "individual_relay_subscription"
                };
                route.RouteHandler = new RelaysIdWebhooksSubidHandler(this);
                server.Routes.Add(route);

                // Start receiving HTTP requests
                server.Register();
            }
            catch (Exception e)
            {
                CrestronConsole.PrintLine("Error in the RestfulRelayServer constructor: " + e.Message);
            }
            finally
            {
            }
        }