experimenting with version detection

This commit is contained in:
Nigel Dokter 2017-01-28 17:01:33 +01:00
parent 24ab9aa712
commit 9d20bb8ad5
4 changed files with 64 additions and 32 deletions

View File

@ -1,4 +1,7 @@
class DSMRObject(object):
"""
Represents all data from a single telegram line.
"""
def __init__(self, values):
self.values = values

View File

@ -27,8 +27,8 @@ class TelegramParser(object):
The telegram str type makes python 2.x integration easier.
:param str telegram: full telegram from start ('/') to checksum
('!ABCD') including line endings inbetween the telegram's lines
:param str telegram_data: full telegram from start ('/') to checksum
('!ABCD') including line endings in between the telegram's lines
:rtype: dict
:returns: Shortened example:
{
@ -51,8 +51,12 @@ class TelegramParser(object):
for signature, parser in self.telegram_specification['objects'].items():
match = re.search(signature, telegram_data, re.DOTALL)
if match:
telegram[signature] = parser.parse(match.group(0))
# All telegram specification lines/signatures are expected to be
# present.
if not match:
raise ParseError('Telegram specification does not match '
'telegram data')
telegram[signature] = parser.parse(match.group(0))
return telegram
@ -90,6 +94,31 @@ class TelegramParser(object):
)
def match_telegram_specification(telegram_data):
"""
Find telegram specification that matches the telegram data by trying all
specifications.
Could be further optimized to check the actual 0.2.8 OBIS reference which
is available for DSMR version 4 and up.
:param str telegram_data: full telegram from start ('/') to checksum
('!ABCD') including line endings in between the telegram's lines
:return: telegram specification
:rtype: dict
"""
# Prevent circular import
from dsmr_parser import telegram_specifications
for specification in telegram_specifications.ALL:
try:
TelegramParser(specification).parse(telegram_data)
except ParseError:
pass
else:
return specification
class DSMRObjectParser(object):
"""
Parses an object (can also be see as a 'line') from a telegram.

View File

@ -42,34 +42,7 @@ V2_2 = {
}
}
V3 = {
'checksum_support': False,
'objects': {
obis.EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
obis.ELECTRICITY_USED_TARIFF_1: CosemParser(ValueParser(Decimal)),
obis.ELECTRICITY_USED_TARIFF_2: CosemParser(ValueParser(Decimal)),
obis.ELECTRICITY_DELIVERED_TARIFF_1: CosemParser(ValueParser(Decimal)),
obis.ELECTRICITY_DELIVERED_TARIFF_2: CosemParser(ValueParser(Decimal)),
obis.ELECTRICITY_ACTIVE_TARIFF: CosemParser(ValueParser(str)),
obis.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)),
obis.ACTUAL_TRESHOLD_ELECTRICITY: CosemParser(ValueParser(Decimal)),
obis.ACTUAL_SWITCH_POSITION: CosemParser(ValueParser(str)),
obis.TEXT_MESSAGE_CODE: CosemParser(ValueParser(int)),
obis.TEXT_MESSAGE: CosemParser(ValueParser(str)),
obis.EQUIPMENT_IDENTIFIER_GAS: CosemParser(ValueParser(str)),
obis.DEVICE_TYPE: CosemParser(ValueParser(str)),
obis.VALVE_POSITION_GAS: CosemParser(ValueParser(str)),
obis.GAS_METER_READING: MBusParser(
ValueParser(timestamp),
ValueParser(int),
ValueParser(int),
ValueParser(int),
ValueParser(str),
ValueParser(Decimal),
),
}
}
V3 = V2_2
V4 = {
'checksum_support': True,
@ -145,3 +118,5 @@ V5 = {
)
}
}
ALL = (V2_2, V3, V4, V5)

View File

@ -0,0 +1,25 @@
import unittest
from dsmr_parser.parsers import match_telegram_specification
from dsmr_parser import telegram_specifications
from test import example_telegrams
class MatchTelegramSpecificationTest(unittest.TestCase):
def test_v2_2(self):
assert match_telegram_specification(example_telegrams.TELEGRAM_V2_2) \
== telegram_specifications.V2_2
def test_v3(self):
assert match_telegram_specification(example_telegrams.TELEGRAM_V3) \
== telegram_specifications.V3
def test_v4_2(self):
assert match_telegram_specification(example_telegrams.TELEGRAM_V4_2) \
== telegram_specifications.V4
def test_v5(self):
assert match_telegram_specification(example_telegrams.TELEGRAM_V5) \
== telegram_specifications.V5