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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. import sys
  11. class CheckHTTP:
  12. def __init__(self, u, r = 600.0):
  13. self.url = u
  14. self.refresh = r
  15. self.successScreen = None
  16. self.failScreen = None
  17. self.response = None
  18. self.get = util.getRequests()
  19. self.restart()
  20. def success(self, s):
  21. self.successScreen = s
  22. def fail(self, f):
  23. self.failScreen = f
  24. # Compatibility to Manager button events
  25. def child_count(self, i):
  26. self.request()
  27. if self.response:
  28. if hasattr(self.successScreen, "child_count"):
  29. return self.successScreen.child_count(i)
  30. else:
  31. if hasattr(self.failScreen, "child_count"):
  32. return self.failScreen.child_count(i)
  33. # Compatibility to Manager button events
  34. def switch_to(self, i, update_flag):
  35. self.request()
  36. if self.response:
  37. if hasattr(self.successScreen, "switch_to"):
  38. self.successScreen.switch_to(i, update_flag)
  39. else:
  40. if hasattr(self.failScreen, "switch_to"):
  41. self.failScreen.switch_to(i, update_flag)
  42. def restart(self):
  43. self.start = time.time()
  44. # when set to None here, manager will cause re-request every time
  45. # we don't do it, caching the response for the full refresh time.
  46. # this assumes the URL never changes, of course.
  47. #self.response = None
  48. self.request()
  49. if self.successScreen != None:
  50. self.successScreen.restart()
  51. if self.failScreen != None:
  52. self.failScreen.restart()
  53. def request(self):
  54. if self.get == None:
  55. return
  56. now = time.time()
  57. if (self.response == None) or ((now - self.start) >= self.refresh) or (now < self.start):
  58. self.start = now
  59. try:
  60. print("Refreshing " + self.url)
  61. r = self.get(self.url)
  62. r.close()
  63. print("Response: " + str(r.status_code))
  64. self.response = (r.status_code < 400)
  65. except Exception as e:
  66. print()
  67. if hasattr(sys, "print_exception"):
  68. sys.print_exception(e)
  69. else:
  70. print(e)
  71. print()
  72. self.response = False
  73. def finished(self):
  74. if self.get == None:
  75. return True
  76. self.request()
  77. if self.response:
  78. return self.successScreen.finished()
  79. else:
  80. return self.failScreen.finished()
  81. def draw(self):
  82. if self.get == None:
  83. return
  84. self.request()
  85. if self.response:
  86. self.successScreen.draw()
  87. else:
  88. self.failScreen.draw()
  89. if __name__ == "__main__":
  90. from solid import Solid
  91. import util
  92. t = util.getTarget()
  93. # show splash screen while connecting to WiFi on Pico
  94. from splash import SplashScreen
  95. splash = SplashScreen(t)
  96. t.loop_start()
  97. splash.draw()
  98. t.loop_end()
  99. d = CheckHTTP("http://xythobuz.de")
  100. d.success(Solid(t, 1.0, (0, 255, 0)))
  101. d.fail(Solid(t, 1.0, (255, 0, 0)))
  102. util.loop(t, d.draw)