Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "Resume playlist" functionality and removed unused play func #30

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 72 additions & 31 deletions scripts/rfid_trigger_play.sh.sample
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ elif [ "$CARDID" == "$CMDPAUSE" ]
then
# pause current track
echo "pause" | nc.openbsd -w 1 localhost 4212
elif [ "$CARDID" == "$CMDNEXT" ]
then
# play / resume current track
echo "play" | nc.openbsd -w 1 localhost 4212
else
# We checked if the card was a special command, seems it wasn't.
# Now we expect it to be a trigger for one or more audio file(s).
Expand All @@ -142,9 +138,11 @@ else
#
# $PATHDATA/../shared/audiofolders/webradio/filewithURL.txt

# Add info into the log, making it easer to monitor cards
echo "Card ID '$CARDID' was used at '$NOW'." > $PATHDATA/../shared/latestID.txt

# Get last CardID from the file (decision to continue or restart playlist)
PLAYEDLAST=`grep -Po 'Card ID \K[^ ]+' $PATHDATA/../shared/latestID.txt`

# Add info into the log, making it easer to monitor cards
echo "Card ID $CARDID was used at '$NOW'." > $PATHDATA/../shared/latestID.txt
# Look for human readable shortcut in folder 'shortcuts'
# check if CARDID has a text file by the same name - which would contain the human readable folder name
if [ -f $PATHDATA/../shared/shortcuts/$CARDID ]
Expand All @@ -167,28 +165,71 @@ else
# if a folder $FOLDERNAME exists, play content
if [ -d $PATHDATA/../shared/audiofolders/$FOLDERNAME ]
then
# create an empty string for the playlist
PLAYLIST=""

# loop through all the files found in the folder
for FILE in $PATHDATA/../shared/audiofolders/$FOLDERNAME/*
do
# add file path to playlist followed by line break
PLAYLIST=$PLAYLIST$FILE$'\n'
done

# write playlist to file using the same name as the folder with ending .m3u
# wrap $PLAYLIST string in "" to keep line breaks
echo "$PLAYLIST" > $PATHDATA/../playlists/$FOLDERNAME.m3u

# first kill any possible running vlc processn => stop playing audio
sudo pkill vlc

# now start the command line version of vlc loading the playlist
# start as a background process (command &) - otherwise the input only works once the playlist finished
#(cvlc $PATHDATA/../playlists/$FOLDERNAME.m3u &)
(cvlc -I rc --rc-host localhost:4212 $PATHDATA/../playlists/$FOLDERNAME.m3u &)


fi
# Get modification dates of the file and folder
# The comparedate gets the last modified time of the log file to see if the card was used in the last 10 seconds
# This will reset the playlist only when the card is used twice in 10 seconds. Using stat to get the same format easily
FILEDATE=`stat -c %Y $PATHDATA/../playlists/$FOLDERNAME.m3u`
FOLDERDATE=`stat -c %Y $PATHDATA/../shared/audiofolders/$FOLDERNAME`
COMPAREDATE=`stat -c %Y $PATHDATA/../shared/latestID.txt`
LASTRUNTIME=`stat -c %Y $PATHDATA/../playlists/$FOLDERNAME.track`
TIMESINCELASTRUN=`expr $COMPAREDATE - $LASTRUNTIME`

# first kill any possible running vlc processn => stop playing audio
# Also kill the playstate tracker if its running
(sudo ps -ef | grep vlc | awk '{print $2}' | xargs kill -9)
(sudo ps -ef | grep track_playstate.sh | awk '{print $2}' | xargs kill -9)
sleep 0.5s
# Check if the folder wasnt updated and if the folder was played last
# Positive test will resume the folder at the last played time
if [ $PLAYEDLAST != $CARDID ] && [ $FILEDATE -gt $FOLDERDATE ] && [ $TIMESINCELASTRUN > 5 ]
then
# Get last played track and elapsed time from file.
LASTPLAYTIME=`sed -n 2p $PATHDATA/../playlists/$FOLDERNAME.track | cut -c 8-`
LASTTRACK=`sed -n 1p $PATHDATA/../playlists/$FOLDERNAME.track | cut -c 9-`

# Get the playlist index of where the song is. This is usually 1 for a normal playlist and something else for shuffled
TEMPINDEX=`grep -Fn "$LASTTRACK" $PATHDATA/../playlists/$FOLDERNAME.m3u | cut -d : -f 1`

# Playlists are offset by 4, means: The first entry has index 5. Because, logic.
TRACKINDEX=`expr $TEMPINDEX + 4`

# now start the command line version of vlc loading the playlist
# start as a background process (command &) - otherwise the input only works once the playlist finished
#(cvlc $PATHDATA/../playlists/$FOLDERNAME.m3u &)
(cvlc -I rc --rc-host localhost:4212 $PATHDATA/../playlists/$FOLDERNAME.m3u &)

# Sleep to let the service start
sleep 0.5s
# Jump to last track and playtime
(echo goto $TRACKINDEX | nc.openbsd -w 1 localhost 4212)
(echo seek $LASTPLAYTIME | nc.openbsd -w 1 localhost 4212)
(bash $PATHDATA/../scripts/track_playstate.sh $FOLDERNAME &)
else
# Negative test means that the same folder is played twice within the last 5 seconds (this will reset progress)
# This is also triggered if the folder was changed (eg. new files have been added) or if the folder is new

# Remove a previous track state
(sudo rm $PATHDATA/../playlists/$FOLDERNAME.track)

# create an empty string for the playlist
PLAYLIST=""
# loop through all the files found in the folder
for FILE in $PATHDATA/../shared/audiofolders/$FOLDERNAME/*
do
# add file path to playlist followed by line break
PLAYLIST=$PLAYLIST$FILE$'\n'
done

# write playlist to file using the same name as the folder with ending .m3u
# wrap $PLAYLIST string in "" to keep line breaks
echo "$PLAYLIST" > $PATHDATA/../playlists/$FOLDERNAME.m3u

# now start the command line version of vlc loading the playlist
# start as a background process (command &) - otherwise the input only works once the playlist finished
#(cvlc $PATHDATA/../playlists/$FOLDERNAME.m3u &)

(cvlc -I rc --rc-host localhost:4212 $PATHDATA/../playlists/$FOLDERNAME.m3u &)
(bash $PATHDATA/../scripts/track_playstate.sh $FOLDERNAME &)
fi
fi
fi
48 changes: 48 additions & 0 deletions scripts/track_playstate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# Polls vlc for track played and time lapsed.
# Removes played items from the playlist

# Definitions
PATHDATA="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
CURRENTTRACKFILE=$PATHDATA/../playlists/$1.track
CURRENTPLAYLIST=$PATHDATA/../playlists/$1.m3u


# While VLC is running. If VLC ends (either by stop or end of playlist) tracking will also stop

while pgrep vlc >/dev/null; do


# Get last entry in the currenttrack from file to remove it from the playlist if its done playing
PREVIOUSTRACK=`sed -n 1p $CURRENTTRACKFILE | cut -c 9-`

# send status request to host and cut it out.
CURRENTTRACK=`echo status | nc.openbsd -w 1 localhost 4212 | sed -n 3p | cut -c 23- | rev | cut -c 4- | rev`

# Same for time: send time request to host and cut it so only the actual time is left
CURRENTTIME=`echo get_time | nc.openbsd -w 1 localhost 4212 | sed -n 3p | cut -c 3-`

# Check if PREVIOUSTRACK is empty (first time playlist is running) and fill it with current if so
if [ ! $PREVIOUSTRACK]
then
PREVIOUSTRACK=$CURRENTTRACK
fi

# Write track and time info to the playlist folder into a file named after the folder
echo 'Track :' $CURRENTTRACK > $CURRENTTRACKFILE
echo 'Time :' $CURRENTTIME >> $CURRENTTRACKFILE

# If the track changed (previous is not the same as current), delete the previous entry from the playlist

if [ "$CURRENTTRACK" != "$PREVIOUSTRACK" ]
then
TODELETE=`grep -Fn "$PREVIOUSTRACK" $CURRENTPLAYLIST | cut -c 1`d
`sudo sed -i $TODELETE $CURRENTPLAYLIST`
fi

# Do this every 5 seconds. Can be tuned but will lower accuracy
sleep 5s

done