Commits

Doug Tyler committed 2faf52c

added MacroCommands

  • Participants
  • Parent commits e85373f

Comments (0)

Files changed (3)

command/__init__.py

     livingRoomLightOn = LightOnCommand(livingRoomLight)
     livingRoomLightOff = LightOffCommand(livingRoomLight)
 
-    # Kitchin light and on/off commands to control it
+    # Kitchen light and on/off commands to control it
     kitchenLight = Light("Kitchen Light")
     kitchenLightOn = LightOnCommand(kitchenLight)
     kitchenLightOff = LightOffCommand(kitchenLight)
     bedroomLightOn = LightOnCommand(bedroomLight)
     bedroomLightOff = LightOffCommand(bedroomLight)
 
+    ceilingFan = CeilingFan("Living Room Fan")
+    ceilingFanOn = CeilingFanOnCommand(ceilingFan)
+    ceilingFanOff = CeilingFanOffCommand(ceilingFan)
+
+    stereo = Stereo("Living Room Stereo")
+    stereoOnWithCD = StereoOnWithCDCommand(stereo)
+    stereoOff = StereoOffCommand(stereo)
+
+    partyOnCommand = MacroCommand([livingRoomLightOn, stereoOnWithCD, ceilingFanOn, kitchenLightOn])
+    partyOffCommand = MacroCommand([kitchenLightOff, ceilingFanOff, stereoOff, livingRoomLightOff])
+
     # Map the on/off commands to "physical" buttons on the remote
     remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff)
     remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff)
     remoteControl.setCommand(2, bedroomLightOn, bedroomLightOff)
+    remoteControl.setCommand(3, partyOnCommand, partyOffCommand)
 
     # Push each button once!
-    remoteControl.onButtonWasPushed(0)  # LR On
-    remoteControl.offButtonWasPushed(0) # LR Off
-    remoteControl.onButtonWasPushed(1)  # K On
-    remoteControl.offButtonWasPushed(1) # K Off
-    remoteControl.onButtonWasPushed(2)  # BR On
-    remoteControl.offButtonWasPushed(2) # BR Off
-    remoteControl.redoButtonWasPushed() # Nothing
+    remoteControl.onButtonWasPushed(0)   # LR On
+    remoteControl.offButtonWasPushed(0)  # LR Off
+    remoteControl.onButtonWasPushed(1)   # K On
+    remoteControl.offButtonWasPushed(1)  # K Off
+    remoteControl.onButtonWasPushed(2)   # BR On
+    remoteControl.offButtonWasPushed(2)  # BR Off
+    remoteControl.redoButtonWasPushed()  # Nothing
     print separator
 
     # Push Undo button repeatedly, stepping through the executed commands
     # in reverse chronological order
-    remoteControl.undoButtonWasPushed() # Undo BR Off
-    remoteControl.undoButtonWasPushed() # Undo BR On
-    remoteControl.undoButtonWasPushed() # Undo K Off
-    remoteControl.undoButtonWasPushed() # Undo K On
-    remoteControl.undoButtonWasPushed() # Undo LR Off
-    remoteControl.undoButtonWasPushed() # Undo LR On
-    remoteControl.undoButtonWasPushed() # Nothing
-    remoteControl.undoButtonWasPushed() # Nothing
-    remoteControl.undoButtonWasPushed() # Nothing
-    remoteControl.undoButtonWasPushed() # Nothing
+    remoteControl.undoButtonWasPushed()  # Undo BR Off
+    remoteControl.undoButtonWasPushed()  # Undo BR On
+    remoteControl.undoButtonWasPushed()  # Undo K Off
+    remoteControl.undoButtonWasPushed()  # Undo K On
+    remoteControl.undoButtonWasPushed()  # Undo LR Off
+    remoteControl.undoButtonWasPushed()  # Undo LR On
+    remoteControl.undoButtonWasPushed()  # Nothing
+    remoteControl.undoButtonWasPushed()  # Nothing
+    remoteControl.undoButtonWasPushed()  # Nothing
+    remoteControl.undoButtonWasPushed()  # Nothing
     print separator
 
-    # Push each botton again
-    remoteControl.onButtonWasPushed(0)  # LR On
-    remoteControl.offButtonWasPushed(0) # LR Off
-    remoteControl.onButtonWasPushed(1)  # K On
-    remoteControl.offButtonWasPushed(1) # K Off
-    remoteControl.onButtonWasPushed(2)  # BR On
-    remoteControl.offButtonWasPushed(2) # BR Off
+    # Push each button again
+    remoteControl.onButtonWasPushed(0)   # LR On
+    remoteControl.offButtonWasPushed(0)  # LR Off
+    remoteControl.onButtonWasPushed(1)   # K On
+    remoteControl.offButtonWasPushed(1)  # K Off
+    remoteControl.onButtonWasPushed(2)   # BR On
+    remoteControl.offButtonWasPushed(2)  # BR Off
 
     # Push each button again
-    remoteControl.onButtonWasPushed(0)  # LR On
-    remoteControl.offButtonWasPushed(0) # LR Off
-    remoteControl.onButtonWasPushed(1)  # K On
-    remoteControl.offButtonWasPushed(1) # K Off
-    remoteControl.onButtonWasPushed(2)  # BR On
-    remoteControl.offButtonWasPushed(2) # BR Off
+    remoteControl.onButtonWasPushed(0)   # LR On
+    remoteControl.offButtonWasPushed(0)  # LR Off
+    remoteControl.onButtonWasPushed(1)   # K On
+    remoteControl.offButtonWasPushed(1)  # K Off
+    remoteControl.onButtonWasPushed(2)   # BR On
+    remoteControl.offButtonWasPushed(2)  # BR Off
     print separator
 
     # Undo the last 4 commands
-    remoteControl.undoButtonWasPushed() # Undo BR Off
-    remoteControl.undoButtonWasPushed() # Undo BR On
-    remoteControl.undoButtonWasPushed() # Undo K Off
-    remoteControl.undoButtonWasPushed() # Undo K On
+    remoteControl.undoButtonWasPushed()  # Undo BR Off
+    remoteControl.undoButtonWasPushed()  # Undo BR On
+    remoteControl.undoButtonWasPushed()  # Undo K Off
+    remoteControl.undoButtonWasPushed()  # Undo K On
     print separator
 
     # Redo the last 4 commands
-    remoteControl.redoButtonWasPushed() # Redo K On
-    remoteControl.redoButtonWasPushed() # Redo K Off
-    remoteControl.redoButtonWasPushed() # Redo BR On
-    remoteControl.redoButtonWasPushed() # Redo BR Off
-    remoteControl.redoButtonWasPushed() # Nothing
+    remoteControl.redoButtonWasPushed()  # Redo K On
+    remoteControl.redoButtonWasPushed()  # Redo K Off
+    remoteControl.redoButtonWasPushed()  # Redo BR On
+    remoteControl.redoButtonWasPushed()  # Redo BR Off
+    remoteControl.redoButtonWasPushed()  # Nothing
+    print separator
+
+    remoteControl.onButtonWasPushed(3)
+    print separator
+
+    remoteControl.offButtonWasPushed(3)
+    print separator
+
+    remoteControl.undoButtonWasPushed()
+    print separator
+
+    remoteControl.redoButtonWasPushed()
 
 RemoteLoaderUndo()
 

command/commands.py

 
     def undo(self):
         self.stereo.on()
+
+class MacroCommand(Command):
+    def __init__(self, commands):
+        self.commands = []
+        for command in commands:
+            if isinstance(command, Command):
+                self.commands.append(command)
+
+    def execute(self):
+        for command in self.commands:
+            if isinstance(command, Command):
+                command.execute()
+
+    def undo(self):
+        for command in reversed(self.commands):
+            if isinstance(command, Command):
+                command.undo()

command/output.txt

 Bedroom Light is off.
 Kitchen Light is on.
 Kitchen Light is off.
+--------------------
+Kitchen Light is on.
+Kitchen Light is off.
+Bedroom Light is on.
+Bedroom Light is off.
+--------------------
+Living Room Light is on.
+Living Room Stereo is on.
+Living Room Fan is on.
+Kitchen Light is on.
+--------------------
+Kitchen Light is off.
+Living Room Fan is OFF.
+Living Room Stereo is off.
+Living Room Light is off.
+--------------------
+Living Room Light is on.
+Living Room Stereo is on.
+Living Room Fan is on.
+Kitchen Light is on.
+--------------------
+Kitchen Light is off.
+Living Room Fan is OFF.
+Living Room Stereo is off.
+Living Room Light is off.