Add crc check before submitting new image to avoid heavy load

This commit is contained in:
sqozz 2024-10-07 23:56:10 +02:00
parent 4de660f612
commit cec643ea63

14
vu1.py
View file

@ -1,10 +1,18 @@
from os.path import join as joinpath from os.path import join as joinpath
import json import json
import zlib
import colorsys import colorsys
import requests import requests
API_BASE = "api/v0/dial" API_BASE = "api/v0/dial"
# taken from https://stackoverflow.com/a/2387880
def file_crc(fileName):
prev = 0
for eachLine in open(fileName, "rb"):
prev = zlib.crc32(eachLine, prev)
return "%X"%(prev & 0xFFFFFFFF)
class DialServer(): class DialServer():
def __init__(self, url, key): def __init__(self, url, key):
self.url = url self.url = url
@ -130,6 +138,11 @@ class Dial():
def color(self, new_color): def color(self, new_color):
self._server._req(f"{self.uid}/backlight", dict(new_color)) self._server._req(f"{self.uid}/backlight", dict(new_color))
@property
def image_crc(self):
crc = self._server._req(f"{self.uid}/image/crc")
return crc
@property @property
def image(self): def image(self):
self._get_status() self._get_status()
@ -137,6 +150,7 @@ class Dial():
@image.setter @image.setter
def image(self, new_image): def image(self, new_image):
if file_crc(new_image) != self.image_crc:
file = {'imgfile': (open(new_image, 'rb'))} file = {'imgfile': (open(new_image, 'rb'))}
self._server._req(f"{self.uid}/image/set", file, post=True) self._server._req(f"{self.uid}/image/set", file, post=True)