Created
September 4, 2020 22:53
-
-
Save BillSimpson/d7a1a531995c8b63492bb47ef8872618 to your computer and use it in GitHub Desktop.
A school bell scheduling code for homeschool / distance students.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
# a python program to have a raspberry pi do school bell alerts | |
# on a schedule. | |
# | |
# the script is run every minute by crond | |
# it exits if the time doesn't match a dictionary entry | |
# if the time does match a dictionary entry, it chimes and | |
# then announces the message | |
belltones = { | |
'warn' : 'bing-bong-chime-hq.mp3', | |
'start' : 'bing-bong-chime-hq.mp3', | |
'end' : 'bing-bong-chime-hq.mp3' | |
} | |
bellschedule = { | |
'09:00' : 'start', # 'Bus' bell to be getting ready | |
'09:28' : 'warn', | |
'09:30' : 'start', # announcements (no end) | |
'09:43' : 'warn', # for period 1 | |
'09:45' : 'start', | |
'10:45' : 'end', | |
'10:58' : 'warn', # for period 2 | |
'11:00' : 'start', | |
'12:00' : 'end', | |
'12:43' : 'warn', # for period 3 | |
'12:45' : 'start', | |
'13:45' : 'end', | |
'13:58' : 'warn', # for period 4 | |
'14:00' : 'start', | |
'15:00' : 'end' | |
} | |
holidays = { | |
'2020-09-07', | |
'2020-09-25', | |
'2020-10-29', | |
'2020-10-30', | |
'2020-11-13', | |
'2020-11-26', | |
'2020-11-27', | |
'2021-01-01', | |
'2021-01-04', | |
'2021-01-18', | |
'2021-02-15', | |
'2021-02-16', | |
'2021-02-17', | |
'2021-03-08', | |
'2021-03-09', | |
'2021-03-10', | |
'2021-03-11', | |
'2021-03-12' | |
} | |
import os | |
import datetime | |
import sys | |
# find filepath where script is running to assure bell files are here | |
path, runfile = os.path.split(os.path.abspath(sys.argv[0])) | |
try: | |
try: # check if a time is being passed and parse it | |
force_dt = datetime.datetime.strptime(sys.argv[1],'%H:%M') | |
filename = None | |
except: # for testing: if you pass a filename, the code will chime it | |
force_dt = None | |
filename = sys.argv[1] | |
except: | |
filename = None | |
if force_dt: | |
dt = force_dt | |
else: | |
dt = datetime.datetime.now() | |
timestr = dt.strftime('%H:%M') | |
datestr = dt.strftime('%Y-%m-%d') | |
if dt.weekday() < 5 and datestr not in holidays: | |
# it is a weekday not a holiday, do chimes | |
print('It is a schoolday, checking time '+timestr) | |
if timestr in bellschedule: | |
filename = belltones[bellschedule[timestr]] | |
else: | |
print('It is a weekend or holiday -- enjoy!') | |
if filename: | |
filepath = os.path.join(path,filename) | |
command = 'mpg321 -a hw:1,0 '+filepath | |
os.system(command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment