Skip to content

Instantly share code, notes, and snippets.

@ThomPuiman
Last active February 8, 2017 17:12
Show Gist options
  • Select an option

  • Save ThomPuiman/2b05517c898055cbed50b73f75fbdc2b to your computer and use it in GitHub Desktop.

Select an option

Save ThomPuiman/2b05517c898055cbed50b73f75fbdc2b to your computer and use it in GitHub Desktop.
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