using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; using RemoteController.ReceiverClasses; namespace RemoteController { public class House { RemoteControl rc; /// /// Represents a Command Slot, a pair of commands objects which turn on or off a single appliance /// public struct command_pair { public string Name; public ICommand onCmd; public ICommand offCmd; } command_pair _cmd_Pair; private ArrayList _ArrAppliances = new ArrayList(); /// /// List of Appliances for the House object /// public ArrayList Appliances { get { return _ArrAppliances; } } private ArrayList _commandsArry = new ArrayList(); /// /// List of Valid Commands that can be assigned to the Remote Control object /// public ArrayList CommandList { get { return _commandsArry; } } /// /// Creats the Client Class which loads the Remote Control and Command Objects /// public House() { PopulateCommands(); rc = new RemoteControl("Default"); } /// /// Sets the Command at the specified slot in slotIndex to the command in CommandsArray /// Both indexes must be valid and withing bounds otherwise no action will be made. /// /// Slot to insert the new Command. Valid range is 0 - 7 /// The index of the command in Command List Array to insert public void setCommand(int slotIndex, int commandArryIndex) { if ((rc.CommandCount < rc.Available_Slots) && // check if free slots available (slotIndex > -1 && slotIndex < 8) && // check valid slot (commandArryIndex > -1 && commandArryIndex < _commandsArry.Count) // check CommandArry index is within bounds ) { command_pair pair = (command_pair)_commandsArry[commandArryIndex]; rc.setCommand(slotIndex, pair.onCmd, pair.offCmd); } } /// /// Invokes the On Command in the specified slot (zero based index) /// /// The zero based index of the command slot to be activated public void On_Button_Pushed(int slot) { rc.onButtonWasPushed(slot); } /// /// Invokes the Off Command in the specified slot (zero based index) /// /// The zero based index of the command slot to be activated public void Off_Button_Pushed(int slot) { rc.offButtonWasPushed(slot); } /// /// Invokes the undo button on the remote control /// public void undo_button_pushed() { rc.undoButtonWasPushed(); } /// /// Retrieves from the database the command_pair strut definitions and then /// populates the _CommandsArry with these command_pair objects for this remote control /// private void PopulateCommands() { _commandsArry = new ArrayList(); RCDatabaseDataContext db = new RCDatabaseDataContext(); var query = from c in db.CommandObjects select c; foreach (var x in query) { _commandsArry.Add(createValidCommand_Pair(x.Name, x.ReceiverType, x.OnCmdName, x.OffCmdName)); } } /// /// Creates and returns the Command Pair strut. Uses reflection to create both Command Objects in this pair. They must /// control the same receiver, so both are passed the same reciever as a parameter in their constructors. /// The reciever object is then stored in the Appliances Array for later use. /// /// Command Name /// Receiver Type to create /// Command Object Type to create representing the ON Command /// Command Object Type to create representing the OFF Command /// a command_pair strut private command_pair createValidCommand_Pair(string CmdName, string strparamType,string strOnCommandType,string strOffCommandType) { command_pair slot; Type TypeOnCmd = Type.GetType(strOnCommandType); Type TypeOffCmd = Type.GetType(strOffCommandType); Type paramType = Type.GetType(strparamType); //creating the Receiver Object object paramObj = Activator.CreateInstance(paramType); //creating the ON command object System.Reflection.ConstructorInfo[] ciOnCmd = TypeOnCmd.GetConstructors(); //create the OFF command object System.Reflection.ConstructorInfo[] ciOffCmd = TypeOffCmd.GetConstructors(); object[] objarr = new object[1]; //store created Receiver object in appliances arraylist _ArrAppliances.Add(paramObj); objarr[0] = paramObj; //create the on and off command objects with the same reciever object ICommand ICmdOnObj = (ICommand)ciOnCmd[0].Invoke(objarr); ICommand ICmdOffObj = (ICommand)ciOffCmd[0].Invoke(objarr); //set command_pair strut values slot.Name = CmdName; slot.onCmd = ICmdOnObj; slot.offCmd = ICmdOffObj; return slot; } } }