issue-51-telegram made Telegram str and json methods more generic
This commit is contained in:
parent
14d9869fc3
commit
a98c9ae4b7
@ -31,7 +31,7 @@ class Telegram(dict):
|
|||||||
# Update name mapping used to get value by attribute. Example: telegram.P1_MESSAGE_HEADER
|
# Update name mapping used to get value by attribute. Example: telegram.P1_MESSAGE_HEADER
|
||||||
obis_name = obis_name_mapping.EN[obis_reference]
|
obis_name = obis_name_mapping.EN[obis_reference]
|
||||||
setattr(self, obis_name, dsmr_object)
|
setattr(self, obis_name, dsmr_object)
|
||||||
if obis_name not in self._item_names: # TODO solve issue with repeating obis references
|
if obis_name not in self._item_names: # TODO repeating obis references
|
||||||
self._item_names.append(obis_name)
|
self._item_names.append(obis_name)
|
||||||
|
|
||||||
# TODO isinstance check: MaxDemandParser (BELGIUM_MAXIMUM_DEMAND_13_MONTHS) returns a list
|
# TODO isinstance check: MaxDemandParser (BELGIUM_MAXIMUM_DEMAND_13_MONTHS) returns a list
|
||||||
@ -77,25 +77,24 @@ class Telegram(dict):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
output = ""
|
output = ""
|
||||||
for attr, value in self:
|
for attr, value in self:
|
||||||
if attr == 'MBUS_DEVICES':
|
if isinstance(value, list):
|
||||||
# Mbus devices are in a list
|
output += ''.join(map(str, value))
|
||||||
for mbus_device in value:
|
|
||||||
output += str(mbus_device)
|
|
||||||
else:
|
else:
|
||||||
output += "{}: \t {}\n".format(attr, str(value))
|
output += "{}: \t {}\n".format(attr, str(value))
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
def to_json(self):
|
def to_json(self):
|
||||||
telegram_data = {obis_name: json.loads(value.to_json())
|
json_data = {}
|
||||||
for obis_name, value in self
|
|
||||||
if isinstance(value, DSMRObject)}
|
|
||||||
telegram_data['MBUS_DEVICES'] = [
|
|
||||||
json.loads(mbus_device.to_json())
|
|
||||||
for mbus_device in self._mbus_devices
|
|
||||||
]
|
|
||||||
|
|
||||||
return json.dumps(telegram_data)
|
for attr, value in self:
|
||||||
|
if isinstance(value, list):
|
||||||
|
json_data[attr] = [json.loads(item.to_json() if hasattr(item, 'to_json') else item)
|
||||||
|
for item in value]
|
||||||
|
elif hasattr(value, 'to_json'):
|
||||||
|
json_data[attr] = json.loads(value.to_json())
|
||||||
|
|
||||||
|
return json.dumps(json_data)
|
||||||
|
|
||||||
|
|
||||||
class DSMRObject(object):
|
class DSMRObject(object):
|
||||||
|
Loading…
Reference in New Issue
Block a user