Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

net.py 3.7KB

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