From 90eb6fb3fe0d090b2f81e16f770d45ddbfffa0a2 Mon Sep 17 00:00:00 2001 From: Johan Bloemberg Date: Sun, 6 Nov 2016 20:40:01 +0100 Subject: [PATCH] Add V2.2 telegram implementation and console read. --- dsmr_parser/__main__.py | 35 ++++++++++++++++++++++++++ dsmr_parser/obis_references.py | 4 +++ dsmr_parser/serial.py | 10 ++++++++ dsmr_parser/telegram_specifications.py | 18 +++++++++++++ setup.py | 5 +++- 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 dsmr_parser/__main__.py diff --git a/dsmr_parser/__main__.py b/dsmr_parser/__main__.py new file mode 100644 index 0000000..b3e127c --- /dev/null +++ b/dsmr_parser/__main__.py @@ -0,0 +1,35 @@ +import argparse +from dsmr_parser.serial import SERIAL_SETTINGS_V2_2, SERIAL_SETTINGS_V4, SerialReader +from dsmr_parser import telegram_specifications +from dsmr_parser.obis_references import P1_MESSAGE_TIMESTAMP + +def console(): + """Output DSMR data to console.""" + + parser = argparse.ArgumentParser(description=console.__doc__) + parser.add_argument('--device', default='/dev/ttyUSB0', + help='port to read DSMR data from') + parser.add_argument('--version', default='2.2', choices=['2.2', '4'], + help='DSMR version (2.2, 4)') + + args = parser.parse_args() + + version = 'V' + args.version.replace('.', '_') + + settings = { + '2.2': (SERIAL_SETTINGS_V2_2, telegram_specifications.V2_2), + '4': (SERIAL_SETTINGS_V4, telegram_specifications.V4), + } + + serial_reader = SerialReader( + device=args.device, + serial_settings=settings[args.version][0], + telegram_specification=settings[args.version][1], + ) + + for telegram in serial_reader.read(): + for obiref, obj in telegram.items(): + if obj: + print(obj.value, obj.unit) + print() + diff --git a/dsmr_parser/obis_references.py b/dsmr_parser/obis_references.py index 7fbb24e..f99d007 100644 --- a/dsmr_parser/obis_references.py +++ b/dsmr_parser/obis_references.py @@ -27,6 +27,10 @@ INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE = r'1-0:42\.7\.0' INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE = r'1-0:62\.7\.0' EQUIPMENT_IDENTIFIER_GAS = r'0-\d:96\.1\.0' HOURLY_GAS_METER_READING = r'0-1:24\.2\.1' +GAS_METER_READING = r'0-\d:24\.3\.0' +ACTUAL_TRESHOLD_ELECTRICITY = r'0-0:17\.0\.0' +ACTUAL_SWITCH_POSITION = r'0-0:96\.3\.10' +VALVE_POSITION_GAS = r'0-\d:24\.4\.0' ELECTRICITY_USED_TARIFF_ALL = ( ELECTRICITY_USED_TARIFF_1, diff --git a/dsmr_parser/serial.py b/dsmr_parser/serial.py index ee80a67..a4726c6 100644 --- a/dsmr_parser/serial.py +++ b/dsmr_parser/serial.py @@ -2,6 +2,16 @@ import serial from dsmr_parser.parsers import TelegramParser +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, diff --git a/dsmr_parser/telegram_specifications.py b/dsmr_parser/telegram_specifications.py index bcab475..b6f5071 100644 --- a/dsmr_parser/telegram_specifications.py +++ b/dsmr_parser/telegram_specifications.py @@ -13,6 +13,24 @@ This module contains DSMR telegram specifications. Each specifications describes how the telegram lines are parsed. """ +V2_2 = { + EQUIPMENT_IDENTIFIER: CosemParser(ValueParser(str)), + ELECTRICITY_USED_TARIFF_1: CosemParser(ValueParser(Decimal)), + ELECTRICITY_USED_TARIFF_2: CosemParser(ValueParser(Decimal)), + ELECTRICITY_DELIVERED_TARIFF_1: CosemParser(ValueParser(Decimal)), + ELECTRICITY_DELIVERED_TARIFF_2: CosemParser(ValueParser(Decimal)), + ELECTRICITY_ACTIVE_TARIFF: CosemParser(ValueParser(str)), + CURRENT_ELECTRICITY_USAGE: CosemParser(ValueParser(Decimal)), + CURRENT_ELECTRICITY_DELIVERY: CosemParser(ValueParser(Decimal)), + ACTUAL_TRESHOLD_ELECTRICITY: CosemParser(ValueParser(Decimal)), + ACTUAL_SWITCH_POSITION: CosemParser(ValueParser(str)), + TEXT_MESSAGE_CODE: CosemParser(ValueParser(int)), + TEXT_MESSAGE: CosemParser(ValueParser(str)), + EQUIPMENT_IDENTIFIER_GAS: CosemParser(ValueParser(str)), + DEVICE_TYPE: CosemParser(ValueParser(str)), + VALVE_POSITION_GAS: CosemParser(ValueParser(str)), +} + V4 = { P1_MESSAGE_HEADER: CosemParser(ValueParser(str)), P1_MESSAGE_TIMESTAMP: CosemParser(ValueParser(timestamp)), diff --git a/setup.py b/setup.py index f95903a..c0f2d76 100644 --- a/setup.py +++ b/setup.py @@ -9,5 +9,8 @@ setup( install_requires=[ 'pyserial==3.0.1', 'pytz==2016.3' - ] + ], + entry_points={ + 'console_scripts': ['dsmr_console=dsmr_parser.__main__:console'] + }, )