def check_data_frame(data, expected_prefix, encrypted=True): prefix = data[:15] suffix = data[-8:] if encrypted: payload_len = struct.unpack( ">B", data[15:16])[0] # big-endian, unsigned char version = data[16:19] checksum = data[19:35] encrypted_json = data[35:-8] json_data = pytuya.AESCipher( LOCAL_KEY.encode('utf-8')).decrypt(encrypted_json) else: json_data = data[16:-8].decode('utf-8') frame_ok = True if prefix != pytuya.hex2bin(expected_prefix): frame_ok = False elif suffix != pytuya.hex2bin("000000000000aa55"): frame_ok = False elif encrypted: if payload_len != len(version) + len(checksum) + len( encrypted_json) + len(suffix): frame_ok = False elif version != b"3.1": frame_ok = False return json_data, frame_ok
def check_data_frame(data, expected_prefix, encrypted=True): prefix = data[:15] suffix = data[-8:] if encrypted: payload_len = struct.unpack(">B",data[15:16])[0] # big-endian, unsigned char version = data[16:19] checksum = data[19:35] encrypted_json = data[35:-8] json_data = pytuya.AESCipher(LOCAL_KEY.encode(mock_byte_encoding)).decrypt(encrypted_json) else: json_data = data[16:-8].decode(mock_byte_encoding) frame_ok = True if prefix != pytuya.hex2bin(expected_prefix): frame_ok = False elif suffix[-4:] != pytuya.hex2bin("0000aa55"): # We only check for the trailing byte signature # We could extend the test to also check the CRC if we wanted. frame_ok = False elif encrypted: if payload_len != len(version) + len(checksum) + len(encrypted_json) + len(suffix): frame_ok = False elif version != b"3.1": frame_ok = False return json_data, frame_ok