Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.70 KB | None | 0 0
  1. from PIL import Image
  2. from PIL import ImageFont
  3. from PIL import ImageDraw
  4. from sense_hat import SenseHat
  5. import threading
  6. import datetime
  7. import picamera
  8. import socket
  9. import time
  10. import os
  11. import urllib3
  12. import sys
  13. #sys.exit()
  14.  
  15. http = urllib3.PoolManager()
  16.  
  17. HOST = "weather.parginli.ch"
  18. PORT = 5000
  19. CHUNK_SIZE = 8192
  20. bufDataPlan = 4
  21. currDataPlan = 4
  22. brightness = 2
  23. menu = False
  24.  
  25. numImagesToday = 0
  26. # 0: 3x/d (8:00, 12:00, 16:00)
  27. # 1: every 2 hours (7:00-19:00)
  28. # 2: every hour (6:00-21:00)
  29. # 3: every 30 minutes (6:00-21:00)
  30. # 4: (with 150MB/month) every 5 minutes (6:00-21:00)
  31. # 4: (for testing) every 30 seconds (anytime)
  32. imageIntervals = [14400, 7200, 3600, 1800, 300, 30]
  33. imageSizes = [100, 40, 20, 10, 25, 10]
  34.  
  35. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  36.  
  37. ##camera = picamera.PiCamera()
  38. ##camera.resolution = (1080, 720)
  39. sense = SenseHat()
  40. lasttime = int(round(time.time() * 1000)) - 60000
  41.  
  42. r = [255, 0, 0]
  43. g = [0, 255, 0]
  44. o = [255, 127, 0]
  45. w = [0, 0, 0]
  46. v = [255, 255, 255]
  47. wifi = [
  48. w,w,w,w,w,w,w,r,
  49. w,w,w,w,w,w,r,w,
  50. w,v,v,v,v,r,v,w,
  51. v,w,w,w,r,w,w,v,
  52. w,w,v,r,v,v,w,w,
  53. w,v,r,w,w,w,v,w,
  54. w,r,w,v,v,w,w,w,
  55. r,w,w,v,v,w,w,w
  56. ]
  57. cross = [
  58. r,w,w,w,w,w,w,r,
  59. w,r,w,w,w,w,r,w,
  60. w,w,r,w,w,r,w,w,
  61. w,w,w,r,r,w,w,w,
  62. w,w,w,r,r,w,w,w,
  63. w,w,r,w,w,r,w,w,
  64. w,r,w,w,w,w,r,w,
  65. r,w,w,w,w,w,w,r
  66. ]
  67. upgrade = [
  68. w,w,w,o,w,w,w,w,
  69. w,w,o,o,o,w,w,w,
  70. w,o,o,w,o,o,w,w,
  71. o,o,w,o,w,o,o,w,
  72. o,w,o,o,o,w,o,w,
  73. w,o,o,w,o,o,w,w,
  74. o,o,w,w,w,o,o,w,
  75. o,w,w,w,w,w,o,w
  76. ]
  77. tick = [
  78. w,w,w,w,w,w,w,g,
  79. w,w,w,w,w,w,g,g,
  80. w,w,w,w,w,g,g,g,
  81. g,w,w,w,g,g,g,w,
  82. g,g,w,g,g,g,w,w,
  83. g,g,g,g,g,w,w,w,
  84. w,g,g,g,w,w,w,w,
  85. w,w,g,w,w,w,w,w
  86. ]
  87.  
  88. def checkVer():
  89.         version = open("version.txt", "r")
  90.         currVersion = version.read()
  91.         try:
  92.             newVersion = http.request("GET", "http://www.parginli.ch/weatherfiles/version.txt").data.decode()
  93.             if (currVersion not in newVersion):
  94.                 file = open("client.py", "w")
  95.                 file.write(http.request("GET", "http://www.parginli.ch/weatherfiles/client.py").data.decode())
  96.                 file.close()
  97.                 version.close()
  98.                 version = open("version.txt", "w")
  99.                 version.write(newVersion)
  100.                 version.close()
  101.                 sense.set_pixels(upgrade)
  102.                 os.system('sudo reboot')
  103.             else:
  104.                 sense.set_pixels(tick)
  105.             version.close()
  106.         except:
  107.             sense.set_pixels(wifi)
  108.             sys.exit()
  109.  
  110. def incMode(event):
  111.     global menu
  112.     if event.action == "pressed":
  113.         menu = True
  114.         global bufDataPlan
  115.         bufDataPlan += 1
  116.         if bufDataPlan > 5:
  117.             bufDataPlan = 0
  118.         sense.show_letter(str(bufDataPlan))
  119.  
  120. def decMode(event):
  121.     global menu
  122.     if event.action == "pressed":
  123.         menu = True
  124.         global bufDataPlan
  125.         bufDataPlan -= 1
  126.         if bufDataPlan < 0:
  127.             bufDataPlan = 5
  128.         sense.show_letter(str(bufDataPlan))
  129.  
  130. def lowLightOn(event):
  131.     if event.action == "pressed":
  132.         global brightness
  133.         brightness += 1
  134.         if brightness > 2:
  135.             brightness = 2
  136.         if (brightness == 1):
  137.             sense.low_light = True
  138.         else:
  139.             sense.low_light = False
  140.  
  141. def lowLightOff(event):
  142.     if event.action == "pressed":
  143.         global brightness
  144.         brightness -= 1
  145.         if brightness <= 0:
  146.             brightness = 0
  147.             sense.clear()
  148.         if (brightness == 1):
  149.             sense.low_light = True
  150.         else:
  151.             sense.low_light = False
  152.  
  153.  
  154. def click(event):
  155.     global currDataPlan
  156.     global bufDataPlan
  157.     global menu
  158.     if event.action == "pressed" and event.direction == "middle":
  159.         currDataPlan = bufDataPlan
  160.         menu = False
  161.  
  162. def takePhoto():
  163.     global numImagesToday
  164.     global lasttime
  165.     global brightness
  166.     global menu
  167.     if (brightness != 0 and menu == False):
  168.         number_pixels = int((time.time() * 1000 - lasttime) / (imageIntervals[currDataPlan] * 1000) * 64)
  169.         sense.clear(89, 242, 24)
  170.         for i in range(0, number_pixels):
  171.             if (i < 64):
  172.                 sense.set_pixel(int(i % 8), int(i / 8), 119, 171, 255)
  173.     if (datetime.datetime.now().hour < 2 and numImagesToday != 0):
  174.         numImagesToday = 0
  175.         checkVer()
  176.     thishour = datetime.datetime.now().hour
  177.     if (lasttime + imageIntervals[currDataPlan] * 1000 < int(round(time.time() * 1000))):
  178.         if (currDataPlan == 0):
  179.             return thishour >= 8 and thishour <= 16 and numImagesToday < 3
  180.         elif (currDataPlan == 1):
  181.             return thishour >= 7 and thishour <= 19 and numImagesToday < 6
  182.         elif (currDataPlan == 2):
  183.             return thishour >= 6 and thishour <= 21 and numImagesToday < 15
  184.         elif (currDataPlan == 3):
  185.             return thishour >= 6 and thishour <= 21 and numImagesToday < 30
  186.         elif (currDataPlan == 4):
  187.             return thishour >= 6 and thishour <= 21 and numImagesToday < 180
  188.         elif (currDataPlan == 5):
  189.             return True
  190.  
  191. def imageloop():
  192.     while True:
  193.         if takePhoto():
  194.             #try:
  195.                 global numImagesToday
  196.                 global lasttime
  197.                 numImagesToday += 1
  198.                 lasttime = int(round(time.time() * 1000))
  199.                 print(lasttime)
  200.                 s.sendto("imagecoming".encode("utf-8"), (HOST, PORT))
  201.                 if (brightness != 0 and menu == False):
  202.                     sense.clear(119, 171, 255)
  203.                 camera.capture("image.jpg")
  204.                 imageCompression = 1
  205.                 print("resizing to " + str(imageSizes[currDataPlan]) + "KB")
  206.                 img = Image.open("image.jpg")
  207.                 while(os.path.getsize("image.jpg") > imageSizes[currDataPlan] * 1000):
  208.                     img = img.resize((int(img.size[0] * imageCompression), int(img.size[1] * imageCompression)), Image.ANTIALIAS)
  209.                     imageCompression -= 0.005
  210.                     img.save("image.jpg", quality=int(10 * imageCompression))
  211.                 draw = ImageDraw.Draw(img)
  212.                 font = ImageFont.truetype("Font.ttf", int(img.size[1] * 0.05))
  213.                 draw.text((1, int(img.size[1] * 0.965)), str(datetime.datetime.now().strftime("%d.%m.%Y %H:%M")), (255, 255, 255), font=font)
  214.                 img.save("resized.jpg")
  215.                 f = open("resized.jpg", "rb")
  216.                 chunk = f.read(CHUNK_SIZE)
  217.                 max = os.path.getsize("resized.jpg")
  218.                 index = 0
  219.                 while chunk:
  220.                     if (brightness != 0 and menu == False):
  221.                         number_pixels = (float(index * CHUNK_SIZE) / max) * 64
  222.                         sense.clear(119, 171, 255)
  223.                         for i in range(0, int(number_pixels)):
  224.                             sense.set_pixel(int(i % 8), int(i / 8), 89, 242, 24)
  225.                     s.sendto(chunk, (HOST, PORT))
  226.                     chunk = f.read(CHUNK_SIZE)
  227.                     index += 1
  228.                     time.sleep(1)
  229.                 f.close()
  230.                 if (brightness != 0 and menu == False):
  231.                     sense.clear(89, 242, 24)
  232.                 time.sleep(1)
  233.                 print("sent.")
  234.                 s.sendto(("isend," + str(index)).encode("utf-8"), (HOST, PORT))
  235.                 time.sleep(2)
  236.             #except:
  237.                 #print("error!")
  238.         else:
  239.             time.sleep(5)
  240.  
  241. sense.stick.direction_left = decMode
  242. sense.stick.direction_right = incMode
  243. sense.stick.direction_up = lowLightOn
  244. sense.stick.direction_down = lowLightOff
  245. sense.stick.direction_any = click
  246.  
  247. sense.clear(255, 252, 119)
  248.  
  249. time.sleep(5)
  250.  
  251. checkVer()
  252.  
  253. time.sleep(2)
  254.  
  255. imageloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement