Added SIGHUP signal handling, adjusted reloading of file changes, added return value of CheckTrackList()

This commit is contained in:
Jan
2019-09-26 17:41:05 +02:00
parent 88dce016de
commit c25733b64c
3 changed files with 85 additions and 74 deletions

View File

@@ -1,7 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import pickle import pickle
import os.path import os
import time import time
import sys import sys
import tm1637 import tm1637
@@ -35,13 +35,26 @@ o_defaultbrightness=0
o_loopdelay=0.25 o_loopdelay=0.25
track=0 track=0
stopprogram=0
reload=False
#GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering #GPIO.setmode(GPIO.BCM) # set up BCM GPIO numbering
#GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP) #GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def signal_handler(signal,frame): #def signal_handler(signal,frame):
global quit # global quit
print("") # print("")
quit=1 # quit=1
def receive_signal(signum, stack):
global stopprogram
global reload
print('Received signal: %d' %signum)
if signum == signal.SIGHUP:
reload=True
if signum == signal.SIGINT:
stopprogram=1
def stamp(): def stamp():
sys.stdout.write(strftime("[%Y%m%d %H%M%S] ")) sys.stdout.write(strftime("[%Y%m%d %H%M%S] "))
@@ -52,11 +65,10 @@ def writesettings():
pickle.dump(settings, handle) pickle.dump(settings, handle)
handle.close() handle.close()
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
looptype=0 looptype=0
colon=0 colon=0
count=0
digit=1 digit=1
showtime=0 showtime=0
playtime=0 playtime=0
@@ -67,15 +79,31 @@ tvol=0
volume=0.0 volume=0.0
brightness=0 brightness=0
snooze=0 snooze=0
count=0
print("")
print('My PID is %d' %os.getpid())
signal.signal(signal.SIGHUP, receive_signal)
signal.signal(signal.SIGINT, receive_signal)
but1=button.Button(21)
but2=button.Button(20)
but3=button.Button(26)
#------------------------------------------------------------------
#------------------------------------------------------------------
while not stopprogram:
print("reload config...")
reload=False
signal.signal(signal.SIGINT,signal_handler)
# display=tm1637.TM1637(CLK=14, DIO=4, brightness=1) # display=tm1637.TM1637(CLK=14, DIO=4, brightness=1)
display=max7219.MAX7219(brightness=1) display=max7219.MAX7219(brightness=1)
display.Clear() display.Clear()
display.SetBrightnessRaw(o_defaultbrightness) display.SetBrightnessRaw(o_defaultbrightness)
print("")
print("reading mp3s from directory:") print("reading mp3s from directory:")
tracks=clockytracklist.ClockyTracklist("mp3",recursive=True,verbose=True) tracks=clockytracklist.ClockyTracklist("mp3",recursive=True,verbose=True)
allmp3s=tracks.tracklist allmp3s=tracks.tracklist
@@ -90,12 +118,12 @@ for m in allmp3s:
print(" %2d : %s"%(c+1,m)) print(" %2d : %s"%(c+1,m))
c+=1 c+=1
print("")
but1=button.Button(21)
but2=button.Button(20)
but3=button.Button(26)
print("")
stamp()
print("Started")
sys.stdout.flush()
# .................................................................
if not os.path.isfile(o_configfilename): if not os.path.isfile(o_configfilename):
print("Createconf: \""+o_configfilename+"\":") print("Createconf: \""+o_configfilename+"\":")
@@ -134,13 +162,11 @@ for i in range(0,10):
settings["track%d"%i]=0 settings["track%d"%i]=0
writesettings() writesettings()
print("")
stamp()
print("Started") # .................................................................
sys.stdout.flush()
#------------------------------------------------------------------ while not reload and not stopprogram:
#------------------------------------------------------------------
while quit!=True:
count+=1; count+=1;
sleep(o_loopdelay) sleep(o_loopdelay)
@@ -571,9 +597,9 @@ while quit!=True:
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if count % 40 == 0: if count % 40 == 0: # every 10 seconds
print("#") if tracks.CheckTrackList():
tracks.CheckTrackList() reload = True;
#------------------------------------------------------------------ #------------------------------------------------------------------
if pygame.mixer.get_init(): if pygame.mixer.get_init():

View File

@@ -6,11 +6,13 @@ EXTENSIONS=(".mp3",".ogg",".wav",".mpa",".m4a",".flac")
class ClockyTracklist: class ClockyTracklist:
"""read and monitor directory with audio files""" """read and monitor directory with audio files"""
def __init__(self,path=".",recursive=False,sort=False,verbose=False): def __init__(self,path=".",recursive=False,sort=True,verbose=False):
"""Read (current or supplied) path for all audio files. Add recursive=True to check all subdirectories too""" """Read (current or supplied) path for all audio files. Add recursive=True to check all subdirectories too"""
self.recursive=recursive self.recursive=recursive
self.path=path self.path=path
self.__verbose=verbose self.__verbose=verbose
self.__sort=sort
self.__reloaded=False
p=Path(self.path) p=Path(self.path)
if(not p.is_dir()): if(not p.is_dir()):
@@ -18,8 +20,6 @@ class ClockyTracklist:
exit(); exit();
self.__ReadTrackList() self.__ReadTrackList()
if sort:
self.tracklist.sort()
if(self.__verbose): if(self.__verbose):
self.ShowTrackList() self.ShowTrackList()
@@ -38,6 +38,8 @@ class ClockyTracklist:
if entry.suffix in EXTENSIONS: if entry.suffix in EXTENSIONS:
self.tracklist.append(entry.as_posix()) self.tracklist.append(entry.as_posix())
self.count+=1 self.count+=1
if self.__sort:
self.tracklist.sort()
def ShowTrackList(self): def ShowTrackList(self):
@@ -58,6 +60,9 @@ class ClockyTracklist:
newtracklist=[] newtracklist=[]
newcount=0 newcount=0
if self.__reloaded:
self.__reloaded=False
newpath=Path(self.path) newpath=Path(self.path)
if(self.recursive): if(self.recursive):
selection='**/*' selection='**/*'
@@ -67,7 +72,12 @@ class ClockyTracklist:
if newentry.suffix in EXTENSIONS: if newentry.suffix in EXTENSIONS:
newtracklist.append(newentry.as_posix()) newtracklist.append(newentry.as_posix())
newcount+=1 newcount+=1
if self.__sort:
newtracklist.sort()
if(newcount != self.count) or (newtracklist != self.tracklist): if(newcount != self.count) or (newtracklist != self.tracklist):
self.__ReadTrackList()
if(self.__verbose): if(self.__verbose):
print("Changes in filenames or number of files detected! Re-read tracklist.") print("Changes in filenames or number of files detected! Re-read tracklist.")
self.__ReadTrackList()
self.__reloaded=True
return self.__reloaded

View File

@@ -1,25 +0,0 @@
#!/usr/bin/python3
import time
import clockytracklist
print("-------------------------------------")
print("-------------------------------------")
tl=clockytracklist.ClockyTracklist("mp3",recursive=True,sort=False,verbose=True)
tl.ShowTrackList()
print("-------------------------------------")
for e in tl.tracklist:
print(e);
t=0
while True:
t+=1
tl.CheckTrackList()
time.sleep(1)
if(t>30):
break
print(t)