blocker.py 882 B

123456789101112131415161718192021222324
  1. from subprocess import run
  2. import sys
  3. import time
  4. def main():
  5. # make sure there is a default bluetooth controller
  6. while True:
  7. try:
  8. # listing might return okay when no device
  9. run(["/usr/bin/bluetoothctl", "list"], check=True)
  10. # show returns negatively when "No default controller available"
  11. run(["/usr/bin/bluetoothctl", "show"], check=True)
  12. print("bluetooth controller is available, terminating\n")
  13. break
  14. except:
  15. print("bluetooth controller not available, replugging USB ports [2..5]\n")
  16. # Rpi PPPS 1-1 is internal hub, port 2 is external ports [2..5], action 2 is cycle
  17. run(["/sbin/uhubctl", "--location=1-1", "--port=2", "--action=2"])
  18. time.sleep(15)
  19. continue
  20. return 0
  21. if(__name__ == "__main__"):
  22. sys.exit(main())