Add crc check before submitting new image to avoid heavy load
This commit is contained in:
parent
4de660f612
commit
cec643ea63
1 changed files with 16 additions and 2 deletions
18
vu1.py
18
vu1.py
|
@ -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,8 +150,9 @@ class Dial():
|
||||||
|
|
||||||
@image.setter
|
@image.setter
|
||||||
def image(self, new_image):
|
def image(self, new_image):
|
||||||
file = {'imgfile': (open(new_image, 'rb'))}
|
if file_crc(new_image) != self.image_crc:
|
||||||
self._server._req(f"{self.uid}/image/set", file, post=True)
|
file = {'imgfile': (open(new_image, 'rb'))}
|
||||||
|
self._server._req(f"{self.uid}/image/set", file, post=True)
|
||||||
|
|
||||||
class VUColor():
|
class VUColor():
|
||||||
def __init__(self, red=0, green=0, blue=0):
|
def __init__(self, red=0, green=0, blue=0):
|
||||||
|
|
Loading…
Reference in a new issue