add json serialization

This commit is contained in:
Hans Erik van Elburg 2020-05-25 01:38:14 +02:00
parent 94447c3571
commit 837ba3b6f7
3 changed files with 60 additions and 13 deletions

View File

@ -1,5 +1,9 @@
Change Log
----------
**0.21** (2020-05-25)
- All objects can produce a json serialization of their state.
**0.20** (2020-05-12)
- All objects can now print their values

View File

@ -1,5 +1,7 @@
import dsmr_parser.obis_name_mapping
import datetime
import json
from decimal import Decimal
class Telegram(object):
@ -52,6 +54,9 @@ class Telegram(object):
output += "{}: \t {}\n".format(attr, str(value))
return output
def to_json(self):
return json.dumps(dict([[attr, json.loads(value.to_json())] for attr, value in self]))
class DSMRObject(object):
"""
@ -92,6 +97,22 @@ class MBusObject(DSMRObject):
output = "{}\t[{}] at {}".format(str(self.value), str(self.unit), str(self.datetime.astimezone().isoformat()))
return output
def to_json(self):
timestamp = self.datetime
if isinstance(self.datetime, datetime.datetime):
timestamp = self.datetime.astimezone().isoformat()
value = self.value
if isinstance(self.value, datetime.datetime):
value = self.value.astimezone().isoformat()
if isinstance(self.value, Decimal):
value = float(self.value)
output = {
'datetime': timestamp,
'value': value,
'unit': self.unit
}
return json.dumps(output)
class CosemObject(DSMRObject):
@ -110,6 +131,18 @@ class CosemObject(DSMRObject):
output = "{}\t[{}]".format(str(print_value), str(self.unit))
return output
def to_json(self):
json_value = self.value
if isinstance(self.value, datetime.datetime):
json_value = self.value.astimezone().isoformat()
if isinstance(self.value, Decimal):
json_value = float(self.value)
output = {
'value': json_value,
'unit': self.unit
}
return json.dumps(output)
class ProfileGenericObject(DSMRObject):
"""
@ -140,18 +173,6 @@ class ProfileGenericObject(DSMRObject):
self._buffer_list.append(MBusObject([self.values[offset], self.values[offset + 1]]))
return self._buffer_list
@property
def value(self):
list = [['buffer_length', self.buffer_length]]
list.append(['buffer_type', self.buffer_type])
buffer_repr = [
dict([['datetime', buffer_item.datetime],
['value', buffer_item.value]])
for buffer_item in self.buffer
]
list.append(['buffer', buffer_repr])
return dict(list)
def __str__(self):
output = "\t buffer length: {}\n".format(self.buffer_length)
output += "\t buffer type: {}".format(self.buffer_type)
@ -162,3 +183,25 @@ class ProfileGenericObject(DSMRObject):
output += "\n\t event occured at: {}".format(timestamp)
output += "\t for: {} [{}]".format(buffer_value.value, buffer_value.unit)
return output
def to_json(self):
"""
:return: A json of all values in the GenericProfileObject , with the following structure
{'buffer_length': n,
'buffer_type': obis_ref,
'buffer': [{'datetime': d1,
'value': v1,
'unit': u1},
...
{'datetime': dn,
'value': vn,
'unit': un}
]
}
"""
list = [['buffer_length', self.buffer_length]]
list.append(['buffer_type', self.buffer_type])
buffer_repr = [json.loads(buffer_item.to_json()) for buffer_item in self.buffer]
list.append(['buffer', buffer_repr])
output = dict(list)
return json.dumps(output)

View File

@ -6,7 +6,7 @@ setup(
author='Nigel Dokter',
author_email='nigel@nldr.net',
url='https://github.com/ndokter/dsmr_parser',
version='0.20',
version='0.21',
packages=find_packages(),
install_requires=[
'pyserial>=3,<4',