Add TCP arguments to console. Implement reconnect logic in protocol.
This commit is contained in:
parent
cdc9e395aa
commit
763237ef1d
@ -1,8 +1,9 @@
|
||||
import argparse
|
||||
import asyncio
|
||||
import logging
|
||||
from functools import partial
|
||||
|
||||
from .protocol import create_dsmr_reader
|
||||
from .protocol import create_dsmr_reader, create_tcp_dsmr_reader
|
||||
|
||||
|
||||
def console():
|
||||
@ -11,6 +12,10 @@ def console():
|
||||
parser = argparse.ArgumentParser(description=console.__doc__)
|
||||
parser.add_argument('--device', default='/dev/ttyUSB0',
|
||||
help='port to read DSMR data from')
|
||||
parser.add_argument('--host', default=None,
|
||||
help='alternatively connect using TCP host.')
|
||||
parser.add_argument('--port', default=None,
|
||||
help='TCP port to use for connection')
|
||||
parser.add_argument('--version', default='2.2', choices=['2.2', '4'],
|
||||
help='DSMR version (2.2, 4)')
|
||||
parser.add_argument('--verbose', '-v', action='count')
|
||||
@ -32,7 +37,29 @@ def console():
|
||||
print(obj.value, obj.unit)
|
||||
print()
|
||||
|
||||
conn = create_dsmr_reader(args.device, args.version, print_callback, loop=loop)
|
||||
# create tcp or serial connection depending on args
|
||||
if args.host and args.port:
|
||||
create_connection = partial(create_tcp_dsmr_reader,
|
||||
args.host, args.port, args.version,
|
||||
print_callback, loop=loop)
|
||||
else:
|
||||
create_connection = partial(create_dsmr_reader,
|
||||
args.device, args.version,
|
||||
print_callback, loop=loop)
|
||||
|
||||
loop.create_task(conn)
|
||||
loop.run_forever()
|
||||
try:
|
||||
# connect and keep connected until interrupted by ctrl-c
|
||||
while True:
|
||||
# create serial or tcp connection
|
||||
conn = create_connection()
|
||||
transport, protocol = loop.run_until_complete(conn)
|
||||
# wait until connection it closed
|
||||
loop.run_until_complete(protocol.wait_closed())
|
||||
# wait 5 seconds before attempting reconnect
|
||||
loop.run_until_complete(asyncio.sleep(5))
|
||||
except KeyboardInterrupt:
|
||||
# cleanup connection after user initiated shutdown
|
||||
transport.close()
|
||||
loop.run_until_complete(asyncio.sleep(0))
|
||||
finally:
|
||||
loop.close()
|
||||
|
@ -8,15 +8,9 @@ from serial_asyncio import create_serial_connection
|
||||
|
||||
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
|
||||
)
|
||||
from .parsers import TelegramParserV2_2, TelegramParserV4
|
||||
from .serial import (SERIAL_SETTINGS_V2_2, SERIAL_SETTINGS_V4,
|
||||
is_end_of_telegram, is_start_of_telegram)
|
||||
|
||||
|
||||
def creater_dsmr_protocol(dsmr_version, telegram_callback, loop=None):
|
||||
@ -47,7 +41,8 @@ def create_dsmr_reader(port, dsmr_version, telegram_callback, loop=None):
|
||||
return conn
|
||||
|
||||
|
||||
def create_tcp_dsmr_reader(host, port, dsmr_version, telegram_callback, loop=None):
|
||||
def create_tcp_dsmr_reader(host, port, dsmr_version,
|
||||
telegram_callback, loop=None):
|
||||
"""Creates a DSMR asyncio protocol coroutine using TCP connection."""
|
||||
protocol, _ = creater_dsmr_protocol(
|
||||
dsmr_version, telegram_callback, loop=None)
|
||||
@ -72,6 +67,8 @@ class DSMRProtocol(asyncio.Protocol):
|
||||
self.telegram = []
|
||||
# buffer to keep incomplete incoming data
|
||||
self.buffer = ''
|
||||
# keep a lock until the connection is closed
|
||||
self._closed = asyncio.Event()
|
||||
|
||||
def connection_made(self, transport):
|
||||
"""Just logging for now."""
|
||||
@ -107,7 +104,11 @@ class DSMRProtocol(asyncio.Protocol):
|
||||
|
||||
def connection_lost(self, exc):
|
||||
"""Stop when connection is lost."""
|
||||
self.log.error('disconnected')
|
||||
if exc:
|
||||
self.log.exception('disconnected due to exception')
|
||||
else:
|
||||
self.log.info('disconnected because of close/abort.')
|
||||
self._closed.set()
|
||||
|
||||
def handle_telegram(self, telegram):
|
||||
"""Send off parsed telegram to handling callback."""
|
||||
@ -115,3 +116,8 @@ class DSMRProtocol(asyncio.Protocol):
|
||||
|
||||
if self.telegram_callback:
|
||||
self.telegram_callback(telegram)
|
||||
|
||||
@asyncio.coroutine
|
||||
def wait_closed(self):
|
||||
"""Wait until connection is closed."""
|
||||
yield from self._closed.wait()
|
||||
|
Loading…
Reference in New Issue
Block a user