2015年4月21日 星期二

Detect Xbox Status and Turn Transmission ON/OFF

This is a small tutorial on bash scripts using ping and if to detect if Xbox is Online/Offline and consequently turn Transmission OFF/ON.

I have a Qnap NAS running Transmission 24/7. Even with QoS enabled on my router, playing Titanfall on my Xbox One is still extremely laggy. Sometimes even stuck on loading screen.
To solve my problem, I would need a service to disable Transmission when I play Xbox, and enable it when I'm not playing. The easiest way I could think of is to constantly ping the IP of my Xbox and react accordingly.
Traditional Ping input/output would look like:


yoshimis-MBP:~ bladepopper$ ping -c 3 192.168.1.1
PING 192.168.1.1 (192.168.1.1): 56 data bytes
64 bytes from 192.168.1.1: icmp_seq=0 ttl=64 time=4.628 ms
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=4.456 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=4.566 ms
--- 192.168.1.1 ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 4.456/4.550/4.628/0.071 ms
Too much garbage information, so I start extracting only the ones I will need: The packets that are received. Using the 'grep' command I will be filter only the line with "received" as my output:


1
2
yoshimis-MBP:~ bladepopper$ ping -c 3 192.168.1.1 | grep received
3 packets transmitted, 3 packets received, 0.0% packet loss

Much better but still slightly cluttered with unnecessary information. To automate my task, it is bet to use a single integer as my input.
yoshimis-MBP:~ bladepopper$ ping -c 3 192.168.1.1 | grep received | awk -F',' '{ print $2 }' | awk '{ print $1 }'
3

My ultimate goal is extremely simple:
- Disable Transmission when I am playing Xbox
- Enable Transmission when I am not playing Xbox
However I find that there are rare cases when I want to download a huge game overnight and want Transmission to operate during those hours. So I added a second element to my logic: The TV.
By defining "playing Xbox" as both Xbox AND TV must ON, and everything else defied as "not playing Xbox", I can now design my pseudocode as:

if (Transmission is ENABLED)
  if (TV && Xbox = ON)
    Disable Transmission
else
  if (TV && Xbox = ON)
    Keep Transmission Disabled
  else
    Enable Transmission

Transforming my pseudocode into actual code, my working script now looks like this:

#!/bin/sh

XBOX="192.168.1.87"
TV="192.168.1.86"
COUNT=1

countxb=$(ping -c $COUNT $XBOX | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
counttv=$(ping -c $COUNT $TV | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
echo $countxb
echo $counttv

if ps -l | grep transmission-daemon | grep -v grep; then
  echo 'Transmission is Online';
  if [ $countxb -gt 0 ] && [ $counttv -gt 0 ]; then
    echo "Xbox and TV Online, Deactivating Transmission";
    /share/HDA_DATA/.qpkg/Transmission/transmission.sh stop
  fi
else
  echo 'Transmission is Offline'
  if [ $countxb -gt 0 ] && [ $counttv -gt 0 ]; then
    echo "Xbox and TV Online, Keeping Transmission Offline";
  else
    echo "XBox or TV Online, Activating Transmission";
    /share/HDA_DATA/.qpkg/Transmission/transmission.sh start
  fi
fi
I added this to my NAS cron job and set it to check it every minute.
So far, works like a charm!

沒有留言:

張貼留言