Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca8392504d | ||
|
|
026b91cd49 | ||
|
|
02eb3a34dd | ||
|
|
142b7359a1 | ||
|
|
21b8547655 | ||
|
|
a2b9fe5e3b | ||
|
|
0a259a3c9d | ||
|
|
f851f48225 | ||
|
|
3a3054324a | ||
|
|
041ac758f4 | ||
|
|
d50d775e38 | ||
|
|
10613acbd9 | ||
|
|
5f58bc8f72 | ||
|
|
371e1f3c5e | ||
|
|
0eae28e183 | ||
|
|
431ddadb5f | ||
|
|
9e01dc32c0 | ||
|
|
650d16862b | ||
|
|
301a00e226 | ||
|
|
d0cc429eda | ||
|
|
d05fe2692b | ||
|
|
e80ba9862b | ||
|
|
cffcac2e87 | ||
|
|
357bae1bce | ||
|
|
98a10998e9 | ||
|
|
00ac7c71be | ||
|
|
594db890fe | ||
|
|
09f4afcada | ||
|
|
2aba206c6f | ||
|
|
247a7446f5 | ||
|
|
5c378f3419 | ||
|
|
b825faa719 | ||
|
|
7f35cd3c73 | ||
|
|
63338fbf06 | ||
|
|
dd9e264f5c | ||
|
|
5c4d6ed98b | ||
|
|
f238eb14a1 | ||
|
|
179a75e58c | ||
|
|
32c20b61ac | ||
|
|
602ed4928a | ||
|
|
f5bedb1e6e | ||
|
|
bc6eab73de | ||
|
|
e5eddd006e | ||
|
|
0ed5fc0220 | ||
|
|
c0a509dab3 | ||
|
|
3d5599289e | ||
|
|
99ab86fffb | ||
|
|
15b3653a02 | ||
|
|
527730781c | ||
|
|
3f41a73b9d | ||
|
|
ddbcb67088 | ||
|
|
746fbc4fff | ||
|
|
304593c246 | ||
|
|
7610646e10 | ||
|
|
a6bc66667b | ||
|
|
0340b25733 | ||
|
|
5c838efbe1 | ||
|
|
9f66c075d6 | ||
|
|
2dafc7e7e4 | ||
|
|
b74572238e | ||
|
|
cedf71dbb5 | ||
|
|
eace91b591 | ||
|
|
dd6d26670e | ||
|
|
7d28d0e370 | ||
|
|
c082cf4868 | ||
|
|
188cac5287 | ||
|
|
c7ed4acb03 | ||
|
|
8b64adb80c | ||
|
|
3eed3654d4 | ||
|
|
b562510e40 | ||
|
|
f0e035c8ed | ||
|
|
83ef354c12 | ||
|
|
dc2f493739 | ||
|
|
0a20dea7ed | ||
|
|
28e3c51f0d | ||
|
|
60af45be25 | ||
|
|
00248f490e | ||
|
|
45cc88942a | ||
|
|
26ac27c347 | ||
|
|
7a89d6e97b | ||
|
|
de4dc2ec98 | ||
|
|
6866e7d585 | ||
|
|
df59f3a498 | ||
|
|
4073917d3e | ||
|
|
3a4fcd7da1 | ||
|
|
d5b427ba4f | ||
|
|
c1202f33e9 | ||
|
|
3092ba8b1f | ||
|
|
8e2bdd3279 | ||
|
|
2b724b87c3 | ||
|
|
17b56ae07b | ||
|
|
e4f384c2b7 | ||
|
|
8b8b952ce1 | ||
|
|
007b3ea089 | ||
|
|
761aaccb3f | ||
|
|
dc608f9da3 | ||
|
|
88ef4f4921 | ||
|
|
61de170a79 | ||
|
|
947fd64377 | ||
|
|
62aa0ac3ab | ||
|
|
da98cf1d05 | ||
|
|
fcb0dc600b | ||
|
|
b3a705a74d | ||
|
|
4688cf9058 | ||
|
|
fb9e488102 | ||
|
|
e30c951c7f | ||
|
|
97786576cf | ||
|
|
e12aff5c0d | ||
|
|
bc7961f840 | ||
|
|
58851b5c5c | ||
|
|
ba9f3f3c25 |
@@ -0,0 +1,46 @@
|
||||
name: Tests
|
||||
|
||||
on:
|
||||
push: ~
|
||||
pull_request: ~
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10 # Don't run forever when stale
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
python-version:
|
||||
- '3.7'
|
||||
- '3.8'
|
||||
- '3.9'
|
||||
- '3.10'
|
||||
- '3.11'
|
||||
|
||||
name: Python ${{ matrix.python-version }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Setup Python ${{ matrix.python-version }}
|
||||
uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
|
||||
- name: Cached PIP dependencies
|
||||
uses: actions/cache@v3
|
||||
with:
|
||||
path: |
|
||||
~/.cache/pip
|
||||
~/.tox/python/.pytest_cache
|
||||
key: pip-${{ matrix.python-version }}-${{ hashFiles('setup.py', 'tox.ini') }}
|
||||
restore-keys: pip-${{ matrix.python-version }}-
|
||||
|
||||
- name: Install dependencies
|
||||
run: pip install tox
|
||||
|
||||
- name: Run tests
|
||||
run: tox
|
||||
|
||||
- name: Code coverage upload
|
||||
uses: codecov/codecov-action@v3
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
language: python
|
||||
|
||||
python:
|
||||
- 2.7
|
||||
- 3.5
|
||||
- 3.6
|
||||
- 3.8
|
||||
|
||||
install: pip install tox-travis codecov
|
||||
|
||||
script: tox
|
||||
|
||||
after_success:
|
||||
- codecov
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- python: 2.7
|
||||
@@ -1,30 +1,68 @@
|
||||
Change Log
|
||||
----------
|
||||
|
||||
**1.1.0** (2023-02-08)
|
||||
|
||||
- Add instantaneous reactive power + fixed swapped reactive total import export (`pull request #124 <https://github.com/ndokter/dsmr_parser/pull/124>`_ by `yada75 <https://github.com/yada75>`_)
|
||||
|
||||
|
||||
**1.0.0** (2022-12-22)
|
||||
|
||||
- switched to new numbering scheme https://semver.org/
|
||||
- Added support for Python 3.11 and dropped support for Python 3.6 (`pull request #112 <https://github.com/ndokter/dsmr_parser/pull/112>`_)
|
||||
- Add support for Fluvius V1.7.1 DSMR messages (`pull request #110 <https://github.com/ndokter/dsmr_parser/pull/113>`_)
|
||||
|
||||
**0.34** (2022-10-19)
|
||||
|
||||
- Adds support for the Sagemcom T210-D-r smart meter (`pull request #110 <https://github.com/ndokter/dsmr_parser/pull/110>`_).
|
||||
|
||||
**0.33** (2022-04-20)
|
||||
|
||||
- Test Python 3.10 in CI + legacy badge fix (`pull request #105 <https://github.com/ndokter/dsmr_parser/pull/105>`_).
|
||||
- Update telegram_specifications.py (`pull request #106 <https://github.com/ndokter/dsmr_parser/pull/106>`_).
|
||||
- Improve compatiblity with Belgian standard (`pull request #107 <https://github.com/ndokter/dsmr_parser/pull/107>`_).
|
||||
- Improve documentation asyncio (`pull request #63 <https://github.com/ndokter/dsmr_parser/pull/63>`_).
|
||||
|
||||
**0.32** (2022-01-04)
|
||||
|
||||
- Support DSMR data read via RFXtrx with integrated P1 reader (`pull request #98 <https://github.com/ndokter/dsmr_parser/pull/98>`_).
|
||||
|
||||
**0.31** (2021-11-21)
|
||||
|
||||
- Support for (German) EasyMeter Q3D using COM-1 Ethernet Gateway (`pull request #92 <https://github.com/ndokter/dsmr_parser/pull/92>`_).
|
||||
|
||||
**0.30** (2021-08-18)
|
||||
|
||||
- Add support for Swedish smart meters (`pull request #86 <https://github.com/ndokter/dsmr_parser/pull/86>`_).
|
||||
|
||||
**0.29** (2021-04-18)
|
||||
|
||||
- Add value and unit properties to ProfileGenericObject to make sure that code like iterators that rely on that do not break (`pull request #71 <https://github.com/ndokter/dsmr_parser/pull/71>`_).
|
||||
Remove deprecated asyncio coroutine decorator (`pull request #76 <https://github.com/ndokter/dsmr_parser/pull/76>`_).
|
||||
|
||||
**0.28** (2021-02-21)
|
||||
|
||||
- Optional keep alive monitoring for TCP/IP connections (`pull request #73 <https://github.com/ndokter/dsmr_parser/pull/73>`_).
|
||||
- Catch parse errors in TelegramParser, ignore lines that can not be parsed (`pull request #74 <https://github.com/ndokter/dsmr_parser/pull/74>`_).
|
||||
|
||||
**0.27** (2020-12-24)
|
||||
|
||||
- fix for empty parentheses in ProfileGenericParser (redone) (`pull request #69 <https://github.com/ndokter/dsmr_parser/pull/69>`_).
|
||||
|
||||
**0.26** (2020-12-15)
|
||||
|
||||
- reverted fix for empty parentheses in ProfileGenericParser (`pull request #68 <https://github.com/ndokter/dsmr_parser/pull/68>`_).
|
||||
|
||||
**0.25** (2020-12-14)
|
||||
|
||||
- fix for empty parentheses in ProfileGenericParser (`pull request #57 <https://github.com/ndokter/dsmr_parser/pull/57>`_).
|
||||
|
||||
**0.24** (2020-11-27)
|
||||
|
||||
- Add Luxembourg equipment identifier (`pull request #62 <https://github.com/ndokter/dsmr_parser/pull/62>`_).
|
||||
|
||||
**0.23** (2020-11-07)
|
||||
|
||||
- Resolved issue with x-x:24.3.0 where it contains non-integer character (`pull request #61 <https://github.com/ndokter/dsmr_parser/pull/61>`_).
|
||||
- Tests are not installed anymore (`pull request #59 <https://github.com/ndokter/dsmr_parser/pull/59>`_).
|
||||
- Example telegram improvement (`pull request #58 <https://github.com/ndokter/dsmr_parser/pull/58>`_).
|
||||
|
||||
+162
-36
@@ -4,8 +4,8 @@ DSMR Parser
|
||||
.. image:: https://img.shields.io/pypi/v/dsmr-parser.svg
|
||||
: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
|
||||
.. image:: https://img.shields.io/github/actions/workflow/status/ndokter/dsmr_parser/tests.yml?branch=master
|
||||
:target: https://github.com/ndokter/dsmr_parser/actions/workflows/tests.yml
|
||||
|
||||
A library for parsing Dutch Smart Meter Requirements (DSMR) telegram data. It
|
||||
also includes client implementation to directly read and parse smart meter data.
|
||||
@@ -14,7 +14,7 @@ also includes client implementation to directly read and parse smart meter data.
|
||||
Features
|
||||
--------
|
||||
|
||||
DSMR Parser supports DSMR versions 2, 3, 4 and 5. It has been tested with Python 3.5, 3.6, 3.7, 3.8 and 3.9.
|
||||
DSMR Parser supports DSMR versions 2, 3, 4 and 5. See for the `currently supported/tested Python versions here <https://github.com/ndokter/dsmr_parser/blob/master/.github/workflows/tests.yml#L14>`_.
|
||||
|
||||
|
||||
Client module usage
|
||||
@@ -39,10 +39,136 @@ process because the code is blocking (not asynchronous):
|
||||
for telegram in serial_reader.read():
|
||||
print(telegram) # see 'Telegram object' docs below
|
||||
|
||||
**Socket client**
|
||||
|
||||
Read a remote serial port (for example using ser2net) and work with the parsed telegrams.
|
||||
It should be run in a separate process because the code is blocking (not asynchronous):
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from dsmr_parser import telegram_specifications
|
||||
from dsmr_parser.clients import SocketReader
|
||||
|
||||
socket_reader = SocketReader(
|
||||
host='127.0.0.1',
|
||||
port=2001,
|
||||
telegram_specification=telegram_specifications.V4
|
||||
)
|
||||
|
||||
for telegram in socket_reader.read():
|
||||
print(telegram) # see 'Telegram object' docs below
|
||||
|
||||
**AsyncIO client**
|
||||
|
||||
To be documented.
|
||||
For a test run using a tcp server (lasting 20 seconds) use the following example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from dsmr_parser import obis_references
|
||||
from dsmr_parser.clients.protocol import create_dsmr_reader, create_tcp_dsmr_reader
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
||||
|
||||
HOST = MY_HOST
|
||||
PORT = MY_PORT
|
||||
DSMR_VERSION = MY_DSMR_VERSION
|
||||
|
||||
logger = logging.getLogger('tcpclient')
|
||||
logger.debug("Logger created")
|
||||
|
||||
def printTelegram(telegram):
|
||||
logger.info(telegram)
|
||||
|
||||
|
||||
async def main():
|
||||
try:
|
||||
logger.debug("Getting loop")
|
||||
loop = asyncio.get_event_loop()
|
||||
logger.debug("Creating reader")
|
||||
await create_tcp_dsmr_reader(
|
||||
HOST,
|
||||
PORT,
|
||||
DSMR_VERSION,
|
||||
printTelegram,
|
||||
loop
|
||||
)
|
||||
logger.debug("Reader created going to sleep now")
|
||||
await asyncio.sleep(20)
|
||||
logger.info('Finished run')
|
||||
except Exception as e:
|
||||
logger.error("Unexpected error: "+ e)
|
||||
|
||||
asyncio.run(main())
|
||||
|
||||
Note the creation of a callback function to call when a telegram is received. In this case `printTelegram`. Normally the used loop is the one running.
|
||||
|
||||
Currently the asyncio implementation does not support returning telegram objects directly as a `read_as_object()` for async tcp is currently not implemented.
|
||||
Moreover, the telegram passed to `telegram_callback(telegram)` is already parsed. Therefore we can't feed it into the telegram constructor directly as that expects unparsed telegrams
|
||||
|
||||
However, if we construct a mock TelegramParser that just returns the already parsed object we can work around this. An example is below:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
#from dsmr_parser import obis_references
|
||||
#from dsmr_parser import telegram_specifications
|
||||
#from dsmr_parser.clients.protocol import create_dsmr_reader, create_tcp_dsmr_reader
|
||||
#from dsmr_parser.objects import Telegram
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format='%(message)s')
|
||||
|
||||
HOST = MY_HOST
|
||||
PORT = MY_PORT
|
||||
DSMR_VERSION = MY_DSMR_VERSION
|
||||
|
||||
logger = logging.getLogger('tcpclient')
|
||||
logger.debug("Logger created")
|
||||
|
||||
class mockTelegramParser(object):
|
||||
|
||||
def parse(self, telegram):
|
||||
return telegram
|
||||
|
||||
telegram_parser = mockTelegramParser()
|
||||
|
||||
def printTelegram(telegram):
|
||||
try:
|
||||
logger.info(Telegram(telegram, telegram_parser, telegram_specifications.V4))
|
||||
except InvalidChecksumError as e:
|
||||
logger.warning(str(e))
|
||||
except ParseError as e:
|
||||
logger.error('Failed to parse telegram: %s', e)
|
||||
|
||||
|
||||
async def main():
|
||||
try:
|
||||
logger.debug("Getting loop")
|
||||
loop = asyncio.get_event_loop()
|
||||
logger.debug("Creating reader")
|
||||
await create_tcp_dsmr_reader(
|
||||
HOST,
|
||||
PORT,
|
||||
DSMR_VERSION,
|
||||
printTelegram,
|
||||
loop
|
||||
)
|
||||
logger.debug("Reader created going to sleep now")
|
||||
while True:
|
||||
await asyncio.sleep(1)
|
||||
except Exception as e:
|
||||
logger.error("Unexpected error: "+ e)
|
||||
raise
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
logger.info('Closing down...')
|
||||
except Exception as e:
|
||||
logger.error("Unexpected error: "+ e)
|
||||
|
||||
Parsing module usage
|
||||
--------------------
|
||||
@@ -51,39 +177,39 @@ into a dictionary.
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from dsmr_parser import telegram_specifications
|
||||
from dsmr_parser.parsers import TelegramParser
|
||||
from dsmr_parser import telegram_specifications
|
||||
from dsmr_parser.parsers import TelegramParser
|
||||
|
||||
# String is formatted in separate lines for readability.
|
||||
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'
|
||||
)
|
||||
# String is formatted in separate lines for readability.
|
||||
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
|
||||
parser = TelegramParser(telegram_specifications.V3)
|
||||
|
||||
telegram = parser.parse(telegram_str)
|
||||
print(telegram) # see 'Telegram object' docs below
|
||||
|
||||
Telegram dictionary
|
||||
-------------------
|
||||
@@ -136,7 +262,7 @@ Example to get some of the values:
|
||||
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.
|
||||
# Note that the available values differ per DSMR version.
|
||||
|
||||
Telegram as an Object
|
||||
---------------------
|
||||
|
||||
@@ -16,8 +16,8 @@ def console():
|
||||
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', '5', '5B', '5L', '5S'],
|
||||
help='DSMR version (2.2, 4, 5, 5B, 5L, 5S)')
|
||||
parser.add_argument('--version', default='2.2', choices=['2.2', '4', '5', '5B', '5L', '5S', 'Q3D'],
|
||||
help='DSMR version (2.2, 4, 5, 5B, 5L, 5S, Q3D)')
|
||||
parser.add_argument('--verbose', '-v', action='count')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from dsmr_parser.clients.settings import SERIAL_SETTINGS_V2_2, \
|
||||
SERIAL_SETTINGS_V4, SERIAL_SETTINGS_V5
|
||||
from dsmr_parser.clients.serial_ import SerialReader, AsyncSerialReader
|
||||
from dsmr_parser.clients.socket_ import SocketReader
|
||||
from dsmr_parser.clients.protocol import create_dsmr_protocol, \
|
||||
create_dsmr_reader, create_tcp_dsmr_reader
|
||||
|
||||
@@ -64,8 +64,11 @@ class FileReader(object):
|
||||
with open(self._file, "rb") as file_handle:
|
||||
while True:
|
||||
data = file_handle.readline()
|
||||
str = data.decode()
|
||||
self.telegram_buffer.append(str)
|
||||
|
||||
if not data:
|
||||
break
|
||||
|
||||
self.telegram_buffer.append(data.decode())
|
||||
|
||||
for telegram in self.telegram_buffer.get_all():
|
||||
try:
|
||||
|
||||
@@ -16,6 +16,13 @@ from dsmr_parser.clients.settings import SERIAL_SETTINGS_V2_2, \
|
||||
|
||||
def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
||||
"""Creates a DSMR asyncio protocol."""
|
||||
protocol = _create_dsmr_protocol(dsmr_version, telegram_callback,
|
||||
DSMRProtocol, loop, **kwargs)
|
||||
return protocol
|
||||
|
||||
|
||||
def _create_dsmr_protocol(dsmr_version, telegram_callback, protocol, loop=None, **kwargs):
|
||||
"""Creates a DSMR asyncio protocol."""
|
||||
|
||||
if dsmr_version == '2.2':
|
||||
specification = telegram_specifications.V2_2
|
||||
@@ -23,6 +30,9 @@ def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
||||
elif dsmr_version == '4':
|
||||
specification = telegram_specifications.V4
|
||||
serial_settings = SERIAL_SETTINGS_V4
|
||||
elif dsmr_version == '4+':
|
||||
specification = telegram_specifications.V5
|
||||
serial_settings = SERIAL_SETTINGS_V4
|
||||
elif dsmr_version == '5':
|
||||
specification = telegram_specifications.V5
|
||||
serial_settings = SERIAL_SETTINGS_V5
|
||||
@@ -35,11 +45,14 @@ def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
||||
elif dsmr_version == "5S":
|
||||
specification = telegram_specifications.SWEDEN
|
||||
serial_settings = SERIAL_SETTINGS_V5
|
||||
elif dsmr_version == "Q3D":
|
||||
specification = telegram_specifications.Q3D
|
||||
serial_settings = SERIAL_SETTINGS_V5
|
||||
else:
|
||||
raise NotImplementedError("No telegram parser found for version: %s",
|
||||
dsmr_version)
|
||||
|
||||
protocol = partial(DSMRProtocol, loop, TelegramParser(specification),
|
||||
protocol = partial(protocol, loop, TelegramParser(specification),
|
||||
telegram_callback=telegram_callback, **kwargs)
|
||||
|
||||
return protocol, serial_settings
|
||||
@@ -99,12 +112,16 @@ class DSMRProtocol(asyncio.Protocol):
|
||||
|
||||
def data_received(self, data):
|
||||
"""Add incoming data to buffer."""
|
||||
data = data.decode('ascii')
|
||||
|
||||
# accept latin-1 (8-bit) on the line, to allow for non-ascii transport or padding
|
||||
data = data.decode("latin1")
|
||||
self._active = True
|
||||
self.log.debug('received data: %s', data)
|
||||
self.telegram_buffer.append(data)
|
||||
|
||||
for telegram in self.telegram_buffer.get_all():
|
||||
# ensure actual telegram is ascii (7-bit) only (ISO 646:1991 IRV required in section 5.5 of IEC 62056-21)
|
||||
telegram = telegram.encode("latin1").decode("ascii")
|
||||
self.handle_telegram(telegram)
|
||||
|
||||
def keep_alive(self):
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
"""Asyncio protocol implementation for handling telegrams over a RFXtrx connection ."""
|
||||
|
||||
import asyncio
|
||||
|
||||
from serial_asyncio import create_serial_connection
|
||||
from .protocol import DSMRProtocol, _create_dsmr_protocol
|
||||
|
||||
|
||||
def create_rfxtrx_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
||||
"""Creates a RFXtrxDSMR asyncio protocol."""
|
||||
protocol = _create_dsmr_protocol(dsmr_version, telegram_callback,
|
||||
RFXtrxDSMRProtocol, loop, **kwargs)
|
||||
return protocol
|
||||
|
||||
|
||||
def create_rfxtrx_dsmr_reader(port, dsmr_version, telegram_callback, loop=None):
|
||||
"""Creates a DSMR asyncio protocol coroutine using a RFXtrx serial port."""
|
||||
protocol, serial_settings = create_rfxtrx_dsmr_protocol(
|
||||
dsmr_version, telegram_callback, loop=None)
|
||||
serial_settings['url'] = port
|
||||
|
||||
conn = create_serial_connection(loop, protocol, **serial_settings)
|
||||
return conn
|
||||
|
||||
|
||||
def create_rfxtrx_tcp_dsmr_reader(host, port, dsmr_version,
|
||||
telegram_callback, loop=None,
|
||||
keep_alive_interval=None):
|
||||
"""Creates a DSMR asyncio protocol coroutine using a RFXtrx TCP connection."""
|
||||
if not loop:
|
||||
loop = asyncio.get_event_loop()
|
||||
protocol, _ = create_rfxtrx_dsmr_protocol(
|
||||
dsmr_version, telegram_callback, loop=loop,
|
||||
keep_alive_interval=keep_alive_interval)
|
||||
conn = loop.create_connection(protocol, host, port)
|
||||
return conn
|
||||
|
||||
|
||||
PACKETTYPE_DSMR = 0x62
|
||||
SUBTYPE_P1 = 0x01
|
||||
|
||||
|
||||
class RFXtrxDSMRProtocol(DSMRProtocol):
|
||||
|
||||
remaining_data = b''
|
||||
|
||||
def data_received(self, data):
|
||||
"""Add incoming data to buffer."""
|
||||
|
||||
data = self.remaining_data + data
|
||||
|
||||
packetlength = data[0] + 1 if len(data) > 0 else 1
|
||||
while packetlength <= len(data):
|
||||
packettype = data[1]
|
||||
subtype = data[2]
|
||||
if (packettype == PACKETTYPE_DSMR and subtype == SUBTYPE_P1):
|
||||
dsmr_data = data[4:packetlength]
|
||||
super().data_received(dsmr_data)
|
||||
data = data[packetlength:]
|
||||
packetlength = data[0] + 1 if len(data) > 0 else 1
|
||||
|
||||
self.remaining_data = data
|
||||
@@ -1,4 +1,3 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import serial
|
||||
import serial_asyncio
|
||||
@@ -96,3 +95,35 @@ class AsyncSerialReader(SerialReader):
|
||||
)
|
||||
except ParseError as e:
|
||||
logger.warning('Failed to parse telegram: %s', e)
|
||||
|
||||
async def read_as_object(self, queue):
|
||||
"""
|
||||
Read complete DSMR telegram's from the serial interface
|
||||
and return a Telegram object.
|
||||
|
||||
Instead of being a generator, Telegram objects are pushed
|
||||
to provided queue for asynchronous processing.
|
||||
|
||||
:rtype: None
|
||||
"""
|
||||
|
||||
# create Serial StreamReader
|
||||
conn = serial_asyncio.open_serial_connection(**self.serial_settings)
|
||||
reader, _ = await conn
|
||||
|
||||
while True:
|
||||
|
||||
# Read line if available or give control back to loop until new
|
||||
# data has arrived.
|
||||
data = await reader.readline()
|
||||
self.telegram_buffer.append(data.decode('ascii'))
|
||||
|
||||
for telegram in self.telegram_buffer.get_all():
|
||||
try:
|
||||
queue.put_nowait(
|
||||
Telegram(telegram, self.telegram_parser, self.telegram_specification)
|
||||
)
|
||||
except InvalidChecksumError as e:
|
||||
logger.warning(str(e))
|
||||
except ParseError as e:
|
||||
logger.error('Failed to parse telegram: %s', e)
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
import logging
|
||||
import socket
|
||||
|
||||
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
|
||||
from dsmr_parser.exceptions import ParseError, InvalidChecksumError
|
||||
from dsmr_parser.parsers import TelegramParser
|
||||
from dsmr_parser.objects import Telegram
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SocketReader(object):
|
||||
|
||||
BUFFER_SIZE = 256
|
||||
|
||||
def __init__(self, host, port, telegram_specification):
|
||||
self.host = host
|
||||
self.port = port
|
||||
|
||||
self.telegram_parser = TelegramParser(telegram_specification)
|
||||
self.telegram_buffer = TelegramBuffer()
|
||||
self.telegram_specification = telegram_specification
|
||||
|
||||
def read(self):
|
||||
"""
|
||||
Read complete DSMR telegram's from remote interface and parse it
|
||||
into CosemObject's and MbusObject's
|
||||
|
||||
:rtype: generator
|
||||
"""
|
||||
buffer = b""
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_handle:
|
||||
|
||||
socket_handle.connect((self.host, self.port))
|
||||
|
||||
while True:
|
||||
buffer += socket_handle.recv(self.BUFFER_SIZE)
|
||||
|
||||
lines = buffer.splitlines(keepends=True)
|
||||
|
||||
if len(lines) == 0:
|
||||
continue
|
||||
|
||||
for data in lines:
|
||||
self.telegram_buffer.append(data.decode('ascii'))
|
||||
|
||||
for telegram in self.telegram_buffer.get_all():
|
||||
try:
|
||||
yield self.telegram_parser.parse(telegram)
|
||||
except InvalidChecksumError as e:
|
||||
logger.warning(str(e))
|
||||
except ParseError as e:
|
||||
logger.error('Failed to parse telegram: %s', e)
|
||||
|
||||
buffer = b""
|
||||
|
||||
def read_as_object(self):
|
||||
"""
|
||||
Read complete DSMR telegram's from remote and return a Telegram object.
|
||||
|
||||
:rtype: generator
|
||||
"""
|
||||
buffer = b""
|
||||
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_handle:
|
||||
|
||||
socket_handle.connect((self.host, self.port))
|
||||
|
||||
while True:
|
||||
buffer += socket_handle.recv(self.BUFFER_SIZE)
|
||||
|
||||
lines = buffer.splitlines(keepends=True)
|
||||
|
||||
if len(lines) == 0:
|
||||
continue
|
||||
|
||||
for data in lines:
|
||||
self.telegram_buffer.append(data.decode('ascii'))
|
||||
|
||||
for telegram in self.telegram_buffer.get_all():
|
||||
try:
|
||||
yield Telegram(telegram, self.telegram_parser, self.telegram_specification)
|
||||
except InvalidChecksumError as e:
|
||||
logger.warning(str(e))
|
||||
except ParseError as e:
|
||||
logger.error('Failed to parse telegram: %s', e)
|
||||
|
||||
buffer = b""
|
||||
@@ -11,8 +11,11 @@ EN = {
|
||||
obis.P1_MESSAGE_HEADER: 'P1_MESSAGE_HEADER',
|
||||
obis.P1_MESSAGE_TIMESTAMP: 'P1_MESSAGE_TIMESTAMP',
|
||||
obis.ELECTRICITY_IMPORTED_TOTAL: 'ELECTRICITY_IMPORTED_TOTAL',
|
||||
obis.ELECTRICITY_REACTIVE_IMPORTED_TOTAL: 'ELECTRICITY_REACTIVE_IMPORTED_TOTAL',
|
||||
obis.ELECTRICITY_USED_TARIFF_1: 'ELECTRICITY_USED_TARIFF_1',
|
||||
obis.ELECTRICITY_USED_TARIFF_2: 'ELECTRICITY_USED_TARIFF_2',
|
||||
obis.ELECTRICITY_EXPORTED_TOTAL: 'ELECTRICITY_EXPORTED_TOTAL',
|
||||
obis.ELECTRICITY_REACTIVE_EXPORTED_TOTAL: 'ELECTRICITY_REACTIVE_EXPORTED_TOTAL',
|
||||
obis.ELECTRICITY_DELIVERED_TARIFF_1: 'ELECTRICITY_DELIVERED_TARIFF_1',
|
||||
obis.ELECTRICITY_DELIVERED_TARIFF_2: 'ELECTRICITY_DELIVERED_TARIFF_2',
|
||||
obis.ELECTRICITY_ACTIVE_TARIFF: 'ELECTRICITY_ACTIVE_TARIFF',
|
||||
@@ -43,18 +46,46 @@ EN = {
|
||||
obis.INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE: 'INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE',
|
||||
obis.INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE: 'INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE',
|
||||
obis.INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE: 'INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE',
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L1_POSITIVE: 'INSTANTANEOUS_REACTIVE_POWER_L1_POSITIVE',
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L1_NEGATIVE: 'INSTANTANEOUS_REACTIVE_POWER_L1_NEGATIVE',
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L2_POSITIVE: 'INSTANTANEOUS_REACTIVE_POWER_L2_POSITIVE',
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L2_NEGATIVE: 'INSTANTANEOUS_REACTIVE_POWER_L2_NEGATIVE',
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L3_POSITIVE: 'INSTANTANEOUS_REACTIVE_POWER_L3_POSITIVE',
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L3_NEGATIVE: 'INSTANTANEOUS_REACTIVE_POWER_L3_NEGATIVE',
|
||||
obis.EQUIPMENT_IDENTIFIER_GAS: 'EQUIPMENT_IDENTIFIER_GAS',
|
||||
obis.HOURLY_GAS_METER_READING: 'HOURLY_GAS_METER_READING',
|
||||
obis.GAS_METER_READING: 'GAS_METER_READING',
|
||||
obis.ACTUAL_TRESHOLD_ELECTRICITY: 'ACTUAL_TRESHOLD_ELECTRICITY',
|
||||
obis.ACTUAL_SWITCH_POSITION: 'ACTUAL_SWITCH_POSITION',
|
||||
obis.VALVE_POSITION_GAS: 'VALVE_POSITION_GAS',
|
||||
obis.BELGIUM_HOURLY_GAS_METER_READING: 'BELGIUM_HOURLY_GAS_METER_READING',
|
||||
obis.BELGIUM_VERSION_INFORMATION: 'BELGIUM_VERSION_INFORMATION',
|
||||
obis.BELGIUM_EQUIPMENT_IDENTIFIER: 'BELGIUM_EQUIPMENT_IDENTIFIER',
|
||||
obis.BELGIUM_CURRENT_AVERAGE_DEMAND: 'BELGIUM_CURRENT_AVERAGE_DEMAND',
|
||||
obis.BELGIUM_MAXIMUM_DEMAND_MONTH: 'BELGIUM_MAXIMUM_DEMAND_MONTH',
|
||||
obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS: 'BELGIUM_MAXIMUM_DEMAND_13_MONTHS',
|
||||
obis.BELGIUM_MAX_POWER_PER_PHASE: 'BELGIUM_MAX_POWER_PER_PHASE',
|
||||
obis.BELGIUM_MAX_CURRENT_PER_PHASE: 'BELGIUM_MAX_CURRENT_PER_PHASE',
|
||||
obis.BELGIUM_MBUS1_DEVICE_TYPE: 'BELGIUM_MBUS1_DEVICE_TYPE',
|
||||
obis.BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER: 'BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER',
|
||||
obis.BELGIUM_MBUS1_VALVE_POSITION: 'BELGIUM_MBUS1_VALVE_POSITION',
|
||||
obis.BELGIUM_MBUS1_METER_READING1: 'BELGIUM_MBUS1_METER_READING1',
|
||||
obis.BELGIUM_MBUS1_METER_READING2: 'BELGIUM_MBUS1_METER_READING2',
|
||||
obis.BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER: 'BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER',
|
||||
obis.BELGIUM_MBUS2_VALVE_POSITION: 'BELGIUM_MBUS2_VALVE_POSITION',
|
||||
obis.BELGIUM_MBUS2_METER_READING1: 'BELGIUM_MBUS2_METER_READING1',
|
||||
obis.BELGIUM_MBUS2_METER_READING2: 'BELGIUM_MBUS2_METER_READING2',
|
||||
obis.BELGIUM_MBUS3_EQUIPMENT_IDENTIFIER: 'BELGIUM_MBUS3_EQUIPMENT_IDENTIFIER',
|
||||
obis.BELGIUM_MBUS3_VALVE_POSITION: 'BELGIUM_MBUS3_VALVE_POSITION',
|
||||
obis.BELGIUM_MBUS3_METER_READING1: 'BELGIUM_MBUS3_METER_READING1',
|
||||
obis.BELGIUM_MBUS3_METER_READING2: 'BELGIUM_MBUS3_METER_READING2',
|
||||
obis.BELGIUM_MBUS4_EQUIPMENT_IDENTIFIER: 'BELGIUM_MBUS4_EQUIPMENT_IDENTIFIER',
|
||||
obis.BELGIUM_MBUS4_VALVE_POSITION: 'BELGIUM_MBUS4_VALVE_POSITION',
|
||||
obis.BELGIUM_MBUS4_METER_READING1: 'BELGIUM_MBUS4_METER_READING1',
|
||||
obis.BELGIUM_MBUS4_METER_READING2: 'BELGIUM_MBUS4_METER_READING2',
|
||||
obis.LUXEMBOURG_EQUIPMENT_IDENTIFIER: 'LUXEMBOURG_EQUIPMENT_IDENTIFIER',
|
||||
obis.LUXEMBOURG_ELECTRICITY_USED_TARIFF_GLOBAL: 'LUXEMBOURG_ELECTRICITY_USED_TARIFF_GLOBAL',
|
||||
obis.LUXEMBOURG_ELECTRICITY_DELIVERED_TARIFF_GLOBAL: 'LUXEMBOURG_ELECTRICITY_DELIVERED_TARIFF_GLOBAL',
|
||||
obis.SWEDEN_ELECTRICITY_USED_TARIFF_GLOBAL: 'SWEDEN_ELECTRICITY_USED_TARIFF_GLOBAL',
|
||||
obis.SWEDEN_ELECTRICITY_DELIVERED_TARIFF_GLOBAL: 'SWEDEN_ELECTRICITY_DELIVERED_TARIFF_GLOBAL',
|
||||
obis.Q3D_EQUIPMENT_IDENTIFIER: 'Q3D_EQUIPMENT_IDENTIFIER',
|
||||
obis.Q3D_EQUIPMENT_STATE: 'Q3D_EQUIPMENT_STATE',
|
||||
obis.Q3D_EQUIPMENT_SERIALNUMBER: 'Q3D_EQUIPMENT_SERIALNUMBER',
|
||||
}
|
||||
|
||||
REVERSE_EN = dict([(v, k) for k, v in EN.items()])
|
||||
|
||||
@@ -8,11 +8,18 @@ objects are introduced.
|
||||
"""
|
||||
P1_MESSAGE_HEADER = r'\d-\d:0\.2\.8.+?\r\n'
|
||||
P1_MESSAGE_TIMESTAMP = r'\d-\d:1\.0\.0.+?\r\n'
|
||||
ELECTRICITY_IMPORTED_TOTAL = r'\d-\d:1\.8\.0.+?\r\n'
|
||||
ELECTRICITY_USED_TARIFF_1 = r'\d-\d:1\.8\.1.+?\r\n'
|
||||
ELECTRICITY_USED_TARIFF_2 = r'\d-\d:1\.8\.2.+?\r\n'
|
||||
ELECTRICITY_DELIVERED_TARIFF_1 = r'\d-\d:2\.8\.1.+?\r\n'
|
||||
ELECTRICITY_DELIVERED_TARIFF_2 = r'\d-\d:2\.8\.2.+?\r\n'
|
||||
CURRENT_REACTIVE_IMPORTED = r'\d-\d:3\.7\.0.+?\r\n'
|
||||
ELECTRICITY_REACTIVE_IMPORTED_TOTAL = r'\d-\d:3\.8\.0.+?\r\n'
|
||||
ELECTRICITY_REACTIVE_IMPORTED_TARIFF_1 = r'\d-\d:3\.8\.1.+?\r\n'
|
||||
ELECTRICITY_REACTIVE_IMPORTED_TARIFF_2 = r'\d-\d:3\.8\.2.+?\r\n'
|
||||
CURRENT_REACTIVE_EXPORTED = r'\d-\d:4\.7\.0.+?\r\n'
|
||||
ELECTRICITY_REACTIVE_EXPORTED_TOTAL = r'\d-\d:4\.8\.0.+?\r\n'
|
||||
ELECTRICITY_REACTIVE_EXPORTED_TARIFF_1 = r'\d-\d:4\.8\.1.+?\r\n'
|
||||
ELECTRICITY_REACTIVE_EXPORTED_TARIFF_2 = r'\d-\d:4\.8\.2.+?\r\n'
|
||||
ELECTRICITY_ACTIVE_TARIFF = r'\d-\d:96\.14\.0.+?\r\n'
|
||||
EQUIPMENT_IDENTIFIER = r'\d-\d:96\.1\.1.+?\r\n'
|
||||
CURRENT_ELECTRICITY_USAGE = r'\d-\d:1\.7\.0.+?\r\n'
|
||||
@@ -41,6 +48,12 @@ INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE = r'\d-\d:61\.7\.0.+?\r\n'
|
||||
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'
|
||||
INSTANTANEOUS_REACTIVE_POWER_L1_POSITIVE = r'\d-\d:23\.7\.0.+?\r\n'
|
||||
INSTANTANEOUS_REACTIVE_POWER_L1_NEGATIVE = r'\d-\d:24\.7\.0.+?\r\n'
|
||||
INSTANTANEOUS_REACTIVE_POWER_L2_POSITIVE = r'\d-\d:43\.7\.0.+?\r\n'
|
||||
INSTANTANEOUS_REACTIVE_POWER_L2_NEGATIVE = r'\d-\d:44\.7\.0.+?\r\n'
|
||||
INSTANTANEOUS_REACTIVE_POWER_L3_POSITIVE = r'\d-\d:63\.7\.0.+?\r\n'
|
||||
INSTANTANEOUS_REACTIVE_POWER_L3_NEGATIVE = r'\d-\d:64\.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'
|
||||
@@ -61,10 +74,51 @@ ELECTRICITY_DELIVERED_TARIFF_ALL = (
|
||||
ELECTRICITY_DELIVERED_TARIFF_2
|
||||
)
|
||||
|
||||
# Alternate codes for foreign countries.
|
||||
BELGIUM_HOURLY_GAS_METER_READING = r'\d-\d:24\.2\.3.+?\r\n' # Different code, same format.
|
||||
# International generalized additions
|
||||
ELECTRICITY_IMPORTED_TOTAL = r'\d-\d:1\.8\.0.+?\r\n' # Total imported energy register (P+)
|
||||
ELECTRICITY_EXPORTED_TOTAL = r'\d-\d:2\.8\.0.+?\r\n' # Total exported energy register (P-)
|
||||
|
||||
# International non generalized additions (country specific) / risk for necessary refactoring
|
||||
BELGIUM_VERSION_INFORMATION = r'\d-\d:96\.1\.4.+?\r\n'
|
||||
BELGIUM_EQUIPMENT_IDENTIFIER = r'\d-0:96\.1\.1.+?\r\n'
|
||||
BELGIUM_CURRENT_AVERAGE_DEMAND = r'\d-\d:1\.4\.0.+?\r\n'
|
||||
BELGIUM_MAXIMUM_DEMAND_MONTH = r'\d-\d:1\.6\.0.+?\r\n'
|
||||
BELGIUM_MAXIMUM_DEMAND_13_MONTHS = r'\d-\d:98\.1\.0.+?\r\n'
|
||||
BELGIUM_MAX_POWER_PER_PHASE = r'\d-\d:17\.0\.0.+?\r\n' # Applicable when power limitation is active
|
||||
BELGIUM_MAX_CURRENT_PER_PHASE = r'\d-\d:31\.4\.0.+?\r\n' # Applicable when current limitation is active
|
||||
|
||||
# Multiple 'slaves' can be linked to the main device.
|
||||
# Mostly MBUS1 = GAS METER with values on 24.2.3
|
||||
# While WATER METER reports it's values on 24.2.1
|
||||
# The GAS METER also reports its valve state on 24.4.0
|
||||
# Dev type for gas = 7 and water = 8
|
||||
BELGIUM_MBUS1_DEVICE_TYPE = r'\d-1:24\.1\.0.+?\r\n'
|
||||
BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER = r'\d-1:96\.1\.1.+?\r\n'
|
||||
BELGIUM_MBUS1_VALVE_POSITION = r'\d-1:24\.4\.0.+?\r\n'
|
||||
BELGIUM_MBUS1_METER_READING1 = r'\d-1:24\.2\.1.+?\r\n'
|
||||
BELGIUM_MBUS1_METER_READING2 = r'\d-1:24\.2\.3.+?\r\n'
|
||||
|
||||
BELGIUM_MBUS2_DEVICE_TYPE = r'\d-2:24\.1\.0.+?\r\n'
|
||||
BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER = r'\d-2:96\.1\.1.+?\r\n'
|
||||
BELGIUM_MBUS2_VALVE_POSITION = r'\d-2:24\.4\.0.+?\r\n'
|
||||
BELGIUM_MBUS2_METER_READING1 = r'\d-2:24\.2\.1.+?\r\n'
|
||||
BELGIUM_MBUS2_METER_READING2 = r'\d-2:24\.2\.3.+?\r\n'
|
||||
|
||||
BELGIUM_MBUS3_DEVICE_TYPE = r'\d-3:24\.1\.0.+?\r\n'
|
||||
BELGIUM_MBUS3_EQUIPMENT_IDENTIFIER = r'\d-3:96\.1\.1.+?\r\n'
|
||||
BELGIUM_MBUS3_VALVE_POSITION = r'\d-3:24\.4\.0.+?\r\n'
|
||||
BELGIUM_MBUS3_METER_READING1 = r'\d-3:24\.2\.1.+?\r\n'
|
||||
BELGIUM_MBUS3_METER_READING2 = r'\d-3:24\.2\.3.+?\r\n'
|
||||
|
||||
BELGIUM_MBUS4_DEVICE_TYPE = r'\d-4:24\.1\.0.+?\r\n'
|
||||
BELGIUM_MBUS4_EQUIPMENT_IDENTIFIER = r'\d-4:96\.1\.1.+?\r\n'
|
||||
BELGIUM_MBUS4_VALVE_POSITION = r'\d-4:24\.4\.0.+?\r\n'
|
||||
BELGIUM_MBUS4_METER_READING1 = r'\d-4:24\.2\.1.+?\r\n'
|
||||
BELGIUM_MBUS4_METER_READING2 = r'\d-4:24\.2\.3.+?\r\n'
|
||||
|
||||
|
||||
LUXEMBOURG_EQUIPMENT_IDENTIFIER = r'\d-\d:42\.0\.0.+?\r\n' # Logical device name
|
||||
LUXEMBOURG_ELECTRICITY_USED_TARIFF_GLOBAL = r'\d-\d:1\.8\.0.+?\r\n' # Total imported energy register (P+)
|
||||
LUXEMBOURG_ELECTRICITY_DELIVERED_TARIFF_GLOBAL = r'\d-\d:2\.8\.0.+?\r\n' # Total exported energy register (P-)
|
||||
SWEDEN_ELECTRICITY_USED_TARIFF_GLOBAL = r'\d-\d:1\.8\.0.+?\r\n' # Total imported energy register (P+)
|
||||
SWEDEN_ELECTRICITY_DELIVERED_TARIFF_GLOBAL = r'\d-\d:2\.8\.0.+?\r\n' # Total exported energy register (P-)
|
||||
|
||||
Q3D_EQUIPMENT_IDENTIFIER = r'\d-\d:0\.0\.0.+?\r\n' # Logical device name
|
||||
Q3D_EQUIPMENT_STATE = r'\d-\d:96\.5\.5.+?\r\n' # Device state (hexadecimal)
|
||||
Q3D_EQUIPMENT_SERIALNUMBER = r'\d-\d:96\.1\.255.+?\r\n' # Device Serialnumber
|
||||
|
||||
+46
-1
@@ -114,6 +114,51 @@ class MBusObject(DSMRObject):
|
||||
return json.dumps(output)
|
||||
|
||||
|
||||
class MBusObjectPeak(DSMRObject):
|
||||
|
||||
@property
|
||||
def datetime(self):
|
||||
return self.values[0]['value']
|
||||
|
||||
@property
|
||||
def occurred(self):
|
||||
return self.values[1]['value']
|
||||
|
||||
@property
|
||||
def value(self):
|
||||
return self.values[2]['value']
|
||||
|
||||
@property
|
||||
def unit(self):
|
||||
return self.values[2]['unit']
|
||||
|
||||
def __str__(self):
|
||||
output = "{}\t[{}] at {} occurred {}"\
|
||||
.format(str(self.value), str(self.unit), str(self.datetime.astimezone().isoformat()),
|
||||
str(self.occurred.astimezone().isoformat()))
|
||||
return output
|
||||
|
||||
def to_json(self):
|
||||
timestamp = self.datetime
|
||||
if isinstance(self.datetime, datetime.datetime):
|
||||
timestamp = self.datetime.astimezone().isoformat()
|
||||
timestamp_occurred = self.occurred
|
||||
if isinstance(self.occurred, datetime.datetime):
|
||||
timestamp_occurred = self.occurred.astimezone().isoformat()
|
||||
value = self.value
|
||||
if isinstance(self.value, datetime.datetime):
|
||||
value = self.value.astimezone().isoformat()
|
||||
if isinstance(self.value, Decimal):
|
||||
value = float(self.value)
|
||||
output = {
|
||||
'datetime': timestamp,
|
||||
'occurred': timestamp_occurred,
|
||||
'value': value,
|
||||
'unit': self.unit
|
||||
}
|
||||
return json.dumps(output)
|
||||
|
||||
|
||||
class CosemObject(DSMRObject):
|
||||
|
||||
@property
|
||||
@@ -179,7 +224,7 @@ class ProfileGenericObject(DSMRObject):
|
||||
self._buffer_list = []
|
||||
values_offset = 2
|
||||
for i in range(self.buffer_length):
|
||||
offset = values_offset + i*2
|
||||
offset = values_offset + i * 2
|
||||
self._buffer_list.append(MBusObject([self.values[offset], self.values[offset + 1]]))
|
||||
return self._buffer_list
|
||||
|
||||
|
||||
+79
-3
@@ -1,10 +1,16 @@
|
||||
import logging
|
||||
import re
|
||||
from binascii import unhexlify
|
||||
|
||||
from ctypes import c_ushort
|
||||
from decimal import Decimal
|
||||
|
||||
from dsmr_parser.objects import MBusObject, CosemObject, ProfileGenericObject
|
||||
from dlms_cosem.connection import XDlmsApduFactory
|
||||
from dlms_cosem.protocol.xdlms import GeneralGlobalCipher
|
||||
|
||||
from dsmr_parser.objects import MBusObject, MBusObjectPeak, CosemObject, ProfileGenericObject
|
||||
from dsmr_parser.exceptions import ParseError, InvalidChecksumError
|
||||
from dsmr_parser.value_types import timestamp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -22,14 +28,15 @@ class TelegramParser(object):
|
||||
self.telegram_specification = telegram_specification
|
||||
self.apply_checksum_validation = apply_checksum_validation
|
||||
|
||||
def parse(self, telegram_data):
|
||||
def parse(self, telegram_data, encryption_key="", authentication_key=""): # noqa: C901
|
||||
"""
|
||||
Parse telegram from string to dict.
|
||||
|
||||
The telegram str type makes python 2.x integration easier.
|
||||
|
||||
:param str telegram_data: full telegram from start ('/') to checksum
|
||||
('!ABCD') including line endings in between the telegram's lines
|
||||
:param str encryption_key: encryption key
|
||||
:param str authentication_key: authentication key
|
||||
:rtype: dict
|
||||
:returns: Shortened example:
|
||||
{
|
||||
@@ -43,6 +50,38 @@ class TelegramParser(object):
|
||||
:raises InvalidChecksumError:
|
||||
"""
|
||||
|
||||
if "general_global_cipher" in self.telegram_specification:
|
||||
if self.telegram_specification["general_global_cipher"]:
|
||||
enc_key = unhexlify(encryption_key)
|
||||
auth_key = unhexlify(authentication_key)
|
||||
telegram_data = unhexlify(telegram_data)
|
||||
apdu = XDlmsApduFactory.apdu_from_bytes(apdu_bytes=telegram_data)
|
||||
if apdu.security_control.security_suite != 0:
|
||||
logger.warning("Untested security suite")
|
||||
if apdu.security_control.authenticated and not apdu.security_control.encrypted:
|
||||
logger.warning("Untested authentication only")
|
||||
if not apdu.security_control.authenticated and not apdu.security_control.encrypted:
|
||||
logger.warning("Untested not encrypted or authenticated")
|
||||
if apdu.security_control.compressed:
|
||||
logger.warning("Untested compression")
|
||||
if apdu.security_control.broadcast_key:
|
||||
logger.warning("Untested broadcast key")
|
||||
telegram_data = apdu.to_plain_apdu(enc_key, auth_key).decode("ascii")
|
||||
else:
|
||||
try:
|
||||
if unhexlify(telegram_data[0:2])[0] == GeneralGlobalCipher.TAG:
|
||||
raise RuntimeError("Looks like a general_global_cipher frame "
|
||||
"but telegram specification is not matching!")
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
if unhexlify(telegram_data[0:2])[0] == GeneralGlobalCipher.TAG:
|
||||
raise RuntimeError(
|
||||
"Looks like a general_global_cipher frame but telegram specification is not matching!")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if self.apply_checksum_validation \
|
||||
and self.telegram_specification['checksum_support']:
|
||||
self.validate_checksum(telegram_data)
|
||||
@@ -177,6 +216,43 @@ class MBusParser(DSMRObjectParser):
|
||||
return MBusObject(self._parse(line))
|
||||
|
||||
|
||||
class MaxDemandParser(DSMRObjectParser):
|
||||
"""
|
||||
Max demand history parser.
|
||||
|
||||
These are lines with multiple values. Each containing 2 timestamps and a value
|
||||
|
||||
Line format:
|
||||
'ID (Count) (ID) (ID) (TST) (TST) (Mv1*U1)'
|
||||
|
||||
1 2 3 4 5 6 7
|
||||
|
||||
1) OBIS Reduced ID-code
|
||||
2) Amount of values in the response
|
||||
3) ID of the source
|
||||
4) ^^
|
||||
5) Time Stamp (TST) of the month
|
||||
6) Time Stamp (TST) when the max demand occured
|
||||
6) Measurement value 1 (most recent entry of buffer attribute without unit)
|
||||
7) Unit of measurement values (Unit of capture objects attribute)
|
||||
"""
|
||||
|
||||
def parse(self, line):
|
||||
pattern = re.compile(r'((?<=\()[0-9a-zA-Z\.\*\-\:]{0,}(?=\)))')
|
||||
values = re.findall(pattern, line)
|
||||
|
||||
objects = []
|
||||
|
||||
count = int(values[0])
|
||||
for i in range(1, count + 1):
|
||||
timestamp_month = ValueParser(timestamp).parse(values[i * 3 + 1])
|
||||
timestamp_occurred = ValueParser(timestamp).parse(values[i * 3 + 1])
|
||||
value = ValueParser(Decimal).parse(values[i * 3 + 2])
|
||||
objects.append(MBusObjectPeak([timestamp_month, timestamp_occurred, value]))
|
||||
|
||||
return objects
|
||||
|
||||
|
||||
class CosemParser(DSMRObjectParser):
|
||||
"""
|
||||
Cosem object parser.
|
||||
|
||||
@@ -7,4 +7,4 @@ PG_HEAD_PARSERS = [ValueParser(int), ValueParser(str)]
|
||||
PG_UNIDENTIFIED_BUFFERTYPE_PARSERS = [ValueParser(str), ValueParser(str)]
|
||||
BUFFER_TYPES = {
|
||||
PG_FAILURE_EVENT: [ValueParser(timestamp), ValueParser(int)]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ from decimal import Decimal
|
||||
from copy import deepcopy
|
||||
|
||||
from dsmr_parser import obis_references as obis
|
||||
from dsmr_parser.parsers import CosemParser, ValueParser, MBusParser, ProfileGenericParser
|
||||
from dsmr_parser.parsers import CosemParser, ValueParser, MBusParser, ProfileGenericParser, MaxDemandParser
|
||||
from dsmr_parser.value_types import timestamp
|
||||
from dsmr_parser.profile_generic_specifications import BUFFER_TYPES, PG_HEAD_PARSERS, PG_UNIDENTIFIED_BUFFERTYPE_PARSERS
|
||||
|
||||
@@ -141,30 +141,23 @@ V5 = {
|
||||
|
||||
ALL = (V2_2, V3, V4, V5)
|
||||
|
||||
|
||||
BELGIUM_FLUVIUS = deepcopy(V5)
|
||||
BELGIUM_FLUVIUS['objects'].update({
|
||||
obis.BELGIUM_HOURLY_GAS_METER_READING: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
)
|
||||
})
|
||||
|
||||
LUXEMBOURG_SMARTY = deepcopy(V5)
|
||||
LUXEMBOURG_SMARTY['objects'].update({
|
||||
obis.LUXEMBOURG_EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||
obis.LUXEMBOURG_ELECTRICITY_USED_TARIFF_GLOBAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.LUXEMBOURG_ELECTRICITY_DELIVERED_TARIFF_GLOBAL: CosemParser(ValueParser(Decimal)),
|
||||
})
|
||||
|
||||
# Source: https://www.energiforetagen.se/globalassets/energiforetagen/det-erbjuder-vi/kurser-och-konferenser/elnat/branschrekommendation-lokalt-granssnitt-v2_0-201912.pdf
|
||||
SWEDEN = {
|
||||
BELGIUM_FLUVIUS = {
|
||||
'checksum_support': True,
|
||||
'objects': {
|
||||
obis.P1_MESSAGE_HEADER: CosemParser(ValueParser(str)),
|
||||
obis.BELGIUM_VERSION_INFORMATION: CosemParser(ValueParser(str)),
|
||||
obis.EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||
obis.P1_MESSAGE_TIMESTAMP: CosemParser(ValueParser(timestamp)),
|
||||
obis.SWEDEN_ELECTRICITY_USED_TARIFF_GLOBAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.SWEDEN_ELECTRICITY_DELIVERED_TARIFF_GLOBAL: CosemParser(ValueParser(Decimal)),
|
||||
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.BELGIUM_CURRENT_AVERAGE_DEMAND: CosemParser(ValueParser(Decimal)),
|
||||
obis.BELGIUM_MAXIMUM_DEMAND_MONTH: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS: MaxDemandParser(),
|
||||
obis.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
|
||||
obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||
@@ -179,5 +172,140 @@ SWEDEN = {
|
||||
obis.INSTANTANEOUS_CURRENT_L1: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_CURRENT_L2: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_CURRENT_L3: CosemParser(ValueParser(Decimal)),
|
||||
obis.ACTUAL_SWITCH_POSITION: CosemParser(ValueParser(int)),
|
||||
obis.ACTUAL_TRESHOLD_ELECTRICITY: CosemParser(ValueParser(Decimal)),
|
||||
obis.BELGIUM_MAX_POWER_PER_PHASE: CosemParser(ValueParser(Decimal)),
|
||||
obis.BELGIUM_MAX_CURRENT_PER_PHASE: CosemParser(ValueParser(Decimal)),
|
||||
obis.TEXT_MESSAGE: CosemParser(ValueParser(str)),
|
||||
obis.BELGIUM_MBUS1_DEVICE_TYPE: CosemParser(ValueParser(int)),
|
||||
obis.BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||
obis.BELGIUM_MBUS1_VALVE_POSITION: CosemParser(ValueParser(int)),
|
||||
obis.BELGIUM_MBUS1_METER_READING1: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
obis.BELGIUM_MBUS1_METER_READING2: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
obis.BELGIUM_MBUS2_DEVICE_TYPE: CosemParser(ValueParser(int)),
|
||||
obis.BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||
obis.BELGIUM_MBUS2_VALVE_POSITION: CosemParser(ValueParser(int)),
|
||||
obis.BELGIUM_MBUS2_METER_READING1: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
obis.BELGIUM_MBUS2_METER_READING2: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
obis.BELGIUM_MBUS3_DEVICE_TYPE: CosemParser(ValueParser(int)),
|
||||
obis.BELGIUM_MBUS3_EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||
obis.BELGIUM_MBUS3_VALVE_POSITION: CosemParser(ValueParser(int)),
|
||||
obis.BELGIUM_MBUS3_METER_READING1: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
obis.BELGIUM_MBUS3_METER_READING2: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
obis.BELGIUM_MBUS4_DEVICE_TYPE: CosemParser(ValueParser(int)),
|
||||
obis.BELGIUM_MBUS4_EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||
obis.BELGIUM_MBUS4_VALVE_POSITION: CosemParser(ValueParser(int)),
|
||||
obis.BELGIUM_MBUS4_METER_READING1: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
obis.BELGIUM_MBUS4_METER_READING2: MBusParser(
|
||||
ValueParser(timestamp),
|
||||
ValueParser(Decimal)
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
LUXEMBOURG_SMARTY = deepcopy(V5)
|
||||
LUXEMBOURG_SMARTY['objects'].update({
|
||||
obis.LUXEMBOURG_EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||
obis.ELECTRICITY_IMPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_EXPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
})
|
||||
|
||||
# Source: https://www.energiforetagen.se/globalassets/energiforetagen/det-erbjuder-vi/kurser-och-konferenser/elnat/
|
||||
# branschrekommendation-lokalt-granssnitt-v2_0-201912.pdf
|
||||
SWEDEN = {
|
||||
'checksum_support': True,
|
||||
'objects': {
|
||||
obis.P1_MESSAGE_HEADER: CosemParser(ValueParser(str)),
|
||||
obis.P1_MESSAGE_TIMESTAMP: CosemParser(ValueParser(timestamp)),
|
||||
obis.ELECTRICITY_IMPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_EXPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_REACTIVE_IMPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_REACTIVE_EXPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
|
||||
obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)),
|
||||
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.INSTANTANEOUS_REACTIVE_POWER_L1_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L1_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L2_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L2_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L3_POSITIVE: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_REACTIVE_POWER_L3_NEGATIVE: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_VOLTAGE_L1: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_VOLTAGE_L2: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_VOLTAGE_L3: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_CURRENT_L1: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_CURRENT_L2: CosemParser(ValueParser(Decimal)),
|
||||
obis.INSTANTANEOUS_CURRENT_L3: CosemParser(ValueParser(Decimal)),
|
||||
}
|
||||
}
|
||||
|
||||
Q3D = {
|
||||
"checksum_support": False,
|
||||
"objects": {
|
||||
obis.Q3D_EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)),
|
||||
obis.ELECTRICITY_IMPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_EXPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
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.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
|
||||
obis.Q3D_EQUIPMENT_STATE: CosemParser(ValueParser(str)),
|
||||
obis.Q3D_EQUIPMENT_SERIALNUMBER: CosemParser(ValueParser(str)),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
SAGEMCOM_T210_D_R = {
|
||||
"general_global_cipher": True,
|
||||
"checksum_support": True,
|
||||
'objects': {
|
||||
obis.P1_MESSAGE_HEADER: CosemParser(ValueParser(str)),
|
||||
obis.P1_MESSAGE_TIMESTAMP: CosemParser(ValueParser(timestamp)),
|
||||
obis.ELECTRICITY_IMPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_USED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_USED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
||||
obis.CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)),
|
||||
|
||||
obis.ELECTRICITY_REACTIVE_EXPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_REACTIVE_EXPORTED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_REACTIVE_EXPORTED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
||||
obis.CURRENT_REACTIVE_IMPORTED: CosemParser(ValueParser(Decimal)),
|
||||
|
||||
obis.ELECTRICITY_EXPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_DELIVERED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_DELIVERED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
||||
obis.CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)),
|
||||
|
||||
obis.ELECTRICITY_REACTIVE_IMPORTED_TOTAL: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_REACTIVE_IMPORTED_TARIFF_1: CosemParser(ValueParser(Decimal)),
|
||||
obis.ELECTRICITY_REACTIVE_IMPORTED_TARIFF_2: CosemParser(ValueParser(Decimal)),
|
||||
obis.CURRENT_REACTIVE_EXPORTED: CosemParser(ValueParser(Decimal)),
|
||||
}
|
||||
}
|
||||
AUSTRIA_ENERGIENETZE_STEIERMARK = SAGEMCOM_T210_D_R
|
||||
|
||||
@@ -4,16 +4,17 @@ setup(
|
||||
name='dsmr-parser',
|
||||
description='Library to parse Dutch Smart Meter Requirements (DSMR)',
|
||||
author='Nigel Dokter and many others',
|
||||
author_email='nigel@nldr.net',
|
||||
author_email='mail@nldr.net',
|
||||
license='MIT',
|
||||
url='https://github.com/ndokter/dsmr_parser',
|
||||
version='0.30',
|
||||
version='1.1.0',
|
||||
packages=find_packages(exclude=('test', 'test.*')),
|
||||
install_requires=[
|
||||
'pyserial>=3,<4',
|
||||
'pyserial-asyncio<1',
|
||||
'pytz',
|
||||
'Tailer==0.4.1'
|
||||
'Tailer==0.4.1',
|
||||
'dlms_cosem==21.3.2'
|
||||
],
|
||||
entry_points={
|
||||
'console_scripts': ['dsmr_console=dsmr_parser.__main__:console']
|
||||
|
||||
@@ -128,3 +128,110 @@ TELEGRAM_V5 = (
|
||||
'0-2:96.1.0()\r\n'
|
||||
'!6EEE\r\n'
|
||||
)
|
||||
|
||||
TELEGRAM_FLUVIUS_V171 = (
|
||||
'/FLU5\253769484_A\r\n'
|
||||
'\r\n'
|
||||
'0-0:96.1.4(50217)\r\n'
|
||||
'0-0:96.1.1(3153414733313031303231363035)\r\n'
|
||||
'0-0:1.0.0(200512135409S)\r\n'
|
||||
'1-0:1.8.1(000000.034*kWh)\r\n'
|
||||
'1-0:1.8.2(000015.758*kWh)\r\n'
|
||||
'1-0:2.8.1(000000.000*kWh)\r\n'
|
||||
'1-0:2.8.2(000000.011*kWh)\r\n'
|
||||
'1-0:1.4.0(02.351*kW)\r\n'
|
||||
'1-0:1.6.0(200509134558S)(02.589*kW)\r\n'
|
||||
'0-0:98.1.0(3)(1-0:1.6.0)(1-0:1.6.0)(200501000000S)(200423192538S)(03.695*kW)(200401000000S)(200305122139S)(05.980*kW)(200301000000S)(200210035421W)(04.318*kW)\r\n'
|
||||
'0-0:96.14.0(0001)\r\n'
|
||||
'1-0:1.7.0(00.000*kW)\r\n'
|
||||
'1-0:2.7.0(00.000*kW)\r\n'
|
||||
'1-0:21.7.0(00.000*kW)\r\n'
|
||||
'1-0:41.7.0(00.000*kW)\r\n'
|
||||
'1-0:61.7.0(00.000*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'
|
||||
'1-0:32.7.0(234.7*V)\r\n'
|
||||
'1-0:52.7.0(234.7*V)\r\n'
|
||||
'1-0:72.7.0(234.7*V)\r\n'
|
||||
'1-0:31.7.0(000.00*A)\r\n'
|
||||
'1-0:51.7.0(000.00*A)\r\n'
|
||||
'1-0:71.7.0(000.00*A)\r\n'
|
||||
'0-0:96.3.10(1)\r\n'
|
||||
'0-0:17.0.0(999.9*kW)\r\n'
|
||||
'1-0:31.4.0(999*A)\r\n'
|
||||
'0-0:96.13.0()\r\n'
|
||||
'0-1:24.1.0(003)\r\n'
|
||||
'0-1:96.1.1(37464C4F32313139303333373333)\r\n'
|
||||
'0-1:24.4.0(1)\r\n'
|
||||
'0-1:24.2.3(200512134558S)(00112.384*m3)\r\n'
|
||||
'0-2:24.1.0(007)\r\n'
|
||||
'0-2:96.1.1(3853414731323334353637383930)\r\n'
|
||||
'0-2:24.2.1(200512134558S)(00872.234*m3)\r\n'
|
||||
'!911C\r\n'
|
||||
)
|
||||
|
||||
# EasyMeter via COM-1 Ethernet Gateway
|
||||
# Q3D Manual (german) https://www.easymeter.com/downloads/products/zaehler/Q3D/Easymeter_Q3D_DE_2016-06-15.pdf
|
||||
# - type code on page 8
|
||||
# - D0-Specs on page 20
|
||||
#
|
||||
# last two lines are added by the COM-1 Ethernet Gateway
|
||||
|
||||
TELEGRAM_ESY5Q3DB1024_V304 = (
|
||||
'/ESY5Q3DB1024 V3.04\r\n'
|
||||
'\r\n'
|
||||
'1-0:0.0.0*255(0272031312565)\r\n'
|
||||
'1-0:1.8.0*255(00052185.7825309*kWh)\r\n'
|
||||
'1-0:2.8.0*255(00019949.3221493*kWh)\r\n'
|
||||
'1-0:21.7.0*255(000747.85*W)\r\n'
|
||||
'1-0:41.7.0*255(000737.28*W)\r\n'
|
||||
'1-0:61.7.0*255(000639.73*W)\r\n'
|
||||
'1-0:1.7.0*255(002124.86*W)\r\n'
|
||||
'1-0:96.5.5*255(80)\r\n'
|
||||
'0-0:96.1.255*255(1ESY1313002565)\r\n'
|
||||
'!\r\n'
|
||||
' 25803103\r\n'
|
||||
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
|
||||
'\xff\xff\xff\xff\xff\r\n'
|
||||
)
|
||||
|
||||
TELEGRAM_ESY5Q3DA1004_V304 = (
|
||||
'/ESY5Q3DA1004 V3.04\r\n'
|
||||
'\r\n'
|
||||
'1-0:0.0.0*255(1336001560)\r\n'
|
||||
'1-0:1.8.0*255(00032549.5061662*kWh)\r\n'
|
||||
'1-0:21.7.0*255(000557.29*W)\r\n'
|
||||
'1-0:41.7.0*255(000521.62*W)\r\n'
|
||||
'1-0:61.7.0*255(000609.30*W)\r\n'
|
||||
'1-0:1.7.0*255(001688.21*W)\r\n'
|
||||
'1-0:96.5.5*255(80)\r\n'
|
||||
'0-0:96.1.255*255(1ESY1336001560)\r\n'
|
||||
'!\r\n'
|
||||
' 25818685\r\n'
|
||||
'DE0000000000000000000000000000003\r\n'
|
||||
)
|
||||
|
||||
TELEGRAM_SAGEMCOM_T210_D_R = (
|
||||
'/EST5\\253710000_A\r\n'
|
||||
'\r\n'
|
||||
'1-3:0.2.8(50)\r\n'
|
||||
'0-0:1.0.0(221006155014S)\r\n'
|
||||
'1-0:1.8.0(006545766*Wh)\r\n'
|
||||
'1-0:1.8.1(005017120*Wh)\r\n'
|
||||
'1-0:1.8.2(001528646*Wh)\r\n'
|
||||
'1-0:1.7.0(000000286*W)\r\n'
|
||||
'1-0:2.8.0(000000058*Wh)\r\n'
|
||||
'1-0:2.8.1(000000000*Wh)\r\n'
|
||||
'1-0:2.8.2(000000058*Wh)\r\n'
|
||||
'1-0:2.7.0(000000000*W)\r\n'
|
||||
'1-0:3.8.0(000000747*varh)\r\n'
|
||||
'1-0:3.8.1(000000000*varh)\r\n'
|
||||
'1-0:3.8.2(000000747*varh)\r\n'
|
||||
'1-0:3.7.0(000000000*var)\r\n'
|
||||
'1-0:4.8.0(003897726*varh)\r\n'
|
||||
'1-0:4.8.1(002692848*varh)\r\n'
|
||||
'1-0:4.8.2(001204878*varh)\r\n'
|
||||
'1-0:4.7.0(000000166*var)\r\n'
|
||||
'!7EF9\r\n'
|
||||
)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import unittest
|
||||
import tempfile
|
||||
|
||||
from dsmr_parser.clients.filereader import FileReader
|
||||
from dsmr_parser.telegram_specifications import V5
|
||||
from test.example_telegrams import TELEGRAM_V5
|
||||
|
||||
|
||||
class FileReaderTest(unittest.TestCase):
|
||||
def test_read_as_object(self):
|
||||
with tempfile.NamedTemporaryFile() as file:
|
||||
with open(file.name, "w") as f:
|
||||
f.write(TELEGRAM_V5)
|
||||
|
||||
telegrams = []
|
||||
reader = FileReader(file=file.name, telegram_specification=V5)
|
||||
# Call
|
||||
for telegram in reader.read_as_object():
|
||||
telegrams.append(telegram)
|
||||
|
||||
self.assertEqual(len(telegrams), 1)
|
||||
@@ -0,0 +1,268 @@
|
||||
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, MBusObjectPeak
|
||||
from dsmr_parser.parsers import TelegramParser
|
||||
from test.example_telegrams import TELEGRAM_FLUVIUS_V171
|
||||
|
||||
|
||||
class TelegramParserFluviusTest(unittest.TestCase):
|
||||
""" Test parsing of a DSMR Fluvius telegram. """
|
||||
|
||||
def test_parse(self):
|
||||
parser = TelegramParser(telegram_specifications.BELGIUM_FLUVIUS)
|
||||
result = parser.parse(TELEGRAM_FLUVIUS_V171)
|
||||
|
||||
# BELGIUM_VERSION_INFORMATION (0-0:96.1.4)
|
||||
assert isinstance(result[obis.BELGIUM_VERSION_INFORMATION], CosemObject)
|
||||
assert result[obis.BELGIUM_VERSION_INFORMATION].unit is None
|
||||
assert isinstance(result[obis.BELGIUM_VERSION_INFORMATION].value, str)
|
||||
assert result[obis.BELGIUM_VERSION_INFORMATION].value == '50217'
|
||||
|
||||
# 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 == '3153414733313031303231363035'
|
||||
|
||||
# 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(2020, 5, 12, 11, 54, 9, 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('0.034')
|
||||
|
||||
# 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('15.758')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# 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.011')
|
||||
|
||||
# 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'
|
||||
|
||||
# BELGIUM_CURRENT_AVERAGE_DEMAND (1-0:1.4.0)
|
||||
assert isinstance(result[obis.BELGIUM_CURRENT_AVERAGE_DEMAND], CosemObject)
|
||||
assert result[obis.BELGIUM_CURRENT_AVERAGE_DEMAND].unit == 'kW'
|
||||
assert isinstance(result[obis.BELGIUM_CURRENT_AVERAGE_DEMAND].value, Decimal)
|
||||
assert result[obis.BELGIUM_CURRENT_AVERAGE_DEMAND].value == Decimal('2.351')
|
||||
|
||||
# BELGIUM_MAXIMUM_DEMAND_MONTH (1-0:1.6.0)
|
||||
assert isinstance(result[obis.BELGIUM_MAXIMUM_DEMAND_MONTH], MBusObject)
|
||||
assert result[obis.BELGIUM_MAXIMUM_DEMAND_MONTH].unit == 'kW'
|
||||
assert isinstance(result[obis.BELGIUM_MAXIMUM_DEMAND_MONTH].value, Decimal)
|
||||
assert result[obis.BELGIUM_MAXIMUM_DEMAND_MONTH].value == Decimal('2.589')
|
||||
|
||||
# BELGIUM_MAXIMUM_DEMAND_13_MONTHS (0-0:98.1.0) Value 0
|
||||
assert isinstance(result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][0], MBusObjectPeak)
|
||||
assert result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][0].unit == 'kW'
|
||||
assert isinstance(result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][0].value, Decimal)
|
||||
assert result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][0].value == Decimal('3.695')
|
||||
# BELGIUM_MAXIMUM_DEMAND_13_MONTHS (0-0:98.1.0) Value 1
|
||||
assert isinstance(result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][1], MBusObjectPeak)
|
||||
assert result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][1].unit == 'kW'
|
||||
assert isinstance(result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][1].value, Decimal)
|
||||
assert result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][1].value == Decimal('5.980')
|
||||
# BELGIUM_MAXIMUM_DEMAND_13_MONTHS (0-0:98.1.0) Value 2
|
||||
assert isinstance(result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][2], MBusObjectPeak)
|
||||
assert result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][2].unit == 'kW'
|
||||
assert isinstance(result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][2].value, Decimal)
|
||||
assert result[obis.BELGIUM_MAXIMUM_DEMAND_13_MONTHS][2].value == Decimal('4.318')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# 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.000')
|
||||
|
||||
# INSTANTANEOUS_VOLTAGE_L1 (1-0:32.7.0)
|
||||
assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L1], CosemObject)
|
||||
assert result[obis.INSTANTANEOUS_VOLTAGE_L1].unit == 'V'
|
||||
assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L1].value, Decimal)
|
||||
assert result[obis.INSTANTANEOUS_VOLTAGE_L1].value == Decimal('234.7')
|
||||
|
||||
# INSTANTANEOUS_VOLTAGE_L2 (1-0:52.7.0)
|
||||
assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L2], CosemObject)
|
||||
assert result[obis.INSTANTANEOUS_VOLTAGE_L2].unit == 'V'
|
||||
assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L2].value, Decimal)
|
||||
assert result[obis.INSTANTANEOUS_VOLTAGE_L2].value == Decimal('234.7')
|
||||
|
||||
# INSTANTANEOUS_VOLTAGE_L3 (1-0:72.7.0)
|
||||
assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L3], CosemObject)
|
||||
assert result[obis.INSTANTANEOUS_VOLTAGE_L3].unit == 'V'
|
||||
assert isinstance(result[obis.INSTANTANEOUS_VOLTAGE_L3].value, Decimal)
|
||||
assert result[obis.INSTANTANEOUS_VOLTAGE_L3].value == Decimal('234.7')
|
||||
|
||||
# INSTANTANEOUS_CURRENT_L1 (1-0:31.7.0)
|
||||
assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L1], CosemObject)
|
||||
assert result[obis.INSTANTANEOUS_CURRENT_L1].unit == 'A'
|
||||
assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L1].value, Decimal)
|
||||
assert result[obis.INSTANTANEOUS_CURRENT_L1].value == Decimal('0.000')
|
||||
|
||||
# INSTANTANEOUS_CURRENT_L2 (1-0:51.7.0)
|
||||
assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L2], CosemObject)
|
||||
assert result[obis.INSTANTANEOUS_CURRENT_L2].unit == 'A'
|
||||
assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L2].value, Decimal)
|
||||
assert result[obis.INSTANTANEOUS_CURRENT_L2].value == Decimal('0.000')
|
||||
|
||||
# INSTANTANEOUS_CURRENT_L3 (1-0:71.7.0)
|
||||
assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L3], CosemObject)
|
||||
assert result[obis.INSTANTANEOUS_CURRENT_L3].unit == 'A'
|
||||
assert isinstance(result[obis.INSTANTANEOUS_CURRENT_L3].value, Decimal)
|
||||
assert result[obis.INSTANTANEOUS_CURRENT_L3].value == Decimal('0.000')
|
||||
|
||||
# ACTUAL_SWITCH_POSITION (0-0:96.3.10)
|
||||
assert isinstance(result[obis.ACTUAL_SWITCH_POSITION], CosemObject)
|
||||
assert result[obis.ACTUAL_SWITCH_POSITION].unit is None
|
||||
assert isinstance(result[obis.ACTUAL_SWITCH_POSITION].value, int)
|
||||
assert result[obis.ACTUAL_SWITCH_POSITION].value == 1
|
||||
|
||||
# BELGIUM_MAX_POWER_PER_PHASE (0-0:17.0.0)
|
||||
assert isinstance(result[obis.BELGIUM_MAX_POWER_PER_PHASE], CosemObject)
|
||||
assert result[obis.BELGIUM_MAX_POWER_PER_PHASE].unit == 'kW'
|
||||
assert isinstance(result[obis.BELGIUM_MAX_POWER_PER_PHASE].value, Decimal)
|
||||
assert result[obis.BELGIUM_MAX_POWER_PER_PHASE].value == Decimal('999.9')
|
||||
|
||||
# BELGIUM_MAX_POWER_PER_PHASE (1-0:31.4.0)
|
||||
assert isinstance(result[obis.BELGIUM_MAX_CURRENT_PER_PHASE], CosemObject)
|
||||
assert result[obis.BELGIUM_MAX_CURRENT_PER_PHASE].unit == 'A'
|
||||
assert isinstance(result[obis.BELGIUM_MAX_CURRENT_PER_PHASE].value, Decimal)
|
||||
assert result[obis.BELGIUM_MAX_CURRENT_PER_PHASE].value == Decimal('999')
|
||||
|
||||
# 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
|
||||
|
||||
# BELGIUM_MBUS1_DEVICE_TYPE (0-1:24.1.0)
|
||||
assert isinstance(result[obis.BELGIUM_MBUS1_DEVICE_TYPE], CosemObject)
|
||||
assert result[obis.BELGIUM_MBUS1_DEVICE_TYPE].unit is None
|
||||
assert isinstance(result[obis.BELGIUM_MBUS1_DEVICE_TYPE].value, int)
|
||||
assert result[obis.BELGIUM_MBUS1_DEVICE_TYPE].value == 3
|
||||
|
||||
# BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER (0-1:96.1.1)
|
||||
assert isinstance(result[obis.BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER], CosemObject)
|
||||
assert result[obis.BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER].unit is None
|
||||
assert isinstance(result[obis.BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER].value, str)
|
||||
assert result[obis.BELGIUM_MBUS1_EQUIPMENT_IDENTIFIER].value == '37464C4F32313139303333373333'
|
||||
|
||||
# BELGIUM_MBUS1_VALVE_POSITION (0-1:24.4.0)
|
||||
assert isinstance(result[obis.BELGIUM_MBUS1_VALVE_POSITION], CosemObject)
|
||||
assert result[obis.BELGIUM_MBUS1_VALVE_POSITION].unit is None
|
||||
assert isinstance(result[obis.BELGIUM_MBUS1_VALVE_POSITION].value, int)
|
||||
assert result[obis.BELGIUM_MBUS1_VALVE_POSITION].value == 1
|
||||
|
||||
# BELGIUM_MBUS1_METER_READING2 (0-1:24.2.3)
|
||||
assert isinstance(result[obis.BELGIUM_MBUS1_METER_READING2], MBusObject)
|
||||
assert result[obis.BELGIUM_MBUS1_METER_READING2].unit == 'm3'
|
||||
assert isinstance(result[obis.BELGIUM_MBUS1_METER_READING2].value, Decimal)
|
||||
assert result[obis.BELGIUM_MBUS1_METER_READING2].value == Decimal('112.384')
|
||||
|
||||
# BELGIUM_MBUS2_DEVICE_TYPE (0-2:24.1.0)
|
||||
assert isinstance(result[obis.BELGIUM_MBUS2_DEVICE_TYPE], CosemObject)
|
||||
assert result[obis.BELGIUM_MBUS2_DEVICE_TYPE].unit is None
|
||||
assert isinstance(result[obis.BELGIUM_MBUS2_DEVICE_TYPE].value, int)
|
||||
assert result[obis.BELGIUM_MBUS2_DEVICE_TYPE].value == 7
|
||||
|
||||
# BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER (0-2:96.1.1)
|
||||
assert isinstance(result[obis.BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER], CosemObject)
|
||||
assert result[obis.BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER].unit is None
|
||||
assert isinstance(result[obis.BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER].value, str)
|
||||
assert result[obis.BELGIUM_MBUS2_EQUIPMENT_IDENTIFIER].value == '3853414731323334353637383930'
|
||||
|
||||
# BELGIUM_MBUS2_METER_READING1 (0-1:24.2.1)
|
||||
assert isinstance(result[obis.BELGIUM_MBUS2_METER_READING1], MBusObject)
|
||||
assert result[obis.BELGIUM_MBUS2_METER_READING1].unit == 'm3'
|
||||
assert isinstance(result[obis.BELGIUM_MBUS2_METER_READING1].value, Decimal)
|
||||
assert result[obis.BELGIUM_MBUS2_METER_READING1].value == Decimal('872.234')
|
||||
|
||||
def test_checksum_valid(self):
|
||||
# No exception is raised.
|
||||
TelegramParser.validate_checksum(TELEGRAM_FLUVIUS_V171)
|
||||
|
||||
def test_checksum_invalid(self):
|
||||
# Remove the electricty used data value. This causes the checksum to
|
||||
# not match anymore.
|
||||
corrupted_telegram = TELEGRAM_FLUVIUS_V171.replace(
|
||||
'1-0:1.8.1(000000.034*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_FLUVIUS_V171.replace('!911C\r\n', '')
|
||||
with self.assertRaises(ParseError):
|
||||
TelegramParser.validate_checksum(corrupted_telegram)
|
||||
@@ -0,0 +1,107 @@
|
||||
from binascii import unhexlify
|
||||
from copy import deepcopy
|
||||
|
||||
import unittest
|
||||
|
||||
from dlms_cosem.exceptions import DecryptionError
|
||||
from dlms_cosem.protocol.xdlms import GeneralGlobalCipher
|
||||
from dlms_cosem.security import SecurityControlField, encrypt
|
||||
|
||||
from dsmr_parser import telegram_specifications
|
||||
from dsmr_parser.exceptions import ParseError
|
||||
from dsmr_parser.parsers import TelegramParser
|
||||
from test.example_telegrams import TELEGRAM_SAGEMCOM_T210_D_R
|
||||
|
||||
|
||||
class TelegramParserEncryptedTest(unittest.TestCase):
|
||||
""" Test parsing of a DSML encypted DSMR v5.x telegram. """
|
||||
DUMMY_ENCRYPTION_KEY = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
DUMMY_AUTHENTICATION_KEY = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
|
||||
|
||||
def __generate_encrypted(self, security_suite=0, authenticated=True, encrypted=True):
|
||||
security_control = SecurityControlField(
|
||||
security_suite=security_suite, authenticated=authenticated, encrypted=encrypted
|
||||
)
|
||||
encryption_key = unhexlify(self.DUMMY_ENCRYPTION_KEY)
|
||||
authentication_key = unhexlify(self.DUMMY_AUTHENTICATION_KEY)
|
||||
system_title = "SYSTEMID".encode("ascii")
|
||||
invocation_counter = int.from_bytes(bytes.fromhex("10000001"), "big")
|
||||
plain_data = TELEGRAM_SAGEMCOM_T210_D_R.encode("ascii")
|
||||
|
||||
encrypted = encrypt(
|
||||
security_control=security_control,
|
||||
key=encryption_key,
|
||||
auth_key=authentication_key,
|
||||
system_title=system_title,
|
||||
invocation_counter=invocation_counter,
|
||||
plain_text=plain_data,
|
||||
)
|
||||
|
||||
full_frame = bytearray(GeneralGlobalCipher.TAG.to_bytes(1, "big", signed=False))
|
||||
full_frame.extend(len(system_title).to_bytes(1, "big", signed=False))
|
||||
full_frame.extend(system_title)
|
||||
full_frame.extend([0x82]) # Length of the following length bytes
|
||||
# https://github.com/pwitab/dlms-cosem/blob/739f81a58e5f07663a512d4a128851333a0ed5e6/dlms_cosem/a_xdr.py#L33
|
||||
|
||||
security_control = security_control.to_bytes()
|
||||
invocation_counter = invocation_counter.to_bytes(4, "big", signed=False)
|
||||
full_frame.extend((len(encrypted)
|
||||
+ len(invocation_counter)
|
||||
+ len(security_control)).to_bytes(2, "big", signed=False))
|
||||
full_frame.extend(security_control)
|
||||
full_frame.extend(invocation_counter)
|
||||
full_frame.extend(encrypted)
|
||||
|
||||
return full_frame
|
||||
|
||||
def test_parse(self):
|
||||
parser = TelegramParser(telegram_specifications.SAGEMCOM_T210_D_R)
|
||||
result = parser.parse(self.__generate_encrypted().hex(),
|
||||
self.DUMMY_ENCRYPTION_KEY,
|
||||
self.DUMMY_AUTHENTICATION_KEY)
|
||||
self.assertEqual(len(result), 18)
|
||||
|
||||
def test_damaged_frame(self):
|
||||
# If the frame is damaged decrypting fails (crc is technically not needed)
|
||||
parser = TelegramParser(telegram_specifications.SAGEMCOM_T210_D_R)
|
||||
|
||||
generated = self.__generate_encrypted()
|
||||
generated[150] = 0x00
|
||||
generated = generated.hex()
|
||||
|
||||
with self.assertRaises(DecryptionError):
|
||||
parser.parse(generated, self.DUMMY_ENCRYPTION_KEY, self.DUMMY_AUTHENTICATION_KEY)
|
||||
|
||||
def test_plain(self):
|
||||
# If a plain request is parsed with "general_global_cipher": True it fails
|
||||
parser = TelegramParser(telegram_specifications.SAGEMCOM_T210_D_R)
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
parser.parse(TELEGRAM_SAGEMCOM_T210_D_R, self.DUMMY_ENCRYPTION_KEY, self.DUMMY_AUTHENTICATION_KEY)
|
||||
|
||||
def test_general_global_cipher_not_specified(self):
|
||||
# If a GGC frame is detected but general_global_cipher is not set it fails
|
||||
parser = TelegramParser(telegram_specifications.SAGEMCOM_T210_D_R)
|
||||
parser = deepcopy(parser) # We do not want to change the module value
|
||||
parser.telegram_specification['general_global_cipher'] = False
|
||||
|
||||
with self.assertRaises(ParseError):
|
||||
parser.parse(self.__generate_encrypted().hex(), self.DUMMY_ENCRYPTION_KEY, self.DUMMY_AUTHENTICATION_KEY)
|
||||
|
||||
def test_only_encrypted(self):
|
||||
# Not implemented by dlms_cosem
|
||||
parser = TelegramParser(telegram_specifications.SAGEMCOM_T210_D_R)
|
||||
|
||||
only_auth = self.__generate_encrypted(0, authenticated=False, encrypted=True).hex()
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
parser.parse(only_auth, self.DUMMY_ENCRYPTION_KEY)
|
||||
|
||||
def test_only_auth(self):
|
||||
# Not implemented by dlms_cosem
|
||||
parser = TelegramParser(telegram_specifications.SAGEMCOM_T210_D_R)
|
||||
|
||||
only_auth = self.__generate_encrypted(0, authenticated=True, encrypted=False).hex()
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
parser.parse(only_auth, authentication_key=self.DUMMY_AUTHENTICATION_KEY)
|
||||
@@ -3,8 +3,6 @@ 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 create_dsmr_protocol
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
from unittest.mock import Mock
|
||||
|
||||
import unittest
|
||||
|
||||
from dsmr_parser import obis_references as obis
|
||||
from dsmr_parser.clients.rfxtrx_protocol import create_rfxtrx_dsmr_protocol, PACKETTYPE_DSMR, SUBTYPE_P1
|
||||
|
||||
|
||||
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'
|
||||
)
|
||||
|
||||
OTHER_RF_PACKET = b'\x03\x01\x02\x03'
|
||||
|
||||
|
||||
def encode_telegram_as_RF_packets(telegram):
|
||||
data = b''
|
||||
|
||||
for line in telegram.split('\n'):
|
||||
packet_data = (line + '\n').encode('ascii')
|
||||
packet_header = bytes(bytearray([
|
||||
len(packet_data) + 3, # excluding length byte
|
||||
PACKETTYPE_DSMR,
|
||||
SUBTYPE_P1,
|
||||
0 # seq num (ignored)
|
||||
]))
|
||||
|
||||
data += packet_header + packet_data
|
||||
# other RF packets can pass by on the line
|
||||
data += OTHER_RF_PACKET
|
||||
|
||||
return data
|
||||
|
||||
|
||||
class RFXtrxProtocolTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
new_protocol, _ = create_rfxtrx_dsmr_protocol('2.2',
|
||||
telegram_callback=Mock(),
|
||||
keep_alive_interval=1)
|
||||
self.protocol = new_protocol()
|
||||
|
||||
def test_complete_packet(self):
|
||||
"""Protocol should assemble incoming lines into complete packet."""
|
||||
|
||||
data = encode_telegram_as_RF_packets(TELEGRAM_V2_2)
|
||||
# send data broken up in two parts
|
||||
self.protocol.data_received(data[0:200])
|
||||
self.protocol.data_received(data[200:])
|
||||
|
||||
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'
|
||||
@@ -1,14 +1,11 @@
|
||||
[tox]
|
||||
envlist = py35,py36,py37,py38,py39
|
||||
|
||||
[testenv]
|
||||
deps=
|
||||
pytest
|
||||
pytest-cov
|
||||
pylama
|
||||
pytest-asyncio
|
||||
pytest-catchlog
|
||||
pytest-mock
|
||||
dlms_cosem
|
||||
commands=
|
||||
py.test --cov=dsmr_parser test {posargs}
|
||||
pylama dsmr_parser test
|
||||
@@ -19,8 +16,11 @@ ignore = W0611
|
||||
[pylama:dsmr_parser/parsers.py]
|
||||
ignore = W605
|
||||
|
||||
[pylama:test/example_telegrams.py]
|
||||
ignore = E501
|
||||
|
||||
[pylama:pylint]
|
||||
max_line_length = 120
|
||||
|
||||
[pylama:pycodestyle]
|
||||
max_line_length = 120
|
||||
max_line_length = 120
|
||||
Reference in New Issue
Block a user