From 236a61e6449b7864ccfc8b4b9185333633222e40 Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Sat, 15 Dec 2018 23:12:24 -0800 Subject: [PATCH] Best practice --- xmas.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/xmas.py b/xmas.py index 7e8db5d..3c5c839 100644 --- a/xmas.py +++ b/xmas.py @@ -3,6 +3,36 @@ import time import datetime import pifacedigitalio as p +TREE = """ + * , + _/^\_ + < > + * /.-.\ * + * `/&\` * + ,@.*;@, + /_o.I %_\ * + * (`'--:o(_@; + /`;--.,__ `') * + ;@`o % O,*`'`&\ * + * (`'--)_@ ;o %'()\ * + /`;--._`''--._O'@; + /&*,()~o`;-.,_ `""`) + * /`,@ ;+& () o*`;-';\ * + (`""--.,_0 +% @' &()\ * + /-.,_ ``''--....-'`) * + * /@%;o`:;'--,.__ __.'\ + ;*,&(); @ % &^;~`"`o;@(); * + /(); o^~; & ().o@*&`;&%O\ + jgs `"="==""==,,,.,="=="==="` + __.----.(\-''#####---...___...-----._ + '` \)_`'''''` + .--' ') + o( )_- + `'''` ` +""" + +START = datetime.time(17, 0, 0) +END = datetime.time(23, 59, 0) def timestamp(): return datetime.datetime.now() @@ -22,23 +52,36 @@ def turn_off(): def check_connectivity(): """ Flick the switch """ - for i in range(10): + print("Checking connectivity:") + for i in range(2): turn_on() - time.sleep(.3) + time.sleep(2) turn_off() - time.sleep(.3) + time.sleep(2) + + + # Turn on if we are in the correct interval + now = datetime.datetime.now().time() + if START <= now <= END: + turn_on() if __name__ == "__main__": # Connect to PiFace + print(TREE) p.init() # Check connectivity check_connectivity() + # Message + print() + print("Starting scheduler:") + print("Xmas enabled between {} and {}.".format(START.strftime("%H:%M"), END.strftime("%H:%M"))) + # Set up the schedule - schedule.every().day.at("17:00").do(turn_on) - schedule.every().day.at("23:59").do(turn_off) + schedule.every().day.at(START.strftime("%H:%M")).do(turn_on) + schedule.every().day.at(END.strftime("%H:%M")).do(turn_off) # Run the event loop while(True):