import RPi.GPIO as GPIO
import time
import urllib
import os

tv_usb = 5

GPIO.setmode(GPIO.BCM)
GPIO.setup(tv_usb, GPIO.IN, GPIO.PUD_DOWN)
GPIO.setwarnings(False)


previous_state = False
current_state = False

try:
  while True:
    time.sleep(0.1)
    previous_state = current_state
    current_state = GPIO.input(tv_usb)
    if current_state != previous_state:
      new_state = "HIGH" if current_state else "LOW"
      print("GPIO pin %s is %s" % (tv_usb, new_state))
      if new_state == "LOW":
        os.system("sudo service hyperion restart")
      else:
        os.system("sudo killall hyperiond")
        
finally:
  GPIO.cleanup();