Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. # ----------------------------------------------------------------------------
  3. # "THE BEER-WARE LICENSE" (Revision 42):
  4. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  5. # you can do whatever you want with this stuff. If we meet some day, and you
  6. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  7. # ----------------------------------------------------------------------------
  8. import time
  9. import util
  10. class CheckHTTP:
  11. def __init__(self, u, r = 600.0):
  12. self.url = u
  13. self.refresh = r
  14. self.successScreen = None
  15. self.failScreen = None
  16. self.get = util.getRequests()
  17. self.restart()
  18. def success(self, s):
  19. self.successScreen = s
  20. def fail(self, f):
  21. self.failScreen = f
  22. def restart(self):
  23. self.start = time.time()
  24. self.response = None
  25. self.request()
  26. if self.successScreen != None:
  27. self.successScreen.restart()
  28. if self.failScreen != None:
  29. self.failScreen.restart()
  30. def request(self):
  31. if self.get == None:
  32. return
  33. if (self.response == None) or ((time.time() - self.start) >= self.refresh):
  34. self.start = time.time()
  35. try:
  36. r = self.get(self.url)
  37. self.response = (r.status_code < 400)
  38. except:
  39. self.response = False
  40. def finished(self):
  41. if self.get == None:
  42. return True
  43. self.request()
  44. if self.response:
  45. return self.successScreen.finished()
  46. else:
  47. return self.failScreen.finished()
  48. def draw(self):
  49. if self.get == None:
  50. return
  51. self.request()
  52. if self.response:
  53. self.successScreen.draw()
  54. else:
  55. self.failScreen.draw()
  56. if __name__ == "__main__":
  57. from draw import ScrollText
  58. import util
  59. t = util.getTarget()
  60. d = CheckHTTP("http://xythobuz.de")
  61. d.success(ScrollText(t, "Success", "ib8x8u"))
  62. d.fail(ScrollText(t, "Failure", "ib8x8u"))
  63. t.loop(d.draw)