mirror of
https://github.com/pommi/python-itho-wpu.git
synced 2024-12-21 18:43:27 +01:00
feat(itho-wpu): check message length
The 5th byte (6th if you include 0x80) contains the message length. To double check if the message is correct we can check this against the actual message length.
This commit is contained in:
parent
c43de957e9
commit
152b6021cf
10
itho-wpu.py
10
itho-wpu.py
@ -89,7 +89,7 @@ class I2CSlave():
|
||||
logger.debug(f"Received {b} bytes! Status {s}")
|
||||
result = [hex(c) for c in d]
|
||||
logger.debug(f"Callback Response: {result}")
|
||||
if self.is_checksum_valid(result):
|
||||
if self.is_checksum_valid(result) and self.is_length_valid(result):
|
||||
self.queue.put(result)
|
||||
else:
|
||||
logger.debug(f"Received number of bytes was {b}")
|
||||
@ -106,6 +106,14 @@ class I2CSlave():
|
||||
return False
|
||||
return True
|
||||
|
||||
def is_length_valid(self, b):
|
||||
length_in_msg = int(b[4], 0)
|
||||
actual_length = len(b) - 6
|
||||
if length_in_msg != actual_length:
|
||||
logger.debug(f"Length invalid ({length_in_msg} != {actual_length})")
|
||||
return False
|
||||
return True
|
||||
|
||||
def close(self):
|
||||
self.event_callback.cancel()
|
||||
self.pi.bsc_i2c(0)
|
||||
|
Loading…
Reference in New Issue
Block a user