Merge pull request #98 from rhpijnacker/feature/rfxtrx
Support DSMR data read via RFXtrx with integrated P1 reader
This commit is contained in:
commit
b74572238e
@ -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):
|
def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
||||||
"""Creates a DSMR asyncio protocol."""
|
"""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':
|
if dsmr_version == '2.2':
|
||||||
specification = telegram_specifications.V2_2
|
specification = telegram_specifications.V2_2
|
||||||
@ -45,7 +52,7 @@ def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None, **kwargs):
|
|||||||
raise NotImplementedError("No telegram parser found for version: %s",
|
raise NotImplementedError("No telegram parser found for version: %s",
|
||||||
dsmr_version)
|
dsmr_version)
|
||||||
|
|
||||||
protocol = partial(DSMRProtocol, loop, TelegramParser(specification),
|
protocol = partial(protocol, loop, TelegramParser(specification),
|
||||||
telegram_callback=telegram_callback, **kwargs)
|
telegram_callback=telegram_callback, **kwargs)
|
||||||
|
|
||||||
return protocol, serial_settings
|
return protocol, serial_settings
|
||||||
|
62
dsmr_parser/clients/rfxtrx_protocol.py
Normal file
62
dsmr_parser/clients/rfxtrx_protocol.py
Normal file
@ -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
|
77
test/test_rfxtrx_protocol.py
Normal file
77
test/test_rfxtrx_protocol.py
Normal file
@ -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'
|
Loading…
Reference in New Issue
Block a user