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