forked from platypush/platypush
Message.Encoder
should serialize binary data to 0x
-led hex strings.
This commit is contained in:
parent
9e5ad0e0b1
commit
31f411868c
2 changed files with 10 additions and 3 deletions
|
@ -57,9 +57,14 @@ if 'raw_sensor' not in Base.metadata:
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
if self.is_binary:
|
if self._value is None:
|
||||||
return self._value.decode()
|
return None
|
||||||
if self.is_json:
|
if self.is_binary and isinstance(self._value, str):
|
||||||
|
value = self._value[2:]
|
||||||
|
return bytes(
|
||||||
|
[int(value[i : i + 2], 16) for i in range(0, len(value), 2)]
|
||||||
|
)
|
||||||
|
if self.is_json and isinstance(self._value, (str, bytes)):
|
||||||
return json.loads(self._value)
|
return json.loads(self._value)
|
||||||
return self._value
|
return self._value
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,8 @@ class Message:
|
||||||
return obj.tolist()
|
return obj.tolist()
|
||||||
if isinstance(obj, decimal.Decimal):
|
if isinstance(obj, decimal.Decimal):
|
||||||
return float(obj)
|
return float(obj)
|
||||||
|
if isinstance(obj, (bytes, bytearray)):
|
||||||
|
return '0x' + ''.join([f'{x:02x}' for x in obj])
|
||||||
if callable(obj):
|
if callable(obj):
|
||||||
return '<function at {}.{}>'.format(obj.__module__, obj.__name__)
|
return '<function at {}.{}>'.format(obj.__module__, obj.__name__)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue