diff --git a/dsmr_parser/clients/filereader.py b/dsmr_parser/clients/filereader.py index e6eeb59..12164f2 100644 --- a/dsmr_parser/clients/filereader.py +++ b/dsmr_parser/clients/filereader.py @@ -1,5 +1,6 @@ import logging import fileinput +import tailer from dsmr_parser.clients.telegram_buffer import TelegramBuffer from dsmr_parser.exceptions import ParseError, InvalidChecksumError @@ -120,3 +121,31 @@ class FileInputReader(object): logger.warning(str(e)) except ParseError as e: logger.error('Failed to parse telegram: %s', e) + + +class FileTailReader(object): + + def __init__(self, file, telegram_specification): + self._file = file + self.telegram_parser = TelegramParser(telegram_specification) + self.telegram_buffer = TelegramBuffer() + self.telegram_specification = telegram_specification + + def read_as_object(self): + """ + Read complete DSMR telegram's from a files tail and return a Telegram object. + :rtype: generator + """ + with open(self._file,"rb") as file_handle: + for data in tailer.follow(file_handle): + str = data.decode() + self.telegram_buffer.append(str) + + for telegram in self.telegram_buffer.get_all(): + try: + yield Telegram(telegram, self.telegram_parser, self.telegram_specification) + except dsmr_parser.exceptions.InvalidChecksumError as e: + logger.warning(str(e)) + except dsmr_parser.exceptions.ParseError as e: + logger.error('Failed to parse telegram: %s', e) +