Added fork option

This commit is contained in:
Shy 2017-04-13 18:08:19 +02:00
parent a642a01f97
commit 310dd05c5c

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# #
# kitchentext: Read text from stdin and put it on the Kitchenlight # kitchentext: Read text from stdin and put it on the Kitchenlight.
def kitchentext(delay=200, single=False, wait=False, restore=False, poweron=False, def kitchentext(delay=200, single=False, wait=False, restore=False, poweron=False,
verbose=False, debug=False): verbose=False, debug=False):
@ -8,7 +8,7 @@ def kitchentext(delay=200, single=False, wait=False, restore=False, poweron=Fals
import sys import sys
from c4ctrl import C4Interface, Kitchenlight from c4ctrl import C4Interface, Kitchenlight
charwidth = { # Width of characters charwidth = { # Width of characters.
'a' : 5, 'A' : 5, 'b' : 4, 'B' : 5, 'a' : 5, 'A' : 5, 'b' : 4, 'B' : 5,
'c' : 3, 'C' : 5, 'd' : 4, 'D' : 5, 'c' : 3, 'C' : 5, 'd' : 4, 'D' : 5,
'e' : 4, 'E' : 5, 'f' : 3, 'F' : 5, 'e' : 4, 'E' : 5, 'f' : 3, 'F' : 5,
@ -38,39 +38,36 @@ def kitchentext(delay=200, single=False, wait=False, restore=False, poweron=Fals
C4Interface.debug = debug C4Interface.debug = debug
kl = Kitchenlight(autopower=poweron) kl = Kitchenlight(autopower=poweron)
# Enforce wait=True if restore=True or single=False # Enforce wait=True if restore=True or single=False.
wait = wait or restore or not single wait = wait or restore or not single
# Store previous state # Store previous state.
if restore: if restore:
c4 = C4Interface() c4 = C4Interface()
safe = c4.pull([kl.topic, kl.powertopic]) safe = c4.pull([kl.topic, kl.powertopic])
try: try:
stop = False while True:
while not stop:
# Only itarate once if single is set
stop = single
try: try:
text = sys.stdin.readline() text = sys.stdin.readline()
except KeyboardInterrupt: # Exit a bit more graceful on CTRL-C except KeyboardInterrupt: # Exit a bit more graceful on CTRL-C.
verbose and sys.stderr.write("\rInterrupted by user\n") verbose and sys.stderr.write("\rInterrupted by user\n")
sys.exit(1) sys.exit(1)
if single and text.rstrip('\n') == "": if single and text.rstrip('\n') == "":
verbose and sys.stderr.write("Error: empty input!\n") verbose and sys.stderr.write("Error: empty input!\n")
sys.exit(1) sys.exit(1)
elif text == "\n": # Empty line elif text == "\n": # Empty line.
verbose and print("Info: skipping empty line") verbose and print("Info: skipping empty line")
continue continue
elif text == "": # EOF elif text == "": # EOF.
break break
# Strip chars Kitchenlight can not display # Strip chars Kitchenlight can not display.
text = text.rstrip('\n').encode("ascii", "ignore").decode("ascii") text = text.rstrip('\n').encode("ascii", "ignore").decode("ascii")
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: if wait:
@ -93,7 +90,7 @@ def kitchentext(delay=200, single=False, wait=False, restore=False, poweron=Fals
sys.exit(1) sys.exit(1)
finally: finally:
# Always restore privious state if "restore" is set # Always restore privious state if "restore" is set.
if restore: if restore:
re = [] re = []
for top in safe: re.append((top.topic, top.payload)) for top in safe: re.append((top.topic, top.payload))
@ -120,6 +117,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(
"-f", "--fork", action="store_true",
help="fork to background")
parser.add_argument( parser.add_argument(
"-v", "--verbose", action="store_true", "-v", "--verbose", action="store_true",
help="be (not much) more verbose") help="be (not much) more verbose")
@ -128,6 +128,15 @@ if __name__ == "__main__":
help="display what would be send to the MQTT broker, but do not actually connect") help="display what would be send to the MQTT broker, but do not actually connect")
args = parser.parse_args() args = parser.parse_args()
if args.fork:
import sys
from posix import fork
child_pid = fork()
if child_pid != 0:
args.verbose and print("Forked to PID", child_pid)
sys.exit()
kitchentext(delay=args.delay, kitchentext(delay=args.delay,
single=args.single, single=args.single,
wait=args.wait, wait=args.wait,