Compare commits
3 Commits
master
...
developmen
Author | SHA1 | Date | |
---|---|---|---|
|
d216996fe6 | ||
|
aef37837c5 | ||
|
6305d3d37f |
@ -1,6 +1,10 @@
|
||||
Change Log
|
||||
----------
|
||||
|
||||
**0.9** (2017-03-02)
|
||||
|
||||
- allow the telegram specification to optionally be autodetected
|
||||
|
||||
**0.8** (2017-01-26)
|
||||
|
||||
- added support for DSMR v3
|
||||
|
@ -4,3 +4,7 @@ class ParseError(Exception):
|
||||
|
||||
class InvalidChecksumError(ParseError):
|
||||
pass
|
||||
|
||||
|
||||
class TelegramSpecificationMatchError(ParseError):
|
||||
pass
|
||||
|
@ -4,16 +4,18 @@ import re
|
||||
from PyCRC.CRC16 import CRC16
|
||||
|
||||
from dsmr_parser.objects import MBusObject, CosemObject
|
||||
from dsmr_parser.exceptions import ParseError, InvalidChecksumError
|
||||
from dsmr_parser.exceptions import ParseError, InvalidChecksumError, \
|
||||
TelegramSpecificationMatchError
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TelegramParser(object):
|
||||
|
||||
def __init__(self, telegram_specification, apply_checksum_validation=True):
|
||||
def __init__(self, telegram_specification=None, apply_checksum_validation=True):
|
||||
"""
|
||||
:param telegram_specification: determines how the telegram is parsed
|
||||
:param telegram_specification: determines how the telegram is parsed.
|
||||
Will attempt to autodetect if omitted.
|
||||
:param apply_checksum_validation: validate checksum if applicable for
|
||||
telegram DSMR version (v4 and up).
|
||||
:type telegram_specification: dict
|
||||
@ -39,8 +41,10 @@ class TelegramParser(object):
|
||||
..
|
||||
}
|
||||
:raises ParseError:
|
||||
:raises InvalidChecksumError:
|
||||
"""
|
||||
if not self.telegram_specification:
|
||||
self.telegram_specification = \
|
||||
match_telegram_specification(telegram_data)
|
||||
|
||||
if self.apply_checksum_validation \
|
||||
and self.telegram_specification['checksum_support']:
|
||||
@ -118,6 +122,11 @@ def match_telegram_specification(telegram_data):
|
||||
else:
|
||||
return specification
|
||||
|
||||
raise TelegramSpecificationMatchError(
|
||||
'Could automatically match telegram specification. Make sure the data'
|
||||
'is not corrupt. Alternatively manually specify one.'
|
||||
)
|
||||
|
||||
|
||||
class DSMRObjectParser(object):
|
||||
"""
|
||||
|
2
setup.py
2
setup.py
@ -6,7 +6,7 @@ setup(
|
||||
author='Nigel Dokter',
|
||||
author_email='nigeldokter@gmail.com',
|
||||
url='https://github.com/ndokter/dsmr_parser',
|
||||
version='0.8',
|
||||
version='0.9',
|
||||
packages=find_packages(),
|
||||
install_requires=[
|
||||
'pyserial>=3,<4',
|
||||
|
@ -1,5 +1,6 @@
|
||||
import unittest
|
||||
|
||||
from dsmr_parser.exceptions import TelegramSpecificationMatchError
|
||||
from dsmr_parser.parsers import match_telegram_specification
|
||||
from dsmr_parser import telegram_specifications
|
||||
from test import example_telegrams
|
||||
@ -22,3 +23,7 @@ class MatchTelegramSpecificationTest(unittest.TestCase):
|
||||
def test_v5(self):
|
||||
assert match_telegram_specification(example_telegrams.TELEGRAM_V5) \
|
||||
== telegram_specifications.V5
|
||||
|
||||
def test_malformed_telegram(self):
|
||||
with self.assertRaises(TelegramSpecificationMatchError):
|
||||
match_telegram_specification(example_telegrams.TELEGRAM_V5[:-4])
|
||||
|
@ -12,6 +12,15 @@ from test.example_telegrams import TELEGRAM_V2_2
|
||||
class TelegramParserV2_2Test(unittest.TestCase):
|
||||
""" Test parsing of a DSMR v2.2 telegram. """
|
||||
|
||||
def test_telegram_specification_matching(self):
|
||||
parser = TelegramParser()
|
||||
parser.parse(TELEGRAM_V2_2)
|
||||
|
||||
self.assertEqual(
|
||||
parser.telegram_specification,
|
||||
telegram_specifications.V2_2
|
||||
)
|
||||
|
||||
def test_parse(self):
|
||||
parser = TelegramParser(telegram_specifications.V2_2)
|
||||
result = parser.parse(TELEGRAM_V2_2)
|
||||
|
@ -12,6 +12,15 @@ from test.example_telegrams import TELEGRAM_V3
|
||||
class TelegramParserV3Test(unittest.TestCase):
|
||||
""" Test parsing of a DSMR v3 telegram. """
|
||||
|
||||
def test_telegram_specification_matching(self):
|
||||
parser = TelegramParser()
|
||||
parser.parse(TELEGRAM_V3)
|
||||
|
||||
self.assertEqual(
|
||||
parser.telegram_specification,
|
||||
telegram_specifications.V3
|
||||
)
|
||||
|
||||
def test_parse(self):
|
||||
parser = TelegramParser(telegram_specifications.V3)
|
||||
result = parser.parse(TELEGRAM_V3)
|
||||
|
@ -15,6 +15,15 @@ from test.example_telegrams import TELEGRAM_V4_2
|
||||
class TelegramParserV4_2Test(unittest.TestCase):
|
||||
""" Test parsing of a DSMR v4.2 telegram. """
|
||||
|
||||
def test_telegram_specification_matching(self):
|
||||
parser = TelegramParser()
|
||||
parser.parse(TELEGRAM_V4_2)
|
||||
|
||||
self.assertEqual(
|
||||
parser.telegram_specification,
|
||||
telegram_specifications.V4
|
||||
)
|
||||
|
||||
def test_parse(self):
|
||||
parser = TelegramParser(telegram_specifications.V4)
|
||||
result = parser.parse(TELEGRAM_V4_2)
|
||||
|
@ -16,6 +16,15 @@ from test.example_telegrams import TELEGRAM_V5
|
||||
class TelegramParserV5Test(unittest.TestCase):
|
||||
""" Test parsing of a DSMR v5.x telegram. """
|
||||
|
||||
def test_telegram_specification_matching(self):
|
||||
parser = TelegramParser()
|
||||
parser.parse(TELEGRAM_V5)
|
||||
|
||||
self.assertEqual(
|
||||
parser.telegram_specification,
|
||||
telegram_specifications.V5
|
||||
)
|
||||
|
||||
def test_parse(self):
|
||||
parser = TelegramParser(telegram_specifications.V5)
|
||||
result = parser.parse(TELEGRAM_V5)
|
||||
|
Loading…
Reference in New Issue
Block a user