Guest User

Untitled

a guest
May 18th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.79 KB | None | 0 0
  1.  
  2. namespace Framework
  3. {
  4.     using System;
  5.  
  6.     using Crestron.SimplSharp;
  7.     using Crestron.SimplSharpPro;
  8.     using Crestron.SimplSharpPro.CrestronThread;
  9.  
  10.     using log4net;
  11.  
  12.     using SSMono.IO;
  13.  
  14.     /// <summary>
  15.     /// The control system.
  16.     /// </summary>
  17.     public class ControlSystem : CrestronControlSystem
  18.     {
  19.         /// <summary>
  20.         /// The log.
  21.         /// </summary>
  22.         private static readonly ILog Log =
  23.             LogManager.GetLogger("Framework");
  24.  
  25.         /// <summary>
  26.         /// Initializes a new instance of the <see cref="ControlSystem"/> class.
  27.         /// ControlSystem Constructor. Starting point for the SIMPL#Pro program.
  28.         /// Use the constructor to:
  29.         /// * Initialize the maximum number of threads (max = 400)
  30.         /// * Register devices
  31.         /// * Register event handlers
  32.         /// * Add Console Commands
  33.         /// Please be aware that the constructor needs to exit quickly; if it doesn't
  34.         /// exit in time, the SIMPL#Pro program will exit.
  35.         /// You cannot send / receive data in the constructor
  36.         /// </summary>
  37.         public ControlSystem()
  38.         {
  39.             try
  40.             {
  41.                 Thread.MaxNumberOfUserThreads = 20;
  42.  
  43.                 // Subscribe to the controller events (System, Program, and Ethernet)
  44.                 CrestronEnvironment.SystemEventHandler += this.ControlSystem_ControllerSystemEventHandler;
  45.                 CrestronEnvironment.ProgramStatusEventHandler += this.ControlSystem_ControllerProgramEventHandler;
  46.                 CrestronEnvironment.EthernetEventHandler += this.ControlSystem_ControllerEthernetEventHandler;
  47.             }
  48.             catch (Exception e)
  49.             {
  50.                 if (Log.IsErrorEnabled)
  51.                 {
  52.                     Log.Error("Error in control system constructor", e);
  53.                 }
  54.                 else
  55.                 {
  56.                     ErrorLog.Error("{0} {1} - {2} {3} {4}", "ERROR", "CrossColorsFramework", "Error in control system constructor", e.Message, e.StackTrace);
  57.                 }
  58.             }
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Gets or sets the builder.
  63.         /// </summary>
  64.         private SystemBuilder Builder { get; set; }
  65.  
  66.         /// <summary>
  67.         /// InitializeSystem - this method gets called after the constructor
  68.         /// has finished.
  69.         /// Use InitializeSystem to:
  70.         /// * Start threads
  71.         /// * Configure ports, such as serial and VERSIPORTS
  72.         /// * Start and initialize socket connections
  73.         /// Send initial device configurations
  74.         /// Please be aware that InitializeSystem needs to exit quickly also;
  75.         /// if it doesn't exit in time, the SIMPL#Pro program will exit.
  76.         /// </summary>
  77.         public override void InitializeSystem()
  78.         {
  79.             try
  80.             {
  81.                 log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(@"\USER\log4net.config"));
  82.                 // this.Builder = new SystemBuilder(this);
  83.             }
  84.             catch (Exception e)
  85.             {
  86.                 Log.Error(string.Format("Error in InitializeSystem"), e);
  87.             }
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Event Handler for Ethernet events: Link Up and Link Down.
  92.         /// Use these events to close / re-open sockets, etc.
  93.         /// </summary>
  94.         /// <param name="ethernetEventArgs">This parameter holds the values
  95.         /// such as whether it's a Link Up or Link Down event. It will also indicate
  96.         /// wich Ethernet adapter this event belongs to.
  97.         /// </param>
  98.         void ControlSystem_ControllerEthernetEventHandler(EthernetEventArgs ethernetEventArgs)
  99.         {
  100.             switch (ethernetEventArgs.EthernetEventType)
  101.             {//Determine the event type Link Up or Link Down
  102.                 case (eEthernetEventType.LinkDown):
  103.                     //Next need to determine which adapter the event is for.
  104.                     //LAN is the adapter is the port connected to external networks.
  105.                     if (ethernetEventArgs.EthernetAdapter == EthernetAdapterType.EthernetLANAdapter)
  106.                     {
  107.                         //
  108.                     }
  109.                     break;
  110.                 case (eEthernetEventType.LinkUp):
  111.                     if (ethernetEventArgs.EthernetAdapter == EthernetAdapterType.EthernetLANAdapter)
  112.                     {
  113.  
  114.                     }
  115.                     break;
  116.             }
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Event Handler for Programmatic events: Stop, Pause, Resume.
  121.         /// Use this event to clean up when a program is stopping, pausing, and resuming.
  122.         /// This event only applies to this SIMPL#Pro program, it doesn't receive events
  123.         /// for other programs stopping
  124.         /// </summary>
  125.         /// <param name="programStatusEventType"></param>
  126.         void ControlSystem_ControllerProgramEventHandler(eProgramStatusEventType programStatusEventType)
  127.         {
  128.             switch (programStatusEventType)
  129.             {
  130.                 case (eProgramStatusEventType.Paused):
  131.                     //The program has been paused.  Pause all user threads/timers as needed.
  132.                     break;
  133.                 case (eProgramStatusEventType.Resumed):
  134.                     //The program has been resumed. Resume all the user threads/timers as needed.
  135.                     break;
  136.                 case (eProgramStatusEventType.Stopping):
  137.                     //The program has been stopped.
  138.                     //Close all threads.
  139.                     //Shutdown all Client/Servers in the system.
  140.                     //General cleanup.
  141.                     //Unsubscribe to all System Monitor events
  142.                     break;
  143.             }
  144.  
  145.         }
  146.  
  147.         /// <summary>
  148.         /// Event Handler for system events, Disk Inserted/Ejected, and Reboot
  149.         /// Use this event to clean up when someone types in reboot, or when your SD /USB
  150.         /// removable media is ejected / re-inserted.
  151.         /// </summary>
  152.         /// <param name="systemEventType"></param>
  153.         void ControlSystem_ControllerSystemEventHandler(eSystemEventType systemEventType)
  154.         {
  155.             switch (systemEventType)
  156.             {
  157.                 case (eSystemEventType.DiskInserted):
  158.                     //Removable media was detected on the system
  159.                     break;
  160.                 case (eSystemEventType.DiskRemoved):
  161.                     //Removable media was detached from the system
  162.                     break;
  163.                 case (eSystemEventType.Rebooting):
  164.                     //The system is rebooting.
  165.                     //Very limited time to preform clean up and save any settings to disk.
  166.                     break;
  167.             }
  168.  
  169.         }
  170.     }
  171. }
Add Comment
Please, Sign In to add comment