From e021b723a474d9a6e65af208cd44672f6c3d504b Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 3 Jan 2020 23:42:59 +0100 Subject: [PATCH] Added current volume level --- alexa_remote_control.sh | 71 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/alexa_remote_control.sh b/alexa_remote_control.sh index dd46126..0d61662 100755 --- a/alexa_remote_control.sh +++ b/alexa_remote_control.sh @@ -47,6 +47,7 @@ # 2019-11-18: v0.14a download 200 routines instead of only the first 20 # 2019-12-23: v0.14b Trigger routines by either utterance or routine name # 2019-12-30: v0.15 re-worked the volume setting for TTS commands +# 2020-01-03: v0.15a introduce some proper "get_volume" # ### # @@ -100,6 +101,9 @@ SET_SPEAKVOL="30" # if no current playing volume can be determined, fall back to normal volume SET_NORMALVOL="10" +# max. age in minutes before volume is read from API +SET_VOLMAXAGE="30" + # Device specific volumes (overriding the above) SET_DEVICEVOLNAME="EchoDot2ndGen Echo1stGen" SET_DEVICEVOLSPEAK="100 30" @@ -124,6 +128,7 @@ TMP=${TMP:-$SET_TMP} OATHTOOL=${OATHTOOL:-$SET_OATHTOOL} SPEAKVOL=${SPEAKVOL:-$SET_SPEAKVOL} NORMALVOL=${NORMALVOL:-$SET_NORMALVOL} +VOLMAXAGE=${VOLMAXAGE:-$SET_VOLMAXAGE} DEVICEVOLNAME=${DEVICEVOLNAME:-$SET_DEVICEVOLNAME} DEVICEVOLSPEAK=${DEVICEVOLSPEAK:-$SET_DEVICEVOLSPEAK} DEVICEVOLNORMAL=${DEVICEVOLNORMAL:-$SET_DEVICEVOLNORMAL} @@ -154,13 +159,14 @@ CHILD="" PLIST="" BLUETOOTH="" LASTALEXA="" +GETVOL="" usage() { echo "$0 [-d |ALL] -e > |" echo " -b [list|<\"AA:BB:CC:DD:EE:FF\">] | -q | -r <\"station name\"|stationid> |" echo " -s | -t | -u | -v | -w |" - echo " -i | -p | -P | -S | -a | -m [device_1 .. device_X] | -lastalexa | -l | -h" + echo " -i | -p | -P | -S | -a | -m [device_1 .. device_X] | -lastalexa | -z | -l | -h" echo echo " -e : run command, additional SEQUENCECMDs:" echo " weather,traffic,flashbriefing,goodmorning,singasong,tellstory,speak:'',automation:''" @@ -179,6 +185,7 @@ usage() echo " -a : list available devices" echo " -m : delete multiroom and/or create new multiroom containing devices" echo " -lastalexa : print device that received the last voice command" + echo " -z : print current volume level" echo " -l : logoff" echo " -h : help" } @@ -316,6 +323,9 @@ while [ "$#" -gt 0 ] ; do -lastalexa) LASTALEXA="true" ;; + -z) + GETVOL="true" + ;; -h|-\?|--help) usage exit 0 @@ -622,10 +632,7 @@ if [ -n "${SEQUENCECMD}" ] ; then done # try to retrieve the "currently playing" volume - VOL=$(${CURL} ${OPTS} -s -b ${COOKIE} -A "${BROWSER}" -H "DNT: 1" -H "Connection: keep-alive" -L\ - -H "Content-Type: application/json; charset=UTF-8" -H "Referer: https://alexa.${AMAZON}/spa/index.html" -H "Origin: https://alexa.${AMAZON}"\ - -H "csrf: $(awk "\$0 ~/.${AMAZON}.*csrf[ \\s\\t]+/ {print \$7}" ${COOKIE})" -X GET \ - "https://${ALEXA}/api/media/state?deviceSerialNumber=${DEVICESERIALNUMBER}&deviceType=${DEVICETYPE}" | grep 'volume' | sed -r 's/^.*"volume":\s*([0-9]+)[^0-9]*$/\1/g') + VOL=$(get_volume) if [ -z "${VOL}" ] ; then # get the normal volume of the current device type @@ -825,6 +832,49 @@ show_queue() "https://${ALEXA}/api/np/queue?deviceSerialNumber=${DEVICESERIALNUMBER}&deviceType=${DEVICETYPE}" | jq '.' } +# +# current volume level +# +get_volume() +{ + VOLFILE=$(find "${TMP}/.alexa.volume.${DEVICESERIALNUMBER}" -mmin -${VOLMAXAGE} 2>/dev/null) + if [ -z "${VOLFILE}" ] ; then + # first, try the volume setting of /media/state of the device + VOL=$(${CURL} ${OPTS} -s -b ${COOKIE} -A "${BROWSER}" -H "DNT: 1" -H "Connection: keep-alive" -L\ + -H "Content-Type: application/json; charset=UTF-8" -H "Referer: https://alexa.${AMAZON}/spa/index.html" -H "Origin: https://alexa.${AMAZON}"\ + -H "csrf: $(awk "\$0 ~/.${AMAZON}.*csrf[ \\s\\t]+/ {print \$7}" ${COOKIE})" -X GET \ + "https://${ALEXA}/api/media/state?deviceSerialNumber=${DEVICESERIALNUMBER}&deviceType=${DEVICETYPE}" | jq -r --arg device "${DEVICESERIALNUMBER}" '"\($device) \(.volume) \(.muted)"') + + VOLCHECK=$(echo "${VOL}" | cut -d' ' -f2) + # if we didn't get a proper volume, try the LemurDevice + if [ "$VOLCHECK" = "0" -o "$VOLCHECK" = "null" -o -z "$VOLCHECK" ] ; then + PARENTID=$(jq --arg device "${DEVICE}" -r '.devices[] | select(.accountName == $device) | .parentClusters[0]' ${DEVLIST}) + if [ "$PARENTID" != "null" ] ; then + PARENTDEVICE=$(jq --arg serial ${PARENTID} -r '.devices[] | select(.serialNumber == $serial) | .deviceType' ${DEVLIST}) + + VOL=$( ${CURL} ${OPTS} -s -b ${COOKIE} -A "${BROWSER}" -H "DNT: 1" -H "Connection: keep-alive" -L\ + -H "Content-Type: application/json; charset=UTF-8" -H "Referer: https://alexa.${AMAZON}/spa/index.html" -H "Origin: https://alexa.${AMAZON}"\ + -H "csrf: $(awk "\$0 ~/.${AMAZON}.*csrf[ \\s\\t]+/ {print \$7}" ${COOKIE})" -X GET \ + "https://${ALEXA}/api/np/player?deviceSerialNumber=${PARENTID}&deviceType=${PARENTDEVICE}" | jq -r '.playerInfo.lemurVolume.memberVolume | to_entries[] | "\(.key) \(.value.volume) \(.value.muted)"') + fi + fi + + # write volume and mute state to file + OIFS=$IFS + IFS=' +' + set -o noglob + for LINE in $VOL ; do + SERIAL=$(echo "${LINE}" | cut -d' ' -f1) + VOLUME=$(echo "${LINE}" | cut -d' ' -f2) + MUTED=$(echo "${LINE}" | cut -d' ' -f3) + echo "${VOLUME} ${MUTED}" > "${TMP}/.alexa.volume.${SERIAL}" + done + IFS=$OIFS + fi + cut -d' ' -f1 "${TMP}/.alexa.volume.${DEVICESERIALNUMBER}" +} + # # deletes a multiroom device # @@ -912,9 +962,10 @@ ${CURL} ${OPTS} -s -c ${COOKIE} -b ${COOKIE} -A "${BROWSER}" -H "DNT: 1" -H "Con rm -f ${DEVLIST} rm -f ${COOKIE} rm -f ${TMP}/.alexa.*.list +rm -f ${TMP}/.alexa.volume.* } -if [ -z "$LASTALEXA" -a -z "$BLUETOOTH" -a -z "$LEMUR" -a -z "$PLIST" -a -z "$HIST" -a -z "$SEEDID" -a -z "$ASIN" -a -z "$PRIME" -a -z "$TYPE" -a -z "$QUEUE" -a -z "$LIST" -a -z "$COMMAND" -a -z "$STATIONID" -a -z "$SONG" -a -n "$LOGOFF" ] ; then +if [ -z "$LASTALEXA" -a -z "$BLUETOOTH" -a -z "$LEMUR" -a -z "$PLIST" -a -z "$HIST" -a -z "$SEEDID" -a -z "$ASIN" -a -z "$PRIME" -a -z "$TYPE" -a -z "$QUEUE" -a -z "$LIST" -a -z "$COMMAND" -a -z "$STATIONID" -a -z "$SONG" -a -z "$GETVOL" -a -n "$LOGOFF" ] ; then echo "only logout option present, logging off ..." log_off exit 0 @@ -945,7 +996,7 @@ if [ ! -f ${DEVLIST} ] ; then fi fi -if [ -n "$COMMAND" -o -n "$QUEUE" ] ; then +if [ -n "$COMMAND" -o -n "$QUEUE" -o -n "$GETVOL" ] ; then if [ "${DEVICE}" = "ALL" ] ; then for DEVICE in $( jq -r '.devices[] | select( ( .deviceFamily == "ECHO" or .deviceFamily == "KNIGHT" or .deviceFamily == "ROOK" or .deviceFamily == "WHA" ) and .online == true ) | .accountName' ${DEVLIST} | sed -r 's/ /%20/g') ; do set_var @@ -955,6 +1006,9 @@ if [ -n "$COMMAND" -o -n "$QUEUE" ] ; then # in order to prevent a "Rate exceeded" we need to delay the command sleep 1 echo + elif [ -n "$GETVOL" ] ; then + echo "get volume for dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER}" + get_volume else echo "queue info for dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER}" show_queue @@ -967,6 +1021,9 @@ if [ -n "$COMMAND" -o -n "$QUEUE" ] ; then echo "sending cmd:${COMMAND} to dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER} customerid:${MEDIAOWNERCUSTOMERID}" run_cmd echo + elif [ -n "$GETVOL" ] ; then + echo "get volume for dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER}" + get_volume else echo "queue info for dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER}" show_queue