From cec643ea630159d0e5835c3c7644291d53784cf2 Mon Sep 17 00:00:00 2001 From: sqozz Date: Mon, 7 Oct 2024 23:56:10 +0200 Subject: [PATCH] Add crc check before submitting new image to avoid heavy load --- vu1.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/vu1.py b/vu1.py index 9050bcd..25e0cdc 100644 --- a/vu1.py +++ b/vu1.py @@ -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):