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

18
vu1.py
View file

@ -1,10 +1,18 @@
from os.path import join as joinpath
import json
import zlib
import colorsys
import requests
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():
def __init__(self, url, key):
self.url = url
@ -130,6 +138,11 @@ class Dial():
def color(self, 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
def image(self):
self._get_status()
@ -137,8 +150,9 @@ class Dial():
@image.setter
def image(self, new_image):
file = {'imgfile': (open(new_image, 'rb'))}
self._server._req(f"{self.uid}/image/set", file, post=True)
if file_crc(new_image) != self.image_crc:
file = {'imgfile': (open(new_image, 'rb'))}
self._server._req(f"{self.uid}/image/set", file, post=True)
class VUColor():
def __init__(self, red=0, green=0, blue=0):