dev progress
This commit is contained in:
@@ -10,7 +10,7 @@ from . import telegram_specifications
|
||||
from .exceptions import ParseError
|
||||
from .parsers import TelegramParserV2_2, TelegramParserV4
|
||||
from .serial import (SERIAL_SETTINGS_V2_2, SERIAL_SETTINGS_V4,
|
||||
is_end_of_telegram, is_start_of_telegram)
|
||||
is_end_of_telegram, is_start_of_telegram, TelegramBuffer)
|
||||
|
||||
|
||||
def create_dsmr_protocol(dsmr_version, telegram_callback, loop=None):
|
||||
@@ -66,10 +66,8 @@ class DSMRProtocol(asyncio.Protocol):
|
||||
self.telegram_parser = telegram_parser
|
||||
# callback to call on complete telegram
|
||||
self.telegram_callback = telegram_callback
|
||||
# buffer to keep incoming telegram lines
|
||||
self.telegram = ''
|
||||
# buffer to keep incomplete incoming data
|
||||
self.buffer = ''
|
||||
self.telegram_buffer = TelegramBuffer(self.handle_telegram)
|
||||
# keep a lock until the connection is closed
|
||||
self._closed = asyncio.Event()
|
||||
|
||||
@@ -81,34 +79,8 @@ class DSMRProtocol(asyncio.Protocol):
|
||||
def data_received(self, data):
|
||||
"""Add incoming data to buffer."""
|
||||
data = data.decode('ascii')
|
||||
self.log.debug('received data: %s', data.strip())
|
||||
self.buffer += data
|
||||
self.handle_lines()
|
||||
|
||||
def handle_lines(self):
|
||||
"""Assemble incoming data into single lines."""
|
||||
crlf = "\r\n"
|
||||
while crlf in self.buffer:
|
||||
line, self.buffer = self.buffer.split(crlf, 1)
|
||||
self.log.debug('got line: %s', line)
|
||||
line += crlf # add the trailing crlf again
|
||||
|
||||
# Telegrams need to be complete because the values belong to a
|
||||
# particular reading and can also be related to eachother.
|
||||
if not self.telegram and not is_start_of_telegram(line):
|
||||
continue
|
||||
|
||||
self.telegram += line
|
||||
|
||||
if is_end_of_telegram(line):
|
||||
try:
|
||||
parsed_telegram = self.telegram_parser.parse(self.telegram)
|
||||
except ParseError as e:
|
||||
self.log.error('Failed to parse telegram: %s', e)
|
||||
else:
|
||||
self.handle_telegram(parsed_telegram)
|
||||
|
||||
self.telegram = []
|
||||
self.log.debug('received data: %s', data)
|
||||
self.telegram_buffer.append(data)
|
||||
|
||||
def connection_lost(self, exc):
|
||||
"""Stop when connection is lost."""
|
||||
|
||||
Reference in New Issue
Block a user