--restore and --wait are now always active

This commit is contained in:
Shy 2017-04-18 18:51:30 +02:00
parent 83dd8fca51
commit 5faa7da585

View file

@ -17,10 +17,11 @@
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>. # this program. If not, see <http://www.gnu.org/licenses/>.
def kitchentext(delay=200, skip_if_off=False, poweron=False, restore=False, def kitchentext(delay=200, skip_if_off=False, poweron=False, verbose=False,
wait=False, verbose=False, debug=False): debug=False):
import sys, signal import sys, signal
from time import sleep
from c4ctrl import C4Interface, Kitchenlight from c4ctrl import C4Interface, Kitchenlight
charwidth = { # Width of characters. charwidth = { # Width of characters.
@ -45,20 +46,16 @@ def kitchentext(delay=200, skip_if_off=False, poweron=False, restore=False,
C4Interface.debug = debug C4Interface.debug = debug
kl = Kitchenlight(autopower=poweron) kl = Kitchenlight(autopower=poweron)
# Enforce wait=True if restore=True
wait = wait or restore
# Store previous state. # Store previous state.
if restore or skip_if_off: c4 = C4Interface()
c4 = C4Interface() saved_state = c4.pull([kl.topic, kl.powertopic])
saved_state = c4.pull([kl.topic, kl.powertopic])
if skip_if_off: if skip_if_off:
# Stop here if kitchenlight is turned off. # Stop here if kitchenlight is turned off.
for state in saved_state: for state in saved_state:
if state.topic == kl.powertopic and state.payload == b'\x00': if state.topic == kl.powertopic and state.payload == b'\x00':
verbose and print("Found Kitchenlight turned off and '-i' flag given. Exiting.") verbose and print("Found Kitchenlight turned off and '-i' flag given. Exiting.")
return return
# We want to be able to restore the saved Kitchenlight if we receive a # We want to be able to restore the saved Kitchenlight if we receive a
# SIGERM signal. We do this by creating and registering a custom exception # SIGERM signal. We do this by creating and registering a custom exception
@ -95,23 +92,21 @@ def kitchentext(delay=200, skip_if_off=False, poweron=False, restore=False,
try: # We might well get interrupted while waiting. try: # We might well get interrupted while waiting.
kl.text(text, delay) kl.text(text, delay)
if wait: # How long shall we wait? Kitchenlight has 30 columns, each
from time import sleep # char uses 2-5 columns followed by an empty one. The
# How long shall we wait? Kitchenlight has 30 columns, each # traversal of one column takes 30 * <delay> ms.
# char uses 2-5 columns followed by an empty one. The text_width = 0
# traversal of one column takes 30 * <delay> ms. for c in text:
text_width = 0 try:
for c in text: text_width += (charwidth[c] + 1)
try: except KeyError:
text_width += (charwidth[c] + 1) # No width specified for this charachter. Let's use
except KeyError: # 4 as default.
# No width specified for this charachter. Let's use text_width += 4
# 4 as default.
text_width += 4
waiting_time = ((30 * delay) + (text_width * delay)) / 1000 waiting_time = ((30 * delay) + (text_width * delay)) / 1000
verbose and print("Waiting for {} seconds ...".format(waiting_time)) verbose and print("Waiting for {} seconds ...".format(waiting_time))
sleep(waiting_time) sleep(waiting_time)
except KeyboardInterrupt: except KeyboardInterrupt:
verbose and print("\nInterrupted by user.", file=sys.stderr) verbose and print("\nInterrupted by user.", file=sys.stderr)
@ -124,10 +119,9 @@ def kitchentext(delay=200, skip_if_off=False, poweron=False, restore=False,
finally: finally:
# Always restore privious state if "restore" is set. # Always restore privious state if "restore" is set.
if restore: re = []
re = [] for top in saved_state: re.append((top.topic, top.payload))
for top in saved_state: re.append((top.topic, top.payload)) c4.push(re)
c4.push(re)
if __name__ == "__main__": if __name__ == "__main__":
@ -155,16 +149,9 @@ if __name__ == "__main__":
parser.add_argument( parser.add_argument(
"-p", "--power-on", action="store_true", default=False, "-p", "--power-on", action="store_true", default=False,
help="turn on Kitchenlight if it is powered off") help="turn on Kitchenlight if it is powered off")
parser.add_argument(
"-r", "--restore", action="store_true", default=False,
help="restore the Kitchenlight to its prior state after the text has \
been displayed (implies --wait)")
parser.add_argument( parser.add_argument(
"-v", "--verbose", action="store_true", "-v", "--verbose", action="store_true",
help="be more verbose") help="be more verbose")
parser.add_argument(
"-w", "--wait", action="store_true", default=False,
help="wait until the text has been displayed")
args = parser.parse_args() args = parser.parse_args()
if args.fork or args.F: if args.fork or args.F:
@ -188,8 +175,6 @@ if __name__ == "__main__":
kitchentext(delay=args.delay, kitchentext(delay=args.delay,
skip_if_off=args.skip_if_off, skip_if_off=args.skip_if_off,
poweron=args.power_on, poweron=args.power_on,
restore=args.restore,
wait=args.wait,
verbose=args.verbose, verbose=args.verbose,
debug=args.debug) debug=args.debug)