Thanks a lot for the work! I updated script for shell usage (not HAss.IO component) form post 41 (I guess)…
I had two issues:
- no “jq” - didnt want to install anything extra, so I updated the scripts using “sed”, which should be available without extra installation
- I didnt see the need for the “playlist_name”, so I generated the name out of the MAC-address, which would need to be provided to make all work.
If somebody is interested, here we go:
#!/bin/bash
#squeezebox_alert_save_playlist.sh
#NOTE: Edit "user", "pass", ip and port below
#JSONRPC="http://user:[email protected]:9000/jsonrpc.js"
JSONRPC="http://192.168.20.201:9001/jsonrpc.js"
#Example command arguments
#mac="00:04:20:01:02:03"
#alert_volume=60
mac=$1
alert_volume=$2
playlist_name=$(echo "$1" | sed 's/\://g')
#get power state
power=$(curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["power","?"]]}' \
$JSONRPC | sed -n 's/.*"_power"\:"\([^"]*\)".*/\1/p')
prev_power=0
if [[ $power =~ .*1.* ]] ; then
prev_power=1
fi
echo "prev_power=$prev_power"
#get play mode
mode=$(curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mode","?"]]}' \
$JSONRPC | sed -n 's/.*"_mode"\:"\([^"]*\)".*/\1/p')
prev_playmode=0
if [[ $mode =~ .*play.* ]] ; then
prev_playmode=1
fi
echo "prev_playmode=$prev_playmode"
noplay=1
prev_time=0
if [ $prev_playmode -eq 1 ] ; then
noplay=0
# pause currently playing song
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["pause"]]}' $JSONRPC
echo "pause currently playing song"
# get paused time
prev_time=$(curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["time","?"]]}' \
$JSONRPC | sed -n 's/.*"_time"\:\([0-9\.]*\).*/\1/p')
echo "prev_time=$prev_time"
fi
# save current playlist
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","save","'"$playlist_name"'","silent:1"]]}' $JSONRPC
echo "save current playlist"
# GET SETTINGS TO RESTORE AFTER PLAYING ALERT SONG
#get current volume
prev_volume=$(curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mixer","volume","?"]]}' \
$JSONRPC | sed -n 's/.*"_volume"\:"\([^"]*\)".*/\1/p')
echo "prev_volume=$prev_volume"
#get current repeat setting
prev_repeat=$(curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","repeat","?"]]}' \
$JSONRPC | sed -n 's/.*"_repeat"\:"\([^"]*\)".*/\1/p')
echo "prev_repeat=$prev_repeat"
#write settings to file
DIRECTORY=$(cd `dirname $0` && pwd)
echo "Save settings to file: $DIRECTORY/$playlist_name.txt"
> "$DIRECTORY/$playlist_name.txt"
printf '%s\n%s\n%s\n%s\n%s\n' $prev_power $prev_playmode $prev_time $prev_volume $prev_repeat > "$DIRECTORY//$playlist_name.txt"
# SET SETTINGS FOR ALERT SONG
#set alert_volume to command argument value
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mixer","volume",'$alert_volume']]}' $JSONRPC
echo "set alert_volume"
#set repeat setting to 0
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","repeat",0]]}' $JSONRPC
echo "set repeat_setting to 0"
And the restore part:
#!/bin/bash
#squeezebox_alert_restore_playlist.sh
#NOTE: Edit "user", "pass", ip and port below
#JSONRPC="http://user:[email protected]:9000/jsonrpc.js"
JSONRPC="http://192.168.20.201:9001/jsonrpc.js"
#Example command arguments
#mac="00:04:20:01:02:03"
mac=$1
playlist_name=$(echo "$1" | sed 's/\://g')
# WAIT FOR ALERT SONG TO STOP PLAYING
echo "wait for alert song to stop playing"
cur_mode="play"
while [[ $cur_mode =~ .*play.* ]]; do
sleep 1
cur_mode=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mode","?"]]}' \
$JSONRPC | sed -n 's/.*"_mode"\:"\([^"]*\)".*/\1/p')
done
echo "alert song stopped playing"
# read settings from file
DIRECTORY=$(cd `dirname $0` && pwd)
echo "Restore settings from file: $DIRECTORY/$playlist_name.txt"
IFS=$'\n' read -d '' -r -a lines < "$DIRECTORY/$playlist_name.txt"
prev_power="${lines[0]}"
prev_playmode="${lines[1]}"
prev_time="${lines[2]}"
prev_volume="${lines[3]}"
prev_repeat="${lines[4]}"
echo "prev_power=$prev_power"
echo "prev_playmode=$prev_playmode"
echo "prev_time=$prev_time"
echo "prev_volume=$prev_volume"
echo "prev_repeat=$prev_repeat"
noplay=1
if [ $prev_playmode -eq 1 ] ; then
noplay=0
fi
# RESTORE PREVIOUS SETTINGS
#restore prev_volume setting
if [ ! -z $prev_volume ] ; then
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mixer","volume",'"$prev_volume"']]}' $JSONRPC
echo "restore prev_volume setting"
fi
#restore prev_repeat setting
if [ ! -z $prev_repeat ] ; then
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","repeat",'"$prev_repeat"']]}' $JSONRPC
echo "restore prev_repeat setting"
fi
# resume previous playlist
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","resume","'"$playlist_name"'","noplay:'$noplay'"]]}' $JSONRPC
echo "resume previous playlist"
# RESUME PREVIOUSLY PLAYING MUSIC
if [ $prev_playmode -eq 1 ] ; then
if [ ! -z $prev_time ] ; then
#skip ahead in song to prev_time
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["time",'"$prev_time"']]}' $JSONRPC
echo "skip ahead in song to prev_time"
fi
fi
#restore prev_power setting
if [ $prev_power -eq 0 ] ; then
echo "prev_power setting was off, power off"
curl -X POST -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["power",0]]}' $JSONRPC
fi