34 lines
935 B
Python

#from Thing import Thing
class BinaryMsg():
id = 0xB1
length = 4
def __init__(self, data, thing = None):
if isinstance(data, bytes):
self.thing_id = data[2]
self.data_length = data[3]
self.data = data[4:]
else:
self.network_id = data
self.thing_id = thing.id
self.thing = thing
def Serialize(self, buffer_ref):
if self.thing_id is None:
return 0
buffer: bytearray = buffer_ref[0]
ix = self.length
self.data_length = self.thing.GenerateBinary(buffer, {ix})
if ix <= self.length:
return 0
print(f'Send BinaryMsg [{self.network_id}/{self.thing_id}] {self.thing_type} {self.parent_id}')
buffer[0] = self.id
buffer[1] = self.network_id
buffer[2] = self.thing_id
buffer[3] = self.data_length
return ix