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.

Revisions

  1. ThomPuiman revised this gist Feb 8, 2017. No changes.
  2. ThomPuiman revised this gist Feb 8, 2017. No changes.
  3. ThomPuiman revised this gist Feb 8, 2017. No changes.
  4. ThomPuiman revised this gist Feb 8, 2017. No changes.
  5. ThomPuiman created this gist Feb 8, 2017.
    55 changes: 55 additions & 0 deletions garagedoor.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    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("> ")))