diff --git a/tools/odrive/remote_object.py b/tools/odrive/remote_object.py index 91869642..0d87dd63 100644 --- a/tools/odrive/remote_object.py +++ b/tools/odrive/remote_object.py @@ -8,14 +8,29 @@ import struct import threading import odrive.protocol -#class ObjectDisappearedError(Exception): -# def __init__(self, channel): -# self._obj = obj -# pass class ObjectDefinitionError(Exception): pass +codecs = {} + +class StructCodec(): + """ + Generic serializer/deserializer based on struct pack + """ + def __init__(self, struct_format, target_type): + self._struct_format = struct_format + self._target_type = target_type + def get_length(self): + return struct.calcsize(self._struct_format) + def serialize(self, value): + value = self._target_type(value) + return struct.pack(self._struct_format, value) + def deserialize(self, buffer): + value = struct.unpack(self._struct_format, buffer) + value = value[0] if len(value) == 1 else value + return self._target_type(value) + class RemoteProperty(): """ Used internally by dynamically created objects to translate @@ -24,6 +39,7 @@ class RemoteProperty(): """ def __init__(self, json_data, parent): self._parent = parent + self.__channel__ = parent.__channel__ id_str = json_data.get("id", None) if id_str is None: raise ObjectDefinitionError("unspecified endpoint ID") @@ -37,51 +53,28 @@ class RemoteProperty(): if type_str is None: raise ObjectDefinitionError("unspecified type") - if type_str == "float": - self._property_type = float - self._struct_format = "