Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #87

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions dsmr_parser/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ class ParseError(Exception):

class InvalidChecksumError(ParseError):
pass


class TelegramSpecificationMatchError(ParseError):
pass
17 changes: 13 additions & 4 deletions dsmr_parser/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
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
Expand All @@ -39,8 +41,10 @@ def parse(self, telegram_data):
..
}
: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']:
Expand Down Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
author='Nigel Dokter',
author_email='[email protected]',
url='https://github.com/ndokter/dsmr_parser',
version='0.8',
version='0.9',
packages=find_packages(),
install_requires=[
'pyserial>=3,<4',
Expand Down
5 changes: 5 additions & 0 deletions test/test_match_telegram_specification.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,3 +23,7 @@ def test_v4_2(self):
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])
9 changes: 9 additions & 0 deletions test/test_parse_v2_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
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)
Expand Down
9 changes: 9 additions & 0 deletions test/test_parse_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
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)
Expand Down
9 changes: 9 additions & 0 deletions test/test_parse_v4_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
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)
Expand Down
9 changes: 9 additions & 0 deletions test/test_parse_v5.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
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)
Expand Down