/// <summary> /// Initializes a new instance of the SystemManager class /// </summary> /// <param name="config">full config data</param> /// <param name="cs">CrestronControlSystem</param> public SystemManager(Configuration.ConfigData.Configuration config, CrestronControlSystem cs) { if (config.Touchpanels != null) { this.touchpanels = new Dictionary <string, BasicTriListWithSmartObject>(); // TODO: Level1. Implement touchPanel + sources + destinations foreach (var touchPanel in config.Touchpanels) { // TODO: Level1. Create new instance of TouchpanelUI // ! Don't forget to use Register() from TouchpanelUI class ! string type = touchPanel.Type; uint id = touchPanel.Id; string label = touchPanel.Label; this.tp = new TouchpanelUI(type, id, label, cs); if (this.tp.Register()) { ErrorLog.Info($"{LogHeader} created and registered tp {label}"); // TODO: Level1. Dynamically set up sources using the config file ErrorLog.Info($"{LogHeader} Nbr of Smart Object Sources: {config.Sources.Length}"); ErrorLog.Info($"{LogHeader} Nbr of Smart Object Destinations: {config.Destinations.Length}"); tp.UserInterface.SmartObjects[1].UShortInput["Set Number of Items"].UShortValue = (ushort)config.Sources.Length; for (var i = 0; i < config.Sources.Length; i++) { tp.UserInterface.SmartObjects[1].UShortInput[$"Set Item {i+1} Icon Analog"].UShortValue = config.Sources[i].Icon; tp.UserInterface.SmartObjects[1].StringInput[$"Set Item {i + 1} Text"].StringValue = config.Sources[i].Label; ErrorLog.Info($"{LogHeader} SOURCE: Set [Item {i+1} Icon Analog/Text] {config.Sources[i].Icon} {config.Sources[i].Label}"); } // TODO: Level1. Dynamically set up destinations using the config file tp.UserInterface.SmartObjects[2].UShortInput["Set Number of Items"].UShortValue = (ushort)config.Destinations.Length; ErrorLog.Info($"{LogHeader} Nbr of Smart Object Destinations: {config.Destinations.Length}"); for (var i = 0; i < config.Destinations.Length; i++) { tp.UserInterface.SmartObjects[2].UShortInput[$"Set Item {i+1} Icon Analog"].UShortValue = config.Destinations[i].Icon; tp.UserInterface.SmartObjects[2].StringInput[$"Set Item {i + 1} Text"].StringValue = config.Destinations[i].Label; ErrorLog.Info($"{LogHeader} DESTINATION: Set [Item {i+1} Icon Analog] {config.Destinations[i].Icon}"); } } else { ErrorLog.Error($"{LogHeader} Error created and registering tp {label}"); } // TODO: Level2. Implement the additional subsystem dynamically. // Please see Student Guide for more explanation. } } }
/// <summary> /// Reads a JSON formatted configuration from disc /// </summary> /// <param name="configFile">Location and name of the config file</param> /// <returns>True or False depending on read success</returns> public bool ReadConfig(string configFile) { // string for file contents string configData = string.Empty; ErrorLog.Notice(LogHeader + "Started loading config file: {0}", configFile); if (string.IsNullOrEmpty(configFile)) { this.readSuccess = false; ErrorLog.Error(LogHeader + "No File?!?"); } if (!File.Exists(configFile)) { this.readSuccess = false; ErrorLog.Error(LogHeader + "Config file doesn't exist"); } else if (File.Exists(configFile)) { configLock.Enter(); // Open, read and close the file // If you don't want an exception to be thrown which would potentially stop the program, // you could throw a try/catch around the method which loads the configuration file so that // the exception can be caught so that you could show the exception message on screen, // rather than leaving it be thrown (unhandled) try { using (StreamReader file = new StreamReader(configFile)) { configData = file.ReadToEnd(); file.Close(); } // Try to deserialize into a Room object. If this fails, the JSON file is probably malformed this.RoomConfig = JsonConvert.DeserializeObject <ConfigData.Configuration>(configData); ErrorLog.Notice(LogHeader + "Config file loaded!"); this.readSuccess = true; } catch (Exception e) { this.readSuccess = false; ErrorLog.Error(LogHeader + "Exception in reading config file: {0}", e.Message); } finally { configLock.Leave(); } } return(this.readSuccess); }
/// <summary> /// Reads a JSON formatted configuration from disc /// </summary> /// <param name="configFile">Location and name of the config file</param> /// <returns>True or False depending on read success</returns> public bool ReadConfig(string configFile) { // string for file contents string configData = string.Empty; ErrorLog.Notice(LogHeader + "Started loading config file: {0}", configFile); if (string.IsNullOrEmpty(configFile)) { this.readSuccess = false; ErrorLog.Error(LogHeader + "No File?!?"); } if (!File.Exists(configFile)) { this.readSuccess = false; ErrorLog.Error(LogHeader + "Config file doesn't exist"); } else if (File.Exists(configFile)) { configLock.Enter(); // Open, read and close the file using (StreamReader file = new StreamReader(configFile)) { configData = file.ReadToEnd(); file.Close(); } try { // Try to deserialize into a Room object. If this fails, the JSON file is probably malformed this.RoomConfig = JsonConvert.DeserializeObject <ConfigData.Configuration>(configData); ErrorLog.Notice(LogHeader + "Config file loaded!"); this.readSuccess = true; } catch (Exception e) { this.readSuccess = false; ErrorLog.Error(LogHeader + "Exception in reading config file: {0}", e.Message); } finally { configLock.Leave(); } } return(this.readSuccess); }
/// <summary> /// TouchpanelUI object to use for registration /// </summary> // PAUL TODO - Revisit this // private UI.TouchpanelUI tp; /// <summary> /// Initializes a new instance of the SystemManager class /// </summary> /// <param name="config">full config data</param> /// <param name="cs">CrestronControlSystem</param> public SystemManager(Configuration.ConfigData.Configuration config, CrestronControlSystem cs) { try { // Added for Exercise 2. this.ConfigurePublisher(); } catch (Exception e) { ErrorLog.Error(LogHeader + "Unable to configure publisher: {0}", e.Message); } if (config.Touchpanels != null) { // TODO: Level1. Implement touchPanel + sources + destinations foreach (var touchpanel in config.Touchpanels) { this.touchpanels[touchpanel.Label] = new UI.TouchpanelUI(touchpanel.Type, touchpanel.Id, touchpanel.Label, cs); // TODO: Level1. Create new instance of TouchpanelUI if (this.touchpanels[touchpanel.Label].Register()) { try { // Set up touchpanel basics based on configuration this.touchpanels[touchpanel.Label].UserInterface.SmartObjects[1].UShortInput["Set Number of Items"].UShortValue = (ushort)config.Sources.Length; this.touchpanels[touchpanel.Label].UserInterface.SmartObjects[2].UShortInput[4].UShortValue = (ushort)config.Destinations.Length; // set the source labels and dynamic icons foreach (var source in config.Sources) { this.touchpanels[touchpanel.Label].UserInterface.SmartObjects[1].StringInput[$"Set Item {source.Id} Text"].StringValue = source.Label; this.touchpanels[touchpanel.Label].UserInterface.SmartObjects[1].UShortInput["Set Item " + source.Id + " Icon Analog"].UShortValue = source.Icon; } foreach (var destination in config.Destinations) { this.touchpanels[touchpanel.Label].UserInterface.SmartObjects[2].StringInput[10 + destination.Id].StringValue = destination.Label; this.touchpanels[touchpanel.Label].UserInterface.SmartObjects[2].UShortInput[10 + destination.Id].UShortValue = destination.Icon; } // Register event handler this.touchpanels[touchpanel.Label].UserInterface.SigChange += this.UserInterface_SigChange; // Register event handlers for Smart Objects foreach (KeyValuePair <uint, SmartObject> smartObject in this.touchpanels[touchpanel.Label].UserInterface.SmartObjects) { smartObject.Value.SigChange += this.SO_Value_SigChange; } } catch (Exception e) { ErrorLog.Error(LogHeader + "Exception trying to set up touchpanel: {0} - {1}", touchpanel.Label, e.Message); } } } } if (config.Nvx != null) { foreach (var nvx in config.Nvx) { try { this.nvxUnits = new Video.DmNvx(nvx.Type, nvx.Id, nvx.Name, nvx.Multicast, NvxExchange, cs); if (this.nvxUnits.Register()) { // Register some events, etc ErrorLog.Notice(LogHeader + "Registered NVX {0} successfully!", nvx.Name); } } catch (Exception e) { ErrorLog.Error(LogHeader + "Error in registering NVX units: {0}", e.Message); } } } }