32 lines
965 B
Python
32 lines
965 B
Python
|
import struct
|
||
|
from serde import Model, fields
|
||
|
|
||
|
from common import EXPERIMENT_APID, UniqueId, make_addressable_id
|
||
|
|
||
|
class CameraParameters(Model):
|
||
|
R: fields.Int()
|
||
|
G: fields.Int()
|
||
|
B: fields.Int()
|
||
|
N: fields.Int()
|
||
|
P: fields.Bool()
|
||
|
E: fields.Int()
|
||
|
W: fields.Int()
|
||
|
|
||
|
def serialize_for_uplink(self) -> bytearray:
|
||
|
return self.to_json().encode('utf-8')
|
||
|
|
||
|
# Example serialization
|
||
|
data = bytearray(make_addressable_id(EXPERIMENT_APID, UniqueId.CameraHandler))
|
||
|
params = CameraParameters(8, 8, 8, 1, True, 200, 1000)
|
||
|
serialized = params.to_json().encode('utf-8')
|
||
|
byte_string = bytearray(struct.pack('!{}s'.format(len(serialized)), serialized))
|
||
|
print(byte_string)
|
||
|
print(params.serialize_for_uplink())
|
||
|
data.extend(params.serialize_for_uplink())
|
||
|
print(data)
|
||
|
|
||
|
# Example deserialization
|
||
|
data = '{"R": 100, "G": 150, "B": 200, "N": 3, "P": true, "E": 10, "W": 20}'
|
||
|
deserialized_params = CameraParameters.from_json(data)
|
||
|
print(deserialized_params)
|