-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.py
86 lines (75 loc) · 3.38 KB
/
player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# player.py - player module using JSON schedule file generated by scraper.py to determine which audio file to play
import subprocess
import datetime as dt
import time
import RPi.GPIO as GPIO
import shared as sh
import logging
audio_dir = 'audio/'
json_dir = 'json/'
schedule_dict = {}
switch_pin = 18
play_status = False
logging.basicConfig(filename='logs/python.log', format='%(asctime)s %(levelname)s-%(module)s-%(funcName)s: %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)
def find_audio_file(schedule_dict):
# Find the right audio file to play
play_file_index = min(schedule_dict, key = time_diff_past_only)
play_file = schedule_dict[play_file_index]['PID']+'.m4a'
return play_file_index, play_file
def find_start_time(schedule_dict, play_file_index):
# Find the time at which to start playing the audio file
start_time = dt.datetime.today()-schedule_dict[play_file_index]['START_TIME']
hours, minutes, seconds = convert_timedelta(start_time)
start_time_str = "%02d" % (hours)+':'+ "%02d" % (minutes)+':'+ "%02d" % (seconds)
return start_time_str
def time_diff_past_only(i):
# Key function to return time difference between a past START_TIME and now (future START_TIMEs effectively ignored by maxing them)
if schedule_dict[i]['START_TIME']<dt.datetime.today():
return dt.datetime.today()-schedule_dict[i]['START_TIME']
else:
return dt.timedelta.max
def convert_timedelta(duration):
days, seconds = duration.days, duration.seconds
hours = days * 24 + (seconds // 3600)
minutes = (seconds % 3600) // 60
seconds = (seconds % 60)
return hours, minutes, seconds
def list_programs(schedule_dict):
# FOR DEBUGGING: list all progs in schedule_dict to check correct file is being played
i = 1
while i <= len(schedule_dict):
print(i, schedule_dict[str(i)]['NAME'],schedule_dict[str(i)]['START_TIME'],schedule_dict[str(i)]['PID'])
i += 1
def radio_play(play_status, json_dir, audio_dir):
global schedule_dict
if play_status == False:
# list_programs(schedule_dict)
year, month, day = sh.set_date()
logging.debug("Current date set")
raw_schedule_dict = sh.load_json(year, month, day, json_dir)
logging.debug("JSON imported")
schedule_dict = sh.convert_dict_dates(raw_schedule_dict)
logging.debug("Dict times converted: %s records", str(len(schedule_dict)))
play_file_index, play_file = find_audio_file(schedule_dict)
logging.debug("Found file to play: %s", play_file)
start_time_str = find_start_time(schedule_dict, play_file_index)
logging.debug("Found start time: %s", start_time_str)
popen = subprocess.Popen(['omxplayer', '-o', 'local', audio_dir+play_file, '--pos='+start_time_str], stdout=subprocess.PIPE, universal_newlines=True)
play_status = True
logging.info("Started playing")
time.sleep(0.25)
return play_status
else:
subprocess.call(['killall', 'omxplayer.bin'])
play_status = False
logging.info("Stopped playing")
time.sleep(0.25)
return play_status
# Main -->
GPIO.setmode(GPIO.BCM)
GPIO.setup(switch_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
logging.info("Player running, waiting for button press")
while True:
GPIO.wait_for_edge(switch_pin, GPIO.FALLING)
play_status = radio_play(play_status, json_dir, audio_dir)