Can send image through HTTP

This commit is contained in:
nemunaire 2023-08-29 11:25:21 +02:00
parent 1a0377ad5d
commit 1c2f08f5aa
1 changed files with 41 additions and 11 deletions

52
main.py
View File

@ -28,6 +28,7 @@ from datetime import datetime, timedelta
import io
import locale
import logging
import math
import os.path
import time
@ -46,8 +47,13 @@ class WidgetPlacement:
self.position = position
self.kwargs = kwargs
def byteToStr(v):
return chr((v & 0xF) + 97) + chr(((v >> 4) & 0xF) + 97)
def main(only_on_coming_evt=False, ignore_module=[], force_coming_event=True, expand_alerts=False, **config_args):
def wordToStr(v):
return byteToStr(v & 0xFF) + byteToStr((v >> 8) & 0xFF)
def main(only_on_coming_evt=False, ignore_module=[], force_coming_event=True, expand_alerts=False, send_to=None, **config_args):
image = Image.new('1', (480, 800), 255)
#image = Image.new('L', (480, 800), 'white')
draw = ImageDraw.Draw(image)
@ -188,20 +194,42 @@ def main(only_on_coming_evt=False, ignore_module=[], force_coming_event=True, ex
)
logging.info("image generated")
try:
import epd7in5
if send_to is not None:
import urllib.request
epd = epd7in5.EPD()
epd.init()
logging.info("initialized")
def do_req(url):
with urllib.request.urlopen(url, b""):
pass
epd.display(epd.getbuffer(image))
do_req(send_to + "EPDw_")
pixels = image.load()
width, height = image.size
buf = [0xFF] * (int(width/8) * height)
for y in range(height):
for x in range(width):
if pixels[x, y] == 0:
buf[int((y + (width - x - 1) * height) / 8)] &= ~(0x80 >> (y % 8))
for i in range(math.ceil(len(buf) / 500)):
data = ''.join([byteToStr(b) for b in buf[500*i:500*(i+1)]])
size = wordToStr(len(data))
do_req(send_to + data + size + "LOAD_")
do_req(send_to + "SHOW_")
logging.info("time to sleep")
epd.sleep()
else:
try:
import epd7in5
except:
image.save("screen.png")
epd = epd7in5.EPD()
epd.init()
logging.info("initialized")
epd.display(epd.getbuffer(image))
logging.info("time to sleep")
epd.sleep()
except:
image.save("screen.png")
if __name__ == '__main__':
import argparse
@ -220,6 +248,7 @@ if __name__ == '__main__':
help='How many minutes to keep the infos in cache')
parser.add_argument('--max-cache-timeout', type=int, default=120,
help='Maximum time to serve the infos in cache in case of issue')
parser.add_argument('--send-to', help='Send the request to a HTTP server')
args = parser.parse_args()
@ -230,4 +259,5 @@ if __name__ == '__main__':
args.expand_alerts,
cache_timeout=args.cache_timeout,
max_cache_timeout=args.max_cache_timeout,
send_to=args.send_to,
)