/// <summary> /// Event Handler for Programmatic events: Stop, Pause, Resume. /// Use this event to clean up when a program is stopping, pausing, and resuming. /// This event only applies to this SIMPL#Pro program, it doesn't receive events /// for other programs stopping /// </summary> /// <param name="programStatusEventType"></param> void ControlSystem_ControllerProgramEventHandler(eProgramStatusEventType programStatusEventType) { switch (programStatusEventType) { case (eProgramStatusEventType.Stopping): //The program has been stopped. //Close all threads. //Shutdown all Client/Servers in the system. //General cleanup. //Unsubscribe to all System Monitor events server.Unregister(); server.Dispose(); break; } }
/// <summary> /// Event Handler for Programmatic events: Stop, Pause, Resume. /// Use this event to clean up when a program is stopping, pausing, and resuming. /// This event only applies to this SIMPL#Pro program, it doesn't receive events /// for other programs stopping /// </summary> /// <param name="programStatusEventType"></param> void ControlSystem_ControllerProgramEventHandler(eProgramStatusEventType programStatusEventType) { switch (programStatusEventType) { case (eProgramStatusEventType.Paused): //The program has been paused. Pause all user threads/timers as needed. break; case (eProgramStatusEventType.Resumed): //The program has been resumed. Resume all the user threads/timers as needed. break; case (eProgramStatusEventType.Stopping): myServer.Dispose(); myServer.Unregister(); break; } }
private void ProgramStatusHandler(eProgramStatusEventType status) { if (status == eProgramStatusEventType.Stopping) { if (_api != null) { _api.Unregister(); _api.Dispose(); } } }
/// <summary> /// ControlSystem Constructor. Starting point for the SIMPL#Pro program. /// Use the constructor to: /// * Initialize the maximum number of threads (max = 400) /// * Register devices /// * Register event handlers /// * Add Console Commands /// /// Please be aware that the constructor needs to exit quickly; if it doesn't /// exit in time, the SIMPL#Pro program will exit. /// /// You cannot send / receive data in the constructor /// </summary> public ControlSystem() { try { server = new HttpCwsServer("/"); server.HttpRequestHandler = new RequestHandler(); server.ReceivedRequestEvent += new HttpCwsRequestEventHandler(server_ReceivedRequestEvent); if (!server.Register()) { CrestronConsole.PrintLine("CWS server registration failed"); server.Dispose(); return; } //Subscribe to the controller events (System, Program, and Ethernet) CrestronEnvironment.SystemEventHandler += new SystemEventHandler(ControlSystem_ControllerSystemEventHandler); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(ControlSystem_ControllerProgramEventHandler); } catch (Exception e) { ErrorLog.Error("Error in the constructor: {0}", e.Message); } }
/// <summary> /// The default Constructor. /// </summary> /// <param name="path"></param> public WebScriptingServer(string path) { _path = path; _server = new HttpCwsServer(path); _server.Register(); Debug.WriteSuccess("HttpCwsServer", "Started for path: {0}", _path); Debug.WriteInfo("HttpCwsServer", "Base URL: {0}", BaseUrlDns); Debug.WriteInfo("HttpCwsServer", "Base URL: {0}", BaseUrlIp); _server.ReceivedRequestEvent += (sender, args) => { #if DEBUG CrestronConsole.PrintLine("Incoming http request from {0} {1}", args.Context.Request.UserHostAddress, args.Context.Request.UserHostName); CrestronConsole.PrintLine("Request AbsolutePath: {0}", args.Context.Request.Url.AbsolutePath); CrestronConsole.PrintLine("Request AbsoluteUri: {0}", args.Context.Request.Url.AbsoluteUri); CrestronConsole.PrintLine("Request PhysicalPath: {0}", args.Context.Request.PhysicalPath); CrestronConsole.PrintLine("Request PathAndQuery: {0}", args.Context.Request.Url.PathAndQuery); CrestronConsole.PrintLine("Request Query: {0}", args.Context.Request.Url.Query); CrestronConsole.PrintLine("Request Path: {0}", args.Context.Request.Path); CrestronConsole.PrintLine("Request Method: {0}", args.Context.Request.HttpMethod); #endif try { if (args.Context.Request.RouteData != null) { #if DEBUG CrestronConsole.PrintLine("Request handler: {0}", args.Context.Request.RouteData.RouteHandler.GetType()); CrestronConsole.PrintLine("Route URL Pattern: {0}", args.Context.Request.RouteData.Route.Url); #endif } else if (RootHandler == null) { #if DEBUG CrestronConsole.PrintLine("Request has no handler!"); #endif HandleError(args.Context, 404, "Not Found", "The requested resource does not exist"); } else if (args.Context.Request.PhysicalPath != string.Format("\\HTML\\{0}", _path) && args.Context.Request.PhysicalPath != string.Format("\\HTML\\{0}\\", _path)) { #if DEBUG CrestronConsole.PrintLine("Request handler: {0}", RootHandler.GetType()); #endif RootHandler.ProcessRequest(args.Context); } } catch (Exception e) { CloudLog.Exception("Error in ApiServer main request handler", e); HandleError(args.Context, 500, "Server Error", string.Format("{0}<BR>{1}", e.Message, e.StackTrace)); } }; CrestronEnvironment.ProgramStatusEventHandler += type => { if (type == eProgramStatusEventType.Stopping) { _server.Dispose(); } }; }