Merge pull request #7 from aequitas/error_handling

Add error handling to async serial.
This commit is contained in:
Nigel Dokter 2016-11-21 20:28:11 +01:00 committed by GitHub
commit 43a3cb7a96
2 changed files with 20 additions and 8 deletions

View File

@ -1,8 +1,15 @@
import serial
import asyncio import asyncio
import logging
import serial
import serial_asyncio import serial_asyncio
from dsmr_parser.exceptions import ParseError
from dsmr_parser.parsers import TelegramParser, TelegramParserV2_2 from dsmr_parser.parsers import TelegramParser, TelegramParserV2_2
logger = logging.getLogger(__name__)
SERIAL_SETTINGS_V2_2 = { SERIAL_SETTINGS_V2_2 = {
'baudrate': 9600, 'baudrate': 9600,
'bytesize': serial.SEVENBITS, 'bytesize': serial.SEVENBITS,
@ -44,7 +51,6 @@ SERIAL_SETTINGS_V4_EVEN = {
} }
def is_start_of_telegram(line): def is_start_of_telegram(line):
return line.startswith('/') return line.startswith('/')
@ -129,6 +135,11 @@ class AsyncSerialReader(SerialReader):
telegram.append(line) telegram.append(line)
if is_end_of_telegram(line): if is_end_of_telegram(line):
try:
parsed_telegram = self.telegram_parser.parse(telegram)
# push new parsed telegram onto queue # push new parsed telegram onto queue
queue.put_nowait(self.telegram_parser.parse(telegram)) queue.put_nowait(parsed_telegram)
except ParseError:
logger.exception("failed to parse telegram")
telegram = [] telegram = []

View File

@ -1,13 +1,13 @@
"""Test parsing of a DSMR v4.2 telegram.""" """Test parsing of a DSMR v4.2 telegram."""
import datetime import datetime
from decimal import Decimal from decimal import Decimal
import pytz import pytz
from dsmr_parser import obis_references as obis
from dsmr_parser import telegram_specifications
from dsmr_parser.objects import CosemObject, MBusObject from dsmr_parser.objects import CosemObject, MBusObject
from dsmr_parser.parsers import TelegramParser from dsmr_parser.parsers import TelegramParser
from dsmr_parser import telegram_specifications
from dsmr_parser import obis_references as obis
TELEGRAM_V4_2 = [ TELEGRAM_V4_2 = [
'1-3:0.2.8(42)', '1-3:0.2.8(42)',
@ -22,7 +22,8 @@ TELEGRAM_V4_2 = [
'1-0:2.7.0(00.000*kW)', '1-0:2.7.0(00.000*kW)',
'0-0:96.7.21(00015)', '0-0:96.7.21(00015)',
'0-0:96.7.9(00007)', '0-0:96.7.9(00007)',
'1-0:99.97.0(3)(0-0:96.7.19)(000103180420W)(0000237126*s)(000101000001W)(2147483647*s)(000101000001W)(2147483647*s)', ('1-0:99.97.0(3)(0-0:96.7.19)(000103180420W)(0000237126*s)'
'(000101000001W)(2147483647*s)(000101000001W)(2147483647*s)'),
'1-0:32.32.0(00000)', '1-0:32.32.0(00000)',
'1-0:52.32.0(00000)', '1-0:52.32.0(00000)',
'1-0:72.32.0(00000)', '1-0:72.32.0(00000)',