From 3eed3654d4839b0cd2e6c2c98d0f6b6804d76fe1 Mon Sep 17 00:00:00 2001 From: Ronald Pijnacker Date: Thu, 30 Dec 2021 17:01:29 +0100 Subject: [PATCH] Wrap DSMR protocol in RFXtrx wrapper --- dsmr_parser/clients/protocol.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dsmr_parser/clients/protocol.py b/dsmr_parser/clients/protocol.py index fce549d..982fd81 100644 --- a/dsmr_parser/clients/protocol.py +++ b/dsmr_parser/clients/protocol.py @@ -142,3 +142,26 @@ class DSMRProtocol(asyncio.Protocol): async def wait_closed(self): """Wait until connection is closed.""" await self._closed.wait() + + +PACKETTYPE_DSMR = 0x62 +SUBTYPE_P1 = 0x01 + +class RFXtrxDSMRProtocol(DSMRProtocol): + + _data = b'' + + def data_received(self, data): + """Add incoming data to buffer.""" + + data = self._data + data + + while (len(data) > 0 and (packetlength := data[0]+1) <= 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:] + + self._data = data