Last active
February 8, 2017 17:12
-
-
Save ThomPuiman/2b05517c898055cbed50b73f75fbdc2b to your computer and use it in GitHub Desktop.
Garage Door: https://www.reddit.com/r/dailyprogrammer/comments/4cb7eh/20160328_challenge_260_easy_garage_door_opener/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from enum import Enum; | |
| class DoorState(Enum): | |
| CLOSING = 1 | |
| CLOSED = 2 | |
| STOPPED_WHILE_CLOSING = 3 | |
| OPEN = 4 | |
| OPENING = 5 | |
| STOPPED_WHILE_OPENING = 6 | |
| EMERGENCY_OPENING = 7 | |
| OPEN_BLOCKED = 8 | |
| class DoorOperator(): | |
| state = DoorState.CLOSED; | |
| def getCommand(self, command): | |
| { | |
| 'button_click': self.buttonClick, | |
| 'cycle_complete': self.cycleComplete, | |
| 'block_detected': self.blockDetected, | |
| 'block_cleared': self.blockCleared | |
| }[command](); | |
| return "Door: " + self.state.name; | |
| def buttonClick(self): | |
| self.state = self.getTransition(); | |
| def cycleComplete(self): | |
| if(self.state.value in [5,3]): | |
| self.state = DoorState.OPEN; | |
| if(self.state.value in [1,6]): | |
| self.state = DoorState.CLOSED; | |
| def blockDetected(self): | |
| self.state = DoorState.EMERGENCY_OPENING; | |
| def blockCleared(self): | |
| self.state = DoorState.OPEN; | |
| def getTransition(self): | |
| return { | |
| DoorState.CLOSING: DoorState.STOPPED_WHILE_CLOSING, | |
| DoorState.CLOSED: DoorState.OPENING, | |
| DoorState.OPENING: DoorState.STOPPED_WHILE_OPENING, | |
| DoorState.STOPPED_WHILE_OPENING: DoorState.CLOSING, | |
| DoorState.STOPPED_WHILE_CLOSING: DoorState.OPENING, | |
| DoorState.OPEN: DoorState.CLOSING, | |
| DoorState.EMERGENCY_OPENING: DoorState.OPEN_BLOCKED, | |
| DoorState.OPEN_BLOCKED: DoorState.OPEN_BLOCKED | |
| }[self.state]; | |
| operator = DoorOperator(); | |
| while(True): | |
| print(operator.getCommand(input("> "))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment