#!/bin/bash # See https://JustinNelsonsProjects.com/hyperion/step6.php for help # User under which the 'hyperion' service runs: HUSER='justin'; # Type of TV: # Note: for Sony you must install netcat: # sudo apt install netcat # # 1 = Sony, 2 = VIZIO, 0 = OTHER TVTYPE=2; # IP Address of your TV: TVIP='192.168.0.136'; # Port used for communication # Sony BRAVIA is typically 20060 # Older VIZIO: 9000; Newer VIZIO: 7345 # For "other", Port doesn't matter TVPORT='7345'; # Do Not Mess with the code below unless you know what you # are doing! ########################################################## if [ -f ~/tv.lock ]; then DIFF=$(($(date +%s) - $(date -r ~/tv.lock +%s))); if [ $DIFF -lt 55 ]; then echo 'checktv may Already be Running - Lock File Found only' $DIFF 'Seconds Old'; exit 0; else echo 'Removing stale lock file (possible hung process)'; rm ~/tv.lock && killall -KILL checktv fi fi touch ~/tv.lock if [ "$TVTYPE" == "1" ]; then # For Sony Bravia TVs ('netcat' must be installed): if echo "*SEPOWR################" | netcat -W 1 $TVIP $TVPORT 2>&1 |grep \ -Eq '.*\*SAPOWR0000000000000001.*'; then RESULT=1; fi elif [ "$TVTYPE" == "2" ]; then # For VIZIO E-Series TVs ('curl' must be installed): if curl --connect-timeout 9 -k -H "Content-Type: application/json" -H "AUTH: 123A456B" \ -X GET https://$TVIP:$TVPORT/state/device/power_mode 2>&1 |grep -Eq '.*"VALUE": 1.*'; then RESULT=1; fi else # For SAMSUNG TVs (and many others), this works if the WiFi turns off # when the TV is off - only requires the 'ping' command: if ping $TVIP -c 1 -W 3 >/dev/null 2>&1; then RESULT=1; fi fi if [ "$RESULT" == "1" ]; then if ! pidof hyperhdr >/dev/null 2>&1; then echo "TV is ON, Starting HyperHDR..." sudo /usr/bin/systemctl start hyperhdr@$HUSER.service >/dev/null 2>&1 else echo "TV is ON, HyperHDR is Running" fi else if pidof hyperhdr >/dev/null 2>&1; then echo "TV is OFF, Stopping HyperHDR..." sudo /usr/bin/systemctl stop hyperhdr@$HUSER.service >/dev/null 2>&1 else echo "TV is OFF, HyperHDR is Not Running" fi fi rm ~/tv.lock exit 0