Updated to use bashtools

This commit is contained in:
Rob Pearce 2022-07-08 12:49:42 +10:00
parent aba30b49ad
commit 195e12eda8
1 changed files with 27 additions and 38 deletions

View File

@ -1,19 +1,10 @@
#!/bin/bash
BOLD="\033[1m"
ITALIC="\033[3m"
STRIKE="\033[9m"
PLAIN="\033[0m"
UNDERLINE="\033[4m"
RED="\033[31m"
MAGENTA="\033[35m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
CYAN="\033[36m"
GREY="\033[2;37m"
LINK="$BLUE$UNDERLINE"
. ${HOME}/.bashtools/bashtools.sh
if [[ -z $HAVE_BASHTOOLS ]]; then
echo "ERROR: bashtools not installed download from https://git.nethack.net/rob/bashtools" >/dev/stderr
exit 1
fi
IRCC_OFF="AAAAAQAAAAEAAAAvAw=="
IRCC_VOLUP="AAAAAQAAAAEAAAASAw=="
@ -74,7 +65,7 @@ function runcurl() {
[[ $verbose -eq 1 ]] && echo "Result: $resultstr"
[[ $verbose -eq 1 ]] && echo "Error: $errorstr"
if [[ $resultstr == *rror* ]]; then
errorstr="TV returned '$(echo "$resultstr" | grep errorDescription | sed -e 's/<errorDescription>//;s,</error.*,,;s/^\s*//' )'"
errorstr="$HOST returned '$(echo "$resultstr" | grep errorDescription | sed -e 's/<errorDescription>//;s,</error.*,,;s/^\s*//' )'"
rv=1
elif [[ $rv -ne 0 ]]; then
errorstr="curl failed (timeout?) $resultstr"
@ -107,17 +98,6 @@ function usage() {
done
}
function action() {
echo -e "$BOLD$GREEN* $PLAIN$GREEN$*$PLAIN"
}
function error() {
echo -e "$BOLD${RED}ERROR: $PLAIN$RED$*$PLAIN" >/dev/stderr
}
function info() {
echo -e "$BOLD${CYAN}>> $PLAIN$CYAN$*$PLAIN"
}
function register_device() {
local me myid initpsk opts
@ -125,25 +105,26 @@ function register_device() {
myid="12345"
initpsk=$1
if [[ -z $initpsk ]]; then
action "Requesting registration from TV..."
notify "Requesting registration from TV..."
curl -s "$URL/sony/accessControl" -o /dev/null -d @- <<EOF
{"id":13,"method":"actRegister","version":"1.0","params":[{"clientid":"$myid","nickname":"$me"},[{"clientid":"$myid","value":"yes","nickname":"$me","function":"WOL"}]]}
EOF
else
#-u "":${initpsk}
#-H "X-Auth-PSK: ${initpsk}"
action "Registering with TV..."
notify "Registering with TV..."
res=$(curl -s -u "":${initpsk} "$URL/sony/accessControl" -d @- <<EOF
{"id":13,"method":"actRegister","version":"1.0","params":[{"clientid":"$myid","nickname":"$me"},[{"clientid":"$myid","value":"yes","nickname":"$me","function":"WOL"}]]}
EOF
)
if [[ "$res" == *rror* ]]; then
fail
error "registration failed"
else
ok
info "Registration successful! PSK set to ${BOLD}$psk"
echo "$psk" >"$pskfile"
info "PSK written to $pskfile"
fi
fi
@ -278,28 +259,34 @@ fi
sendcount=1
if [[ $cmd == "upnp" ]]; then
info "UPnP info from TV:"
fullurl="$URL:52323/dmr.xml"
curlarg=""
notify "Obtaining UPnP info from $HOST"
res=$(curl $timeout -s "$fullurl")
rv=$?
if [[ $rv -eq 0 ]]; then
if which -s xq; then
res=$(echo "$res" | xq '.root.device."av:X_IRCCCodeList"')
fi
ok
info "Results:"
echo "$res"
else
fail
error "curl to $fullurl failed"
fi
exit $rv
elif [[ $wolok -eq 1 && $cmd == "on" ]]; then
notify "Sending wake-on-lan packet to $HOST"
if [[ $wolok -eq 1 ]]; then
n=0
while [[ $n -lt 3 ]]; do
wakeonlan ${MAC} >/dev/null
n=$((n + 1))
done
ok
else
fail
error "'wakeonlan' binary is not available - please install it"
exit 1
fi
@ -311,9 +298,9 @@ elif [[ $cmd == "channel" ]]; then
cname=${arg[0]}
cnum=$(channametonum $cname)
if [[ $cname == $cnum ]]; then
action "Changing channel to $BOLD$cname"
notify "Changing channel to $BOLD$cname"
else
action "Changing channel to $BOLD$cname$PLAIN$GREEN ($cnum)"
notify "Changing channel to $BOLD$cname$PLAIN$GREEN ($cnum)"
fi
for (( i=0; i<${#cnum}; i++ )); do
@ -322,7 +309,7 @@ elif [[ $cmd == "channel" ]]; then
[[ ! -z $code ]] && runcurl ircc POST "" $code
sleep $IRCC_DELAY
done
info "Done"
ok
elif [[ $cmd == "slowoff" ]]; then
if [[ -z ${arg[0]} ]]; then
waittime=2.0
@ -336,7 +323,7 @@ elif [[ $cmd == "slowoff" ]]; then
fi
# slowly drop volume
code="AAAAAQAAAAEAAAATAw=="
action "Slowly dropping volume on $HOST..."
notify "Slowly dropping volume on $HOST..."
n=0
while [[ $n -lt $count ]]; do
runcurl ircc POST "" $code
@ -350,9 +337,9 @@ elif [[ $cmd == "slowoff" ]]; then
httpmethod="POST"
jqs=""
action "Sending $what to $HOST${ntimes}..."
notify "Sending $what to $HOST${ntimes}..."
runcurl $reqtype $httpmethod "$method" $curlarg
info "Done"
ok
else
reqtype="method"
jqs="try (.results[]) // try (.result[]) // ."
@ -454,15 +441,16 @@ EOF
ntimes=""
fi
err=0
action "Sending $what to $HOST${ntimes}..."
csecho -n "$GREEN" "Sending $what to $HOST${ntimes}... "
allresults=""
while [[ $n -lt $sendcount ]]; do
runcurl $reqtype $httpmethod "$method" $curlarg
rv=$?
if [[ $rv -eq 0 ]]; then
if [[ $sendcount -gt 1 ]]; then
echo -en "${YELLOW}*${BOLD}$((n + 1))${PLAIN}${YELLOW}* ${PLAIN}"
csecho -n "$YELLOW" "*^b$((n + 1))^p* "
else
echo
info "Success"
fi
if [[ ! -z $resultstr ]]; then
@ -473,6 +461,7 @@ EOF
fi
fi
else
fail
error "$rv: $errorstr"
err=1
break