Compare commits
81
Commits
v0.3
..
development
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d216996fe6 | ||
|
|
aef37837c5 | ||
|
|
6305d3d37f | ||
|
|
d2f57a8926 | ||
|
|
3d64fea247 | ||
|
|
46860e04c1 | ||
|
|
148bdabc12 | ||
|
|
9d20bb8ad5 | ||
|
|
24ab9aa712 | ||
|
|
0c40070752 | ||
|
|
6c8a9dcbdb | ||
|
|
c1a6b930c8 | ||
|
|
9e74c4c23c | ||
|
|
b4a520c8b4 | ||
|
|
a88dfe1a41 | ||
|
|
c4dcc73191 | ||
|
|
c4caf54576 | ||
|
|
8a868ce826 | ||
|
|
9623f3b092 | ||
|
|
adcfdfe2ae | ||
|
|
45ee8dbb32 | ||
|
|
7a4c204850 | ||
|
|
45f5fe2c36 | ||
|
|
07634abed1 | ||
|
|
e2e4bb36a2 | ||
|
|
4eeefec426 | ||
|
|
f3d8311ac2 | ||
|
|
9b488e74f8 | ||
|
|
fadf206715 | ||
|
|
e97ab7c7ea | ||
|
|
759e0a0d92 | ||
|
|
21334e5a0a | ||
|
|
11672d0512 | ||
|
|
87a5a2d2fa | ||
|
|
663024239f | ||
|
|
0e7819b535 | ||
|
|
60317a0dc5 | ||
|
|
d990a316ad | ||
|
|
f10032f701 | ||
|
|
1373d570d2 | ||
|
|
29fc97a65c | ||
|
|
3b43cbf841 | ||
|
|
9d8cad8b46 | ||
|
|
0d6c763e86 | ||
|
|
8b60d48edd | ||
|
|
03b761e15b | ||
|
|
061334f702 | ||
|
|
920c9aedc2 | ||
|
|
991dd09e2a | ||
|
|
ce4d5b0e62 | ||
|
|
e512456cc2 | ||
|
|
3c9db523fa | ||
|
|
763237ef1d | ||
|
|
cdc9e395aa | ||
|
|
6dec45ae2c | ||
|
|
c2a67bff6d | ||
|
|
b3014823c1 | ||
|
|
4b392522c3 | ||
|
|
1c69b4e9ee | ||
|
|
bbea9de445 | ||
|
|
19d3f60aec | ||
|
|
b228bd524b | ||
|
|
220db73fbe | ||
|
|
81fd581e57 | ||
|
|
4df6ba75a2 | ||
|
|
819d0d0696 | ||
|
|
a3685f0310 | ||
|
|
5ae6ad4156 | ||
|
|
e330e9db21 | ||
|
|
0ac2990df2 | ||
|
|
e0d91f1ec4 | ||
|
|
c058feca5f | ||
|
|
6f3c74ce7c | ||
|
|
43a3cb7a96 | ||
|
|
5e88182301 | ||
|
|
e3569e0719 | ||
|
|
f8a3c76c68 | ||
|
|
a1d077d6f2 | ||
|
|
616db8b1cc | ||
|
|
927a4bc8e7 | ||
|
|
e7ff8f2444 |
@@ -0,0 +1,2 @@
|
|||||||
|
[run]
|
||||||
|
branch = True
|
||||||
@@ -3,3 +3,6 @@
|
|||||||
.tox
|
.tox
|
||||||
.cache
|
.cache
|
||||||
*.egg-info
|
*.egg-info
|
||||||
|
/.project
|
||||||
|
/.pydevproject
|
||||||
|
/.coverage
|
||||||
|
|||||||
+9
-1
@@ -1,10 +1,18 @@
|
|||||||
language: python
|
language: python
|
||||||
|
|
||||||
python:
|
python:
|
||||||
- 2.7
|
- 2.7
|
||||||
- 3.4
|
- 3.4
|
||||||
- 3.5
|
- 3.5
|
||||||
install: pip install tox-travis
|
- 3.6
|
||||||
|
|
||||||
|
install: pip install tox-travis codecov
|
||||||
|
|
||||||
script: tox
|
script: tox
|
||||||
|
|
||||||
|
after_success:
|
||||||
|
- codecov
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- python: 2.7
|
- python: 2.7
|
||||||
|
|||||||
+42
-3
@@ -1,13 +1,52 @@
|
|||||||
Change Log
|
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
|
||||||
|
- added support for DSMR v5
|
||||||
|
|
||||||
|
**IMPORTANT: this release has the following backwards incompatible changes:**
|
||||||
|
|
||||||
|
- Removed TelegramParserV2_2 in favor of TelegramParser
|
||||||
|
- Removed TelegramParserV4 in favor of TelegramParser
|
||||||
|
|
||||||
|
**0.7** (2017-01-14)
|
||||||
|
|
||||||
|
- Internal refactoring related to the way clients feed their data into the parse module. Clients can now supply the telegram data in single characters, lines (which was common) or complete telegram strings. (`pull request #17 <https://github.com/ndokter/dsmr_parser/pull/17>`_)
|
||||||
|
|
||||||
|
**IMPORTANT: this release has the following backwards incompatible changes:**
|
||||||
|
|
||||||
|
- Client related imports from dsmr_parser.serial and dsmr_parser.protocol have been moved to dsmr_parser.clients (import these from the clients/__init__.py module)
|
||||||
|
- The .parse() method of TelegramParser, TelegramParserV2_2, TelegramParserV4 now accepts a string containing the entire telegram (including \r\n characters) and not a list
|
||||||
|
|
||||||
|
|
||||||
|
**0.6** (2017-01-04)
|
||||||
|
|
||||||
|
- Fixed bug in CRC checksum verification for the asyncio client (`pull request #15 <https://github.com/ndokter/dsmr_parser/pull/15>`_)
|
||||||
|
- Support added for TCP connections using the asyncio client (`pull request #12 <https://github.com/ndokter/dsmr_parser/pull/12/>`_)
|
||||||
|
|
||||||
|
**0.5** (2016-12-29)
|
||||||
|
|
||||||
|
- CRC checksum verification for DSMR v4 telegrams (`issue #10 <https://github.com/ndokter/dsmr_parser/issues/10>`_)
|
||||||
|
|
||||||
|
**0.4** (2016-11-21)
|
||||||
|
|
||||||
|
- DSMR v2.2 serial settings now uses parity serial.EVEN by default (`pull request #5 <https://github.com/ndokter/dsmr_parser/pull/5>`_)
|
||||||
|
- improved asyncio reader and improve it's error handling (`pull request #8 <https://github.com/ndokter/dsmr_parser/pull/8>`_)
|
||||||
|
|
||||||
**0.3** (2016-11-12)
|
**0.3** (2016-11-12)
|
||||||
|
|
||||||
- Added asyncio reader for non-blocking reads. (thanks to https://github.com/aequitas)
|
- asyncio reader for non-blocking reads (`pull request #3 <https://github.com/ndokter/dsmr_parser/pull/3>`_)
|
||||||
|
|
||||||
**0.2** (2016-11-08)
|
**0.2** (2016-11-08)
|
||||||
|
|
||||||
- Added support for DMSR version 2.2 (thanks to https://github.com/aequitas)
|
- support for DMSR version 2.2 (`pull request #2 <https://github.com/ndokter/dsmr_parser/pull/2>`_)
|
||||||
|
|
||||||
**0.1** (2016-08-22)
|
**0.1** (2016-08-22)
|
||||||
|
|
||||||
- Initial version with a serial reader and support for DSMR version 4.x
|
- initial version with a serial reader and support for DSMR version 4.x
|
||||||
|
|||||||
+119
-32
@@ -4,51 +4,138 @@ DSMR Parser
|
|||||||
.. image:: https://img.shields.io/pypi/v/dsmr-parser.svg
|
.. image:: https://img.shields.io/pypi/v/dsmr-parser.svg
|
||||||
:target: https://pypi.python.org/pypi/dsmr-parser
|
:target: https://pypi.python.org/pypi/dsmr-parser
|
||||||
|
|
||||||
|
.. image:: https://travis-ci.org/ndokter/dsmr_parser.svg?branch=master
|
||||||
|
:target: https://travis-ci.org/ndokter/dsmr_parser
|
||||||
|
|
||||||
A library for parsing Dutch Smart Meter Requirements (DSMR) telegram data. It
|
A library for parsing Dutch Smart Meter Requirements (DSMR) telegram data. It
|
||||||
also includes a serial client to directly read and parse smart meter data.
|
also includes client implementation to directly read and parse smart meter data.
|
||||||
|
|
||||||
|
|
||||||
Features
|
Features
|
||||||
--------
|
--------
|
||||||
|
|
||||||
DSMR Parser currently supports DSMR versions 2.2 and 4.x. It has been tested with Python 3.5 and 3.4.
|
DSMR Parser supports DSMR versions 2, 3, 4 and 5. It has been tested with Python 3.4, 3.5 and 3.6.
|
||||||
|
|
||||||
|
|
||||||
Examples
|
Client module usage
|
||||||
--------
|
-------------------
|
||||||
|
|
||||||
Using the serial reader to connect to your smart meter and parse it's telegrams:
|
**Serial client**
|
||||||
|
|
||||||
|
Read the serial port and work with the parsed telegrams. It should be run in a separate
|
||||||
|
process because the code is blocking (not asynchronous):
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
from dsmr_parser import telegram_specifications
|
from dsmr_parser import telegram_specifications
|
||||||
from dsmr_parser.obis_references import P1_MESSAGE_TIMESTAMP
|
from dsmr_parser.clients import SerialReader, SERIAL_SETTINGS_V4
|
||||||
from dsmr_parser.serial import SerialReader, SERIAL_SETTINGS_V4
|
|
||||||
|
serial_reader = SerialReader(
|
||||||
|
device='/dev/ttyUSB0',
|
||||||
|
serial_settings=SERIAL_SETTINGS_V4,
|
||||||
|
telegram_specification=telegram_specifications.V4
|
||||||
|
)
|
||||||
|
|
||||||
|
for telegram in serial_reader.read():
|
||||||
|
print(telegram) # see 'Telegram object' docs below
|
||||||
|
|
||||||
serial_reader = SerialReader(
|
**AsyncIO client**
|
||||||
device='/dev/ttyUSB0',
|
|
||||||
serial_settings=SERIAL_SETTINGS_V4,
|
|
||||||
telegram_specification=telegram_specifications.V4
|
|
||||||
)
|
|
||||||
|
|
||||||
for telegram in serial_reader.read():
|
To be documented.
|
||||||
|
|
||||||
# The telegram message timestamp.
|
|
||||||
message_datetime = telegram[P1_MESSAGE_TIMESTAMP]
|
|
||||||
|
|
||||||
# Using the active tariff to determine the electricity being used and
|
Parsing module usage
|
||||||
# delivered for the right tariff.
|
--------------------
|
||||||
tariff = telegram[ELECTRICITY_ACTIVE_TARIFF]
|
The parsing module accepts complete unaltered telegram strings and parses these
|
||||||
tariff = int(tariff.value)
|
into a dictionary.
|
||||||
|
|
||||||
electricity_used_total \
|
.. code-block:: python
|
||||||
= telegram[ELECTRICITY_USED_TARIFF_ALL[tariff - 1]]
|
|
||||||
electricity_delivered_total = \
|
|
||||||
telegram[ELECTRICITY_DELIVERED_TARIFF_ALL[tariff - 1]]
|
|
||||||
|
|
||||||
gas_reading = telegram[HOURLY_GAS_METER_READING]
|
from dsmr_parser import telegram_specifications
|
||||||
|
from dsmr_parser.parsers import TelegramParser
|
||||||
|
|
||||||
# See dsmr_reader.obis_references for all readable telegram values.
|
telegram_str = (
|
||||||
|
'/ISk5\2MT382-1000\r\n'
|
||||||
|
'\r\n'
|
||||||
|
'0-0:96.1.1(4B384547303034303436333935353037)\r\n'
|
||||||
|
'1-0:1.8.1(12345.678*kWh)\r\n'
|
||||||
|
'1-0:1.8.2(12345.678*kWh)\r\n'
|
||||||
|
'1-0:2.8.1(12345.678*kWh)\r\n'
|
||||||
|
'1-0:2.8.2(12345.678*kWh)\r\n'
|
||||||
|
'0-0:96.14.0(0002)\r\n'
|
||||||
|
'1-0:1.7.0(001.19*kW)\r\n'
|
||||||
|
'1-0:2.7.0(000.00*kW)\r\n'
|
||||||
|
'0-0:17.0.0(016*A)\r\n'
|
||||||
|
'0-0:96.3.10(1)\r\n'
|
||||||
|
'0-0:96.13.1(303132333435363738)\r\n'
|
||||||
|
'0-0:96.13.0(303132333435363738393A3B3C3D3E3F303132333435363738393A3B3C3D3E'
|
||||||
|
'3F303132333435363738393A3B3C3D3E3F303132333435363738393A3B3C3D3E3F30313233'
|
||||||
|
'3435363738393A3B3C3D3E3F)\r\n'
|
||||||
|
'0-1:96.1.0(3232323241424344313233343536373839)\r\n'
|
||||||
|
'0-1:24.1.0(03)\r\n'
|
||||||
|
'0-1:24.3.0(090212160000)(00)(60)(1)(0-1:24.2.1)(m3)\r\n'
|
||||||
|
'(00001.001)\r\n'
|
||||||
|
'0-1:24.4.0(1)\r\n'
|
||||||
|
'!\r\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
parser = TelegramParser(telegram_specifications.V3)
|
||||||
|
|
||||||
|
telegram = parser.parse(telegram_str)
|
||||||
|
print(telegram) # see 'Telegram object' docs below
|
||||||
|
|
||||||
|
Telegram object
|
||||||
|
---------------
|
||||||
|
|
||||||
|
A dictionary of which the key indicates the field type. These regex values
|
||||||
|
correspond to one of dsmr_parser.obis_reference constants.
|
||||||
|
|
||||||
|
The value is either a CosemObject or MBusObject. These have a 'value' and 'unit'
|
||||||
|
property. MBusObject's additionally have a 'datetime' property. The 'value' can
|
||||||
|
contain any python type (int, str, Decimal) depending on the field. The 'unit'
|
||||||
|
contains 'kW', 'A', 'kWh' or 'm3'.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
# Contents of a parsed DSMR v3 telegram
|
||||||
|
{'\\d-\\d:17\\.0\\.0.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39eb8>,
|
||||||
|
'\\d-\\d:1\\.7\\.0.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10f916390>,
|
||||||
|
'\\d-\\d:1\\.8\\.1.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39e10>,
|
||||||
|
'\\d-\\d:1\\.8\\.2.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39ef0>,
|
||||||
|
'\\d-\\d:24\\.1\\.0.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fbaef28>,
|
||||||
|
'\\d-\\d:24\\.3\\.0.+?\\r\\n.+?\\r\\n': <dsmr_parser.objects.MBusObject object at 0x10f9163c8>,
|
||||||
|
'\\d-\\d:24\\.4\\.0.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39f60>,
|
||||||
|
'\\d-\\d:2\\.7\\.0.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39fd0>,
|
||||||
|
'\\d-\\d:2\\.8\\.1.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fbaee10>,
|
||||||
|
'\\d-\\d:2\\.8\\.2.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39e80>,
|
||||||
|
'\\d-\\d:96\\.13\\.0.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39d30>,
|
||||||
|
'\\d-\\d:96\\.13\\.1.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fbaeeb8>,
|
||||||
|
'\\d-\\d:96\\.14\\.0.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fbaef98>,
|
||||||
|
'\\d-\\d:96\\.1\\.0.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fbaef60>,
|
||||||
|
'\\d-\\d:96\\.1\\.1.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39f98>,
|
||||||
|
'\\d-\\d:96\\.3\\.10.+?\\r\\n': <dsmr_parser.objects.CosemObject object at 0x10fc39dd8>}
|
||||||
|
|
||||||
|
Example to get some of the values:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
from dsmr_parser import obis_references
|
||||||
|
|
||||||
|
# The telegram message timestamp.
|
||||||
|
message_datetime = telegram[obis_references.P1_MESSAGE_TIMESTAMP]
|
||||||
|
|
||||||
|
# Using the active tariff to determine the electricity being used and
|
||||||
|
# delivered for the right tariff.
|
||||||
|
active_tariff = telegram[obis_references.ELECTRICITY_ACTIVE_TARIFF]
|
||||||
|
active_tariff = int(tariff.value)
|
||||||
|
|
||||||
|
electricity_used_total = telegram[obis_references.ELECTRICITY_USED_TARIFF_ALL[active_tariff - 1]]
|
||||||
|
electricity_delivered_total = telegram[obis_references.ELECTRICITY_DELIVERED_TARIFF_ALL[active_tariff - 1]]
|
||||||
|
|
||||||
|
gas_reading = telegram[obis_references.HOURLY_GAS_METER_READING]
|
||||||
|
|
||||||
|
# See dsmr_reader.obis_references for all readable telegram values.
|
||||||
|
# Note that the avilable values differ per DSMR version.
|
||||||
|
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
@@ -60,10 +147,10 @@ To install DSMR Parser:
|
|||||||
|
|
||||||
$ pip install dsmr-parser
|
$ pip install dsmr-parser
|
||||||
|
|
||||||
|
Known issues
|
||||||
|
------------
|
||||||
|
|
||||||
TODO
|
If the serial settings SERIAL_SETTINGS_V2_2 or SERIAL_SETTINGS_V4 don't work.
|
||||||
----
|
Make sure to try and replace the parity settings to EVEN or NONE.
|
||||||
|
It's possible that alternative settings will be added in the future if these
|
||||||
- add unit tests
|
settings don't work for the majority of meters.
|
||||||
- verify telegram checksum
|
|
||||||
- improve ease of use
|
|
||||||
|
|||||||
+45
-12
@@ -1,6 +1,9 @@
|
|||||||
|
from functools import partial
|
||||||
import argparse
|
import argparse
|
||||||
from dsmr_parser.serial import SERIAL_SETTINGS_V2_2, SERIAL_SETTINGS_V4, SerialReader
|
import asyncio
|
||||||
from dsmr_parser import telegram_specifications
|
import logging
|
||||||
|
|
||||||
|
from dsmr_parser.clients import create_dsmr_reader, create_tcp_dsmr_reader
|
||||||
|
|
||||||
|
|
||||||
def console():
|
def console():
|
||||||
@@ -9,24 +12,54 @@ def console():
|
|||||||
parser = argparse.ArgumentParser(description=console.__doc__)
|
parser = argparse.ArgumentParser(description=console.__doc__)
|
||||||
parser.add_argument('--device', default='/dev/ttyUSB0',
|
parser.add_argument('--device', default='/dev/ttyUSB0',
|
||||||
help='port to read DSMR data from')
|
help='port to read DSMR data from')
|
||||||
|
parser.add_argument('--host', default=None,
|
||||||
|
help='alternatively connect using TCP host.')
|
||||||
|
parser.add_argument('--port', default=None,
|
||||||
|
help='TCP port to use for connection')
|
||||||
parser.add_argument('--version', default='2.2', choices=['2.2', '4'],
|
parser.add_argument('--version', default='2.2', choices=['2.2', '4'],
|
||||||
help='DSMR version (2.2, 4)')
|
help='DSMR version (2.2, 4)')
|
||||||
|
parser.add_argument('--verbose', '-v', action='count')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
settings = {
|
if args.verbose:
|
||||||
'2.2': (SERIAL_SETTINGS_V2_2, telegram_specifications.V2_2),
|
level = logging.DEBUG
|
||||||
'4': (SERIAL_SETTINGS_V4, telegram_specifications.V4),
|
else:
|
||||||
}
|
level = logging.ERROR
|
||||||
|
logging.basicConfig(level=level)
|
||||||
|
|
||||||
serial_reader = SerialReader(
|
loop = asyncio.get_event_loop()
|
||||||
device=args.device,
|
|
||||||
serial_settings=settings[args.version][0],
|
|
||||||
telegram_specification=settings[args.version][1],
|
|
||||||
)
|
|
||||||
|
|
||||||
for telegram in serial_reader.read():
|
def print_callback(telegram):
|
||||||
|
"""Callback that prints telegram values."""
|
||||||
for obiref, obj in telegram.items():
|
for obiref, obj in telegram.items():
|
||||||
if obj:
|
if obj:
|
||||||
print(obj.value, obj.unit)
|
print(obj.value, obj.unit)
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
# create tcp or serial connection depending on args
|
||||||
|
if args.host and args.port:
|
||||||
|
create_connection = partial(create_tcp_dsmr_reader,
|
||||||
|
args.host, args.port, args.version,
|
||||||
|
print_callback, loop=loop)
|
||||||
|
else:
|
||||||
|
create_connection = partial(create_dsmr_reader,
|
||||||
|
args.device, args.version,
|
||||||
|
print_callback, loop=loop)
|
||||||
|
|
||||||
|
try:
|
||||||
|
# connect and keep connected until interrupted by ctrl-c
|
||||||
|
while True:
|
||||||
|
# create serial or tcp connection
|
||||||
|
conn = create_connection()
|
||||||
|
transport, protocol = loop.run_until_complete(conn)
|
||||||
|
# wait until connection it closed
|
||||||
|
loop.run_until_complete(protocol.wait_closed())
|
||||||
|
# wait 5 seconds before attempting reconnect
|
||||||
|
loop.run_until_complete(asyncio.sleep(5))
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
# cleanup connection after user initiated shutdown
|
||||||
|
transport.close()
|
||||||
|
loop.run_until_complete(asyncio.sleep(0))
|
||||||
|
finally:
|
||||||
|
loop.close()
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
from dsmr_parser.clients.settings import SERIAL_SETTINGS_V2_2, \
|
||||||
|
SERIAL_SETTINGS_V4
|
||||||
|
from dsmr_parser.clients.serial_ import SerialReader, AsyncSerialReader
|
||||||
|
from dsmr_parser.clients.protocol import create_dsmr_protocol, \
|
||||||
|
create_dsmr_reader, create_tcp_dsmr_reader
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
"""Asyncio protocol implementation for handling telegrams."""
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from serial_asyncio import create_serial_connection
|
||||||
|
|
||||||
|
from dsmr_parser import telegram_specifications
|
||||||
|
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
|
||||||
|
from dsmr_parser.exceptions import ParseError
|
||||||
|
from dsmr_parser.parsers import TelegramParser
|
||||||
|
from dsmr_parser.clients.settings import SERIAL_SETTINGS_V2_2, \
|
||||||
|
SERIAL_SETTINGS_V4
|
||||||
|
|
||||||
|
|
||||||
|
def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None):
|
||||||
|
"""Creates a DSMR asyncio protocol."""
|
||||||
|
|
||||||
|
if dsmr_version == '2.2':
|
||||||
|
specification = telegram_specifications.V2_2
|
||||||
|
serial_settings = SERIAL_SETTINGS_V2_2
|
||||||
|
elif dsmr_version == '4':
|
||||||
|
specification = telegram_specifications.V4
|
||||||
|
serial_settings = SERIAL_SETTINGS_V4
|
||||||
|
else:
|
||||||
|
raise NotImplementedError("No telegram parser found for version: %s",
|
||||||
|
dsmr_version)
|
||||||
|
|
||||||
|
protocol = partial(DSMRProtocol, loop, TelegramParser(specification),
|
||||||
|
telegram_callback=telegram_callback)
|
||||||
|
|
||||||
|
return protocol, serial_settings
|
||||||
|
|
||||||
|
|
||||||
|
def create_dsmr_reader(port, dsmr_version, telegram_callback, loop=None):
|
||||||
|
"""Creates a DSMR asyncio protocol coroutine using serial port."""
|
||||||
|
protocol, serial_settings = create_dsmr_protocol(
|
||||||
|
dsmr_version, telegram_callback, loop=None)
|
||||||
|
serial_settings['url'] = port
|
||||||
|
|
||||||
|
conn = create_serial_connection(loop, protocol, **serial_settings)
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
def create_tcp_dsmr_reader(host, port, dsmr_version,
|
||||||
|
telegram_callback, loop=None):
|
||||||
|
"""Creates a DSMR asyncio protocol coroutine using TCP connection."""
|
||||||
|
protocol, _ = create_dsmr_protocol(
|
||||||
|
dsmr_version, telegram_callback, loop=None)
|
||||||
|
conn = loop.create_connection(protocol, host, port)
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
|
class DSMRProtocol(asyncio.Protocol):
|
||||||
|
"""Assemble and handle incoming data into complete DSM telegrams."""
|
||||||
|
|
||||||
|
transport = None
|
||||||
|
telegram_callback = None
|
||||||
|
|
||||||
|
def __init__(self, loop, telegram_parser, telegram_callback=None):
|
||||||
|
"""Initialize class."""
|
||||||
|
self.loop = loop
|
||||||
|
self.log = logging.getLogger(__name__)
|
||||||
|
self.telegram_parser = telegram_parser
|
||||||
|
# callback to call on complete telegram
|
||||||
|
self.telegram_callback = telegram_callback
|
||||||
|
# buffer to keep incomplete incoming data
|
||||||
|
self.telegram_buffer = TelegramBuffer()
|
||||||
|
# keep a lock until the connection is closed
|
||||||
|
self._closed = asyncio.Event()
|
||||||
|
|
||||||
|
def connection_made(self, transport):
|
||||||
|
"""Just logging for now."""
|
||||||
|
self.transport = transport
|
||||||
|
self.log.debug('connected')
|
||||||
|
|
||||||
|
def data_received(self, data):
|
||||||
|
"""Add incoming data to buffer."""
|
||||||
|
data = data.decode('ascii')
|
||||||
|
self.log.debug('received data: %s', data)
|
||||||
|
self.telegram_buffer.append(data)
|
||||||
|
|
||||||
|
for telegram in self.telegram_buffer.get_all():
|
||||||
|
self.handle_telegram(telegram)
|
||||||
|
|
||||||
|
def connection_lost(self, exc):
|
||||||
|
"""Stop when connection is lost."""
|
||||||
|
if exc:
|
||||||
|
self.log.exception('disconnected due to exception')
|
||||||
|
else:
|
||||||
|
self.log.info('disconnected because of close/abort.')
|
||||||
|
self._closed.set()
|
||||||
|
|
||||||
|
def handle_telegram(self, telegram):
|
||||||
|
"""Send off parsed telegram to handling callback."""
|
||||||
|
self.log.debug('got telegram: %s', telegram)
|
||||||
|
|
||||||
|
try:
|
||||||
|
parsed_telegram = self.telegram_parser.parse(telegram)
|
||||||
|
except ParseError:
|
||||||
|
self.log.exception("failed to parse telegram")
|
||||||
|
else:
|
||||||
|
self.telegram_callback(parsed_telegram)
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def wait_closed(self):
|
||||||
|
"""Wait until connection is closed."""
|
||||||
|
yield from self._closed.wait()
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import serial
|
||||||
|
import serial_asyncio
|
||||||
|
|
||||||
|
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
|
||||||
|
from dsmr_parser.exceptions import ParseError
|
||||||
|
from dsmr_parser.parsers import TelegramParser
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class SerialReader(object):
|
||||||
|
PORT_KEY = 'port'
|
||||||
|
|
||||||
|
def __init__(self, device, serial_settings, telegram_specification):
|
||||||
|
self.serial_settings = serial_settings
|
||||||
|
self.serial_settings[self.PORT_KEY] = device
|
||||||
|
|
||||||
|
self.telegram_parser = TelegramParser(telegram_specification)
|
||||||
|
self.telegram_buffer = TelegramBuffer()
|
||||||
|
|
||||||
|
def read(self):
|
||||||
|
"""
|
||||||
|
Read complete DSMR telegram's from the serial interface and parse it
|
||||||
|
into CosemObject's and MbusObject's
|
||||||
|
|
||||||
|
:rtype: generator
|
||||||
|
"""
|
||||||
|
with serial.Serial(**self.serial_settings) as serial_handle:
|
||||||
|
while True:
|
||||||
|
data = serial_handle.readline()
|
||||||
|
self.telegram_buffer.append(data.decode('ascii'))
|
||||||
|
|
||||||
|
for telegram in self.telegram_buffer.get_all():
|
||||||
|
try:
|
||||||
|
yield self.telegram_parser.parse(telegram)
|
||||||
|
except ParseError as e:
|
||||||
|
logger.error('Failed to parse telegram: %s', e)
|
||||||
|
|
||||||
|
|
||||||
|
class AsyncSerialReader(SerialReader):
|
||||||
|
"""Serial reader using asyncio pyserial."""
|
||||||
|
|
||||||
|
PORT_KEY = 'url'
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def read(self, queue):
|
||||||
|
"""
|
||||||
|
Read complete DSMR telegram's from the serial interface and parse it
|
||||||
|
into CosemObject's and MbusObject's.
|
||||||
|
|
||||||
|
Instead of being a generator, values are pushed to provided queue for
|
||||||
|
asynchronous processing.
|
||||||
|
|
||||||
|
:rtype: None
|
||||||
|
"""
|
||||||
|
# create Serial StreamReader
|
||||||
|
conn = serial_asyncio.open_serial_connection(**self.serial_settings)
|
||||||
|
reader, _ = yield from conn
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# Read line if available or give control back to loop until new
|
||||||
|
# data has arrived.
|
||||||
|
data = yield from reader.readline()
|
||||||
|
self.telegram_buffer.append(data.decode('ascii'))
|
||||||
|
|
||||||
|
for telegram in self.telegram_buffer.get_all():
|
||||||
|
try:
|
||||||
|
# Push new parsed telegram onto queue.
|
||||||
|
queue.put_nowait(
|
||||||
|
self.telegram_parser.parse(telegram)
|
||||||
|
)
|
||||||
|
except ParseError as e:
|
||||||
|
logger.warning('Failed to parse telegram: %s', e)
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import serial
|
||||||
|
|
||||||
|
|
||||||
|
SERIAL_SETTINGS_V2_2 = {
|
||||||
|
'baudrate': 9600,
|
||||||
|
'bytesize': serial.SEVENBITS,
|
||||||
|
'parity': serial.PARITY_EVEN,
|
||||||
|
'stopbits': serial.STOPBITS_ONE,
|
||||||
|
'xonxoff': 0,
|
||||||
|
'rtscts': 0,
|
||||||
|
'timeout': 20
|
||||||
|
}
|
||||||
|
|
||||||
|
SERIAL_SETTINGS_V4 = {
|
||||||
|
'baudrate': 115200,
|
||||||
|
'bytesize': serial.SEVENBITS,
|
||||||
|
'parity': serial.PARITY_EVEN,
|
||||||
|
'stopbits': serial.STOPBITS_ONE,
|
||||||
|
'xonxoff': 0,
|
||||||
|
'rtscts': 0,
|
||||||
|
'timeout': 20
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
class TelegramBuffer(object):
|
||||||
|
"""
|
||||||
|
Used as a buffer for a stream of telegram data. Constructs full telegram
|
||||||
|
strings from the buffered data and returns it.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._buffer = ''
|
||||||
|
|
||||||
|
def get_all(self):
|
||||||
|
"""
|
||||||
|
Remove complete telegrams from buffer and yield them.
|
||||||
|
:rtype generator:
|
||||||
|
"""
|
||||||
|
for telegram in self._find_telegrams():
|
||||||
|
self._remove(telegram)
|
||||||
|
yield telegram
|
||||||
|
|
||||||
|
def append(self, data):
|
||||||
|
"""
|
||||||
|
Add telegram data to buffer.
|
||||||
|
:param str data: chars, lines or full telegram strings of telegram data
|
||||||
|
"""
|
||||||
|
self._buffer += data
|
||||||
|
|
||||||
|
def _remove(self, telegram):
|
||||||
|
"""
|
||||||
|
Remove telegram from buffer and incomplete data preceding it. This
|
||||||
|
is easier than validating the data before adding it to the buffer.
|
||||||
|
:param str telegram:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
# Remove data leading up to the telegram and the telegram itself.
|
||||||
|
index = self._buffer.index(telegram) + len(telegram)
|
||||||
|
|
||||||
|
self._buffer = self._buffer[index:]
|
||||||
|
|
||||||
|
def _find_telegrams(self):
|
||||||
|
"""
|
||||||
|
Find complete telegrams in buffer from start ('/') till ending
|
||||||
|
checksum ('!AB12\r\n').
|
||||||
|
:rtype: list
|
||||||
|
"""
|
||||||
|
# - Match all characters after start of telegram except for the start
|
||||||
|
# itself again '^\/]+', which eliminates incomplete preceding telegrams.
|
||||||
|
# - Do non greedy match using '?' so start is matched up to the first
|
||||||
|
# checksum that's found.
|
||||||
|
# - The checksum is optional '{0,4}' because not all telegram versions
|
||||||
|
# support it.
|
||||||
|
return re.findall(
|
||||||
|
r'\/[^\/]+?\![A-F0-9]{0,4}\r\n',
|
||||||
|
self._buffer,
|
||||||
|
re.DOTALL
|
||||||
|
)
|
||||||
@@ -1,2 +1,10 @@
|
|||||||
class ParseError(Exception):
|
class ParseError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidChecksumError(ParseError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class TelegramSpecificationMatchError(ParseError):
|
||||||
|
pass
|
||||||
|
|||||||
@@ -1,36 +1,48 @@
|
|||||||
P1_MESSAGE_HEADER = r'1-3:0\.2\.8'
|
"""
|
||||||
P1_MESSAGE_TIMESTAMP = r'0-0:1\.0\.0'
|
Contains the signatures of each telegram line.
|
||||||
ELECTRICITY_USED_TARIFF_1 = r'1-0:1\.8\.1'
|
|
||||||
ELECTRICITY_USED_TARIFF_2 = r'1-0:1\.8\.2'
|
Previously contained the channel + obis reference signatures, but has been
|
||||||
ELECTRICITY_DELIVERED_TARIFF_1 = r'1-0:2\.8\.1'
|
refactored to full line signatures to maintain backwards compatibility.
|
||||||
ELECTRICITY_DELIVERED_TARIFF_2 = r'1-0:2\.8\.2'
|
Might be refactored in a backwards incompatible way as soon as proper telegram
|
||||||
ELECTRICITY_ACTIVE_TARIFF = r'0-0:96\.14\.0'
|
objects are introduced.
|
||||||
EQUIPMENT_IDENTIFIER = r'0-0:96\.1\.1'
|
"""
|
||||||
CURRENT_ELECTRICITY_USAGE = r'1-0:1\.7\.0'
|
P1_MESSAGE_HEADER = r'\d-\d:0\.2\.8.+?\r\n'
|
||||||
CURRENT_ELECTRICITY_DELIVERY = r'1-0:2\.7\.0'
|
P1_MESSAGE_TIMESTAMP = r'\d-\d:1\.0\.0.+?\r\n'
|
||||||
LONG_POWER_FAILURE_COUNT = r'96\.7\.9'
|
ELECTRICITY_USED_TARIFF_1 = r'\d-\d:1\.8\.1.+?\r\n'
|
||||||
POWER_EVENT_FAILURE_LOG = r'99\.97\.0'
|
ELECTRICITY_USED_TARIFF_2 = r'\d-\d:1\.8\.2.+?\r\n'
|
||||||
VOLTAGE_SAG_L1_COUNT = r'1-0:32\.32\.0'
|
ELECTRICITY_DELIVERED_TARIFF_1 = r'\d-\d:2\.8\.1.+?\r\n'
|
||||||
VOLTAGE_SAG_L2_COUNT = r'1-0:52\.32\.0'
|
ELECTRICITY_DELIVERED_TARIFF_2 = r'\d-\d:2\.8\.2.+?\r\n'
|
||||||
VOLTAGE_SAG_L3_COUNT = r'1-0:72\.32\.0'
|
ELECTRICITY_ACTIVE_TARIFF = r'\d-\d:96\.14\.0.+?\r\n'
|
||||||
VOLTAGE_SWELL_L1_COUNT = r'1-0:32\.36\.0'
|
EQUIPMENT_IDENTIFIER = r'\d-\d:96\.1\.1.+?\r\n'
|
||||||
VOLTAGE_SWELL_L2_COUNT = r'1-0:52\.36\.0'
|
CURRENT_ELECTRICITY_USAGE = r'\d-\d:1\.7\.0.+?\r\n'
|
||||||
VOLTAGE_SWELL_L3_COUNT = r'1-0:72\.36\.0'
|
CURRENT_ELECTRICITY_DELIVERY = r'\d-\d:2\.7\.0.+?\r\n'
|
||||||
TEXT_MESSAGE_CODE = r'0-0:96\.13\.1'
|
LONG_POWER_FAILURE_COUNT = r'96\.7\.9.+?\r\n'
|
||||||
TEXT_MESSAGE = r'0-0:96\.13\.0'
|
POWER_EVENT_FAILURE_LOG = r'99\.97\.0.+?\r\n'
|
||||||
DEVICE_TYPE = r'0-\d:24\.1\.0'
|
VOLTAGE_SAG_L1_COUNT = r'\d-\d:32\.32\.0.+?\r\n'
|
||||||
INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE = r'1-0:21\.7\.0'
|
VOLTAGE_SAG_L2_COUNT = r'\d-\d:52\.32\.0.+?\r\n'
|
||||||
INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE = r'1-0:41\.7\.0'
|
VOLTAGE_SAG_L3_COUNT = r'\d-\d:72\.32\.0.+?\r\n'
|
||||||
INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE = r'1-0:61\.7\.0'
|
VOLTAGE_SWELL_L1_COUNT = r'\d-\d:32\.36\.0.+?\r\n'
|
||||||
INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE = r'1-0:22\.7\.0'
|
VOLTAGE_SWELL_L2_COUNT = r'\d-\d:52\.36\.0.+?\r\n'
|
||||||
INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE = r'1-0:42\.7\.0'
|
VOLTAGE_SWELL_L3_COUNT = r'\d-\d:72\.36\.0.+?\r\n'
|
||||||
INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE = r'1-0:62\.7\.0'
|
TEXT_MESSAGE_CODE = r'\d-\d:96\.13\.1.+?\r\n'
|
||||||
EQUIPMENT_IDENTIFIER_GAS = r'0-\d:96\.1\.0'
|
TEXT_MESSAGE = r'\d-\d:96\.13\.0.+?\r\n'
|
||||||
HOURLY_GAS_METER_READING = r'0-1:24\.2\.1'
|
DEVICE_TYPE = r'\d-\d:24\.1\.0.+?\r\n'
|
||||||
GAS_METER_READING = r'0-\d:24\.3\.0'
|
INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE = r'\d-\d:21\.7\.0.+?\r\n'
|
||||||
ACTUAL_TRESHOLD_ELECTRICITY = r'0-0:17\.0\.0'
|
INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE = r'\d-\d:41\.7\.0.+?\r\n'
|
||||||
ACTUAL_SWITCH_POSITION = r'0-0:96\.3\.10'
|
INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE = r'\d-\d:61\.7\.0.+?\r\n'
|
||||||
VALVE_POSITION_GAS = r'0-\d:24\.4\.0'
|
INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE = r'\d-\d:22\.7\.0.+?\r\n'
|
||||||
|
INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE = r'\d-\d:42\.7\.0.+?\r\n'
|
||||||
|
INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE = r'\d-\d:62\.7\.0.+?\r\n'
|
||||||
|
EQUIPMENT_IDENTIFIER_GAS = r'\d-\d:96\.1\.0.+?\r\n'
|
||||||
|
# TODO differences between gas meter readings in v3 and lower and v4 and up
|
||||||
|
HOURLY_GAS_METER_READING = r'\d-\d:24\.2\.1.+?\r\n'
|
||||||
|
GAS_METER_READING = r'\d-\d:24\.3\.0.+?\r\n.+?\r\n'
|
||||||
|
ACTUAL_TRESHOLD_ELECTRICITY = r'\d-\d:17\.0\.0.+?\r\n'
|
||||||
|
ACTUAL_SWITCH_POSITION = r'\d-\d:96\.3\.10.+?\r\n'
|
||||||
|
VALVE_POSITION_GAS = r'\d-\d:24\.4\.0.+?\r\n'
|
||||||
|
|
||||||
|
# TODO 17.0.0
|
||||||
|
# TODO 96.3.10
|
||||||
|
|
||||||
ELECTRICITY_USED_TARIFF_ALL = (
|
ELECTRICITY_USED_TARIFF_ALL = (
|
||||||
ELECTRICITY_USED_TARIFF_1,
|
ELECTRICITY_USED_TARIFF_1,
|
||||||
|
|||||||
+18
-19
@@ -1,4 +1,7 @@
|
|||||||
class DSMRObject(object):
|
class DSMRObject(object):
|
||||||
|
"""
|
||||||
|
Represents all data from a single telegram line.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, values):
|
def __init__(self, values):
|
||||||
self.values = values
|
self.values = values
|
||||||
@@ -12,26 +15,23 @@ class MBusObject(DSMRObject):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
return self.values[1]['value']
|
# TODO temporary workaround for DSMR v2.2. Maybe use the same type of
|
||||||
|
# TODO object, but let the parse set them differently? So don't use
|
||||||
|
# TODO hardcoded indexes here.
|
||||||
|
if len(self.values) != 2: # v2
|
||||||
|
return self.values[5]['value']
|
||||||
|
else:
|
||||||
|
return self.values[1]['value']
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit(self):
|
def unit(self):
|
||||||
return self.values[1]['unit']
|
# TODO temporary workaround for DSMR v2.2. Maybe use the same type of
|
||||||
|
# TODO object, but let the parse set them differently? So don't use
|
||||||
|
# TODO hardcoded indexes here.
|
||||||
class MBusObjectV2_2(DSMRObject):
|
if len(self.values) != 2: # v2
|
||||||
|
return self.values[4]['value']
|
||||||
@property
|
else:
|
||||||
def datetime(self):
|
return self.values[1]['unit']
|
||||||
return self.values[0]['value']
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self):
|
|
||||||
return self.values[5]['value']
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unit(self):
|
|
||||||
return self.values[4]['value']
|
|
||||||
|
|
||||||
|
|
||||||
class CosemObject(DSMRObject):
|
class CosemObject(DSMRObject):
|
||||||
@@ -46,5 +46,4 @@ class CosemObject(DSMRObject):
|
|||||||
|
|
||||||
|
|
||||||
class ProfileGeneric(DSMRObject):
|
class ProfileGeneric(DSMRObject):
|
||||||
pass
|
pass # TODO implement
|
||||||
# TODO implement
|
|
||||||
|
|||||||
+110
-41
@@ -1,73 +1,137 @@
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from .objects import MBusObject, MBusObjectV2_2, CosemObject
|
from PyCRC.CRC16 import CRC16
|
||||||
from .exceptions import ParseError
|
|
||||||
from .obis_references import GAS_METER_READING
|
from dsmr_parser.objects import MBusObject, CosemObject
|
||||||
|
from dsmr_parser.exceptions import ParseError, InvalidChecksumError, \
|
||||||
|
TelegramSpecificationMatchError
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class TelegramParser(object):
|
class TelegramParser(object):
|
||||||
|
|
||||||
def __init__(self, telegram_specification):
|
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
|
:type telegram_specification: dict
|
||||||
"""
|
"""
|
||||||
self.telegram_specification = telegram_specification
|
self.telegram_specification = telegram_specification
|
||||||
|
self.apply_checksum_validation = apply_checksum_validation
|
||||||
|
|
||||||
def _find_line_parser(self, line_value):
|
def parse(self, telegram_data):
|
||||||
|
"""
|
||||||
|
Parse telegram from string to dict.
|
||||||
|
|
||||||
for obis_reference, parser in self.telegram_specification.items():
|
The telegram str type makes python 2.x integration easier.
|
||||||
if re.search(obis_reference, line_value):
|
|
||||||
return obis_reference, parser
|
|
||||||
|
|
||||||
return None, None
|
: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:
|
||||||
|
{
|
||||||
|
..
|
||||||
|
r'\d-\d:96\.1\.1.+?\r\n': <CosemObject>, # EQUIPMENT_IDENTIFIER
|
||||||
|
r'\d-\d:1\.8\.1.+?\r\n': <CosemObject>, # ELECTRICITY_USED_TARIFF_1
|
||||||
|
r'\d-\d:24\.3\.0.+?\r\n.+?\r\n': <MBusObject>, # GAS_METER_READING
|
||||||
|
..
|
||||||
|
}
|
||||||
|
:raises ParseError:
|
||||||
|
"""
|
||||||
|
if not self.telegram_specification:
|
||||||
|
self.telegram_specification = \
|
||||||
|
match_telegram_specification(telegram_data)
|
||||||
|
|
||||||
|
if self.apply_checksum_validation \
|
||||||
|
and self.telegram_specification['checksum_support']:
|
||||||
|
self.validate_checksum(telegram_data)
|
||||||
|
|
||||||
def parse(self, line_values):
|
|
||||||
telegram = {}
|
telegram = {}
|
||||||
|
|
||||||
for line_value in line_values:
|
for signature, parser in self.telegram_specification['objects'].items():
|
||||||
obis_reference, dsmr_object = self.parse_line(line_value.strip())
|
match = re.search(signature, telegram_data, re.DOTALL)
|
||||||
|
|
||||||
telegram[obis_reference] = dsmr_object
|
# 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
|
return telegram
|
||||||
|
|
||||||
def parse_line(self, line_value):
|
@staticmethod
|
||||||
logger.debug('Parsing line\'%s\'', line_value)
|
def validate_checksum(telegram):
|
||||||
|
"""
|
||||||
|
:param str telegram:
|
||||||
|
:raises ParseError:
|
||||||
|
:raises InvalidChecksumError:
|
||||||
|
"""
|
||||||
|
|
||||||
obis_reference, parser = self._find_line_parser(line_value)
|
# Extract the part for which the checksum applies.
|
||||||
|
checksum_contents = re.search(r'\/.+\!', telegram, re.DOTALL)
|
||||||
|
|
||||||
if not parser:
|
# Extract the hexadecimal checksum value itself.
|
||||||
logger.warning("No line class found for: '%s'", line_value)
|
# The line ending '\r\n' for the checksum line can be ignored.
|
||||||
return None, None
|
checksum_hex = re.search(r'((?<=\!)[0-9A-Z]{4})+', telegram)
|
||||||
|
|
||||||
return obis_reference, parser.parse(line_value)
|
if not checksum_contents or not checksum_hex:
|
||||||
|
raise ParseError(
|
||||||
|
'Failed to perform CRC validation because the telegram is '
|
||||||
|
'incomplete. The checksum and/or content values are missing.'
|
||||||
|
)
|
||||||
|
|
||||||
|
calculated_crc = CRC16().calculate(checksum_contents.group(0))
|
||||||
|
expected_crc = int(checksum_hex.group(0), base=16)
|
||||||
|
|
||||||
|
if calculated_crc != expected_crc:
|
||||||
|
raise InvalidChecksumError(
|
||||||
|
"Invalid telegram. The CRC checksum '{}' does not match the "
|
||||||
|
"expected '{}'".format(
|
||||||
|
calculated_crc,
|
||||||
|
expected_crc
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class TelegramParserV2_2(TelegramParser):
|
def match_telegram_specification(telegram_data):
|
||||||
def parse(self, line_values):
|
"""
|
||||||
"""Join lines for gas meter."""
|
Find telegram specification that matches the telegram data by trying all
|
||||||
|
specifications.
|
||||||
|
|
||||||
def join_lines(line_values):
|
Could be further optimized to check the actual 0.2.8 OBIS reference which
|
||||||
join_next = re.compile(GAS_METER_READING)
|
is available for DSMR version 4 and up.
|
||||||
|
|
||||||
join = None
|
:param str telegram_data: full telegram from start ('/') to checksum
|
||||||
for line_value in line_values:
|
('!ABCD') including line endings in between the telegram's lines
|
||||||
if join:
|
:return: telegram specification
|
||||||
yield join.strip() + line_value
|
:rtype: dict
|
||||||
join = None
|
"""
|
||||||
elif join_next.match(line_value):
|
# Prevent circular import
|
||||||
join = line_value
|
from dsmr_parser import telegram_specifications
|
||||||
else:
|
|
||||||
yield line_value
|
|
||||||
|
|
||||||
return super().parse(join_lines(line_values))
|
for specification in telegram_specifications.ALL:
|
||||||
|
try:
|
||||||
|
TelegramParser(specification).parse(telegram_data)
|
||||||
|
except ParseError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
return specification
|
||||||
|
|
||||||
|
raise TelegramSpecificationMatchError(
|
||||||
|
'Could automatically match telegram specification. Make sure the data'
|
||||||
|
'is not corrupt. Alternatively manually specify one.'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class DSMRObjectParser(object):
|
class DSMRObjectParser(object):
|
||||||
|
"""
|
||||||
|
Parses an object (can also be see as a 'line') from a telegram.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, *value_formats):
|
def __init__(self, *value_formats):
|
||||||
self.value_formats = value_formats
|
self.value_formats = value_formats
|
||||||
@@ -105,11 +169,7 @@ class MBusParser(DSMRObjectParser):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def parse(self, line):
|
def parse(self, line):
|
||||||
values = self._parse(line)
|
return MBusObject(self._parse(line))
|
||||||
if len(values) == 2:
|
|
||||||
return MBusObject(values)
|
|
||||||
else:
|
|
||||||
return MBusObjectV2_2(values)
|
|
||||||
|
|
||||||
|
|
||||||
class CosemParser(DSMRObjectParser):
|
class CosemParser(DSMRObjectParser):
|
||||||
@@ -162,6 +222,15 @@ class ProfileGenericParser(DSMRObjectParser):
|
|||||||
|
|
||||||
|
|
||||||
class ValueParser(object):
|
class ValueParser(object):
|
||||||
|
"""
|
||||||
|
Parses a single value from DSMRObject's.
|
||||||
|
|
||||||
|
Example with coerce_type being int:
|
||||||
|
(002*A) becomes {'value': 1, 'unit': 'A'}
|
||||||
|
|
||||||
|
Example with coerce_type being str:
|
||||||
|
(42) becomes {'value': '42', 'unit': None}
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, coerce_type):
|
def __init__(self, coerce_type):
|
||||||
self.coerce_type = coerce_type
|
self.coerce_type = coerce_type
|
||||||
|
|||||||
@@ -1,113 +0,0 @@
|
|||||||
import serial
|
|
||||||
import asyncio
|
|
||||||
import serial_asyncio
|
|
||||||
from dsmr_parser.parsers import TelegramParser, TelegramParserV2_2
|
|
||||||
|
|
||||||
SERIAL_SETTINGS_V2_2 = {
|
|
||||||
'baudrate': 9600,
|
|
||||||
'bytesize': serial.SEVENBITS,
|
|
||||||
'parity': serial.PARITY_NONE,
|
|
||||||
'stopbits': serial.STOPBITS_ONE,
|
|
||||||
'xonxoff': 0,
|
|
||||||
'rtscts': 0,
|
|
||||||
'timeout': 20
|
|
||||||
}
|
|
||||||
|
|
||||||
SERIAL_SETTINGS_V4 = {
|
|
||||||
'baudrate': 115200,
|
|
||||||
'bytesize': serial.SEVENBITS,
|
|
||||||
'parity': serial.PARITY_EVEN,
|
|
||||||
'stopbits': serial.STOPBITS_ONE,
|
|
||||||
'xonxoff': 0,
|
|
||||||
'rtscts': 0,
|
|
||||||
'timeout': 20
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def is_start_of_telegram(line):
|
|
||||||
return line.startswith('/')
|
|
||||||
|
|
||||||
|
|
||||||
def is_end_of_telegram(line):
|
|
||||||
return line.startswith('!')
|
|
||||||
|
|
||||||
|
|
||||||
class SerialReader(object):
|
|
||||||
|
|
||||||
PORT_KEY = 'port'
|
|
||||||
|
|
||||||
def __init__(self, device, serial_settings, telegram_specification):
|
|
||||||
self.serial_settings = serial_settings
|
|
||||||
self.serial_settings[self.PORT_KEY] = device
|
|
||||||
|
|
||||||
if serial_settings is SERIAL_SETTINGS_V2_2:
|
|
||||||
telegram_parser = TelegramParserV2_2
|
|
||||||
else:
|
|
||||||
telegram_parser = TelegramParser
|
|
||||||
self.telegram_parser = telegram_parser(telegram_specification)
|
|
||||||
|
|
||||||
def read(self):
|
|
||||||
"""
|
|
||||||
Read complete DSMR telegram's from the serial interface and parse it
|
|
||||||
into CosemObject's and MbusObject's
|
|
||||||
|
|
||||||
:rtype dict
|
|
||||||
"""
|
|
||||||
with serial.Serial(**self.serial_settings) as serial_handle:
|
|
||||||
telegram = []
|
|
||||||
|
|
||||||
while True:
|
|
||||||
line = serial_handle.readline()
|
|
||||||
line = line.decode('ascii')
|
|
||||||
|
|
||||||
# Telegrams need to be complete because the values belong to a
|
|
||||||
# particular reading and can also be related to eachother.
|
|
||||||
if not telegram and not is_start_of_telegram(line):
|
|
||||||
continue
|
|
||||||
|
|
||||||
telegram.append(line)
|
|
||||||
|
|
||||||
if is_end_of_telegram(line):
|
|
||||||
yield self.telegram_parser.parse(telegram)
|
|
||||||
telegram = []
|
|
||||||
|
|
||||||
|
|
||||||
class AsyncSerialReader(SerialReader):
|
|
||||||
"""Serial reader using asyncio pyserial."""
|
|
||||||
|
|
||||||
PORT_KEY = 'url'
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
|
||||||
def read(self, queue):
|
|
||||||
"""
|
|
||||||
Read complete DSMR telegram's from the serial interface and parse it
|
|
||||||
into CosemObject's and MbusObject's.
|
|
||||||
|
|
||||||
Instead of being a generator, values are pushed to provided queue for
|
|
||||||
asynchronous processing.
|
|
||||||
|
|
||||||
:rtype Generator/Async
|
|
||||||
"""
|
|
||||||
# create Serial StreamReader
|
|
||||||
conn = serial_asyncio.open_serial_connection(**self.serial_settings)
|
|
||||||
reader, _ = yield from conn
|
|
||||||
|
|
||||||
telegram = []
|
|
||||||
|
|
||||||
while True:
|
|
||||||
# read line if available or give control back to loop until
|
|
||||||
# new data has arrived
|
|
||||||
line = yield from reader.readline()
|
|
||||||
line = line.decode('ascii')
|
|
||||||
|
|
||||||
# Telegrams need to be complete because the values belong to a
|
|
||||||
# particular reading and can also be related to eachother.
|
|
||||||
if not telegram and not is_start_of_telegram(line):
|
|
||||||
continue
|
|
||||||
|
|
||||||
telegram.append(line)
|
|
||||||
|
|
||||||
if is_end_of_telegram(line):
|
|
||||||
# push new parsed telegram onto queue
|
|
||||||
queue.put_nowait(self.telegram_parser.parse(telegram))
|
|
||||||
telegram = []
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from . import obis_references as obis
|
from dsmr_parser import obis_references as obis
|
||||||
from .parsers import CosemParser, ValueParser, MBusParser
|
from dsmr_parser.parsers import CosemParser, ValueParser, MBusParser
|
||||||
from .value_types import timestamp
|
from dsmr_parser.value_types import timestamp
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@@ -14,60 +14,109 @@ how the telegram lines are parsed.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
V2_2 = {
|
V2_2 = {
|
||||||
obis.EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
'checksum_support': False,
|
||||||
obis.ELECTRICITY_USED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
'objects': {
|
||||||
obis.ELECTRICITY_USED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
obis.EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||||
obis.ELECTRICITY_DELIVERED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
obis.ELECTRICITY_USED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
||||||
obis.ELECTRICITY_DELIVERED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
obis.ELECTRICITY_USED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
||||||
obis.ELECTRICITY_ACTIVE_TARIFF: CosemParser(ValueParser(str)),
|
obis.ELECTRICITY_DELIVERED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
||||||
obis.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
|
obis.ELECTRICITY_DELIVERED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
||||||
obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)),
|
obis.ELECTRICITY_ACTIVE_TARIFF: CosemParser(ValueParser(str)),
|
||||||
obis.ACTUAL_TRESHOLD_ELECTRICITY: CosemParser(ValueParser(Decimal)),
|
obis.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
|
||||||
obis.ACTUAL_SWITCH_POSITION: CosemParser(ValueParser(str)),
|
obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)),
|
||||||
obis.TEXT_MESSAGE_CODE: CosemParser(ValueParser(int)),
|
obis.ACTUAL_TRESHOLD_ELECTRICITY: CosemParser(ValueParser(Decimal)),
|
||||||
obis.TEXT_MESSAGE: CosemParser(ValueParser(str)),
|
obis.ACTUAL_SWITCH_POSITION: CosemParser(ValueParser(str)),
|
||||||
obis.EQUIPMENT_IDENTIFIER_GAS: CosemParser(ValueParser(str)),
|
obis.TEXT_MESSAGE_CODE: CosemParser(ValueParser(int)),
|
||||||
obis.DEVICE_TYPE: CosemParser(ValueParser(str)),
|
obis.TEXT_MESSAGE: CosemParser(ValueParser(str)),
|
||||||
obis.VALVE_POSITION_GAS: CosemParser(ValueParser(str)),
|
obis.EQUIPMENT_IDENTIFIER_GAS: CosemParser(ValueParser(str)),
|
||||||
obis.GAS_METER_READING: MBusParser(
|
obis.DEVICE_TYPE: CosemParser(ValueParser(str)),
|
||||||
ValueParser(timestamp),
|
obis.VALVE_POSITION_GAS: CosemParser(ValueParser(str)),
|
||||||
ValueParser(int),
|
obis.GAS_METER_READING: MBusParser(
|
||||||
ValueParser(int),
|
ValueParser(timestamp),
|
||||||
ValueParser(int),
|
ValueParser(int),
|
||||||
ValueParser(str),
|
ValueParser(int),
|
||||||
ValueParser(Decimal),
|
ValueParser(int),
|
||||||
),
|
ValueParser(str),
|
||||||
|
ValueParser(Decimal),
|
||||||
|
),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
V3 = V2_2
|
||||||
|
|
||||||
V4 = {
|
V4 = {
|
||||||
obis.P1_MESSAGE_HEADER: CosemParser(ValueParser(str)),
|
'checksum_support': True,
|
||||||
obis.P1_MESSAGE_TIMESTAMP: CosemParser(ValueParser(timestamp)),
|
'objects': {
|
||||||
obis.ELECTRICITY_USED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
obis.P1_MESSAGE_HEADER: CosemParser(ValueParser(str)),
|
||||||
obis.ELECTRICITY_USED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
obis.P1_MESSAGE_TIMESTAMP: CosemParser(ValueParser(timestamp)),
|
||||||
obis.ELECTRICITY_DELIVERED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
obis.EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||||
obis.ELECTRICITY_DELIVERED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
obis.ELECTRICITY_USED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
||||||
obis.ELECTRICITY_ACTIVE_TARIFF: CosemParser(ValueParser(str)),
|
obis.ELECTRICITY_USED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
||||||
obis.EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
obis.ELECTRICITY_DELIVERED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
||||||
obis.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
|
obis.ELECTRICITY_DELIVERED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
||||||
obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)),
|
obis.ELECTRICITY_ACTIVE_TARIFF: CosemParser(ValueParser(str)),
|
||||||
obis.LONG_POWER_FAILURE_COUNT: CosemParser(ValueParser(int)),
|
obis.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
|
||||||
# POWER_EVENT_FAILURE_LOG: ProfileGenericParser(), TODO
|
obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)),
|
||||||
obis.VOLTAGE_SAG_L1_COUNT: CosemParser(ValueParser(int)),
|
obis.LONG_POWER_FAILURE_COUNT: CosemParser(ValueParser(int)),
|
||||||
obis.VOLTAGE_SAG_L2_COUNT: CosemParser(ValueParser(int)),
|
# POWER_EVENT_FAILURE_LOG: ProfileGenericParser(), TODO
|
||||||
obis.VOLTAGE_SAG_L3_COUNT: CosemParser(ValueParser(int)),
|
obis.VOLTAGE_SAG_L1_COUNT: CosemParser(ValueParser(int)),
|
||||||
obis.VOLTAGE_SWELL_L1_COUNT: CosemParser(ValueParser(int)),
|
obis.VOLTAGE_SAG_L2_COUNT: CosemParser(ValueParser(int)),
|
||||||
obis.VOLTAGE_SWELL_L2_COUNT: CosemParser(ValueParser(int)),
|
obis.VOLTAGE_SAG_L3_COUNT: CosemParser(ValueParser(int)),
|
||||||
obis.VOLTAGE_SWELL_L3_COUNT: CosemParser(ValueParser(int)),
|
obis.VOLTAGE_SWELL_L1_COUNT: CosemParser(ValueParser(int)),
|
||||||
obis.TEXT_MESSAGE_CODE: CosemParser(ValueParser(int)),
|
obis.VOLTAGE_SWELL_L2_COUNT: CosemParser(ValueParser(int)),
|
||||||
obis.TEXT_MESSAGE: CosemParser(ValueParser(str)),
|
obis.VOLTAGE_SWELL_L3_COUNT: CosemParser(ValueParser(int)),
|
||||||
obis.DEVICE_TYPE: CosemParser(ValueParser(int)),
|
obis.TEXT_MESSAGE_CODE: CosemParser(ValueParser(int)),
|
||||||
obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE: CosemParser(ValueParser(Decimal)),
|
obis.TEXT_MESSAGE: CosemParser(ValueParser(str)),
|
||||||
obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE: CosemParser(ValueParser(Decimal)),
|
obis.DEVICE_TYPE: CosemParser(ValueParser(int)),
|
||||||
obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE: CosemParser(ValueParser(Decimal)),
|
obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||||
obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||||
obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||||
obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||||
obis.EQUIPMENT_IDENTIFIER_GAS: CosemParser(ValueParser(str)),
|
obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||||
obis.HOURLY_GAS_METER_READING: MBusParser(ValueParser(timestamp),
|
obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||||
ValueParser(Decimal))
|
obis.EQUIPMENT_IDENTIFIER_GAS: CosemParser(ValueParser(str)),
|
||||||
|
obis.HOURLY_GAS_METER_READING: MBusParser(
|
||||||
|
ValueParser(timestamp),
|
||||||
|
ValueParser(Decimal)
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
V5 = {
|
||||||
|
'checksum_support': True,
|
||||||
|
'objects': {
|
||||||
|
obis.P1_MESSAGE_HEADER: CosemParser(ValueParser(str)),
|
||||||
|
obis.P1_MESSAGE_TIMESTAMP: CosemParser(ValueParser(timestamp)),
|
||||||
|
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.LONG_POWER_FAILURE_COUNT: CosemParser(ValueParser(int)),
|
||||||
|
# POWER_EVENT_FAILURE_LOG: ProfileGenericParser(), TODO
|
||||||
|
obis.VOLTAGE_SAG_L1_COUNT: CosemParser(ValueParser(int)),
|
||||||
|
obis.VOLTAGE_SAG_L2_COUNT: CosemParser(ValueParser(int)),
|
||||||
|
obis.VOLTAGE_SAG_L3_COUNT: CosemParser(ValueParser(int)),
|
||||||
|
obis.VOLTAGE_SWELL_L1_COUNT: CosemParser(ValueParser(int)),
|
||||||
|
obis.VOLTAGE_SWELL_L2_COUNT: CosemParser(ValueParser(int)),
|
||||||
|
obis.VOLTAGE_SWELL_L3_COUNT: CosemParser(ValueParser(int)),
|
||||||
|
obis.TEXT_MESSAGE: CosemParser(ValueParser(str)),
|
||||||
|
obis.DEVICE_TYPE: CosemParser(ValueParser(int)),
|
||||||
|
obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||||
|
obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||||
|
obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||||
|
obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||||
|
obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||||
|
obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||||
|
obis.EQUIPMENT_IDENTIFIER_GAS: CosemParser(ValueParser(str)),
|
||||||
|
obis.HOURLY_GAS_METER_READING: MBusParser(
|
||||||
|
ValueParser(timestamp),
|
||||||
|
ValueParser(Decimal)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ALL = (V2_2, V3, V4, V5)
|
||||||
|
|||||||
@@ -4,8 +4,9 @@ import pytz
|
|||||||
|
|
||||||
|
|
||||||
def timestamp(value):
|
def timestamp(value):
|
||||||
|
|
||||||
naive_datetime = datetime.datetime.strptime(value[:-1], '%y%m%d%H%M%S')
|
naive_datetime = datetime.datetime.strptime(value[:-1], '%y%m%d%H%M%S')
|
||||||
|
|
||||||
|
# TODO comment on this exception
|
||||||
if len(value) == 13:
|
if len(value) == 13:
|
||||||
is_dst = value[12] == 'S' # assume format 160322150000W
|
is_dst = value[12] == 'S' # assume format 160322150000W
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ setup(
|
|||||||
author='Nigel Dokter',
|
author='Nigel Dokter',
|
||||||
author_email='nigeldokter@gmail.com',
|
author_email='nigeldokter@gmail.com',
|
||||||
url='https://github.com/ndokter/dsmr_parser',
|
url='https://github.com/ndokter/dsmr_parser',
|
||||||
version='0.3',
|
version='0.9',
|
||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'pyserial>=3,<4',
|
'pyserial>=3,<4',
|
||||||
'pyserial-asyncio<1',
|
'pyserial-asyncio<1',
|
||||||
'pytz'
|
'pytz',
|
||||||
|
'PyCRC>=1.2,<2'
|
||||||
],
|
],
|
||||||
entry_points={
|
entry_points={
|
||||||
'console_scripts': ['dsmr_console=dsmr_parser.__main__:console']
|
'console_scripts': ['dsmr_console=dsmr_parser.__main__:console']
|
||||||
|
|||||||
@@ -0,0 +1,130 @@
|
|||||||
|
TELEGRAM_V2_2 = (
|
||||||
|
'/ISk5\2MT382-1004\r\n'
|
||||||
|
'\r\n'
|
||||||
|
'0-0:96.1.1(00000000000000)\r\n'
|
||||||
|
'1-0:1.8.1(00001.001*kWh)\r\n'
|
||||||
|
'1-0:1.8.2(00001.001*kWh)\r\n'
|
||||||
|
'1-0:2.8.1(00001.001*kWh)\r\n'
|
||||||
|
'1-0:2.8.2(00001.001*kWh)\r\n'
|
||||||
|
'0-0:96.14.0(0001)\r\n'
|
||||||
|
'1-0:1.7.0(0001.01*kW)\r\n'
|
||||||
|
'1-0:2.7.0(0000.00*kW)\r\n'
|
||||||
|
'0-0:17.0.0(0999.00*kW)\r\n'
|
||||||
|
'0-0:96.3.10(1)\r\n'
|
||||||
|
'0-0:96.13.1()\r\n'
|
||||||
|
'0-0:96.13.0()\r\n'
|
||||||
|
'0-1:24.1.0(3)\r\n'
|
||||||
|
'0-1:96.1.0(000000000000)\r\n'
|
||||||
|
'0-1:24.3.0(161107190000)(00)(60)(1)(0-1:24.2.1)(m3)\r\n'
|
||||||
|
'(00001.001)\r\n'
|
||||||
|
'0-1:24.4.0(1)\r\n'
|
||||||
|
'!\r\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
TELEGRAM_V3 = (
|
||||||
|
'/ISk5\2MT382-1000\r\n'
|
||||||
|
'\r\n'
|
||||||
|
'0-0:96.1.1(4B384547303034303436333935353037)\r\n'
|
||||||
|
'1-0:1.8.1(12345.678*kWh)\r\n'
|
||||||
|
'1-0:1.8.2(12345.678*kWh)\r\n'
|
||||||
|
'1-0:2.8.1(12345.678*kWh)\r\n'
|
||||||
|
'1-0:2.8.2(12345.678*kWh)\r\n'
|
||||||
|
'0-0:96.14.0(0002)\r\n'
|
||||||
|
'1-0:1.7.0(001.19*kW)\r\n'
|
||||||
|
'1-0:2.7.0(000.00*kW)\r\n'
|
||||||
|
'0-0:17.0.0(016*A)\r\n'
|
||||||
|
'0-0:96.3.10(1)\r\n'
|
||||||
|
'0-0:96.13.1(303132333435363738)\r\n'
|
||||||
|
'0-0:96.13.0(303132333435363738393A3B3C3D3E3F303132333435363738393A3B3C3D3E'
|
||||||
|
'3F303132333435363738393A3B3C3D3E3F303132333435363738393A3B3C3D3E3F30313233'
|
||||||
|
'3435363738393A3B3C3D3E3F)\r\n'
|
||||||
|
'0-1:96.1.0(3232323241424344313233343536373839)\r\n'
|
||||||
|
'0-1:24.1.0(03)\r\n'
|
||||||
|
'0-1:24.3.0(090212160000)(00)(60)(1)(0-1:24.2.1)(m3)\r\n'
|
||||||
|
'(00001.001)\r\n'
|
||||||
|
'0-1:24.4.0(1)\r\n'
|
||||||
|
'!\r\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
TELEGRAM_V4_2 = (
|
||||||
|
'/KFM5KAIFA-METER\r\n'
|
||||||
|
'\r\n'
|
||||||
|
'1-3:0.2.8(42)\r\n'
|
||||||
|
'0-0:1.0.0(161113205757W)\r\n'
|
||||||
|
'0-0:96.1.1(3960221976967177082151037881335713)\r\n'
|
||||||
|
'1-0:1.8.1(001581.123*kWh)\r\n'
|
||||||
|
'1-0:1.8.2(001435.706*kWh)\r\n'
|
||||||
|
'1-0:2.8.1(000000.000*kWh)\r\n'
|
||||||
|
'1-0:2.8.2(000000.000*kWh)\r\n'
|
||||||
|
'0-0:96.14.0(0002)\r\n'
|
||||||
|
'1-0:1.7.0(02.027*kW)\r\n'
|
||||||
|
'1-0:2.7.0(00.000*kW)\r\n'
|
||||||
|
'0-0:96.7.21(00015)\r\n'
|
||||||
|
'0-0:96.7.9(00007)\r\n'
|
||||||
|
'1-0:99.97.0(3)(0-0:96.7.19)(000104180320W)(0000237126*s)(000101000001W)'
|
||||||
|
'(2147583646*s)(000102000003W)(2317482647*s)\r\n'
|
||||||
|
'1-0:32.32.0(00000)\r\n'
|
||||||
|
'1-0:52.32.0(00000)\r\n'
|
||||||
|
'1-0:72.32.0(00000)\r\n'
|
||||||
|
'1-0:32.36.0(00000)\r\n'
|
||||||
|
'1-0:52.36.0(00000)\r\n'
|
||||||
|
'1-0:72.36.0(00000)\r\n'
|
||||||
|
'0-0:96.13.1()\r\n'
|
||||||
|
'0-0:96.13.0()\r\n'
|
||||||
|
'1-0:31.7.0(000*A)\r\n'
|
||||||
|
'1-0:51.7.0(006*A)\r\n'
|
||||||
|
'1-0:71.7.0(002*A)\r\n'
|
||||||
|
'1-0:21.7.0(00.170*kW)\r\n'
|
||||||
|
'1-0:22.7.0(00.000*kW)\r\n'
|
||||||
|
'1-0:41.7.0(01.247*kW)\r\n'
|
||||||
|
'1-0:42.7.0(00.000*kW)\r\n'
|
||||||
|
'1-0:61.7.0(00.209*kW)\r\n'
|
||||||
|
'1-0:62.7.0(00.000*kW)\r\n'
|
||||||
|
'0-1:24.1.0(003)\r\n'
|
||||||
|
'0-1:96.1.0(4819243993373755377509728609491464)\r\n'
|
||||||
|
'0-1:24.2.1(161129200000W)(00981.443*m3)\r\n'
|
||||||
|
'!6796\r\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
TELEGRAM_V5 = (
|
||||||
|
'/ISk5\2MT382-1000\r\n'
|
||||||
|
'\r\n'
|
||||||
|
'1-3:0.2.8(50)\r\n'
|
||||||
|
'0-0:1.0.0(170102192002W)\r\n'
|
||||||
|
'0-0:96.1.1(4B384547303034303436333935353037)\r\n'
|
||||||
|
'1-0:1.8.1(000004.426*kWh)\r\n'
|
||||||
|
'1-0:1.8.2(000002.399*kWh)\r\n'
|
||||||
|
'1-0:2.8.1(000002.444*kWh)\r\n'
|
||||||
|
'1-0:2.8.2(000000.000*kWh)\r\n'
|
||||||
|
'0-0:96.14.0(0002)\r\n'
|
||||||
|
'1-0:1.7.0(00.244*kW)\r\n'
|
||||||
|
'1-0:2.7.0(00.000*kW)\r\n'
|
||||||
|
'0-0:96.7.21(00013)\r\n'
|
||||||
|
'0-0:96.7.9(00000)\r\n'
|
||||||
|
'1-0:99.97.0(0)(0-0:96.7.19)\r\n'
|
||||||
|
'1-0:32.32.0(00000)\r\n'
|
||||||
|
'1-0:52.32.0(00000)\r\n'
|
||||||
|
'1-0:72.32.0(00000)\r\n'
|
||||||
|
'1-0:32.36.0(00000)\r\n'
|
||||||
|
'1-0:52.36.0(00000)\r\n'
|
||||||
|
'1-0:72.36.0(00000)\r\n'
|
||||||
|
'0-0:96.13.0()\r\n'
|
||||||
|
'1-0:32.7.0(0230.0*V)\r\n'
|
||||||
|
'1-0:52.7.0(0230.0*V)\r\n'
|
||||||
|
'1-0:72.7.0(0229.0*V)\r\n'
|
||||||
|
'1-0:31.7.0(0.48*A)\r\n'
|
||||||
|
'1-0:51.7.0(0.44*A)\r\n'
|
||||||
|
'1-0:71.7.0(0.86*A)\r\n'
|
||||||
|
'1-0:21.7.0(00.070*kW)\r\n'
|
||||||
|
'1-0:41.7.0(00.032*kW)\r\n'
|
||||||
|
'1-0:61.7.0(00.142*kW)\r\n'
|
||||||
|
'1-0:22.7.0(00.000*kW)\r\n'
|
||||||
|
'1-0:42.7.0(00.000*kW)\r\n'
|
||||||
|
'1-0:62.7.0(00.000*kW)\r\n'
|
||||||
|
'0-1:24.1.0(003)\r\n'
|
||||||
|
'0-1:96.1.0(3232323241424344313233343536373839)\r\n'
|
||||||
|
'0-1:24.2.1(170102161005W)(00000.107*m3)\r\n'
|
||||||
|
'0-2:24.1.0(003)\r\n'
|
||||||
|
'0-2:96.1.0()\r\n'
|
||||||
|
'!87B3\r\n'
|
||||||
|
)
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
def test_malformed_telegram(self):
|
||||||
|
with self.assertRaises(TelegramSpecificationMatchError):
|
||||||
|
match_telegram_specification(example_telegrams.TELEGRAM_V5[:-4])
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
"""Test telegram parsing."""
|
|
||||||
|
|
||||||
from dsmr_parser.parsers import TelegramParserV2_2
|
|
||||||
from dsmr_parser import telegram_specifications
|
|
||||||
from dsmr_parser.obis_references import CURRENT_ELECTRICITY_USAGE, GAS_METER_READING
|
|
||||||
|
|
||||||
TELEGRAM_V2_2 = [
|
|
||||||
"/ISk5\2MT382-1004",
|
|
||||||
"",
|
|
||||||
"0-0:96.1.1(00000000000000)",
|
|
||||||
"1-0:1.8.1(00001.001*kWh)",
|
|
||||||
"1-0:1.8.2(00001.001*kWh)",
|
|
||||||
"1-0:2.8.1(00001.001*kWh)",
|
|
||||||
"1-0:2.8.2(00001.001*kWh)",
|
|
||||||
"0-0:96.14.0(0001)",
|
|
||||||
"1-0:1.7.0(0001.01*kW)",
|
|
||||||
"1-0:2.7.0(0000.00*kW)",
|
|
||||||
"0-0:17.0.0(0999.00*kW)",
|
|
||||||
"0-0:96.3.10(1)",
|
|
||||||
"0-0:96.13.1()",
|
|
||||||
"0-0:96.13.0()",
|
|
||||||
"0-1:24.1.0(3)",
|
|
||||||
"0-1:96.1.0(000000000000)",
|
|
||||||
"0-1:24.3.0(161107190000)(00)(60)(1)(0-1:24.2.1)(m3)",
|
|
||||||
"(00001.001)",
|
|
||||||
"0-1:24.4.0(1)",
|
|
||||||
"!",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def test_parse_v2_2():
|
|
||||||
"""Test if telegram parsing results in correct results."""
|
|
||||||
|
|
||||||
parser = TelegramParserV2_2(telegram_specifications.V2_2)
|
|
||||||
result = parser.parse(TELEGRAM_V2_2)
|
|
||||||
|
|
||||||
assert float(result[CURRENT_ELECTRICITY_USAGE].value) == 1.01
|
|
||||||
assert result[CURRENT_ELECTRICITY_USAGE].unit == 'kW'
|
|
||||||
assert float(result[GAS_METER_READING].value) == 1.001
|
|
||||||
assert result[GAS_METER_READING].unit == 'm3'
|
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from dsmr_parser.objects import MBusObject, CosemObject
|
||||||
|
from dsmr_parser.parsers import TelegramParser
|
||||||
|
from dsmr_parser import telegram_specifications
|
||||||
|
from dsmr_parser import obis_references as obis
|
||||||
|
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)
|
||||||
|
|
||||||
|
# ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_1].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_1].value == Decimal('1.001')
|
||||||
|
|
||||||
|
# ELECTRICITY_USED_TARIFF_2 (1-0:1.8.2)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_2].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_2].value == Decimal('1.001')
|
||||||
|
|
||||||
|
# ELECTRICITY_DELIVERED_TARIFF_1 (1-0:2.8.1)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value == Decimal('1.001')
|
||||||
|
|
||||||
|
# ELECTRICITY_DELIVERED_TARIFF_2 (1-0:2.8.2)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value == Decimal('1.001')
|
||||||
|
|
||||||
|
# ELECTRICITY_ACTIVE_TARIFF (0-0:96.14.0)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_ACTIVE_TARIFF].unit is None
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF].value, str)
|
||||||
|
assert result[obis.ELECTRICITY_ACTIVE_TARIFF].value == '0001'
|
||||||
|
|
||||||
|
# EQUIPMENT_IDENTIFIER (0-0:96.1.1)
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER], CosemObject)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER].unit is None
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER].value, str)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER].value == '00000000000000'
|
||||||
|
|
||||||
|
# CURRENT_ELECTRICITY_USAGE (1-0:1.7.0)
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE], CosemObject)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE].value, Decimal)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_USAGE].value == Decimal('1.01')
|
||||||
|
|
||||||
|
# CURRENT_ELECTRICITY_DELIVERY (1-0:2.7.0)
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY], CosemObject)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_DELIVERY].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY].value, Decimal)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_DELIVERY].value == Decimal('0')
|
||||||
|
|
||||||
|
# TEXT_MESSAGE_CODE (0-0:96.13.1)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE_CODE], CosemObject)
|
||||||
|
assert result[obis.TEXT_MESSAGE_CODE].unit is None
|
||||||
|
|
||||||
|
# TEXT_MESSAGE (0-0:96.13.0)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)
|
||||||
|
assert result[obis.TEXT_MESSAGE].unit is None
|
||||||
|
assert result[obis.TEXT_MESSAGE].value is None
|
||||||
|
|
||||||
|
# DEVICE_TYPE (0-x:24.1.0)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)
|
||||||
|
assert result[obis.DEVICE_TYPE].unit is None
|
||||||
|
assert isinstance(result[obis.DEVICE_TYPE].value, str)
|
||||||
|
assert result[obis.DEVICE_TYPE].value == '3'
|
||||||
|
|
||||||
|
# EQUIPMENT_IDENTIFIER_GAS (0-x:96.1.0)
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS], CosemObject)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER_GAS].unit is None
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS].value, str)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER_GAS].value == '000000000000'
|
||||||
|
|
||||||
|
# GAS_METER_READING (0-1:24.3.0)
|
||||||
|
assert isinstance(result[obis.GAS_METER_READING], MBusObject)
|
||||||
|
assert result[obis.GAS_METER_READING].unit == 'm3'
|
||||||
|
assert isinstance(result[obis.GAS_METER_READING].value, Decimal)
|
||||||
|
assert result[obis.GAS_METER_READING].value == Decimal('1.001')
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from dsmr_parser.objects import CosemObject, MBusObject
|
||||||
|
from dsmr_parser.parsers import TelegramParser
|
||||||
|
from dsmr_parser import telegram_specifications
|
||||||
|
from dsmr_parser import obis_references as obis
|
||||||
|
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)
|
||||||
|
|
||||||
|
# ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_1].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_1].value == Decimal('12345.678')
|
||||||
|
|
||||||
|
# ELECTRICITY_USED_TARIFF_2 (1-0:1.8.2)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_2].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_2].value == Decimal('12345.678')
|
||||||
|
|
||||||
|
# ELECTRICITY_DELIVERED_TARIFF_1 (1-0:2.8.1)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value == Decimal('12345.678')
|
||||||
|
|
||||||
|
# ELECTRICITY_DELIVERED_TARIFF_2 (1-0:2.8.2)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value == Decimal('12345.678')
|
||||||
|
|
||||||
|
# ELECTRICITY_ACTIVE_TARIFF (0-0:96.14.0)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_ACTIVE_TARIFF].unit is None
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF].value, str)
|
||||||
|
assert result[obis.ELECTRICITY_ACTIVE_TARIFF].value == '0002'
|
||||||
|
|
||||||
|
# EQUIPMENT_IDENTIFIER (0-0:96.1.1)
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER], CosemObject)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER].unit is None
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER].value, str)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER].value == '4B384547303034303436333935353037'
|
||||||
|
|
||||||
|
# CURRENT_ELECTRICITY_USAGE (1-0:1.7.0)
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE], CosemObject)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE].value, Decimal)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_USAGE].value == Decimal('1.19')
|
||||||
|
|
||||||
|
# CURRENT_ELECTRICITY_DELIVERY (1-0:2.7.0)
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY], CosemObject)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_DELIVERY].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY].value, Decimal)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_DELIVERY].value == Decimal('0')
|
||||||
|
|
||||||
|
# TEXT_MESSAGE_CODE (0-0:96.13.1)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE_CODE], CosemObject)
|
||||||
|
assert result[obis.TEXT_MESSAGE_CODE].unit is None
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE_CODE].value, int)
|
||||||
|
assert result[obis.TEXT_MESSAGE_CODE].value == 303132333435363738
|
||||||
|
|
||||||
|
# TEXT_MESSAGE (0-0:96.13.0)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)
|
||||||
|
assert result[obis.TEXT_MESSAGE].unit is None
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE].value, str)
|
||||||
|
assert result[obis.TEXT_MESSAGE].value == \
|
||||||
|
'303132333435363738393A3B3C3D3E3F303132333435363738393A3B3C3D3E3F' \
|
||||||
|
'303132333435363738393A3B3C3D3E3F303132333435363738393A3B3C3D3E3F' \
|
||||||
|
'303132333435363738393A3B3C3D3E3F'
|
||||||
|
|
||||||
|
# DEVICE_TYPE (0-x:24.1.0)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)
|
||||||
|
assert result[obis.DEVICE_TYPE].unit is None
|
||||||
|
assert isinstance(result[obis.DEVICE_TYPE].value, str)
|
||||||
|
assert result[obis.DEVICE_TYPE].value == '03'
|
||||||
|
|
||||||
|
# EQUIPMENT_IDENTIFIER_GAS (0-x:96.1.0)
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS], CosemObject)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER_GAS].unit is None
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS].value, str)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER_GAS].value == '3232323241424344313233343536373839'
|
||||||
|
|
||||||
|
# GAS_METER_READING (0-1:24.3.0)
|
||||||
|
assert isinstance(result[obis.GAS_METER_READING], MBusObject)
|
||||||
|
assert result[obis.GAS_METER_READING].unit == 'm3'
|
||||||
|
assert isinstance(result[obis.GAS_METER_READING].value, Decimal)
|
||||||
|
assert result[obis.GAS_METER_READING].value == Decimal('1.001')
|
||||||
@@ -0,0 +1,230 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
import datetime
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import pytz
|
||||||
|
|
||||||
|
from dsmr_parser import obis_references as obis
|
||||||
|
from dsmr_parser import telegram_specifications
|
||||||
|
from dsmr_parser.exceptions import InvalidChecksumError, ParseError
|
||||||
|
from dsmr_parser.objects import CosemObject, MBusObject
|
||||||
|
from dsmr_parser.parsers import TelegramParser
|
||||||
|
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)
|
||||||
|
|
||||||
|
# P1_MESSAGE_HEADER (1-3:0.2.8)
|
||||||
|
assert isinstance(result[obis.P1_MESSAGE_HEADER], CosemObject)
|
||||||
|
assert result[obis.P1_MESSAGE_HEADER].unit is None
|
||||||
|
assert isinstance(result[obis.P1_MESSAGE_HEADER].value, str)
|
||||||
|
assert result[obis.P1_MESSAGE_HEADER].value == '42'
|
||||||
|
|
||||||
|
# P1_MESSAGE_TIMESTAMP (0-0:1.0.0)
|
||||||
|
assert isinstance(result[obis.P1_MESSAGE_TIMESTAMP], CosemObject)
|
||||||
|
assert result[obis.P1_MESSAGE_TIMESTAMP].unit is None
|
||||||
|
assert isinstance(result[obis.P1_MESSAGE_TIMESTAMP].value, datetime.datetime)
|
||||||
|
assert result[obis.P1_MESSAGE_TIMESTAMP].value == \
|
||||||
|
datetime.datetime(2016, 11, 13, 19, 57, 57, tzinfo=pytz.UTC)
|
||||||
|
|
||||||
|
# ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_1].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_1].value == Decimal('1581.123')
|
||||||
|
|
||||||
|
# ELECTRICITY_USED_TARIFF_2 (1-0:1.8.2)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_2].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_2].value == Decimal('1435.706')
|
||||||
|
|
||||||
|
# ELECTRICITY_DELIVERED_TARIFF_1 (1-0:2.8.1)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value == Decimal('0')
|
||||||
|
|
||||||
|
# ELECTRICITY_DELIVERED_TARIFF_2 (1-0:2.8.2)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value == Decimal('0')
|
||||||
|
|
||||||
|
# ELECTRICITY_ACTIVE_TARIFF (0-0:96.14.0)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_ACTIVE_TARIFF].unit is None
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF].value, str)
|
||||||
|
assert result[obis.ELECTRICITY_ACTIVE_TARIFF].value == '0002'
|
||||||
|
|
||||||
|
# EQUIPMENT_IDENTIFIER (0-0:96.1.1)
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER], CosemObject)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER].unit is None
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER].value, str)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER].value == '3960221976967177082151037881335713'
|
||||||
|
|
||||||
|
# CURRENT_ELECTRICITY_USAGE (1-0:1.7.0)
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE], CosemObject)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE].value, Decimal)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_USAGE].value == Decimal('2.027')
|
||||||
|
|
||||||
|
# CURRENT_ELECTRICITY_DELIVERY (1-0:2.7.0)
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY], CosemObject)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_DELIVERY].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY].value, Decimal)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_DELIVERY].value == Decimal('0')
|
||||||
|
|
||||||
|
# LONG_POWER_FAILURE_COUNT (96.7.9)
|
||||||
|
assert isinstance(result[obis.LONG_POWER_FAILURE_COUNT], CosemObject)
|
||||||
|
assert result[obis.LONG_POWER_FAILURE_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.LONG_POWER_FAILURE_COUNT].value, int)
|
||||||
|
assert result[obis.LONG_POWER_FAILURE_COUNT].value == 7
|
||||||
|
|
||||||
|
# VOLTAGE_SAG_L1_COUNT (1-0:32.32.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L1_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L1_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L1_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L1_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SAG_L2_COUNT (1-0:52.32.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L2_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L2_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L2_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L2_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SAG_L3_COUNT (1-0:72.32.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L3_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L3_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L3_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L3_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SWELL_L1_COUNT (1-0:32.36.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L1_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L1_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L1_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L1_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SWELL_L2_COUNT (1-0:52.36.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L2_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L2_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L2_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L2_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SWELL_L3_COUNT (1-0:72.36.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L3_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L3_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L3_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L3_COUNT].value == 0
|
||||||
|
|
||||||
|
# TEXT_MESSAGE_CODE (0-0:96.13.1)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE_CODE], CosemObject)
|
||||||
|
assert result[obis.TEXT_MESSAGE_CODE].unit is None
|
||||||
|
assert result[obis.TEXT_MESSAGE_CODE].value is None
|
||||||
|
|
||||||
|
# TEXT_MESSAGE (0-0:96.13.0)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)
|
||||||
|
assert result[obis.TEXT_MESSAGE].unit is None
|
||||||
|
assert result[obis.TEXT_MESSAGE].value is None
|
||||||
|
|
||||||
|
# DEVICE_TYPE (0-x:24.1.0)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)
|
||||||
|
assert result[obis.DEVICE_TYPE].unit is None
|
||||||
|
assert isinstance(result[obis.DEVICE_TYPE].value, int)
|
||||||
|
assert result[obis.DEVICE_TYPE].value == 3
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE (1-0:21.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].value == Decimal('0.170')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE (1-0:41.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].value == Decimal('1.247')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE (1-0:61.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].value == Decimal('0.209')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE (1-0:22.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].value == Decimal('0')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE (1-0:42.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].value == Decimal('0')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE (1-0:62.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].value == Decimal('0')
|
||||||
|
|
||||||
|
# EQUIPMENT_IDENTIFIER_GAS (0-x:96.1.0)
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS], CosemObject)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER_GAS].unit is None
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS].value, str)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER_GAS].value == '4819243993373755377509728609491464'
|
||||||
|
|
||||||
|
# HOURLY_GAS_METER_READING (0-1:24.2.1)
|
||||||
|
assert isinstance(result[obis.HOURLY_GAS_METER_READING], MBusObject)
|
||||||
|
assert result[obis.HOURLY_GAS_METER_READING].unit == 'm3'
|
||||||
|
assert isinstance(result[obis.HOURLY_GAS_METER_READING].value, Decimal)
|
||||||
|
assert result[obis.HOURLY_GAS_METER_READING].value == Decimal('981.443')
|
||||||
|
|
||||||
|
# POWER_EVENT_FAILURE_LOG (99.97.0)
|
||||||
|
# TODO to be implemented
|
||||||
|
|
||||||
|
# ACTUAL_TRESHOLD_ELECTRICITY (0-0:17.0.0)
|
||||||
|
# TODO to be implemented
|
||||||
|
|
||||||
|
# ACTUAL_SWITCH_POSITION (0-0:96.3.10)
|
||||||
|
# TODO to be implemented
|
||||||
|
|
||||||
|
# VALVE_POSITION_GAS (0-x:24.4.0)
|
||||||
|
# TODO to be implemented
|
||||||
|
|
||||||
|
def test_checksum_valid(self):
|
||||||
|
# No exception is raised.
|
||||||
|
TelegramParser.validate_checksum(TELEGRAM_V4_2)
|
||||||
|
|
||||||
|
def test_checksum_invalid(self):
|
||||||
|
# Remove the electricty used data value. This causes the checksum to
|
||||||
|
# not match anymore.
|
||||||
|
corrupted_telegram = TELEGRAM_V4_2.replace(
|
||||||
|
'1-0:1.8.1(001581.123*kWh)\r\n',
|
||||||
|
''
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(InvalidChecksumError):
|
||||||
|
TelegramParser.validate_checksum(corrupted_telegram)
|
||||||
|
|
||||||
|
def test_checksum_missing(self):
|
||||||
|
# Remove the checksum value causing a ParseError.
|
||||||
|
corrupted_telegram = TELEGRAM_V4_2.replace('!6796\r\n', '')
|
||||||
|
|
||||||
|
with self.assertRaises(ParseError):
|
||||||
|
TelegramParser.validate_checksum(corrupted_telegram)
|
||||||
@@ -0,0 +1,214 @@
|
|||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
|
import datetime
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import pytz
|
||||||
|
|
||||||
|
from dsmr_parser import obis_references as obis
|
||||||
|
from dsmr_parser import telegram_specifications
|
||||||
|
from dsmr_parser.exceptions import InvalidChecksumError, ParseError
|
||||||
|
from dsmr_parser.objects import CosemObject, MBusObject
|
||||||
|
from dsmr_parser.parsers import TelegramParser
|
||||||
|
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)
|
||||||
|
|
||||||
|
# P1_MESSAGE_HEADER (1-3:0.2.8)
|
||||||
|
assert isinstance(result[obis.P1_MESSAGE_HEADER], CosemObject)
|
||||||
|
assert result[obis.P1_MESSAGE_HEADER].unit is None
|
||||||
|
assert isinstance(result[obis.P1_MESSAGE_HEADER].value, str)
|
||||||
|
assert result[obis.P1_MESSAGE_HEADER].value == '50'
|
||||||
|
|
||||||
|
# P1_MESSAGE_TIMESTAMP (0-0:1.0.0)
|
||||||
|
assert isinstance(result[obis.P1_MESSAGE_TIMESTAMP], CosemObject)
|
||||||
|
assert result[obis.P1_MESSAGE_TIMESTAMP].unit is None
|
||||||
|
assert isinstance(result[obis.P1_MESSAGE_TIMESTAMP].value, datetime.datetime)
|
||||||
|
assert result[obis.P1_MESSAGE_TIMESTAMP].value == \
|
||||||
|
datetime.datetime(2017, 1, 2, 18, 20, 2, tzinfo=pytz.UTC)
|
||||||
|
|
||||||
|
# ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_1].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_1].value == Decimal('4.426')
|
||||||
|
|
||||||
|
# ELECTRICITY_USED_TARIFF_2 (1-0:1.8.2)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_2].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_2].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_USED_TARIFF_2].value == Decimal('2.399')
|
||||||
|
|
||||||
|
# ELECTRICITY_DELIVERED_TARIFF_1 (1-0:2.8.1)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_1].value == Decimal('2.444')
|
||||||
|
|
||||||
|
# ELECTRICITY_DELIVERED_TARIFF_2 (1-0:2.8.2)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].unit == 'kWh'
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value, Decimal)
|
||||||
|
assert result[obis.ELECTRICITY_DELIVERED_TARIFF_2].value == Decimal('0')
|
||||||
|
|
||||||
|
# ELECTRICITY_ACTIVE_TARIFF (0-0:96.14.0)
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF], CosemObject)
|
||||||
|
assert result[obis.ELECTRICITY_ACTIVE_TARIFF].unit is None
|
||||||
|
assert isinstance(result[obis.ELECTRICITY_ACTIVE_TARIFF].value, str)
|
||||||
|
assert result[obis.ELECTRICITY_ACTIVE_TARIFF].value == '0002'
|
||||||
|
|
||||||
|
# EQUIPMENT_IDENTIFIER (0-0:96.1.1)
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER], CosemObject)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER].unit is None
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER].value, str)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER].value == '4B384547303034303436333935353037'
|
||||||
|
|
||||||
|
# CURRENT_ELECTRICITY_USAGE (1-0:1.7.0)
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE], CosemObject)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_USAGE].value, Decimal)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_USAGE].value == Decimal('0.244')
|
||||||
|
|
||||||
|
# CURRENT_ELECTRICITY_DELIVERY (1-0:2.7.0)
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY], CosemObject)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_DELIVERY].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.CURRENT_ELECTRICITY_DELIVERY].value, Decimal)
|
||||||
|
assert result[obis.CURRENT_ELECTRICITY_DELIVERY].value == Decimal('0')
|
||||||
|
|
||||||
|
# LONG_POWER_FAILURE_COUNT (96.7.9)
|
||||||
|
assert isinstance(result[obis.LONG_POWER_FAILURE_COUNT], CosemObject)
|
||||||
|
assert result[obis.LONG_POWER_FAILURE_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.LONG_POWER_FAILURE_COUNT].value, int)
|
||||||
|
assert result[obis.LONG_POWER_FAILURE_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SAG_L1_COUNT (1-0:32.32.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L1_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L1_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L1_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L1_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SAG_L2_COUNT (1-0:52.32.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L2_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L2_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L2_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L2_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SAG_L3_COUNT (1-0:72.32.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L3_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L3_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SAG_L3_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SAG_L3_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SWELL_L1_COUNT (1-0:32.36.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L1_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L1_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L1_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L1_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SWELL_L2_COUNT (1-0:52.36.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L2_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L2_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L2_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L2_COUNT].value == 0
|
||||||
|
|
||||||
|
# VOLTAGE_SWELL_L3_COUNT (1-0:72.36.0)
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L3_COUNT], CosemObject)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L3_COUNT].unit is None
|
||||||
|
assert isinstance(result[obis.VOLTAGE_SWELL_L3_COUNT].value, int)
|
||||||
|
assert result[obis.VOLTAGE_SWELL_L3_COUNT].value == 0
|
||||||
|
|
||||||
|
# TEXT_MESSAGE (0-0:96.13.0)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)
|
||||||
|
assert result[obis.TEXT_MESSAGE].unit is None
|
||||||
|
assert result[obis.TEXT_MESSAGE].value is None
|
||||||
|
|
||||||
|
# DEVICE_TYPE (0-x:24.1.0)
|
||||||
|
assert isinstance(result[obis.TEXT_MESSAGE], CosemObject)
|
||||||
|
assert result[obis.DEVICE_TYPE].unit is None
|
||||||
|
assert isinstance(result[obis.DEVICE_TYPE].value, int)
|
||||||
|
assert result[obis.DEVICE_TYPE].value == 3
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE (1-0:21.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE].value == Decimal('0.070')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE (1-0:41.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE].value == Decimal('0.032')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE (1-0:61.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE].value == Decimal('0.142')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE (1-0:22.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE].value == Decimal('0')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE (1-0:42.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE].value == Decimal('0')
|
||||||
|
|
||||||
|
# INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE (1-0:62.7.0)
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE], CosemObject)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].unit == 'kW'
|
||||||
|
assert isinstance(result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].value, Decimal)
|
||||||
|
assert result[obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE].value == Decimal('0')
|
||||||
|
|
||||||
|
# EQUIPMENT_IDENTIFIER_GAS (0-x:96.1.0)
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS], CosemObject)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER_GAS].unit is None
|
||||||
|
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS].value, str)
|
||||||
|
assert result[obis.EQUIPMENT_IDENTIFIER_GAS].value == '3232323241424344313233343536373839'
|
||||||
|
|
||||||
|
# HOURLY_GAS_METER_READING (0-1:24.2.1)
|
||||||
|
assert isinstance(result[obis.HOURLY_GAS_METER_READING], MBusObject)
|
||||||
|
assert result[obis.HOURLY_GAS_METER_READING].unit == 'm3'
|
||||||
|
assert isinstance(result[obis.HOURLY_GAS_METER_READING].value, Decimal)
|
||||||
|
assert result[obis.HOURLY_GAS_METER_READING].value == Decimal('0.107')
|
||||||
|
|
||||||
|
def test_checksum_valid(self):
|
||||||
|
# No exception is raised.
|
||||||
|
TelegramParser.validate_checksum(TELEGRAM_V5)
|
||||||
|
|
||||||
|
def test_checksum_invalid(self):
|
||||||
|
# Remove the electricty used data value. This causes the checksum to
|
||||||
|
# not match anymore.
|
||||||
|
corrupted_telegram = TELEGRAM_V5.replace(
|
||||||
|
'1-0:1.8.1(000004.426*kWh)\r\n',
|
||||||
|
''
|
||||||
|
)
|
||||||
|
|
||||||
|
with self.assertRaises(InvalidChecksumError):
|
||||||
|
TelegramParser.validate_checksum(corrupted_telegram)
|
||||||
|
|
||||||
|
def test_checksum_missing(self):
|
||||||
|
# Remove the checksum value causing a ParseError.
|
||||||
|
corrupted_telegram = TELEGRAM_V5.replace('!87B3\r\n', '')
|
||||||
|
|
||||||
|
with self.assertRaises(ParseError):
|
||||||
|
TelegramParser.validate_checksum(corrupted_telegram)
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
from unittest.mock import Mock
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from dsmr_parser import obis_references as obis
|
||||||
|
from dsmr_parser import telegram_specifications
|
||||||
|
from dsmr_parser.parsers import TelegramParser
|
||||||
|
from dsmr_parser.clients.protocol import DSMRProtocol
|
||||||
|
|
||||||
|
|
||||||
|
TELEGRAM_V2_2 = (
|
||||||
|
'/ISk5\2MT382-1004\r\n'
|
||||||
|
'\r\n'
|
||||||
|
'0-0:96.1.1(00000000000000)\r\n'
|
||||||
|
'1-0:1.8.1(00001.001*kWh)\r\n'
|
||||||
|
'1-0:1.8.2(00001.001*kWh)\r\n'
|
||||||
|
'1-0:2.8.1(00001.001*kWh)\r\n'
|
||||||
|
'1-0:2.8.2(00001.001*kWh)\r\n'
|
||||||
|
'0-0:96.14.0(0001)\r\n'
|
||||||
|
'1-0:1.7.0(0001.01*kW)\r\n'
|
||||||
|
'1-0:2.7.0(0000.00*kW)\r\n'
|
||||||
|
'0-0:17.0.0(0999.00*kW)\r\n'
|
||||||
|
'0-0:96.3.10(1)\r\n'
|
||||||
|
'0-0:96.13.1()\r\n'
|
||||||
|
'0-0:96.13.0()\r\n'
|
||||||
|
'0-1:24.1.0(3)\r\n'
|
||||||
|
'0-1:96.1.0(000000000000)\r\n'
|
||||||
|
'0-1:24.3.0(161107190000)(00)(60)(1)(0-1:24.2.1)(m3)\r\n'
|
||||||
|
'(00001.001)\r\n'
|
||||||
|
'0-1:24.4.0(1)\r\n'
|
||||||
|
'!\r\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class ProtocolTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
telegram_parser = TelegramParser(telegram_specifications.V2_2)
|
||||||
|
self.protocol = DSMRProtocol(None, telegram_parser,
|
||||||
|
telegram_callback=Mock())
|
||||||
|
|
||||||
|
def test_complete_packet(self):
|
||||||
|
"""Protocol should assemble incoming lines into complete packet."""
|
||||||
|
|
||||||
|
self.protocol.data_received(TELEGRAM_V2_2.encode('ascii'))
|
||||||
|
|
||||||
|
telegram = self.protocol.telegram_callback.call_args_list[0][0][0]
|
||||||
|
assert isinstance(telegram, dict)
|
||||||
|
|
||||||
|
assert float(telegram[obis.CURRENT_ELECTRICITY_USAGE].value) == 1.01
|
||||||
|
assert telegram[obis.CURRENT_ELECTRICITY_USAGE].unit == 'kW'
|
||||||
|
|
||||||
|
assert float(telegram[obis.GAS_METER_READING].value) == 1.001
|
||||||
|
assert telegram[obis.GAS_METER_READING].unit == 'm3'
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
import unittest
|
||||||
|
|
||||||
|
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
|
||||||
|
from test.example_telegrams import TELEGRAM_V2_2, TELEGRAM_V4_2
|
||||||
|
|
||||||
|
|
||||||
|
class TelegramBufferTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.telegram_buffer = TelegramBuffer()
|
||||||
|
|
||||||
|
def test_v22_telegram(self):
|
||||||
|
self.telegram_buffer.append(TELEGRAM_V2_2)
|
||||||
|
|
||||||
|
telegram = next(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertEqual(telegram, TELEGRAM_V2_2)
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, '')
|
||||||
|
|
||||||
|
def test_v42_telegram(self):
|
||||||
|
self.telegram_buffer.append(TELEGRAM_V4_2)
|
||||||
|
|
||||||
|
telegram = next(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertEqual(telegram, TELEGRAM_V4_2)
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, '')
|
||||||
|
|
||||||
|
def test_multiple_mixed_telegrams(self):
|
||||||
|
self.telegram_buffer.append(
|
||||||
|
''.join((TELEGRAM_V2_2, TELEGRAM_V4_2, TELEGRAM_V2_2))
|
||||||
|
)
|
||||||
|
|
||||||
|
telegrams = list(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertListEqual(
|
||||||
|
telegrams,
|
||||||
|
[
|
||||||
|
TELEGRAM_V2_2,
|
||||||
|
TELEGRAM_V4_2,
|
||||||
|
TELEGRAM_V2_2
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, '')
|
||||||
|
|
||||||
|
def test_v42_telegram_preceded_with_unclosed_telegram(self):
|
||||||
|
# There are unclosed telegrams at the start of the buffer.
|
||||||
|
incomplete_telegram = TELEGRAM_V4_2[:-1]
|
||||||
|
|
||||||
|
self.telegram_buffer.append(incomplete_telegram + TELEGRAM_V4_2)
|
||||||
|
|
||||||
|
telegram = next(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertEqual(telegram, TELEGRAM_V4_2)
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, '')
|
||||||
|
|
||||||
|
def test_v42_telegram_preceded_with_unopened_telegram(self):
|
||||||
|
# There is unopened telegrams at the start of the buffer indicating that
|
||||||
|
# the buffer was being filled while the telegram was outputted halfway.
|
||||||
|
incomplete_telegram = TELEGRAM_V4_2[1:]
|
||||||
|
|
||||||
|
self.telegram_buffer.append(incomplete_telegram + TELEGRAM_V4_2)
|
||||||
|
|
||||||
|
telegram = next(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertEqual(telegram, TELEGRAM_V4_2)
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, '')
|
||||||
|
|
||||||
|
def test_v42_telegram_trailed_by_unclosed_telegram(self):
|
||||||
|
incomplete_telegram = TELEGRAM_V4_2[:-1]
|
||||||
|
|
||||||
|
self.telegram_buffer.append(TELEGRAM_V4_2 + incomplete_telegram)
|
||||||
|
|
||||||
|
telegram = next(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertEqual(telegram, TELEGRAM_V4_2)
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, incomplete_telegram)
|
||||||
|
|
||||||
|
def test_v42_telegram_trailed_by_unopened_telegram(self):
|
||||||
|
incomplete_telegram = TELEGRAM_V4_2[1:]
|
||||||
|
|
||||||
|
self.telegram_buffer.append(TELEGRAM_V4_2 + incomplete_telegram)
|
||||||
|
|
||||||
|
telegram = next(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertEqual(telegram, TELEGRAM_V4_2)
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, incomplete_telegram)
|
||||||
|
|
||||||
|
def test_v42_telegram_adding_line_by_line(self):
|
||||||
|
for line in TELEGRAM_V4_2.splitlines(keepends=True):
|
||||||
|
self.telegram_buffer.append(line)
|
||||||
|
|
||||||
|
telegram = next(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertEqual(telegram, TELEGRAM_V4_2)
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, '')
|
||||||
|
|
||||||
|
def test_v42_telegram_adding_char_by_char(self):
|
||||||
|
for char in TELEGRAM_V4_2:
|
||||||
|
self.telegram_buffer.append(char)
|
||||||
|
|
||||||
|
telegram = next(self.telegram_buffer.get_all())
|
||||||
|
|
||||||
|
self.assertEqual(telegram, TELEGRAM_V4_2)
|
||||||
|
self.assertEqual(self.telegram_buffer._buffer, '')
|
||||||
@@ -1,14 +1,22 @@
|
|||||||
[tox]
|
[tox]
|
||||||
envlist = py34,py35
|
envlist = py34,py35,p36
|
||||||
|
|
||||||
[testenv]
|
[testenv]
|
||||||
deps=
|
deps=
|
||||||
pytest
|
pytest
|
||||||
|
pytest-cov
|
||||||
pylama
|
pylama
|
||||||
|
pytest-asyncio
|
||||||
|
pytest-catchlog
|
||||||
|
pytest-mock
|
||||||
|
PyCRC
|
||||||
commands=
|
commands=
|
||||||
py.test test {posargs}
|
py.test --cov=dsmr_parser test {posargs}
|
||||||
pylama dsmr_parser test
|
pylama dsmr_parser test
|
||||||
|
|
||||||
|
[pylama:dsmr_parser/clients/__init__.py]
|
||||||
|
ignore = W0611
|
||||||
|
|
||||||
[pylama:pylint]
|
[pylama:pylint]
|
||||||
max_line_length = 100
|
max_line_length = 100
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user