From 789871899c4617ecb6a82ae82d571ec5abc698db Mon Sep 17 00:00:00 2001 From: Hans Erik van Elburg Date: Sun, 17 May 2020 01:25:02 +0200 Subject: [PATCH] ProfileGeneric parser working, ProfileGenericObject implemented and Test for V4 telegram completed. --- dsmr_parser/objects.py | 37 +++++++++++++++++++++++++- dsmr_parser/telegram_specifications.py | 5 +++- test/test_telegram.py | 22 ++++++++++++--- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/dsmr_parser/objects.py b/dsmr_parser/objects.py index 877934a..ce48a01 100644 --- a/dsmr_parser/objects.py +++ b/dsmr_parser/objects.py @@ -112,6 +112,41 @@ class CosemObject(DSMRObject): class ProfileGenericObject(DSMRObject): + """ + Represents all data in a GenericProfile value. + All buffer values are returned as a list of MBusObjects, + containing the datetime (timestamp) and the value. + """ + + def __init__(self, values): + super().__init__(values) + self._buffer_list = None + + @property + def buffer_length(self): + return self.values[0]['value'] + + @property + def buffer_type(self): + return self.values[1]['value'] + + @property + def buffer(self): + if self._buffer_list is None: + self._buffer_list = [] + values_offset = 2 + for i in range(self.buffer_length): + offset = values_offset + i*2 + self._buffer_list.append(MBusObject([self.values[offset], self.values[offset + 1]])) + return self._buffer_list + def __str__(self): - output = "{}".format(self.values) + output = "\t buffer length: {}\n".format(self.buffer_length) + output += "\t buffer type: {}".format(self.buffer_type) + for buffer_value in self.buffer: + timestamp = buffer_value.datetime + if isinstance(timestamp, datetime.datetime): + timestamp = str(timestamp.astimezone().isoformat()) + output += "\n\t event occured at: {}".format(timestamp) + output += "\t for: {} [{}]".format(buffer_value.value, buffer_value.unit) return output diff --git a/dsmr_parser/telegram_specifications.py b/dsmr_parser/telegram_specifications.py index 161ac91..1341ded 100644 --- a/dsmr_parser/telegram_specifications.py +++ b/dsmr_parser/telegram_specifications.py @@ -107,7 +107,10 @@ V5 = { obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)), obis.LONG_POWER_FAILURE_COUNT: CosemParser(ValueParser(int)), obis.SHORT_POWER_FAILURE_COUNT: CosemParser(ValueParser(int)), - # POWER_EVENT_FAILURE_LOG: ProfileGenericParser(), TODO + obis.POWER_EVENT_FAILURE_LOG: + ProfileGenericParser(BUFFER_TYPES, + PG_HEAD_PARSERS, + PG_UNIDENTIFIED_BUFFERTYPE_PARSERS), obis.VOLTAGE_SAG_L1_COUNT: CosemParser(ValueParser(int)), obis.VOLTAGE_SAG_L2_COUNT: CosemParser(ValueParser(int)), obis.VOLTAGE_SAG_L3_COUNT: CosemParser(ValueParser(int)), diff --git a/test/test_telegram.py b/test/test_telegram.py index a330bc4..90b8eff 100644 --- a/test/test_telegram.py +++ b/test/test_telegram.py @@ -287,14 +287,30 @@ class TelegramTest(unittest.TestCase): unit_val='m3', value_type=Decimal, value_val=Decimal('981.443')) + # POWER_EVENT_FAILURE_LOG (1-0:99.97.0) testitem_name = 'POWER_EVENT_FAILURE_LOG' object_type = ProfileGenericObject testitem = eval("telegram.{}".format(testitem_name)) assert isinstance(testitem, object_type) -# assert testitem.unit == unit_val -# assert isinstance(testitem.value, value_type) -# assert testitem.value == value_val + assert testitem.buffer_length == 3 + assert testitem.buffer_type == '0-0:96.7.19' + buffer = testitem.buffer + assert isinstance(testitem.buffer, list) + assert len(buffer) == 3 + assert all([isinstance(item, MBusObject) for item in buffer]) + date0 = datetime.datetime(2000, 1, 4, 17, 3, 20, tzinfo=datetime.timezone.utc) + date1 = datetime.datetime(1999, 12, 31, 23, 0, 1, tzinfo=datetime.timezone.utc) + date2 = datetime.datetime(2000, 1, 1, 23, 0, 3, tzinfo=datetime.timezone.utc) + assert buffer[0].datetime == date0 + assert buffer[1].datetime == date1 + assert buffer[2].datetime == date2 + assert buffer[0].value == 237126 + assert buffer[1].value == 2147583646 + assert buffer[2].value == 2317482647 + assert all([isinstance(item.value, int) for item in buffer]) + assert all([isinstance(item.unit, str) for item in buffer]) + assert all([(item.unit == 's') for item in buffer]) self.item_names_tested.append(testitem_name) # check if all items in telegram V4 specification are covered