Move input parsing into separate function
This commit is contained in:
parent
e551bb233e
commit
16064921e0
1 changed files with 21 additions and 13 deletions
34
c4ctrl.py
34
c4ctrl.py
|
@ -542,8 +542,8 @@ class C4Room: # {{{1
|
||||||
def get_switch_state(self, max_age=5):
|
def get_switch_state(self, max_age=5):
|
||||||
""" Returns current state of switches as a string of 1s and 0s.
|
""" Returns current state of switches as a string of 1s and 0s.
|
||||||
|
|
||||||
max_age specifies how old (in seconds) a cached responce from a
|
max_age specifies how old (in seconds) a cached responce must be
|
||||||
previously done request may be before it is considered outdated. """
|
before it is considered outdated. """
|
||||||
|
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
|
@ -572,17 +572,8 @@ class C4Room: # {{{1
|
||||||
self._switch_state = (state, time())
|
self._switch_state = (state, time())
|
||||||
return state
|
return state
|
||||||
|
|
||||||
def light_switch(self, userinput=""):
|
def _parse_switch_input(self, userinput):
|
||||||
""" Switch lamps in a room on or off. """
|
""" Parse user input to the switch command. """
|
||||||
|
|
||||||
if not userinput:
|
|
||||||
# Derive user input from stdin.
|
|
||||||
userinput = self._interactive_light_switch()
|
|
||||||
if userinput == "": return
|
|
||||||
|
|
||||||
if userinput == '-':
|
|
||||||
print(self.get_switch_state())
|
|
||||||
return
|
|
||||||
|
|
||||||
# Let's support some binary operations!
|
# Let's support some binary operations!
|
||||||
ops = "" # Store operators.
|
ops = "" # Store operators.
|
||||||
|
@ -667,6 +658,23 @@ class C4Room: # {{{1
|
||||||
userinput, switch_state))
|
userinput, switch_state))
|
||||||
ops = ops[:-1]
|
ops = ops[:-1]
|
||||||
|
|
||||||
|
return userinput
|
||||||
|
|
||||||
|
def light_switch(self, userinput=""):
|
||||||
|
""" Switch lamps in a room on or off. """
|
||||||
|
|
||||||
|
if not userinput:
|
||||||
|
# Derive user input from stdin.
|
||||||
|
userinput = self._interactive_light_switch()
|
||||||
|
if userinput == "": return
|
||||||
|
|
||||||
|
if userinput == '-':
|
||||||
|
print(self.get_switch_state())
|
||||||
|
return
|
||||||
|
|
||||||
|
userinput = self._parse_switch_input(userinput)
|
||||||
|
if not userinput: sys.exit(1)
|
||||||
|
|
||||||
command=[]
|
command=[]
|
||||||
for i in range(len(self.switches)):
|
for i in range(len(self.switches)):
|
||||||
# Skip unchanged switches if we happen to know their state.
|
# Skip unchanged switches if we happen to know their state.
|
||||||
|
|
Loading…
Reference in a new issue